Skip to content

Commit

Permalink
add API for IFile.readNBytes(n)
Browse files Browse the repository at this point in the history
A convenience method that avoids callers to care about streams
  • Loading branch information
EcljpseB0T authored and jukzi committed Jun 24, 2024
1 parent 6f2754b commit 906371d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,27 @@ public default byte[] readAllBytes() throws CoreException {
}
}

/**
* Reads the first bytes of the content in a byte array. Equivalent of calling
* {@code getContents(true).readNBytes(maxBytes)} but also closing the stream.
*
* @param maxBytes The maximal length of the returned array. If maxBytes is
* greater or equal to the file length the whole content is
* returned. If maxBytes is smaller then the file length then
* only the first maxBytes bytes are returned.
* @return content bytes
* @throws CoreException on error
* @see #getContents(boolean)
* @since 3.21
*/
public default byte[] readNBytes(int maxBytes) throws CoreException {
try (InputStream stream = getContents(true)) {
return stream.readNBytes(maxBytes);
} catch (IOException e) {
throw new CoreException(Status.error("Error reading " + getFullPath(), e)); //$NON-NLS-1$
}
}

/**
* Reads the content as char array. Skips the UTF BOM header if any. This method
* is not intended for reading in large files that do not fit in a char array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,39 @@ public void testCreateByteArray() throws IOException, CoreException {
assertEquals(testString, target.readString());
}

@Test
public void testReadNBytes() throws IOException, CoreException {
IFile file = projects[0].getFile("smallfile");
byte[] bytes = "1234".getBytes(StandardCharsets.US_ASCII);
file.write(bytes, false, false, false, null);
try {
file.readNBytes(-1);
assertFalse(true);
} catch (IllegalArgumentException expected) {
// expected
}
byte[] nBytes0 = file.readNBytes(0);
assertEquals(0, nBytes0.length);
byte[] nBytes1 = file.readNBytes(1);
assertEquals(1, nBytes1.length);
assertEquals('1', nBytes1[0]);
byte[] nBytes4 = file.readNBytes(4);
assertEquals(4, nBytes4.length);
byte[] nBytes5 = file.readNBytes(5);
assertEquals(4, nBytes5.length);
byte[] nBytesMax = file.readNBytes(Integer.MAX_VALUE);
assertEquals(4, nBytesMax.length);
assertArrayEquals(bytes, nBytesMax);

IFile largefile = projects[0].getFile("largefile");
byte[] largeContent = new byte[50_000_000]; // only 50 MB to prevent OutOfMemoryError on jenkins
largefile.write(largeContent, false, false, false, null);
byte[] largeBytes = largefile.readNBytes(Integer.MAX_VALUE);
assertEquals(largeContent.length, largeBytes.length);
byte[] largeBytes1 = largefile.readNBytes(1);
assertEquals(1, largeBytes1.length);
}

@Test
public void testReadAll() throws IOException, CoreException {
List<Charset> charsets = List.of(StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8, StandardCharsets.UTF_16BE,
Expand Down

0 comments on commit 906371d

Please sign in to comment.