Skip to content

Commit a327d41

Browse files
committed
Add InputSource and InputBuffer classes for I/O
1 parent df82d91 commit a327d41

3 files changed

Lines changed: 149 additions & 1 deletion

File tree

‎src/main/java/org/glavo/nbt/internal/IOUtils.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
public final class IOUtils {
2424
public static final Tag.Unsafe TAG_UNSAFE = Tag.Unsafe.getUnsafe(MethodHandles.lookup());
25-
2625
public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN;
2726

27+
public static final int DEFAULT_BUFFER_SIZE = 8192;
28+
2829
private IOUtils() {
2930
}
3031
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2026 Glavo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.glavo.nbt.internal.input;
17+
18+
import org.glavo.nbt.internal.IOUtils;
19+
import org.jetbrains.annotations.Nullable;
20+
21+
import java.nio.ByteBuffer;
22+
import java.nio.ByteOrder;
23+
24+
public final class InputBuffer {
25+
public static InputBuffer allocateHeap(ByteOrder byteOrder) {
26+
return new InputBuffer(ByteBuffer.allocate(IOUtils.DEFAULT_BUFFER_SIZE).order(byteOrder));
27+
}
28+
29+
public static InputBuffer allocateDirect(ByteOrder byteOrder) {
30+
return new InputBuffer(ByteBuffer.allocateDirect(IOUtils.DEFAULT_BUFFER_SIZE).order(byteOrder));
31+
}
32+
33+
ByteBuffer bytesBuffer;
34+
private @Nullable StringBuilder charsBuffer;
35+
36+
public InputBuffer(ByteBuffer bytesBuffer) {
37+
this.bytesBuffer = bytesBuffer;
38+
}
39+
40+
public ByteBuffer bytesBuffer() {
41+
return bytesBuffer;
42+
}
43+
44+
public ByteOrder order() {
45+
return bytesBuffer.order();
46+
}
47+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2026 Glavo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.glavo.nbt.internal.input;
17+
18+
import java.io.Closeable;
19+
import java.io.EOFException;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.ByteBuffer;
23+
24+
public sealed abstract class InputSource implements Closeable {
25+
private boolean closed = false;
26+
protected long bytesRead = 0L;
27+
28+
@Override
29+
public final void close() throws IOException {
30+
if (!closed) {
31+
closed = true;
32+
closeImpl();
33+
}
34+
}
35+
36+
protected abstract void closeImpl() throws IOException;
37+
38+
protected final void ensureOpen() throws IOException {
39+
if (closed) {
40+
throw new IOException("Stream closed");
41+
}
42+
}
43+
44+
public final void fillBuffer(InputBuffer buffer, int required) throws IOException {
45+
ByteBuffer bytesBuffer = buffer.bytesBuffer();
46+
47+
if (bytesBuffer.remaining() > required) {
48+
return;
49+
}
50+
51+
if (buffer.bytesBuffer().capacity() < required) {
52+
ByteBuffer newBuffer = ByteBuffer.allocate(Math.max(required, bytesBuffer.capacity() * 2))
53+
.order(bytesBuffer.order());
54+
newBuffer.put(bytesBuffer);
55+
newBuffer.flip();
56+
buffer.bytesBuffer = bytesBuffer = newBuffer;
57+
} else if (bytesBuffer.position() > 0) {
58+
bytesBuffer.compact();
59+
bytesBuffer.flip();
60+
}
61+
62+
fillBufferImpl(bytesBuffer, required);
63+
}
64+
65+
protected abstract void fillBufferImpl(ByteBuffer target, int required) throws IOException;
66+
67+
public static final class OfInputStream extends InputSource {
68+
private final InputStream inputStream;
69+
private final boolean closeInputStream;
70+
71+
public OfInputStream(InputStream inputStream, boolean closeInputStream) {
72+
this.inputStream = inputStream;
73+
this.closeInputStream = closeInputStream;
74+
}
75+
76+
@Override
77+
protected void closeImpl() throws IOException {
78+
if (closeInputStream) {
79+
inputStream.close();
80+
}
81+
}
82+
83+
@Override
84+
protected void fillBufferImpl(ByteBuffer target, int required) throws IOException {
85+
assert target.hasArray();
86+
87+
while (target.remaining() < required) {
88+
int offset = target.limit() + target.arrayOffset();
89+
int len = target.capacity() - target.limit();
90+
91+
int read = inputStream.read(target.array(), offset, len);
92+
if (read < 0) {
93+
throw new EOFException("Unexpected end of stream");
94+
}
95+
target.limit(target.limit() + read);
96+
bytesRead += read;
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)