Skip to content

Commit daa9145

Browse files
committed
DataHandle: add readFully(byte[], int, int) impl
1 parent 6f2994f commit daa9145

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/org/scijava/io/handle/DataHandle.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.io.Closeable;
3535
import java.io.DataInput;
36+
import java.io.DataInputStream;
3637
import java.io.DataOutput;
3738
import java.io.EOFException;
3839
import java.io.IOException;
@@ -445,6 +446,20 @@ default void readFully(final byte[] b) throws IOException {
445446
readFully(b, 0, b.length);
446447
}
447448

449+
@Override
450+
default void readFully(final byte[] b, final int off, final int len)
451+
throws IOException
452+
{
453+
// NB: Adapted from java.io.DataInputStream.readFully(byte[], int, int).
454+
if (len < 0) throw new IndexOutOfBoundsException();
455+
int n = 0;
456+
while (n < len) {
457+
int count = read(b, off + n, len - n);
458+
if (count < 0) throw new EOFException();
459+
n += count;
460+
}
461+
}
462+
448463
@Override
449464
default int skipBytes(final int n) throws IOException {
450465
// NB: Cast here is safe since the value of n bounds the result to an int.

0 commit comments

Comments
 (0)