Skip to content

Commit 85c150e

Browse files
committed
assorted changes™
1 parent 137c266 commit 85c150e

File tree

2 files changed

+17
-342
lines changed

2 files changed

+17
-342
lines changed

worldedit-core/src/main/java/com/sk89q/jnbt/NBTInputStream.java

+8-112
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919

2020
package com.sk89q.jnbt;
2121

22+
import com.sk89q.worldedit.util.nbt.BinaryTagIO;
23+
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
24+
2225
import java.io.Closeable;
26+
import java.io.DataInput;
2327
import java.io.DataInputStream;
2428
import java.io.IOException;
2529
import java.io.InputStream;
26-
import java.util.ArrayList;
27-
import java.util.HashMap;
28-
import java.util.List;
2930
import java.util.Map;
3031

3132
/**
@@ -63,115 +64,10 @@ public NBTInputStream(InputStream is) {
6364
* @throws IOException if an I/O error occurs.
6465
*/
6566
public NamedTag readNamedTag() throws IOException {
66-
return readNamedTag(0);
67-
}
68-
69-
/**
70-
* Reads an NBT from the stream.
71-
*
72-
* @param depth the depth of this tag
73-
* @return The tag that was read.
74-
* @throws IOException if an I/O error occurs.
75-
*/
76-
private NamedTag readNamedTag(int depth) throws IOException {
77-
int type = is.readByte() & 0xFF;
78-
79-
String name;
80-
if (type != NBTConstants.TYPE_END) {
81-
int nameLength = is.readShort() & 0xFFFF;
82-
byte[] nameBytes = new byte[nameLength];
83-
is.readFully(nameBytes);
84-
name = new String(nameBytes, NBTConstants.CHARSET);
85-
} else {
86-
name = "";
87-
}
88-
89-
return new NamedTag(name, readTagPayload(type, depth));
90-
}
91-
92-
/**
93-
* Reads the payload of a tag given the type.
94-
*
95-
* @param type the type
96-
* @param depth the depth
97-
* @return the tag
98-
* @throws IOException if an I/O error occurs.
99-
*/
100-
private Tag readTagPayload(int type, int depth) throws IOException {
101-
switch (type) {
102-
case NBTConstants.TYPE_END:
103-
if (depth == 0) {
104-
throw new IOException(
105-
"TAG_End found without a TAG_Compound/TAG_List tag preceding it.");
106-
} else {
107-
return new EndTag();
108-
}
109-
case NBTConstants.TYPE_BYTE:
110-
return new ByteTag(is.readByte());
111-
case NBTConstants.TYPE_SHORT:
112-
return new ShortTag(is.readShort());
113-
case NBTConstants.TYPE_INT:
114-
return new IntTag(is.readInt());
115-
case NBTConstants.TYPE_LONG:
116-
return new LongTag(is.readLong());
117-
case NBTConstants.TYPE_FLOAT:
118-
return new FloatTag(is.readFloat());
119-
case NBTConstants.TYPE_DOUBLE:
120-
return new DoubleTag(is.readDouble());
121-
case NBTConstants.TYPE_BYTE_ARRAY:
122-
int length = is.readInt();
123-
byte[] bytes = new byte[length];
124-
is.readFully(bytes);
125-
return new ByteArrayTag(bytes);
126-
case NBTConstants.TYPE_STRING:
127-
length = is.readShort();
128-
bytes = new byte[length];
129-
is.readFully(bytes);
130-
return new StringTag(new String(bytes, NBTConstants.CHARSET));
131-
case NBTConstants.TYPE_LIST:
132-
int childType = is.readByte();
133-
length = is.readInt();
134-
135-
List<Tag> tagList = new ArrayList<>();
136-
for (int i = 0; i < length; ++i) {
137-
Tag tag = readTagPayload(childType, depth + 1);
138-
if (tag instanceof EndTag) {
139-
throw new IOException("TAG_End not permitted in a list.");
140-
}
141-
tagList.add(tag);
142-
}
143-
144-
return new ListTag(NBTUtils.getTypeClass(childType), tagList);
145-
case NBTConstants.TYPE_COMPOUND:
146-
Map<String, Tag> tagMap = new HashMap<>();
147-
while (true) {
148-
NamedTag namedTag = readNamedTag(depth + 1);
149-
Tag tag = namedTag.getTag();
150-
if (tag instanceof EndTag) {
151-
break;
152-
} else {
153-
tagMap.put(namedTag.getName(), tag);
154-
}
155-
}
156-
157-
return new CompoundTag(tagMap);
158-
case NBTConstants.TYPE_INT_ARRAY:
159-
length = is.readInt();
160-
int[] data = new int[length];
161-
for (int i = 0; i < length; i++) {
162-
data[i] = is.readInt();
163-
}
164-
return new IntArrayTag(data);
165-
case NBTConstants.TYPE_LONG_ARRAY:
166-
length = is.readInt();
167-
long[] longData = new long[length];
168-
for (int i = 0; i < length; i++) {
169-
longData[i] = is.readLong();
170-
}
171-
return new LongArrayTag(longData);
172-
default:
173-
throw new IOException("Invalid tag type: " + type + ".");
174-
}
67+
Map.Entry<String, CompoundBinaryTag> named = BinaryTagIO.reader().readNamed(
68+
(DataInput) this.is
69+
);
70+
return new NamedTag(named.getKey(), new CompoundTag(named.getValue()));
17571
}
17672

