Skip to content

Commit 4ccf63d

Browse files
committed
Extract InputStream impl to BoundedDataReader
1 parent 105e0f7 commit 4ccf63d

2 files changed

Lines changed: 51 additions & 42 deletions

File tree

src/main/java/org/glavo/nbt/internal/input/BoundedDataReader.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
*/
1616
package org.glavo.nbt.internal.input;
1717

18+
import org.jetbrains.annotations.Nullable;
19+
20+
import java.io.EOFException;
1821
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.util.Objects;
1924

2025
public abstract non-sealed class BoundedDataReader extends DataReader {
2126
private final RawDataReader rawReader;
@@ -51,4 +56,49 @@ public void close() throws IOException {
5156

5257
getRawReader().skip(endPosition - currentPosition);
5358
}
59+
60+
protected final InputStream asInputStream() {
61+
return new InputStream() {
62+
private byte @Nullable [] singleByte;
63+
64+
@Override
65+
public int read() throws IOException {
66+
if (singleByte == null) {
67+
singleByte = new byte[1];
68+
}
69+
70+
if (read(singleByte) < 1) {
71+
return -1;
72+
} else {
73+
return Byte.toUnsignedInt(singleByte[0]);
74+
}
75+
}
76+
77+
@Override
78+
public int read(byte[] b, int off, int len) throws IOException {
79+
Objects.checkFromIndexSize(off, len, b.length);
80+
if (len == 0) {
81+
return 0;
82+
}
83+
84+
long rawRemaining = endPosition - rawReader.position();
85+
if (rawRemaining <= 0) {
86+
return -1;
87+
}
88+
89+
if (rawReader.getBuffer().remaining() == 0) {
90+
try {
91+
rawReader.ensureBufferRemaining(1);
92+
} catch (EOFException e) {
93+
return -1;
94+
}
95+
}
96+
97+
int n = (int) Math.min(Math.min(len, rawReader.getBuffer().remaining()), rawRemaining);
98+
rawReader.getBuffer().getBytes(b, off, n);
99+
return n;
100+
}
101+
102+
};
103+
}
54104
}

src/main/java/org/glavo/nbt/internal/input/LZ4DataReader.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,7 @@ public final class LZ4DataReader extends BoundedDataReader {
3131

3232
public LZ4DataReader(RawDataReader rawReader, long limit) {
3333
super(rawReader, rawReader.getDecompressBuffer(), limit);
34-
this.lz4Stream = LZ4BlockInputStream.newBuilder().build(new InputStream() {
35-
private byte @Nullable [] singleByte;
36-
37-
@Override
38-
public int read() throws IOException {
39-
if (singleByte == null) {
40-
singleByte = new byte[1];
41-
}
42-
43-
if (read(singleByte) < 1) {
44-
return -1;
45-
} else {
46-
return Byte.toUnsignedInt(singleByte[0]);
47-
}
48-
}
49-
50-
@Override
51-
public int read(byte[] b, int off, int len) throws IOException {
52-
Objects.checkFromIndexSize(off, len, b.length);
53-
if (len == 0) {
54-
return 0;
55-
}
56-
57-
long rawRemaining = endPosition - rawReader.position();
58-
if (rawRemaining <= 0) {
59-
return -1;
60-
}
61-
62-
if (rawReader.getBuffer().remaining() == 0) {
63-
try {
64-
rawReader.ensureBufferRemaining(1);
65-
} catch (EOFException e) {
66-
return -1;
67-
}
68-
}
69-
70-
int n = (int) Math.min(Math.min(len, rawReader.getBuffer().remaining()), rawRemaining);
71-
rawReader.getBuffer().getBytes(b, off, n);
72-
return n;
73-
}
74-
75-
});
34+
this.lz4Stream = LZ4BlockInputStream.newBuilder().build(asInputStream());
7635

7736
assert getBuffer().getByteBuffer().hasArray();
7837
}

0 commit comments

Comments
 (0)