Skip to content

Commit c134f4a

Browse files
committed
DataHandle: provide some default implementations
1 parent fd5bf70 commit c134f4a

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

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

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ default String findString(final boolean saveString, final int blockSize,
331331
* @return the next byte of data, or -1 if the end of the stream is reached.
332332
* @throws IOException - if an I/O error occurs.
333333
*/
334-
int read() throws IOException;
334+
default int read() throws IOException {
335+
return offset() < length() ? readByte() & 0xff : -1;
336+
}
335337

336338
/**
337339
* Reads up to b.length bytes of data from the stream into an array of bytes.
@@ -369,4 +371,102 @@ default long skip(final long n) throws IOException {
369371
return num;
370372
}
371373

374+
// -- DataInput methods --
375+
376+
@Override
377+
default boolean readBoolean() throws IOException {
378+
return readByte() != 0;
379+
}
380+
381+
@Override
382+
default void readFully(final byte[] b) throws IOException {
383+
readFully(b, 0, b.length);
384+
}
385+
386+
@Override
387+
default int readUnsignedByte() throws IOException {
388+
return readByte() & 0xff;
389+
}
390+
391+
@Override
392+
default int readUnsignedShort() throws IOException {
393+
return readShort() & 0xffff;
394+
}
395+
396+
@Override
397+
default String readLine() throws IOException {
398+
// NB: Code adapted from java.io.RandomAccessFile.readLine().
399+
400+
final StringBuffer input = new StringBuffer();
401+
int c = -1;
402+
boolean eol = false;
403+
404+
while (!eol) {
405+
switch (c = read()) {
406+
case -1:
407+
case '\n':
408+
eol = true;
409+
break;
410+
case '\r':
411+
eol = true;
412+
long cur = offset();
413+
if (read() != '\n') seek(cur);
414+
break;
415+
default:
416+
input.append((char)c);
417+
break;
418+
}
419+
}
420+
421+
if (c == -1 && input.length() == 0) {
422+
return null;
423+
}
424+
return input.toString();
425+
}
426+
427+
@Override
428+
default String readUTF() throws IOException {
429+
final int length = readUnsignedShort();
430+
final byte[] b = new byte[length];
431+
read(b);
432+
return new String(b, "UTF-8");
433+
}
434+
435+
@Override
436+
default int skipBytes(final int n) throws IOException {
437+
final int skipped = (int) Math.min(n, length() - offset());
438+
if (skipped < 0) return 0;
439+
seek(offset() + skipped);
440+
return skipped;
441+
}
442+
443+
// -- DataOutput methods --
444+
445+
@Override
446+
default void write(final byte[] b) throws IOException {
447+
write(b, 0, b.length);
448+
}
449+
450+
@Override
451+
default void writeBoolean(final boolean v) throws IOException {
452+
write(v ? 1 : 0);
453+
}
454+
455+
@Override
456+
default void writeByte(final int v) throws IOException {
457+
write(v);
458+
}
459+
460+
@Override
461+
default void writeBytes(final String s) throws IOException {
462+
write(s.getBytes("UTF-8"));
463+
}
464+
465+
@Override
466+
default void writeUTF(final String str) throws IOException {
467+
final byte[] b = str.getBytes("UTF-8");
468+
writeShort(b.length);
469+
write(b);
470+
}
471+
372472
}

0 commit comments

Comments
 (0)