Skip to content

Commit 345a465

Browse files
committed
Rename TagLoader to NBTCodec and load() to readTag()
1 parent 0c64424 commit 345a465

7 files changed

Lines changed: 71 additions & 65 deletions

File tree

src/main/java/org/glavo/nbt/tag/TagLoader.java renamed to src/main/java/org/glavo/nbt/NBTCodec.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.glavo.nbt.tag;
16+
package org.glavo.nbt;
1717

18-
import org.glavo.nbt.MinecraftEdition;
19-
import org.glavo.nbt.internal.input.TagLoaderImpl;
18+
import org.glavo.nbt.internal.NBTCodecImpl;
19+
import org.glavo.nbt.tag.Tag;
2020
import org.jetbrains.annotations.ApiStatus;
2121
import org.jetbrains.annotations.Contract;
2222
import org.jetbrains.annotations.Nullable;
@@ -28,18 +28,18 @@
2828
import java.nio.file.Path;
2929

3030
/// The loader for reading NBT tags.
31-
public sealed interface TagLoader permits TagLoaderImpl {
31+
public sealed interface NBTCodec permits NBTCodecImpl {
3232

33-
/// Returns the default [TagLoader].
33+
/// Returns the default [NBTCodec].
3434
@Contract(pure = true)
35-
static TagLoader getDefault() {
36-
return TagLoaderImpl.DEFAULT;
35+
static NBTCodec getDefault() {
36+
return NBTCodecImpl.DEFAULT;
3737
}
3838

39-
/// Returns a new [TagLoader.Builder].
39+
/// Returns a new [NBTCodec.Builder].
4040
@Contract("-> new")
41-
static TagLoader.Builder newBuilder() {
42-
return new TagLoaderImpl.BuilderImpl();
41+
static NBTCodec.Builder newBuilder() {
42+
return new NBTCodecImpl.BuilderImpl();
4343
}
4444

4545
/// Returns whether the loader automatically decompresses the NBT data.
@@ -60,74 +60,74 @@ static TagLoader.Builder newBuilder() {
6060

6161
/// Loads a NBT tag from a byte array.
6262
@Contract(pure = true)
63-
Tag load(byte[] array) throws IOException;
63+
Tag readTag(byte[] array) throws IOException;
6464

6565
/// Loads the specified NBT tag from a byte array.
6666
@Contract(pure = true)
67-
default <T extends Tag> T load(byte[] array, Class<T> tagClass) throws IOException {
68-
return check(load(array), tagClass);
67+
default <T extends Tag> T readTag(byte[] array, Class<T> tagClass) throws IOException {
68+
return check(readTag(array), tagClass);
6969
}
7070

7171
/// Loads a NBT tag from a byte array with the specified offset and length.
7272
@Contract(pure = true)
73-
default Tag load(byte[] array, int offset, int length) throws IOException {
74-
return load(ByteBuffer.wrap(array, offset, length));
73+
default Tag readTag(byte[] array, int offset, int length) throws IOException {
74+
return readTag(ByteBuffer.wrap(array, offset, length));
7575
}
7676

7777
/// Loads the specified NBT tag from a byte array with the specified offset and length.
7878
@Contract(pure = true)
79-
default <T extends Tag> T load(byte[] array, int offset, int length, Class<T> tagClass) throws IOException {
80-
return check(load(array, offset, length), tagClass);
79+
default <T extends Tag> T readTag(byte[] array, int offset, int length, Class<T> tagClass) throws IOException {
80+
return check(readTag(array, offset, length), tagClass);
8181
}
8282

8383
/// Loads a NBT tag from a byte buffer.
8484
///
8585
/// This method does not change the position and the limit of the buffer.
8686
@Contract(pure = true)
87-
Tag load(ByteBuffer buffer) throws IOException;
87+
Tag readTag(ByteBuffer buffer) throws IOException;
8888

8989
/// Loads the specified NBT tag from a byte buffer.
9090
///
9191
/// This method does not change the position and the limit of the buffer.
9292
@Contract(pure = true)
93-
default <T extends Tag> T load(ByteBuffer buffer, Class<T> tagClass) throws IOException {
94-
return check(load(buffer), tagClass);
93+
default <T extends Tag> T readTag(ByteBuffer buffer, Class<T> tagClass) throws IOException {
94+
return check(readTag(buffer), tagClass);
9595
}
9696

9797
/// Loads a NBT tag from an input stream.
9898
///
9999
/// After this method is called, the state of the `inputStream` is undefined.
100100
@Contract(mutates = "param1")
101-
Tag load(InputStream inputStream) throws IOException;
101+
Tag readTag(InputStream inputStream) throws IOException;
102102

103103
/// Loads the specified NBT tag from an input stream.
104104
///
105105
/// After this method is called, the state of the `inputStream` is undefined.
106106
@Contract(mutates = "param1")
107-
default <T extends Tag> T load(InputStream inputStream, Class<T> tagClass) throws IOException {
108-
return check(load(inputStream), tagClass);
107+
default <T extends Tag> T readTag(InputStream inputStream, Class<T> tagClass) throws IOException {
108+
return check(readTag(inputStream), tagClass);
109109
}
110110

111111
/// Loads a NBT tag from a readable byte channel.
112112
///
113113
/// After this method is called, the state of the `channel` is undefined.
114114
@Contract(mutates = "param1")
115-
Tag load(ReadableByteChannel channel) throws IOException;
115+
Tag readTag(ReadableByteChannel channel) throws IOException;
116116

117117
/// Loads the specified NBT tag from a readable byte channel.
118118
///
119119
/// After this method is called, the state of the `channel` is undefined.
120120
@Contract(mutates = "param1")
121-
default <T extends Tag> T load(ReadableByteChannel channel, Class<T> tagClass) throws IOException {
122-
return check(load(channel), tagClass);
121+
default <T extends Tag> T readTag(ReadableByteChannel channel, Class<T> tagClass) throws IOException {
122+
return check(readTag(channel), tagClass);
123123
}
124124

125125
/// Loads a NBT tag from a file.
126-
Tag load(Path path) throws IOException;
126+
Tag readTag(Path path) throws IOException;
127127

128128
/// Loads the specified NBT tag from a file.
129-
default <T extends Tag> T load(Path path, Class<T> tagClass) throws IOException {
130-
return check(load(path), tagClass);
129+
default <T extends Tag> T readTag(Path path, Class<T> tagClass) throws IOException {
130+
return check(readTag(path), tagClass);
131131
}
132132

133133
private static <T extends Tag> T check(@Nullable Tag tag, Class<T> tagClass) throws IOException {
@@ -141,8 +141,8 @@ private static <T extends Tag> T check(@Nullable Tag tag, Class<T> tagClass) thr
141141
}
142142
}
143143

144-
/// The builder for creating a [TagLoader].
145-
sealed interface Builder permits TagLoaderImpl.BuilderImpl {
144+
/// The builder for creating a [NBTCodec].
145+
sealed interface Builder permits NBTCodecImpl.BuilderImpl {
146146

147147
/// Sets the Minecraft edition of the NBT data.
148148
///
@@ -160,7 +160,7 @@ sealed interface Builder permits TagLoaderImpl.BuilderImpl {
160160
@Contract(value = "_ -> this", mutates = "this")
161161
Builder setAutoDecompress(boolean autoDecompress);
162162

163-
/// Builds a new [TagLoader] with the specified configuration.
164-
TagLoader build();
163+
/// Builds a new [NBTCodec] with the specified configuration.
164+
NBTCodec build();
165165
}
166166
}

‎src/main/java/org/glavo/nbt/chunk/ChunkRegion.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.glavo.nbt.NBTParent;
2121
import org.glavo.nbt.internal.ChunkRegionHeader;
2222
import org.glavo.nbt.internal.ChunkUtils;
23+
import org.glavo.nbt.internal.NBTCodecImpl;
2324
import org.glavo.nbt.internal.input.*;
2425
import org.glavo.nbt.tag.CompoundTag;
2526
import org.jetbrains.annotations.Contract;
@@ -98,7 +99,7 @@ static ChunkRegion readRegion(RawDataReader rawReader) throws IOException {
9899
};
99100

100101
try (reader) {
101-
var tag = TagLoaderImpl.readTag(reader);
102+
var tag = NBTCodecImpl.readTag(reader);
102103
if (tag instanceof CompoundTag rootTag) {
103104
region.setChunk(localIndex, new Chunk(
104105
Instant.ofEpochSecond(header.getTimestampEpochSeconds(localIndex)),

src/main/java/org/glavo/nbt/internal/input/TagLoaderImpl.java renamed to src/main/java/org/glavo/nbt/internal/NBTCodecImpl.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.glavo.nbt.internal.input;
16+
package org.glavo.nbt.internal;
1717

1818
import org.glavo.nbt.MinecraftEdition;
19-
import org.glavo.nbt.internal.Access;
19+
import org.glavo.nbt.internal.input.DataReader;
20+
import org.glavo.nbt.internal.input.DecompressStreamDataReader;
21+
import org.glavo.nbt.internal.input.InputSource;
22+
import org.glavo.nbt.internal.input.RawDataReader;
2023
import org.glavo.nbt.tag.Tag;
21-
import org.glavo.nbt.tag.TagLoader;
24+
import org.glavo.nbt.NBTCodec;
2225
import org.glavo.nbt.tag.TagType;
2326
import org.jetbrains.annotations.Nullable;
2427

@@ -31,13 +34,13 @@
3134
import java.nio.file.StandardOpenOption;
3235
import java.util.Objects;
3336

34-
public record TagLoaderImpl(MinecraftEdition edition, boolean autoDecompress) implements TagLoader {
37+
public record NBTCodecImpl(MinecraftEdition edition, boolean autoDecompress) implements NBTCodec {
3538

36-
public TagLoaderImpl {
39+
public NBTCodecImpl {
3740
Objects.requireNonNull(edition, "edition");
3841
}
3942

40-
public static final TagLoaderImpl DEFAULT = new TagLoaderImpl(MinecraftEdition.JAVA_EDITION, true);
43+
public static final NBTCodecImpl DEFAULT = new NBTCodecImpl(MinecraftEdition.JAVA_EDITION, true);
4144

4245
public static @Nullable Tag readTag(DataReader reader) throws IOException {
4346
byte tagByte = reader.readByte();
@@ -93,35 +96,35 @@ private Tag check(@Nullable Tag tag) throws IOException {
9396
return tag;
9497
}
9598

96-
public Tag load(byte[] array) throws IOException {
99+
public Tag readTag(byte[] array) throws IOException {
97100
try (var reader = new RawDataReader(new InputSource.OfByteBuffer(array), edition)) {
98101
return check(autoDecompress ? readTagAutoDecompress(reader) : readTag(reader));
99102
}
100103
}
101104

102105
@Override
103-
public Tag load(ByteBuffer buffer) throws IOException {
106+
public Tag readTag(ByteBuffer buffer) throws IOException {
104107
try (var reader = new RawDataReader(new InputSource.OfByteBuffer(buffer), edition)) {
105108
return check(autoDecompress ? readTagAutoDecompress(reader) : readTag(reader));
106109
}
107110
}
108111

109112
@Override
110-
public Tag load(InputStream inputStream) throws IOException {
113+
public Tag readTag(InputStream inputStream) throws IOException {
111114
try (var reader = new RawDataReader(new InputSource.OfInputStream(inputStream, false), edition)) {
112115
return check(autoDecompress ? readTagAutoDecompress(reader) : readTag(reader));
113116
}
114117
}
115118

116119
@Override
117-
public Tag load(ReadableByteChannel channel) throws IOException {
120+
public Tag readTag(ReadableByteChannel channel) throws IOException {
118121
try (var reader = new RawDataReader(new InputSource.OfByteChannel(channel, false), edition)) {
119122
return check(autoDecompress ? readTagAutoDecompress(reader) : readTag(reader));
120123
}
121124
}
122125

123126
@Override
124-
public Tag load(Path path) throws IOException {
127+
public Tag readTag(Path path) throws IOException {
125128
try (var channel = Files.newByteChannel(path, StandardOpenOption.READ);
126129
var reader = new RawDataReader(new InputSource.OfByteChannel(channel, false), edition)) {
127130
return check(autoDecompress ? readTagAutoDecompress(reader) : readTag(reader));
@@ -145,8 +148,8 @@ public BuilderImpl setAutoDecompress(boolean autoDecompress) {
145148
}
146149

147150
@Override
148-
public TagLoader build() {
149-
return new TagLoaderImpl(edition, autoDecompress);
151+
public NBTCodec build() {
152+
return new NBTCodecImpl(edition, autoDecompress);
150153
}
151154
}
152155
}

‎src/main/java/org/glavo/nbt/tag/CompoundTag.java‎

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

1818
import org.glavo.nbt.internal.input.DataReader;
19-
import org.glavo.nbt.internal.input.TagLoaderImpl;
19+
import org.glavo.nbt.internal.NBTCodecImpl;
2020
import org.jetbrains.annotations.Contract;
2121
import org.jetbrains.annotations.Nullable;
2222

@@ -212,7 +212,7 @@ protected void readContent(DataReader reader) throws IOException {
212212
int count = 0;
213213

214214
Tag subTag;
215-
while ((subTag = TagLoaderImpl.readTag(reader)) != null) {
215+
while ((subTag = NBTCodecImpl.readTag(reader)) != null) {
216216
count++;
217217
add(subTag);
218218
}

src/test/java/org/glavo/nbt/tag/TagLoaderTest.java renamed to src/test/java/org/glavo/nbt/NBTCodecTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.glavo.nbt.tag;
16+
package org.glavo.nbt;
1717

18-
import org.glavo.nbt.TestResources;
18+
import org.glavo.nbt.tag.CompoundTag;
19+
import org.glavo.nbt.tag.Tag;
1920
import org.junit.jupiter.params.ParameterizedTest;
2021
import org.junit.jupiter.params.provider.EnumSource;
2122

@@ -30,53 +31,53 @@
3031

3132
import static org.junit.jupiter.api.Assertions.*;
3233

33-
public final class TagLoaderTest {
34+
public final class NBTCodecTest {
3435

3536
enum Loader {
3637
BYTE_ARRAY {
3738
@Override
3839
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
39-
return TagLoader.getDefault().load(Files.readAllBytes(file), tagClass);
40+
return NBTCodec.getDefault().readTag(Files.readAllBytes(file), tagClass);
4041
}
4142
},
4243
HEAP_BYTE_BUFFER {
4344
@Override
4445
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
45-
return TagLoader.getDefault().load(ByteBuffer.wrap(Files.readAllBytes(file)), tagClass);
46+
return NBTCodec.getDefault().readTag(ByteBuffer.wrap(Files.readAllBytes(file)), tagClass);
4647
}
4748
},
4849
DIRECT_BYTE_BUFFER {
4950
@Override
5051
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
51-
return TagLoader.getDefault().load(ByteBuffer.allocateDirect((int) Files.size(file)).put(Files.readAllBytes(file)).flip(), tagClass);
52+
return NBTCodec.getDefault().readTag(ByteBuffer.allocateDirect((int) Files.size(file)).put(Files.readAllBytes(file)).flip(), tagClass);
5253
}
5354
},
5455
INPUT_STREAM {
5556
@Override
5657
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
57-
return TagLoader.getDefault().load(file, tagClass);
58+
return NBTCodec.getDefault().readTag(file, tagClass);
5859
}
5960
},
6061
FILE_CHANNEL {
6162
@Override
6263
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
6364
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) {
64-
return TagLoader.getDefault().load(channel, tagClass);
65+
return NBTCodec.getDefault().readTag(channel, tagClass);
6566
}
6667
}
6768
},
6869
READABLE_BYTE_CHANNEL {
6970
@Override
7071
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
7172
try (ReadableByteChannel channel = Channels.newChannel(Files.newInputStream(file))) {
72-
return TagLoader.getDefault().load(channel, tagClass);
73+
return NBTCodec.getDefault().readTag(channel, tagClass);
7374
}
7475
}
7576
},
7677
PATH {
7778
@Override
7879
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
79-
return TagLoader.getDefault().load(file, tagClass);
80+
return NBTCodec.getDefault().readTag(file, tagClass);
8081
}
8182
};
8283

@@ -86,7 +87,7 @@ <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
8687
/// Loads a level.dat file (compressed with gzip)
8788
@ParameterizedTest
8889
@EnumSource
89-
void testLoadLevelDat(Loader loader) throws IOException {
90+
void testReadLevelDat(Loader loader) throws IOException {
9091
Path levelDatPath = TestResources.getResource("/assets/nbt/level.dat");
9192
CompoundTag levelDat = loader.load(levelDatPath, CompoundTag.class);
9293
assertEquals("", levelDat.getName());
@@ -100,7 +101,7 @@ void testLoadLevelDat(Loader loader) throws IOException {
100101
/// Loads an uncompressed level.dat file
101102
@ParameterizedTest
102103
@EnumSource
103-
void testLoadLevelDatRaw(Loader loader) throws IOException {
104+
void testReadLevelDatRaw(Loader loader) throws IOException {
104105
Path levelDatPath = TestResources.getResource("/assets/nbt/level.dat.raw");
105106
CompoundTag levelDat = loader.load(levelDatPath, CompoundTag.class);
106107
assertEquals("", levelDat.getName());

‎src/test/java/org/glavo/nbt/chunk/ChunkRegionTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.glavo.nbt.internal.input.InputSource;
2424
import org.glavo.nbt.internal.input.RawDataReader;
2525
import org.glavo.nbt.tag.CompoundTag;
26-
import org.glavo.nbt.tag.TagLoader;
26+
import org.glavo.nbt.NBTCodec;
2727
import org.junit.jupiter.api.Test;
2828
import org.junit.jupiter.params.ParameterizedTest;
2929
import org.junit.jupiter.params.provider.ValueSource;
@@ -150,7 +150,7 @@ static RefChunkRegion load(Path file) throws IOException {
150150
}
151151

152152
try (InputStream in = input) {
153-
var tag = TagLoader.getDefault().load(in);
153+
var tag = NBTCodec.getDefault().readTag(in);
154154

155155
if (tag instanceof CompoundTag chunk) {
156156
result.chunks[i / 4] = chunk;

0 commit comments

Comments
 (0)