17773
@Override

worldedit-core/src/main/java/com/sk89q/jnbt/NBTOutputStream.java

+9-230
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
package com.sk89q.jnbt;
2121

22+
import com.google.common.collect.Maps;
23+
import com.sk89q.worldedit.util.nbt.BinaryTagIO;
24+
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
25+
2226
import java.io.Closeable;
27+
import java.io.DataOutput;
2328
import java.io.DataOutputStream;
2429
import java.io.IOException;
2530
import java.io.OutputStream;
@@ -70,236 +75,10 @@ public NBTOutputStream(OutputStream os) throws IOException {
7075
* if an I/O error occurs.
7176
*/
7277
public void writeNamedTag(String name, Tag tag) throws IOException {
73-
checkNotNull(name);
74-
checkNotNull(tag);
75-
76-
int type = NBTUtils.getTypeCode(tag.getClass());
77-
byte[] nameBytes = name.getBytes(NBTConstants.CHARSET);
78-
79-
os.writeByte(type);
80-
os.writeShort(nameBytes.length);
81-
os.write(nameBytes);
82-
83-
if (type == NBTConstants.TYPE_END) {
84-
throw new IOException("Named TAG_End not permitted.");
85-
}
86-
87-
writeTagPayload(tag);
88-
}
89-
90-
/**
91-
* Writes tag payload.
92-
*
93-
* @param tag
94-
* The tag.
95-
* @throws IOException
96-
* if an I/O error occurs.
97-
*/
98-
private void writeTagPayload(Tag tag) throws IOException {
99-
int type = NBTUtils.getTypeCode(tag.getClass());
100-
switch (type) {
101-
case NBTConstants.TYPE_END:
102-
writeEndTagPayload((EndTag) tag);
103-
break;
104-
case NBTConstants.TYPE_BYTE:
105-
writeByteTagPayload((ByteTag) tag);
106-
break;
107-
case NBTConstants.TYPE_SHORT:
108-
writeShortTagPayload((ShortTag) tag);
109-
break;
110-
case NBTConstants.TYPE_INT:
111-
writeIntTagPayload((IntTag) tag);
112-
break;
113-
case NBTConstants.TYPE_LONG:
114-
writeLongTagPayload((LongTag) tag);
115-
break;
116-
case NBTConstants.TYPE_FLOAT:
117-
writeFloatTagPayload((FloatTag) tag);
118-
break;
119-
case NBTConstants.TYPE_DOUBLE:
120-
writeDoubleTagPayload((DoubleTag) tag);
121-
break;
122-
case NBTConstants.TYPE_BYTE_ARRAY:
123-
writeByteArrayTagPayload((ByteArrayTag) tag);
124-
break;
125-
case NBTConstants.TYPE_STRING:
126-
writeStringTagPayload((StringTag) tag);
127-
break;
128-
case NBTConstants.TYPE_LIST:
129-
writeListTagPayload((ListTag) tag);
130-
break;
131-
case NBTConstants.TYPE_COMPOUND:
132-
writeCompoundTagPayload((CompoundTag) tag);
133-
break;
134-
case NBTConstants.TYPE_INT_ARRAY:
135-
writeIntArrayTagPayload((IntArrayTag) tag);
136-
break;
137-
case NBTConstants.TYPE_LONG_ARRAY:
138-
writeLongArrayTagPayload((LongArrayTag) tag);
139-
break;
140-
default:
141-
throw new IOException("Invalid tag type: " + type + ".");
142-
}
143-
}
144-
145-
/**
146-
* Writes a {@code TAG_Byte} tag.
147-
*
148-
* @param tag
149-
* The tag.
150-
* @throws IOException
151-
* if an I/O error occurs.
152-
*/
153-
private void writeByteTagPayload(ByteTag tag) throws IOException {
154-
os.writeByte(tag.getValue());
155-
}
156-
157-
/**
158-
* Writes a {@code TAG_Byte_Array} tag.
159-
*
160-
* @param tag
161-
* The tag.
162-
* @throws IOException
163-
* if an I/O error occurs.
164-
*/
165-
private void writeByteArrayTagPayload(ByteArrayTag tag) throws IOException {
166-
byte[] bytes = tag.getValue();
167-
os.writeInt(bytes.length);
168-
os.write(bytes);
169-
}
170-
171-
/**
172-
* Writes a {@code TAG_Compound} tag.
173-
*
174-
* @param tag
175-
* The tag.
176-
* @throws IOException
177-
* if an I/O error occurs.
178-
*/
179-
private void writeCompoundTagPayload(CompoundTag tag) throws IOException {
180-
for (Map.Entry<String, Tag> entry : tag.getValue().entrySet()) {
181-
writeNamedTag(entry.getKey(), entry.getValue());
182-
}
183-
os.writeByte((byte) 0); // end tag - better way?
184-
}
185-
186-
/**
187-
* Writes a {@code TAG_List} tag.
188-
*
189-
* @param tag
190-
* The tag.
191-
* @throws IOException
192-
* if an I/O error occurs.
193-
*/
194-
private void writeListTagPayload(ListTag tag) throws IOException {
195-
Class<? extends Tag> clazz = tag.getType();
196-
List<Tag> tags = tag.getValue();
197-
int size = tags.size();
198-
199-
os.writeByte(NBTUtils.getTypeCode(clazz));
200-
os.writeInt(size);
201-
for (Tag tag1 : tags) {
202-
writeTagPayload(tag1);
203-
}
204-
}
205-
206-
/**
207-
* Writes a {@code TAG_String} tag.
208-
*
209-
* @param tag
210-
* The tag.
211-
* @throws IOException
212-
* if an I/O error occurs.
213-
*/
214-
private void writeStringTagPayload(StringTag tag) throws IOException {
215-
byte[] bytes = tag.getValue().getBytes(NBTConstants.CHARSET);
216-
os.writeShort(bytes.length);
217-
os.write(bytes);
218-
}
219-
220-
/**
221-
* Writes a {@code TAG_Double} tag.
222-
*
223-
* @param tag
224-
* The tag.
225-
* @throws IOException
226-
* if an I/O error occurs.
227-
*/
228-
private void writeDoubleTagPayload(DoubleTag tag) throws IOException {
229-
os.writeDouble(tag.getValue());
230-
}
231-
232-
/**
233-
* Writes a {@code TAG_Float} tag.
234-
*
235-
* @param tag
236-
* The tag.
237-
* @throws IOException
238-
* if an I/O error occurs.
239-
*/
240-
private void writeFloatTagPayload(FloatTag tag) throws IOException {
241-
os.writeFloat(tag.getValue());
242-
}
243-
244-
/**
245-
* Writes a {@code TAG_Long} tag.
246-
*
247-
* @param tag
248-
* The tag.
249-
* @throws IOException
250-
* if an I/O error occurs.
251-
*/
252-
private void writeLongTagPayload(LongTag tag) throws IOException {
253-
os.writeLong(tag.getValue());
254-
}
255-
256-
/**
257-
* Writes a {@code TAG_Int} tag.
258-
*
259-
* @param tag
260-
* The tag.
261-
* @throws IOException
262-
* if an I/O error occurs.
263-
*/
264-
private void writeIntTagPayload(IntTag tag) throws IOException {
265-
os.writeInt(tag.getValue());
266-
}
267-
268-
/**
269-
* Writes a {@code TAG_Short} tag.
270-
*
271-
* @param tag
272-
* The tag.
273-
* @throws IOException
274-
* if an I/O error occurs.
275-
*/
276-
private void writeShortTagPayload(ShortTag tag) throws IOException {
277-
os.writeShort(tag.getValue());
278-
}
279-
280-
/**
281-
* Writes a {@code TAG_Empty} tag.
282-
*
283-
* @param tag the tag
284-
*/
285-
private void writeEndTagPayload(EndTag tag) {
286-
/* empty */
287-
}
288-
289-
private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException {
290-
int[] data = tag.getValue();
291-
os.writeInt(data.length);
292-
for (int aData : data) {
293-
os.writeInt(aData);
294-
}
295-
}
296-
297-
private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException {
298-
long[] data = tag.getValue();
299-
os.writeInt(data.length);
300-
for (long aData : data) {
301-
os.writeLong(aData);
302-
}
78+
BinaryTagIO.writer().writeNamed(
79+
Maps.immutableEntry(name, (CompoundBinaryTag) tag.asBinaryTag()),
80+
(DataOutput) this.os
81+
);
30382
}
30483

30584
@Override

0 commit comments

Comments
 (0)