Skip to content

Commit 906371d

Browse files
committed
add API for IFile.readNBytes(n)
A convenience method that avoids callers to care about streams
1 parent 6f2754b commit 906371d

File tree

2 files changed

+54
-0
lines changed
  • resources
    • bundles/org.eclipse.core.resources/src/org/eclipse/core/resources
    • tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources

2 files changed

+54
-0
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/IFile.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,27 @@ public default byte[] readAllBytes() throws CoreException {
13331333
}
13341334
}
13351335

1336+
/**
1337+
* Reads the first bytes of the content in a byte array. Equivalent of calling
1338+
* {@code getContents(true).readNBytes(maxBytes)} but also closing the stream.
1339+
*
1340+
* @param maxBytes The maximal length of the returned array. If maxBytes is
1341+
* greater or equal to the file length the whole content is
1342+
* returned. If maxBytes is smaller then the file length then
1343+
* only the first maxBytes bytes are returned.
1344+
* @return content bytes
1345+
* @throws CoreException on error
1346+
* @see #getContents(boolean)
1347+
* @since 3.21
1348+
*/
1349+
public default byte[] readNBytes(int maxBytes) throws CoreException {
1350+
try (InputStream stream = getContents(true)) {
1351+
return stream.readNBytes(maxBytes);
1352+
} catch (IOException e) {
1353+
throw new CoreException(Status.error("Error reading " + getFullPath(), e)); //$NON-NLS-1$
1354+
}
1355+
}
1356+
13361357
/**
13371358
* Reads the content as char array. Skips the UTF BOM header if any. This method
13381359
* is not intended for reading in large files that do not fit in a char array.

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IFileTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,39 @@ public void testCreateByteArray() throws IOException, CoreException {
10521052
assertEquals(testString, target.readString());
10531053
}
10541054

1055+
@Test
1056+
public void testReadNBytes() throws IOException, CoreException {
1057+
IFile file = projects[0].getFile("smallfile");
1058+
byte[] bytes = "1234".getBytes(StandardCharsets.US_ASCII);
1059+
file.write(bytes, false, false, false, null);
1060+
try {
1061+
file.readNBytes(-1);
1062+
assertFalse(true);
1063+
} catch (IllegalArgumentException expected) {
1064+
// expected
1065+
}
1066+
byte[] nBytes0 = file.readNBytes(0);
1067+
assertEquals(0, nBytes0.length);
1068+
byte[] nBytes1 = file.readNBytes(1);
1069+
assertEquals(1, nBytes1.length);
1070+
assertEquals('1', nBytes1[0]);
1071+
byte[] nBytes4 = file.readNBytes(4);
1072+
assertEquals(4, nBytes4.length);
1073+
byte[] nBytes5 = file.readNBytes(5);
1074+
assertEquals(4, nBytes5.length);
1075+
byte[] nBytesMax = file.readNBytes(Integer.MAX_VALUE);
1076+
assertEquals(4, nBytesMax.length);
1077+
assertArrayEquals(bytes, nBytesMax);
1078+
1079+
IFile largefile = projects[0].getFile("largefile");
1080+
byte[] largeContent = new byte[50_000_000]; // only 50 MB to prevent OutOfMemoryError on jenkins
1081+
largefile.write(largeContent, false, false, false, null);
1082+
byte[] largeBytes = largefile.readNBytes(Integer.MAX_VALUE);
1083+
assertEquals(largeContent.length, largeBytes.length);
1084+
byte[] largeBytes1 = largefile.readNBytes(1);
1085+
assertEquals(1, largeBytes1.length);
1086+
}
1087+
10551088
@Test
10561089
public void testReadAll() throws IOException, CoreException {
10571090
List<Charset> charsets = List.of(StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8, StandardCharsets.UTF_16BE,

0 commit comments

Comments
 (0)