Skip to content

Commit

Permalink
Inline methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Dec 17, 2024
1 parent b6a60fb commit b132505
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -843,42 +843,6 @@ public static String readContent(Path inputFile, Charset charset) throws IOExcep
return asByteSource(inputFile).asCharSource(charset).read();
}

/**
* Reads at most {@code limit} bytes from {@code inputFile} and returns them as a byte array.
*
* @throws IOException if there was an error.
*/
public static byte[] readContentWithLimit(Path inputFile, int limit) throws IOException {
return readContentWithLimit(inputFile, limit, /* expectEOF= */ false);
}

/**
* Reads at most {@code limit} bytes from {@code inputFile} and returns them as a byte array.
*
* @throws IOException if there was an error or if the file is longer than {@code limit} bytes.
*/
private static byte[] readContentWithLimitExpectingEOF(Path inputFile, int limit)
throws IOException {
return readContentWithLimit(inputFile, limit, /* expectEOF= */ true);
}

private static byte[] readContentWithLimit(Path inputFile, int limit, boolean expectEOF)
throws IOException {
Preconditions.checkArgument(limit >= 0, "limit needs to be >=0, but it is %s", limit);
ByteSource byteSource = asByteSource(inputFile);
byte[] buffer = new byte[limit];
try (InputStream inputStream = byteSource.openBufferedStream()) {
int read = ByteStreams.read(inputStream, buffer, 0, limit);
if (expectEOF) {
int eof = inputStream.read();
if (eof != -1) {
throw new LongReadIOException(inputFile, limit);
}
}
return read == limit ? buffer : Arrays.copyOf(buffer, read);
}
}

/**
* The type of {@link IOException} thrown by {@link #readWithKnownFileSize} when fewer bytes than
* expected are read.
Expand All @@ -898,8 +862,8 @@ private ShortReadIOException(Path path, int fileSize, int numBytesRead) {
}

/**
* The type of {@link IOException} thrown by {@link #readContentWithLimitExpectingEOF} when more
* bytes than expected could be read.
* The type of {@link IOException} thrown by {@link #readWithKnownFileSize} when more bytes than
* expected could be read.
*/
public static class LongReadIOException extends IOException {
public final Path path;
Expand All @@ -917,19 +881,27 @@ private LongReadIOException(Path path, int fileSize) {
* the number of bytes read.
*
* <p>Use this method when you already know the size of the file. The check is intended to catch
* issues where filesystems or external interference results in truncated or concurrently modified
* files.
* issues where filesystems or external interference results in truncated or concurrent
* modifications.
*
* @throws IOException if there was an error, or if fewer than {@code fileSize} bytes were read.
*/
public static byte[] readWithKnownFileSize(Path path, long fileSize) throws IOException {
Preconditions.checkArgument(fileSize >= 0, "fileSize needs to be >=0, but it is %s", fileSize);
if (fileSize > Integer.MAX_VALUE) {
throw new IOException("Cannot read file with size larger than 2GB");
}
int fileSizeInt = (int) fileSize;
byte[] bytes = readContentWithLimitExpectingEOF(path, fileSizeInt);
if (fileSizeInt > bytes.length) {
throw new ShortReadIOException(path, fileSizeInt, bytes.length);
int size = (int) fileSize;
byte[] bytes = new byte[size];
try (InputStream in = asByteSource(path).openBufferedStream()) {
int read = ByteStreams.read(in, bytes, 0, size);
if (read != size) {
throw new ShortReadIOException(path, size, read);
}
int eof = in.read();
if (eof != -1) {
throw new LongReadIOException(path, size);
}
}
return bytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
Expand Down Expand Up @@ -406,28 +405,14 @@ public void testMoveFileFixPermissions() throws Exception {
assertThat(FileSystemUtils.readContent(target, UTF_8)).isEqualTo("linear-a");
}

@Test
public void testReadContentWithKnownSize() throws IOException {
createTestDirectoryTree();
String str = "this is a test of readContentWithLimit method";
FileSystemUtils.writeContent(file1, StandardCharsets.ISO_8859_1, str);
assertThat(readStringFromFile(file1, 0)).isEmpty();
assertThat(str.substring(0, 10)).isEqualTo(readStringFromFile(file1, 10));
assertThat(str).isEqualTo(readStringFromFile(file1, 1000000));
}

private String readStringFromFile(Path file, int limit) throws IOException {
byte[] bytes = FileSystemUtils.readContentWithLimit(file, limit);
return new String(bytes, StandardCharsets.ISO_8859_1);
}

@Test
public void testReadWithKnownFileSize() throws IOException {
createTestDirectoryTree();
String str = "this is a test of readContentWithLimit method";
FileSystemUtils.writeContent(file1, StandardCharsets.ISO_8859_1, str);
FileSystemUtils.writeContent(file1, ISO_8859_1, str);

assertThat(FileSystemUtils.readWithKnownFileSize(file1, str.length())).hasLength(str.length());
assertThat(FileSystemUtils.readWithKnownFileSize(file1, str.length()))
.isEqualTo(str.getBytes(ISO_8859_1));
assertThrows(
FileSystemUtils.LongReadIOException.class,
() -> FileSystemUtils.readWithKnownFileSize(file1, str.length() - 1));
Expand Down

0 comments on commit b132505

Please sign in to comment.