diff options
author | nharmata <nharmata@google.com> | 2018-07-25 08:19:33 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-25 08:21:20 -0700 |
commit | e1ed9f5488644dcc6bb11943d3a60c76a2b39af7 (patch) | |
tree | c023f18e8145779fc669a471bb6206bfbd9a1eed /src/main/java/com/google/devtools/build/lib/vfs | |
parent | 7699f20c896ce3d84535e53ea36534515e77791c (diff) |
Use a structured Exception type for the "unexpected short read" error detected by FileSystemUtils#readWithKnownFileSize.
RELNOTES: None
PiperOrigin-RevId: 205995852
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java index e1ac9fb7ae..af3054766e 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java @@ -858,6 +858,24 @@ public class FileSystemUtils { } /** + * The type of {@link IOException} thrown by {@link #readWithKnownFileSize} when fewer bytes than + * expected are read. + */ + public static class ShortReadIOException extends IOException { + public final Path path; + public final int fileSize; + public final int numBytesRead; + + private ShortReadIOException(Path path, int fileSize, int numBytesRead) { + super("Unexpected short read from file '" + path + "' (expected " + fileSize + ", got " + + numBytesRead + " bytes)"); + this.path = path; + this.fileSize = fileSize; + this.numBytesRead = numBytesRead; + } + } + + /** * Reads the given file {@code path}, assumed to have size {@code fileSize}, and does a sanity * check on the number of bytes read. * @@ -873,8 +891,7 @@ public class FileSystemUtils { int fileSizeInt = (int) fileSize; byte[] bytes = readContentWithLimit(path, fileSizeInt); if (fileSizeInt > bytes.length) { - throw new IOException("Unexpected short read from file '" + path - + "' (expected " + fileSizeInt + ", got " + bytes.length + " bytes)"); + throw new ShortReadIOException(path, fileSizeInt, bytes.length); } return bytes; } |