Skip to content

Commit b83e237

Browse files
committed
Refactor byte order handling in IOUtils and Tag classes
1 parent c99f99f commit b83e237

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import org.glavo.nbt.tag.Tag;
1919

2020
import java.lang.invoke.MethodHandles;
21-
import java.nio.ByteOrder;
2221

2322
public final class IOUtils {
2423
public static final Tag.Unsafe TAG_UNSAFE = Tag.Unsafe.getUnsafe(MethodHandles.lookup());
25-
public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN;
2624

2725
public static final int DEFAULT_BUFFER_SIZE = 8192;
2826

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ public final class NBTReader implements Closeable {
3535

3636
private final InputSource source;
3737
private final InputBuffer buffer;
38+
private final ByteOrder byteOrder;
3839

3940
/// Used for reading UTF-8 strings
4041
private @Nullable StringBuilder charsBuffer;
4142

4243
public NBTReader(InputSource source, ByteOrder byteOrder) {
4344
this.source = source;
4445
this.buffer = InputBuffer.allocate(IOUtils.DEFAULT_BUFFER_SIZE, source.supportDirectBuffer(), byteOrder);
46+
this.byteOrder = byteOrder;
47+
}
48+
49+
public ByteOrder byteOrder() {
50+
return this.byteOrder;
4551
}
4652

4753
@Override
@@ -165,7 +171,7 @@ public String readString() throws IOException {
165171
bytes.position(limit);
166172

167173
// For Minecraft Bedrock Edition, the string is encoded in standard UTF-8
168-
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) {
174+
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
169175
return getUTF8(bytes, offset, len);
170176
}
171177

src/main/java/org/glavo/nbt/tag/Tag.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.glavo.nbt.tag;
1717

1818
import org.glavo.nbt.NBTElement;
19-
import org.glavo.nbt.internal.IOUtils;
2019
import org.glavo.nbt.internal.input.InputSource;
2120
import org.glavo.nbt.internal.input.NBTReader;
2221
import org.glavo.nbt.internal.output.NBTWriter;
@@ -52,7 +51,7 @@ public sealed abstract class Tag implements NBTElement
5251
}
5352

5453
public static Tag readTag(InputStream inputStream) throws IOException {
55-
return readTag(inputStream, IOUtils.DEFAULT_BYTE_ORDER);
54+
return readTag(inputStream, ByteOrder.BIG_ENDIAN);
5655
}
5756

5857
public static Tag readTag(InputStream inputStream, ByteOrder byteOrder) throws IOException {
@@ -66,7 +65,7 @@ public static Tag readTag(InputStream inputStream, ByteOrder byteOrder) throws I
6665
}
6766

6867
public static CompoundTag<?> readCompoundTag(InputStream inputStream) throws IOException {
69-
return readCompoundTag(inputStream, IOUtils.DEFAULT_BYTE_ORDER);
68+
return readCompoundTag(inputStream, ByteOrder.BIG_ENDIAN);
7069
}
7170

7271
public static CompoundTag<?> readCompoundTag(InputStream inputStream, ByteOrder byteOrder) throws IOException {
@@ -125,7 +124,7 @@ public int getIndex() {
125124
}
126125

127126
public void writeTo(OutputStream outputStream) throws IOException {
128-
writeTo(outputStream, IOUtils.DEFAULT_BYTE_ORDER);
127+
writeTo(outputStream, ByteOrder.BIG_ENDIAN);
129128
}
130129

131130
public void writeTo(OutputStream outputStream, ByteOrder byteOrder) throws IOException {

0 commit comments

Comments
 (0)