Skip to content

Commit 19ec34a

Browse files
committed
Add NBT tag write API for ByteBuffer
1 parent 75d6652 commit 19ec34a

4 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
for writing chunk regions to files.
77
- New API `NBTCodec#byteSize(Tag)` and `NBTCodec#contentByteSize(Tag)` for calculating the encoded byte size of NBT
88
tags.
9+
- New API `NBTCodec#writeTag(ByteBuffer, Tag)` for writing NBT tags to a byte buffer.
10+
- New API `NBTCodec#writeTagToByteArray(Tag)` for writing NBT tags to a byte array.
911
- New API `ArrayTag#getElementType()` for getting the element type of array tags.
1012

1113
## 0.1.0 (2026-03-16)

‎src/main/java/org/glavo/nbt/internal/output/OutputTarget.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,55 @@ public void skip(long bytes) throws IOException {
218218
}
219219
}
220220
}
221+
222+
public static final class OfByteBuffer extends OutputTarget {
223+
private final ByteBuffer targetBuffer;
224+
private long position;
225+
226+
public OfByteBuffer(ByteBuffer targetBuffer) {
227+
this.targetBuffer = targetBuffer;
228+
}
229+
230+
@Override
231+
public long position() {
232+
return position;
233+
}
234+
235+
@Override
236+
protected void closeImpl() {
237+
}
238+
239+
@Override
240+
public void write(OutputBuffer buffer) throws IOException {
241+
ensureOpen();
242+
OutputBuffer.assertStatus(buffer);
243+
244+
int pending = buffer.pending();
245+
if (targetBuffer.remaining() < pending) {
246+
throw new IOException("Not enough space in target buffer");
247+
}
248+
249+
targetBuffer.put(buffer.getByteBuffer().flip());
250+
buffer.getByteBuffer().clear();
251+
position += pending;
252+
}
253+
254+
@Override
255+
public void skip(long bytes) throws IOException {
256+
ensureOpen();
257+
if (bytes < 0) {
258+
throw new IllegalArgumentException("Negative skip length: " + bytes);
259+
}
260+
if (bytes == 0) {
261+
return;
262+
}
263+
264+
if (targetBuffer.remaining() < bytes) {
265+
throw new IOException("Not enough space in target buffer");
266+
}
267+
268+
targetBuffer.position(targetBuffer.position() + (int) bytes);
269+
position += bytes;
270+
}
271+
}
221272
}

‎src/main/java/org/glavo/nbt/io/NBTCodec.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,37 @@ public void writeTag(WritableByteChannel channel, Tag tag) throws IOException {
445445
}
446446
}
447447

448+
/// Writes a NBT tag to the byte buffer.
449+
///
450+
/// `buffer.remaining()` must be greater than or equal to [`byteSize(tag)`](#byteSize(Tag)),
451+
/// otherwise an `IOException` will be thrown, and the state of the `buffer` is undefined.
452+
///
453+
/// After the method call, `buffer.position()` will increase by [`byteSize(tag)`](#byteSize(Tag)).
454+
public void writeTag(ByteBuffer buffer, Tag tag) throws IOException {
455+
try (var writer = new RawDataWriter(new OutputTarget.OfByteBuffer(buffer), getEdition())) {
456+
NBTOutput.writeTag(writer, tag);
457+
}
458+
}
459+
460+
/// Writes a NBT tag to a byte array.
461+
///
462+
/// The returned byte array will have a length equal to [`byteSize(tag)`](#byteSize(Tag)).
463+
public byte[] writeTagToByteArray(Tag tag) throws IOException {
464+
ByteBuffer buffer = ByteBuffer.allocate((int) byteSize(tag));
465+
466+
try {
467+
writeTag(buffer, tag);
468+
} catch (IOException e) {
469+
throw new AssertionError("Unexpected error when writing tag to byte array", e);
470+
}
471+
472+
if (buffer.remaining() != 0) {
473+
throw new AssertionError("Unexpected remaining bytes in buffer: " + buffer.remaining());
474+
}
475+
476+
return buffer.array();
477+
}
478+
448479
/// Reads a chunk region from a file.
449480
///
450481
/// @see #getExternalChunkAccessorFactory()

‎src/test/java/org/glavo/nbt/io/WriteTagTest.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,20 @@ public byte[] toByteArray(Tag tag) throws IOException {
6767
return buffer.toByteArray();
6868
}
6969
}
70+
71+
record ByteBuffer(MinecraftEdition edition) implements Validator {
72+
@Override
73+
public byte[] toByteArray(Tag tag) throws IOException {
74+
return NBTCodec.of(edition).writeTagToByteArray(tag);
75+
}
76+
}
7077
}
7178

7279
static Stream<Validator> validators() {
7380
return Stream.of(MinecraftEdition.values()).flatMap(edition -> Stream.of(
7481
new Validator.OutputStream(edition),
75-
new Validator.ByteChannel(edition)
82+
new Validator.ByteChannel(edition),
83+
new Validator.ByteBuffer(edition)
7684
));
7785
}
7886

0 commit comments

Comments
 (0)