Remove some unnecessary exceptions and custom tags.

This commit is contained in:
Steveice10 2019-03-23 17:25:12 -07:00
parent 9a45dec2a4
commit 4aa89a6a32
10 changed files with 2 additions and 599 deletions

View File

@ -3,10 +3,7 @@ package com.github.steveice10.opennbt.conversion;
import com.github.steveice10.opennbt.conversion.builtin.*;
import com.github.steveice10.opennbt.conversion.builtin.custom.DoubleArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.FloatArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.SerializableArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.SerializableTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.ShortArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.StringArrayTagConverter;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
@ -22,10 +19,7 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.opennbt.tag.builtin.custom.DoubleArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.FloatArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableTag;
import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag;
import java.io.Serializable;
import java.util.HashMap;
@ -58,10 +52,7 @@ public class ConverterRegistry {
register(DoubleArrayTag.class, double[].class, new DoubleArrayTagConverter());
register(FloatArrayTag.class, float[].class, new FloatArrayTagConverter());
register(SerializableArrayTag.class, Serializable[].class, new SerializableArrayTagConverter());
register(SerializableTag.class, Serializable.class, new SerializableTagConverter());
register(ShortArrayTag.class, short[].class, new ShortArrayTagConverter());
register(StringArrayTag.class, String[].class, new StringArrayTagConverter());
}
/**
@ -94,17 +85,8 @@ public class ConverterRegistry {
* @param <V> Value type to unregister.
* @param tag Tag type class to unregister.
* @param type Value type class to unregister.
* @throws ConverterUnregisterException If an error occurs while unregistering the converter.
*/
public static <T extends Tag, V> void unregister(Class<T> tag, Class<V> type) throws ConverterUnregisterException {
if (!tagToConverter.containsKey(tag)) {
throw new ConverterUnregisterException("Type conversion to tag " + tag.getName() + " is not registered.");
}
if (!typeToConverter.containsKey(type)) {
throw new ConverterUnregisterException("Tag conversion to type " + type.getName() + " is not registered.");
}
public static <T extends Tag, V> void unregister(Class<T> tag, Class<V> type) {
tagToConverter.remove(tag);
typeToConverter.remove(type);
}

View File

@ -1,24 +0,0 @@
package com.github.steveice10.opennbt.conversion;
/**
* An exception thrown when an error occurs while unregistering a converter.
*/
public class ConverterUnregisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
public ConverterUnregisterException() {
super();
}
public ConverterUnregisterException(String message) {
super(message);
}
public ConverterUnregisterException(Throwable cause) {
super(cause);
}
public ConverterUnregisterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,21 +0,0 @@
package com.github.steveice10.opennbt.conversion.builtin.custom;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableArrayTag;
import java.io.Serializable;
/**
* A converter that converts between SerializableArrayTag and Serializable[].
*/
public class SerializableArrayTagConverter implements TagConverter<SerializableArrayTag, Serializable[]> {
@Override
public Serializable[] convert(SerializableArrayTag tag) {
return tag.getValue();
}
@Override
public SerializableArrayTag convert(String name, Serializable[] value) {
return new SerializableArrayTag(name, value);
}
}

View File

@ -1,21 +0,0 @@
package com.github.steveice10.opennbt.conversion.builtin.custom;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableTag;
import java.io.Serializable;
/**
* A converter that converts between SerializableTag and Serializable.
*/
public class SerializableTagConverter implements TagConverter<SerializableTag, Serializable> {
@Override
public Serializable convert(SerializableTag tag) {
return tag.getValue();
}
@Override
public SerializableTag convert(String name, Serializable value) {
return new SerializableTag(name, value);
}
}

View File

@ -1,19 +0,0 @@
package com.github.steveice10.opennbt.conversion.builtin.custom;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag;
/**
* A converter that converts between StringArrayTag and String[].
*/
public class StringArrayTagConverter implements TagConverter<StringArrayTag, String[]> {
@Override
public String[] convert(StringArrayTag tag) {
return tag.getValue();
}
@Override
public StringArrayTag convert(String name, String[] value) {
return new StringArrayTag(name, value);
}
}

View File

@ -15,10 +15,7 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.opennbt.tag.builtin.custom.DoubleArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.FloatArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableTag;
import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.StringArrayTag;
import java.lang.reflect.Constructor;
import java.util.HashMap;
@ -47,10 +44,7 @@ public class TagRegistry {
register(60, DoubleArrayTag.class);
register(61, FloatArrayTag.class);
register(63, SerializableArrayTag.class);
register(64, SerializableTag.class);
register(65, ShortArrayTag.class);
register(66, StringArrayTag.class);
}
/**
@ -77,13 +71,8 @@ public class TagRegistry {
* Unregisters a tag class.
*
* @param id ID of the tag to unregister.
* @throws TagUnregisterException If an error occurs while unregistering the tag.
*/
public static void unregister(int id) throws TagUnregisterException {
if(!idToTag.containsKey(id)) {
throw new TagUnregisterException("Tag ID \"" + id + "\" isn't registered.");
}
public static void unregister(int id) {
tagToId.remove(getClassFor(id));
idToTag.remove(id);
}

View File

@ -1,24 +0,0 @@
package com.github.steveice10.opennbt.tag;
/**
* An exception thrown when an error occurs while unregistering a tag.
*/
public class TagUnregisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
public TagUnregisterException() {
super();
}
public TagUnregisterException(String message) {
super(message);
}
public TagUnregisterException(Throwable cause) {
super(cause);
}
public TagUnregisterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,198 +0,0 @@
package com.github.steveice10.opennbt.tag.builtin.custom;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/**
* A tag containing an array of serializable objects.
*/
public class SerializableArrayTag extends Tag {
private Serializable[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public SerializableArrayTag(String name) {
this(name, new Serializable[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public SerializableArrayTag(String name, Serializable[] value) {
super(name);
this.value = value;
}
@Override
public Serializable[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Serializable[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public Serializable getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, Serializable value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new Serializable[in.readInt()];
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
for(int index = 0; index < this.value.length; index++) {
try {
this.value[index] = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableArrayTag!", e);
}
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]);
}
}
@Override
public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue());
}
private static class DataInputInputStream extends InputStream {
private DataInput in;
public DataInputInputStream(DataInput in) {
this.in = in;
}
@Override
public int read() throws IOException {
return this.in.readUnsignedByte();
}
@Override
public int read(byte[] b) throws IOException {
this.in.readFully(b);
return b.length;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len);
return len;
}
@Override
public long skip(long l) throws IOException {
return this.in.skipBytes((int) l);
}
@Override
public int available() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
}
@Override
public synchronized void mark(int i) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
}
private static class DataOutputOutputStream extends OutputStream {
private DataOutput out;
public DataOutputOutputStream(DataOutput out) {
this.out = out;
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
}
}

View File

@ -1,159 +0,0 @@
package com.github.steveice10.opennbt.tag.builtin.custom;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/**
* A tag containing a serializable object.
*/
public class SerializableTag extends Tag {
private Serializable value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public SerializableTag(String name) {
this(name, 0);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public SerializableTag(String name, Serializable value) {
super(name);
this.value = value;
}
@Override
public Serializable getValue() {
return this.value;
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(Serializable value) {
this.value = value;
}
@Override
public void read(DataInput in) throws IOException {
ObjectInputStream str = new ObjectInputStream(new DataInputInputStream(in));
try {
this.value = (Serializable) str.readObject();
} catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableTag!", e);
}
}
@Override
public void write(DataOutput out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(new DataOutputOutputStream(out));
str.writeObject(this.value);
}
@Override
public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue());
}
private static class DataInputInputStream extends InputStream {
private DataInput in;
public DataInputInputStream(DataInput in) {
this.in = in;
}
@Override
public int read() throws IOException {
return this.in.readUnsignedByte();
}
@Override
public int read(byte[] b) throws IOException {
this.in.readFully(b);
return b.length;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.in.readFully(b, off, len);
return len;
}
@Override
public long skip(long l) throws IOException {
return this.in.skipBytes((int) l);
}
@Override
public int available() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
}
@Override
public synchronized void mark(int i) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
}
private static class DataOutputOutputStream extends OutputStream {
private DataOutput out;
public DataOutputOutputStream(DataOutput out) {
this.out = out;
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.out.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
}
}

View File

@ -1,102 +0,0 @@
package com.github.steveice10.opennbt.tag.builtin.custom;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* A tag containing a string array.
*/
public class StringArrayTag extends Tag {
private String[] value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
*/
public StringArrayTag(String name) {
this(name, new String[0]);
}
/**
* Creates a tag with the specified name.
*
* @param name The name of the tag.
* @param value The value of the tag.
*/
public StringArrayTag(String name, String[] value) {
super(name);
this.value = value;
}
@Override
public String[] getValue() {
return this.value.clone();
}
/**
* Sets the value of this tag.
*
* @param value New value of this tag.
*/
public void setValue(String[] value) {
if(value == null) {
return;
}
this.value = value.clone();
}
/**
* Gets a value in this tag's array.
*
* @param index Index of the value.
* @return The value at the given index.
*/
public String getValue(int index) {
return this.value[index];
}
/**
* Sets a value in this tag's array.
*
* @param index Index of the value.
* @param value Value to set.
*/
public void setValue(int index, String value) {
this.value[index] = value;
}
/**
* Gets the length of this tag's array.
*
* @return This tag's array length.
*/
public int length() {
return this.value.length;
}
@Override
public void read(DataInput in) throws IOException {
this.value = new String[in.readInt()];
for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readUTF();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) {
out.writeUTF(this.value[index]);
}
}
@Override
public StringArrayTag clone() {
return new StringArrayTag(this.getName(), this.getValue());
}
}