Skip to content

Commit b48c5a7

Browse files
committed
IO: Stream wrappers for the Byte-based I/O.
1 parent 305bada commit b48c5a7

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

io/src/main/java/co/casterlabs/commons/io/bytes/reading/ByteReader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
package co.casterlabs.commons.io.bytes.reading;
1313

1414
import java.io.IOException;
15+
import java.io.InputStream;
1516

1617
import co.casterlabs.commons.io.bytes.EndOfStreamException;
1718

1819
public abstract class ByteReader implements AutoCloseable {
1920
public final Endian le = new LittleEndian();
2021
public final Endian be = new BigEndian();
2122

23+
public final InputStream asStream = new _AsInputStream(this);
24+
2225
/**
2326
* Skips len bytes from the source.
2427
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package co.casterlabs.commons.io.bytes.reading;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import co.casterlabs.commons.io.bytes.EndOfStreamException;
7+
import lombok.AllArgsConstructor;
8+
9+
@AllArgsConstructor
10+
class _AsInputStream extends InputStream {
11+
private final ByteReader reader;
12+
13+
@Override
14+
public int read() throws IOException {
15+
try {
16+
return this.reader.read();
17+
} catch (EndOfStreamException e) {
18+
return -1;
19+
}
20+
}
21+
22+
@Override
23+
public int read(byte[] b, int off, int len) throws IOException {
24+
try {
25+
this.reader.read(b, off, len);
26+
return len;
27+
} catch (EndOfStreamException e) {
28+
return -1;
29+
}
30+
}
31+
32+
}

io/src/main/java/co/casterlabs/commons/io/bytes/writing/ByteWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
package co.casterlabs.commons.io.bytes.writing;
1313

1414
import java.io.IOException;
15+
import java.io.OutputStream;
1516

1617
public abstract class ByteWriter implements AutoCloseable {
1718
public final Endian le = new LittleEndian();
1819
public final Endian be = new BigEndian();
1920

21+
public final OutputStream asStream = new _AsOutputStream(this);
22+
2023
/**
2124
* @throws IOException if an I/O error occurs
2225
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package co.casterlabs.commons.io.bytes.writing;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
6+
import lombok.AllArgsConstructor;
7+
8+
@AllArgsConstructor
9+
class _AsOutputStream extends OutputStream {
10+
private final ByteWriter writer;
11+
12+
@Override
13+
public void write(int b) throws IOException {
14+
this.writer.write(b);
15+
}
16+
17+
@Override
18+
public void write(byte[] b) throws IOException {
19+
this.writer.write(b);
20+
}
21+
22+
@Override
23+
public void write(byte[] b, int off, int len) throws IOException {
24+
this.writer.write(b, off, len);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)