Skip to content

Commit d00c57f

Browse files
committed
Refactor Tag.Unsafe into TagAccess and Access
1 parent c338615 commit d00c57f

5 files changed

Lines changed: 89 additions & 63 deletions

File tree

src/main/java/org/glavo/nbt/internal/IOUtils.java renamed to src/main/java/org/glavo/nbt/internal/Access.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
*/
1616
package org.glavo.nbt.internal;
1717

18-
import org.glavo.nbt.tag.Tag;
18+
import org.glavo.nbt.tag.TagAccess;
1919

2020
import java.lang.invoke.MethodHandles;
2121

22-
public final class IOUtils {
23-
public static final Tag.Unsafe TAG_UNSAFE = Tag.Unsafe.getUnsafe(MethodHandles.lookup());
22+
public final class Access {
23+
public static final TagAccess TAG;
2424

25-
public static final int DEFAULT_BUFFER_SIZE = 8192;
25+
static {
26+
MethodHandles.Lookup lookup = MethodHandles.lookup();
2627

27-
private IOUtils() {
28+
TAG = TagAccess.getInstance(lookup);
2829
}
2930
}

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

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

1818
import org.glavo.nbt.MinecraftEdition;
19-
import org.glavo.nbt.internal.IOUtils;
2019
import org.glavo.nbt.internal.StringCache;
2120
import org.jetbrains.annotations.Nullable;
2221

@@ -27,6 +26,8 @@
2726
import java.util.Map;
2827

2928
public final class RawDataReader extends DataReader implements Closeable {
29+
public static final int DEFAULT_BUFFER_SIZE = 8192;
30+
3031
// Used for reading UTF-8 strings
3132
private static final StringCache DEFAULT_CACHE = new StringCache(
3233
"data", "Data", "DataVersion"
@@ -47,7 +48,7 @@ public final class RawDataReader extends DataReader implements Closeable {
4748
public RawDataReader(InputSource source, MinecraftEdition edition) {
4849
this.source = source;
4950
this.edition = edition;
50-
this.buffer = InputBuffer.allocate(IOUtils.DEFAULT_BUFFER_SIZE, source.supportDirectBuffer(), edition.byteOrder());
51+
this.buffer = InputBuffer.allocate(DEFAULT_BUFFER_SIZE, source.supportDirectBuffer(), edition.byteOrder());
5152
this.sourceStartPosition = source.position();
5253
}
5354

@@ -94,7 +95,7 @@ public void skip(long bytes) throws IOException {
9495

9596
public InputBuffer getDecompressBuffer() {
9697
if (decompressBuffer == null) {
97-
return InputBuffer.allocate(IOUtils.DEFAULT_BUFFER_SIZE, false, edition.byteOrder());
98+
return InputBuffer.allocate(DEFAULT_BUFFER_SIZE, false, edition.byteOrder());
9899
} else {
99100
decompressBuffer.drop();
100101
return decompressBuffer;

src/main/java/org/glavo/nbt/internal/output/NBTWriter.java

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

1818
import org.glavo.nbt.MinecraftEdition;
19-
import org.glavo.nbt.internal.IOUtils;
19+
import org.glavo.nbt.internal.Access;
2020
import org.glavo.nbt.tag.*;
2121

2222
import java.io.*;
@@ -74,11 +74,11 @@ private void writeContent(Tag tag) throws IOException {
7474
} else if (tag instanceof StringTag stringTag) {
7575
writeString(stringTag.get());
7676
} else if (tag instanceof ByteArrayTag byteArrayTag) {
77-
writeByteArray(IOUtils.TAG_UNSAFE.getInternalArray(byteArrayTag));
77+
writeByteArray(Access.TAG.getInternalArray(byteArrayTag));
7878
} else if (tag instanceof IntArrayTag intArrayTag) {
79-
writeIntArray(IOUtils.TAG_UNSAFE.getInternalArray(intArrayTag));
79+
writeIntArray(Access.TAG.getInternalArray(intArrayTag));
8080
} else if (tag instanceof LongArrayTag longArrayTag) {
81-
writeLongArray(IOUtils.TAG_UNSAFE.getInternalArray(longArrayTag));
81+
writeLongArray(Access.TAG.getInternalArray(longArrayTag));
8282
} else if (tag instanceof ListTag<?> listTag) {
8383
writeByte(listTag.getElementType() != null ? listTag.getElementType().id() : 0);
8484
writeInt(listTag.size());

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

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -220,55 +220,4 @@ public boolean equals(Object obj) {
220220
&& this.contentEquals(that);
221221
}
222222

223-
/// Unsafe operations for internal use.
224-
public static final class Unsafe {
225-
private static final Unsafe INSTANCE = new Unsafe();
226-
227-
/// Get an instance of the unsafe operations.
228-
///
229-
/// @param lookup A lookup object used to check whether a user has access rights to the [Tag].
230-
/// @return An instance of the unsafe operations.
231-
/// @throws UnsupportedOperationException if the user does not have access rights to the [Tag].
232-
public static Unsafe getUnsafe(MethodHandles.Lookup lookup) {
233-
try {
234-
MethodHandles.privateLookupIn(Tag.class, lookup);
235-
} catch (IllegalAccessException e) {
236-
throw new UnsupportedOperationException(e);
237-
}
238-
return INSTANCE;
239-
}
240-
241-
private Unsafe() {
242-
}
243-
244-
/// Returns the internal value of the tag without cloning.
245-
public byte[] getInternalArray(ByteArrayTag tag) {
246-
return tag.value;
247-
}
248-
249-
/// Sets the internal value of the tag without cloning.
250-
public void setInternalArray(ByteArrayTag tag, byte[] value) {
251-
tag.value = value;
252-
}
253-
254-
/// Returns the internal value of the tag without cloning.
255-
public int[] getInternalArray(IntArrayTag tag) {
256-
return tag.value;
257-
}
258-
259-
/// Sets the internal value of the tag without cloning.
260-
public void setInternalArray(IntArrayTag tag, int[] value) {
261-
tag.value = value;
262-
}
263-
264-
/// Returns the internal value of the tag without cloning.
265-
public long[] getInternalArray(LongArrayTag tag) {
266-
return tag.value;
267-
}
268-
269-
/// Sets the internal value of the tag without cloning.
270-
public void setInternalArray(LongArrayTag tag, long[] value) {
271-
tag.value = value;
272-
}
273-
}
274223
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.tag;
17+
18+
import org.jetbrains.annotations.ApiStatus;
19+
20+
import java.lang.invoke.MethodHandles;
21+
22+
/// Internal access to the tag implementation.
23+
///
24+
/// This class is **NOT** a public API and should not be used directly.
25+
@ApiStatus.Internal
26+
public final class TagAccess {
27+
private static final TagAccess INSTANCE = new TagAccess();
28+
29+
/// Get an instance of the internal operations.
30+
///
31+
/// @param lookup A lookup object used to check whether a user has access rights to the [Tag].
32+
/// @return An instance of the internal operations.
33+
/// @throws UnsupportedOperationException if the user does not have access rights to the [Tag].
34+
public static TagAccess getInstance(MethodHandles.Lookup lookup) {
35+
try {
36+
MethodHandles.privateLookupIn(Tag.class, lookup);
37+
} catch (IllegalAccessException e) {
38+
throw new UnsupportedOperationException(e);
39+
}
40+
return INSTANCE;
41+
}
42+
43+
private TagAccess() {
44+
}
45+
46+
/// Returns the internal value of the tag without cloning.
47+
public byte[] getInternalArray(ByteArrayTag tag) {
48+
return tag.value;
49+
}
50+
51+
/// Sets the internal value of the tag without cloning.
52+
public void setInternalArray(ByteArrayTag tag, byte[] value) {
53+
tag.value = value;
54+
}
55+
56+
/// Returns the internal value of the tag without cloning.
57+
public int[] getInternalArray(IntArrayTag tag) {
58+
return tag.value;
59+
}
60+
61+
/// Sets the internal value of the tag without cloning.
62+
public void setInternalArray(IntArrayTag tag, int[] value) {
63+
tag.value = value;
64+
}
65+
66+
/// Returns the internal value of the tag without cloning.
67+
public long[] getInternalArray(LongArrayTag tag) {
68+
return tag.value;
69+
}
70+
71+
/// Sets the internal value of the tag without cloning.
72+
public void setInternalArray(LongArrayTag tag, long[] value) {
73+
tag.value = value;
74+
}
75+
}

0 commit comments

Comments
 (0)