From 0353d28c60345fb72d796d36f85d1c6dd0b5a657 Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Thu, 21 Nov 2013 21:06:55 -0800 Subject: [PATCH] Rewrote library to be a bit more flexible --- pom.xml | 2 +- .../ch/spacebase/opennbt/NBTConstants.java | 80 ---- .../java/ch/spacebase/opennbt/NBTFileIO.java | 139 ++++++ src/main/java/ch/spacebase/opennbt/NBTIO.java | 91 ++++ .../java/ch/spacebase/opennbt/NBTIOUtils.java | 122 ----- .../java/ch/spacebase/opennbt/NBTUtils.java | 275 ------------ .../java/ch/spacebase/opennbt/TagBuilder.java | 164 ------- .../opennbt/TagRegisterException.java | 14 + .../ch/spacebase/opennbt/TagRegistry.java | 120 +++++ .../exception/InvalidNBTException.java | 17 - .../opennbt/stream/NBTInputStream.java | 303 ------------- .../opennbt/stream/NBTOutputStream.java | 419 ------------------ .../spacebase/opennbt/tag/ByteArrayTag.java | 126 +++--- .../ch/spacebase/opennbt/tag/ByteTag.java | 89 ++-- .../ch/spacebase/opennbt/tag/CompoundTag.java | 141 +++--- .../ch/spacebase/opennbt/tag/DoubleTag.java | 91 ++-- .../java/ch/spacebase/opennbt/tag/EndTag.java | 63 --- .../ch/spacebase/opennbt/tag/FloatTag.java | 93 ++-- .../ch/spacebase/opennbt/tag/IntArrayTag.java | 160 ++++--- .../java/ch/spacebase/opennbt/tag/IntTag.java | 91 ++-- .../ch/spacebase/opennbt/tag/ListTag.java | 168 +++---- .../ch/spacebase/opennbt/tag/LongTag.java | 93 ++-- .../ch/spacebase/opennbt/tag/ShortTag.java | 93 ++-- .../ch/spacebase/opennbt/tag/StringTag.java | 97 ++-- .../java/ch/spacebase/opennbt/tag/Tag.java | 131 ++++-- .../opennbt/tag/custom/DoubleArrayTag.java | 144 +++--- .../opennbt/tag/custom/FloatArrayTag.java | 144 +++--- .../opennbt/tag/custom/LongArrayTag.java | 144 +++--- .../opennbt/tag/custom/ObjectArrayTag.java | 96 ---- .../opennbt/tag/custom/ObjectTag.java | 78 ---- .../tag/custom/SerializableArrayTag.java | 112 +++++ .../opennbt/tag/custom/SerializableTag.java | 76 ++++ .../opennbt/tag/custom/ShortArrayTag.java | 144 +++--- .../opennbt/tag/custom/StringArrayTag.java | 149 ++++--- .../opennbt/tag/custom/UnknownTag.java | 81 ---- .../opennbt/utils/DoubleIntHashMap.java | 78 ---- 36 files changed, 1611 insertions(+), 2817 deletions(-) delete mode 100644 src/main/java/ch/spacebase/opennbt/NBTConstants.java create mode 100644 src/main/java/ch/spacebase/opennbt/NBTFileIO.java create mode 100644 src/main/java/ch/spacebase/opennbt/NBTIO.java delete mode 100644 src/main/java/ch/spacebase/opennbt/NBTIOUtils.java delete mode 100644 src/main/java/ch/spacebase/opennbt/NBTUtils.java delete mode 100644 src/main/java/ch/spacebase/opennbt/TagBuilder.java create mode 100644 src/main/java/ch/spacebase/opennbt/TagRegisterException.java create mode 100644 src/main/java/ch/spacebase/opennbt/TagRegistry.java delete mode 100644 src/main/java/ch/spacebase/opennbt/exception/InvalidNBTException.java delete mode 100644 src/main/java/ch/spacebase/opennbt/stream/NBTInputStream.java delete mode 100644 src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java delete mode 100644 src/main/java/ch/spacebase/opennbt/tag/EndTag.java delete mode 100644 src/main/java/ch/spacebase/opennbt/tag/custom/ObjectArrayTag.java delete mode 100644 src/main/java/ch/spacebase/opennbt/tag/custom/ObjectTag.java create mode 100644 src/main/java/ch/spacebase/opennbt/tag/custom/SerializableArrayTag.java create mode 100644 src/main/java/ch/spacebase/opennbt/tag/custom/SerializableTag.java delete mode 100644 src/main/java/ch/spacebase/opennbt/tag/custom/UnknownTag.java delete mode 100644 src/main/java/ch/spacebase/opennbt/utils/DoubleIntHashMap.java diff --git a/pom.xml b/pom.xml index b4b0f23..380928a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 ch.spacebase opennbt - 1.1-SNAPSHOT + 1.1 jar OpenNBT diff --git a/src/main/java/ch/spacebase/opennbt/NBTConstants.java b/src/main/java/ch/spacebase/opennbt/NBTConstants.java deleted file mode 100644 index 445dc9c..0000000 --- a/src/main/java/ch/spacebase/opennbt/NBTConstants.java +++ /dev/null @@ -1,80 +0,0 @@ -package ch.spacebase.opennbt; - -import java.nio.charset.Charset; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the OpenNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * A class which holds constant values. - */ -public final class NBTConstants { - - /** - * The character set used by NBT (UTF-8). - */ - public static final Charset CHARSET = Charset.forName("UTF-8"); - - /** - * Tag type constants. - */ - public static final int TYPE_END = 0, - TYPE_BYTE = 1, - TYPE_SHORT = 2, - TYPE_INT = 3, - TYPE_LONG = 4, - TYPE_FLOAT = 5, - TYPE_DOUBLE = 6, - TYPE_BYTE_ARRAY = 7, - TYPE_STRING = 8, - TYPE_LIST = 9, - TYPE_COMPOUND = 10, - TYPE_INT_ARRAY = 11, - TYPE_DOUBLE_ARRAY = 60, - TYPE_FLOAT_ARRAY = 61, - TYPE_LONG_ARRAY = 62, - TYPE_OBJECT_ARRAY = 63, - TYPE_OBJECT = 64, - TYPE_SHORT_ARRAY = 65, - TYPE_STRING_ARRAY = 66, - TYPE_UNKNOWN = 67; - - /** - * Default private constructor. - */ - private NBTConstants() { - - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/NBTFileIO.java b/src/main/java/ch/spacebase/opennbt/NBTFileIO.java new file mode 100644 index 0000000..81c5fd2 --- /dev/null +++ b/src/main/java/ch/spacebase/opennbt/NBTFileIO.java @@ -0,0 +1,139 @@ +package ch.spacebase.opennbt; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import ch.spacebase.opennbt.tag.CompoundTag; +import ch.spacebase.opennbt.tag.Tag; + +public class NBTFileIO { + + /** + * Reads the root CompoundTag from the given file. + * @param path Path of the file. + * @return The read compound tag. + * @throws IOException If an I/O error occurs. + */ + public static CompoundTag readFile(String path) throws IOException { + return readFile(new File(path)); + } + + /** + * Reads the root CompoundTag from the given file. + * @param file File to read from. + * @return The read compound tag. + * @throws IOException If an I/O error occurs. + */ + public static CompoundTag readFile(File file) throws IOException { + return readFile(file, true); + } + + /** + * Reads the root CompoundTag from the given file. + * @param path Path of the file. + * @param compressed Whether the NBT file is compressed. + * @return The read compound tag. + * @throws IOException If an I/O error occurs. + */ + public static CompoundTag readFile(String path, boolean compressed) throws IOException { + return readFile(new File(path), compressed); + } + + /** + * Reads the root CompoundTag from the given file. + * @param file File to read from. + * @param compressed Whether the NBT file is compressed. + * @return The read compound tag. + * @throws IOException If an I/O error occurs. + */ + public static CompoundTag readFile(File file, boolean compressed) throws IOException { + try { + TagRegistry.registerDefaultTags(); + } catch(TagRegisterException e) { + throw new IOException("Failed to register default tags.", e); + } + + InputStream in = new FileInputStream(file); + if(compressed) { + in = new GZIPInputStream(in); + } + + Tag tag = NBTIO.readTag(new DataInputStream(in)); + if(!(tag instanceof CompoundTag)) { + throw new IOException("Root tag is not a CompoundTag!"); + } + + return (CompoundTag) tag; + } + + /** + * Writes the given root CompoundTag to the given file. + * @param tag Tag to write. + * @param path Path to write to. + * @throws IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, String path) throws IOException { + writeFile(tag, new File(path)); + } + + /** + * Writes the given root CompoundTag to the given file. + * @param tag Tag to write. + * @param file File to write to. + * @throws IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, File file) throws IOException { + writeFile(tag, file, true); + } + + /** + * Writes the given root CompoundTag to the given file. + * @param tag Tag to write. + * @param path Path to write to. + * @param compressed Whether the NBT file should be compressed. + * @throws IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException { + writeFile(tag, new File(path), compressed); + } + + /** + * Writes the given root CompoundTag to the given file. + * @param tag Tag to write. + * @param file File to write to. + * @param compressed Whether the NBT file should be compressed. + * @throws IOException If an I/O error occurs. + */ + public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException { + try { + TagRegistry.registerDefaultTags(); + } catch(TagRegisterException e) { + throw new IOException("Failed to register default tags.", e); + } + + if(!file.exists()) { + if(file.getParentFile() != null && !file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + + file.createNewFile(); + } + + OutputStream out = new FileOutputStream(file); + if(compressed) { + out = new GZIPOutputStream(out); + } + + NBTIO.writeTag(new DataOutputStream(out), tag); + out.close(); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/NBTIO.java b/src/main/java/ch/spacebase/opennbt/NBTIO.java new file mode 100644 index 0000000..d913212 --- /dev/null +++ b/src/main/java/ch/spacebase/opennbt/NBTIO.java @@ -0,0 +1,91 @@ +package ch.spacebase.opennbt; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.EOFException; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import ch.spacebase.opennbt.tag.Tag; + +/** + * A class containing methods for reading/writing NBT tags. + */ +public class NBTIO { + + public static final Charset CHARSET = Charset.forName("UTF-8"); + + /** + * Reads NBT tags until an end tag is reached. + * @param in Input stream to read from. + * @return The read tags. + * @throws IOException If an I/O error occurs. + */ + public static List readUntilEndTag(DataInputStream in) throws IOException { + List ret = new ArrayList(); + try { + Tag tag; + while((tag = readTag(in)) != null) { + ret.add(tag); + } + } catch(EOFException e) { + throw new IOException("Closing EndTag was not found!"); + } + + return ret; + } + + /** + * Reads an NBT tag. + * @param in Input stream to read from. + * @return The read tag, or null if the tag is an end tag. + * @throws IOException If an I/O error occurs. + */ + public static Tag readTag(DataInputStream in) throws IOException { + int id = in.readByte() & 0xFF; + if(id == 0) { + return null; + } + + byte[] nameBytes = new byte[in.readShort() & 0xFFFF]; + in.readFully(nameBytes); + String name = new String(nameBytes, CHARSET); + Tag tag = TagRegistry.createInstance(id, name); + if(tag == null) { + throw new IOException("Invalid tag: " + id); + } + + tag.read(in); + return tag; + } + + /** + * Writes a collection of tags to an output stream. + * @param out Output stream to write to. + * @param tags Tags to write. + * @throws IOException If an I/O error occurs. + */ + public static void writeTags(DataOutputStream out, Collection tags) throws IOException { + for(Tag tag : tags) { + writeTag(out, tag); + } + } + + /** + * Writes a tag to an output stream. + * @param out Output stream to write to. + * @param tag Tag to write. + * @throws IOException If an I/O error occurs. + */ + public static void writeTag(DataOutputStream out, Tag tag) throws IOException { + byte[] nameBytes = tag.getName().getBytes(CHARSET); + out.writeByte(tag.getId()); + out.writeShort(nameBytes.length); + out.write(nameBytes); + tag.write(out); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/NBTIOUtils.java b/src/main/java/ch/spacebase/opennbt/NBTIOUtils.java deleted file mode 100644 index f386a5c..0000000 --- a/src/main/java/ch/spacebase/opennbt/NBTIOUtils.java +++ /dev/null @@ -1,122 +0,0 @@ -package ch.spacebase.opennbt; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import ch.spacebase.opennbt.stream.NBTInputStream; -import ch.spacebase.opennbt.stream.NBTOutputStream; -import ch.spacebase.opennbt.tag.Tag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * A utility for reading and writing NBTs to/from files. - */ -public class NBTIOUtils { - - public static List loadNBT(File file) { - List result = new ArrayList(); - - NBTInputStream input = null; - - try { - input = new NBTInputStream(new FileInputStream(file)); - - Tag next = null; - - while((next = input.readTag()) != null) { - result.add(next); - } - } catch(IOException ioe) { - System.out.println("Failed to create NBTInputStream from file " + file.getName() + "."); - return null; - } finally { - if(input != null) { - try { - input.close(); - } catch (IOException e) { - System.out.println("Failed to close NBTInputStream."); - e.printStackTrace(); - } - } - } - - return result; - } - - public static void writeNBT(File file, List tags) { - NBTOutputStream output = null; - - try { - output = new NBTOutputStream(new FileOutputStream(file)); - - for(Tag tag : tags) { - output.writeTag(tag); - } - } catch(IOException ioe) { - System.out.println("Failed to create NBTOutputStream from file " + file.getName() + "."); - return; - } finally { - if(output != null) { - try { - output.close(); - } catch (IOException e) { - System.out.println("Failed to close NBTOutputStream."); - e.printStackTrace(); - } - } - } - } - - public static Tag[] loadNBTArray(File file) { - List tags = loadNBT(file); - - if(tags != null) { - return tags.toArray(new Tag[tags.size()]); - } - - return null; - } - - public static void writeNBTArray(File file, Tag[] tags) { - writeNBT(file, Arrays.asList(tags)); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/NBTUtils.java b/src/main/java/ch/spacebase/opennbt/NBTUtils.java deleted file mode 100644 index 25c3ea6..0000000 --- a/src/main/java/ch/spacebase/opennbt/NBTUtils.java +++ /dev/null @@ -1,275 +0,0 @@ -package ch.spacebase.opennbt; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.logging.Logger; - -import ch.spacebase.opennbt.exception.InvalidNBTException; -import ch.spacebase.opennbt.tag.ByteArrayTag; -import ch.spacebase.opennbt.tag.ByteTag; -import ch.spacebase.opennbt.tag.CompoundTag; -import ch.spacebase.opennbt.tag.DoubleTag; -import ch.spacebase.opennbt.tag.EndTag; -import ch.spacebase.opennbt.tag.FloatTag; -import ch.spacebase.opennbt.tag.IntArrayTag; -import ch.spacebase.opennbt.tag.IntTag; -import ch.spacebase.opennbt.tag.ListTag; -import ch.spacebase.opennbt.tag.LongTag; -import ch.spacebase.opennbt.tag.ShortTag; -import ch.spacebase.opennbt.tag.StringTag; -import ch.spacebase.opennbt.tag.Tag; -import ch.spacebase.opennbt.tag.custom.DoubleArrayTag; -import ch.spacebase.opennbt.tag.custom.FloatArrayTag; -import ch.spacebase.opennbt.tag.custom.LongArrayTag; -import ch.spacebase.opennbt.tag.custom.ObjectArrayTag; -import ch.spacebase.opennbt.tag.custom.ObjectTag; -import ch.spacebase.opennbt.tag.custom.ShortArrayTag; -import ch.spacebase.opennbt.tag.custom.StringArrayTag; -import ch.spacebase.opennbt.tag.custom.UnknownTag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the OpenNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * A class which contains NBT-related utility methods. - */ -public final class NBTUtils { - - private static final Logger logger = Logger.getLogger("NBTUtils"); - - /** - * Gets the type name of a tag. - * @param clazz The tag class. - * @return The type name. - */ - public static String getTypeName(Class clazz) { - if(clazz.equals(ByteArrayTag.class)) { - return "TAG_Byte_Array"; - } else if(clazz.equals(ByteTag.class)) { - return "TAG_Byte"; - } else if(clazz.equals(CompoundTag.class)) { - return "TAG_Compound"; - } else if(clazz.equals(DoubleTag.class)) { - return "TAG_Double"; - } else if(clazz.equals(EndTag.class)) { - return "TAG_End"; - } else if(clazz.equals(FloatTag.class)) { - return "TAG_Float"; - } else if(clazz.equals(IntTag.class)) { - return "TAG_Int"; - } else if(clazz.equals(ListTag.class)) { - return "TAG_List"; - } else if(clazz.equals(LongTag.class)) { - return "TAG_Long"; - } else if(clazz.equals(ShortTag.class)) { - return "TAG_Short"; - } else if(clazz.equals(StringTag.class)) { - return "TAG_String"; - } else if (clazz.equals(IntArrayTag.class)) { - return "TAG_Int_Array"; - } else if (clazz.equals(DoubleArrayTag.class)) { - return "TAG_Double_Array"; - } else if (clazz.equals(FloatArrayTag.class)) { - return "TAG_Float_Array"; - } else if (clazz.equals(LongArrayTag.class)) { - return "TAG_Long_Array"; - } else if (clazz.equals(ObjectArrayTag.class)) { - return "TAG_Object_Array"; - } else if (clazz.equals(ObjectTag.class)) { - return "TAG_Object"; - } else if (clazz.equals(ShortArrayTag.class)) { - return "TAG_Short_Array"; - } else if (clazz.equals(StringArrayTag.class)) { - return "TAG_String_Array"; - } else if (clazz.equals(UnknownTag.class)) { - return "TAG_Unknown"; - } else { - logger.warning("Unknown tag class (" + clazz.getName() + ") found."); - return "TAG_Unknown"; - } - } - - /** - * Gets the type code of a tag class. - * @param clazz The tag class. - * @return The type code. - * @throws IllegalArgumentException if the tag class is invalid. - */ - public static int getTypeCode(Class clazz) { - if(clazz.equals(ByteArrayTag.class)) { - return NBTConstants.TYPE_BYTE_ARRAY; - } else if(clazz.equals(ByteTag.class)) { - return NBTConstants.TYPE_BYTE; - } else if(clazz.equals(CompoundTag.class)) { - return NBTConstants.TYPE_COMPOUND; - } else if(clazz.equals(DoubleTag.class)) { - return NBTConstants.TYPE_DOUBLE; - } else if(clazz.equals(EndTag.class)) { - return NBTConstants.TYPE_END; - } else if(clazz.equals(FloatTag.class)) { - return NBTConstants.TYPE_FLOAT; - } else if(clazz.equals(IntTag.class)) { - return NBTConstants.TYPE_INT; - } else if(clazz.equals(ListTag.class)) { - return NBTConstants.TYPE_LIST; - } else if(clazz.equals(LongTag.class)) { - return NBTConstants.TYPE_LONG; - } else if(clazz.equals(ShortTag.class)) { - return NBTConstants.TYPE_SHORT; - } else if(clazz.equals(StringTag.class)) { - return NBTConstants.TYPE_STRING; - } else if(clazz.equals(IntArrayTag.class)) { - return NBTConstants.TYPE_INT_ARRAY; - } else if(clazz.equals(DoubleArrayTag.class)) { - return NBTConstants.TYPE_DOUBLE_ARRAY; - } else if(clazz.equals(FloatArrayTag.class)) { - return NBTConstants.TYPE_FLOAT_ARRAY; - } else if(clazz.equals(LongArrayTag.class)) { - return NBTConstants.TYPE_LONG_ARRAY; - } else if(clazz.equals(ObjectArrayTag.class)) { - return NBTConstants.TYPE_OBJECT_ARRAY; - } else if(clazz.equals(ObjectTag.class)) { - return NBTConstants.TYPE_OBJECT; - } else if(clazz.equals(ShortArrayTag.class)) { - return NBTConstants.TYPE_SHORT_ARRAY; - } else if(clazz.equals(StringArrayTag.class)) { - return NBTConstants.TYPE_STRING_ARRAY; - } else if(clazz.equals(UnknownTag.class)) { - return NBTConstants.TYPE_UNKNOWN; - } else { - logger.warning("Unknown tag class (" + clazz.getName() + ") found."); - return NBTConstants.TYPE_UNKNOWN; - } - } - - /** - * Gets the class of a type of tag. - * @param type The type. - * @return The class. - * @throws IllegalArgumentException if the tag type is invalid. - */ - public static Class getTypeClass(int type) { - switch(type) { - case NBTConstants.TYPE_END: - return EndTag.class; - case NBTConstants.TYPE_BYTE: - return ByteTag.class; - case NBTConstants.TYPE_SHORT: - return ShortTag.class; - case NBTConstants.TYPE_INT: - return IntTag.class; - case NBTConstants.TYPE_LONG: - return LongTag.class; - case NBTConstants.TYPE_FLOAT: - return FloatTag.class; - case NBTConstants.TYPE_DOUBLE: - return DoubleTag.class; - case NBTConstants.TYPE_BYTE_ARRAY: - return ByteArrayTag.class; - case NBTConstants.TYPE_STRING: - return StringTag.class; - case NBTConstants.TYPE_LIST: - return ListTag.class; - case NBTConstants.TYPE_COMPOUND: - return CompoundTag.class; - case NBTConstants.TYPE_INT_ARRAY: - return IntArrayTag.class; - case NBTConstants.TYPE_UNKNOWN: - return UnknownTag.class; - default: - logger.warning("Unknown tag type (" + type + ") found."); - return UnknownTag.class; - } - } - - /** - * Clones a Map. - * @param map to clone - * @return clone of map - */ - public static Map cloneMap(Map map) { - Map newMap = new HashMap(); - - for(Entry entry : map.entrySet()) { - newMap.put(entry.getKey(), entry.getValue().clone()); - } - - return newMap; - } - - /** - * Get child tag of a NBT structure. - * - * @param items - * @param key - * @param expected - * @return child tag - * @throws InvalidNBTException - */ - public static T getChildTag(CompoundTag items, String key, Class expected) throws InvalidNBTException { - return getChildTag(items.getValue(), key, expected); - } - - /** - * Get child tag of a NBT structure. - * - * @param items - * @param key - * @param expected - * @return child tag - * @throws InvalidNBTException - */ - public static T getChildTag(Map items, String key, Class expected) throws InvalidNBTException { - if (!items.containsKey(key)) { - throw new InvalidNBTException("Missing a \"" + key + "\" tag"); - } - - Tag tag = items.get(key); - - if (!expected.isInstance(tag)) { - throw new InvalidNBTException(key + " tag is not of tag type " + expected.getName()); - } - - return expected.cast(tag); - } - - /** - * Default private constructor. - */ - private NBTUtils() { - - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/TagBuilder.java b/src/main/java/ch/spacebase/opennbt/TagBuilder.java deleted file mode 100644 index 1a6cc89..0000000 --- a/src/main/java/ch/spacebase/opennbt/TagBuilder.java +++ /dev/null @@ -1,164 +0,0 @@ -package ch.spacebase.opennbt; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import ch.spacebase.opennbt.stream.NBTOutputStream; -import ch.spacebase.opennbt.tag.*; -import ch.spacebase.opennbt.tag.custom.*; - -public class TagBuilder { - - private String name; - private List tags = new ArrayList(); - - public TagBuilder() { - this(""); - } - - public TagBuilder(String name) { - this.name = name; - } - - public TagBuilder append(String name, byte b[]) { - this.tags.add(new ByteArrayTag(name, b)); - return this; - } - - public TagBuilder append(String name, byte b) { - this.tags.add(new ByteTag(name, b)); - return this; - } - - public TagBuilder append(CompoundTag compound) { - this.tags.add(compound); - return this; - } - - public TagBuilder append(String name, double d) { - this.tags.add(new DoubleTag(name, d)); - return this; - } - - public TagBuilder append(String name, double d[]) { - this.tags.add(new DoubleArrayTag(name, d)); - return this; - } - - public TagBuilder append(EndTag tag) { - this.tags.add(tag); - return this; - } - - public TagBuilder append(String name, float f) { - this.tags.add(new FloatTag(name, f)); - return this; - } - - public TagBuilder append(String name, float f[]) { - this.tags.add(new FloatArrayTag(name, f)); - return this; - } - - public TagBuilder append(String name, int i[]) { - this.tags.add(new IntArrayTag(name, i)); - return this; - } - - public TagBuilder append(String name, int i) { - this.tags.add(new IntTag(name, i)); - return this; - } - - public TagBuilder append(String name, Class clazz, List l) { - this.tags.add(new ListTag(name, clazz, l)); - return this; - } - - public TagBuilder append(String name, long l) { - this.tags.add(new LongTag(name, l)); - return this; - } - - public TagBuilder append(String name, long l[]) { - this.tags.add(new LongArrayTag(name, l)); - return this; - } - - public TagBuilder append(String name, short s) { - this.tags.add(new ShortTag(name, s)); - return this; - } - - public TagBuilder append(String name, short s[]) { - this.tags.add(new ShortArrayTag(name, s)); - return this; - } - - public TagBuilder append(String name, String s) { - this.tags.add(new StringTag(name, s)); - return this; - } - - public TagBuilder append(String name, String s[]) { - this.tags.add(new StringArrayTag(name, s)); - return this; - } - - public TagBuilder append(String name, Object o) { - this.tags.add(new ObjectTag(name, o)); - return this; - } - - public TagBuilder append(String name, Object o[]) { - this.tags.add(new ObjectArrayTag(name, o)); - return this; - } - - public TagBuilder append(TagBuilder builder) { - this.tags.add(builder.toCompoundTag()); - return this; - } - - public CompoundTag toCompoundTag() { - Map tagMap = new HashMap(); - for(Tag tag : this.tags) { - tagMap.put(tag.getName(), tag); - } - - return new CompoundTag(this.name, tagMap); - } - - public List toList() { - return new ArrayList(this.tags); - } - - public NBTOutputStream toOutputStream(String file) { - File nbt = new File(file); - - try { - if(!nbt.exists()) { - if(nbt.getParentFile() != null) nbt.getParentFile().mkdirs(); - nbt.createNewFile(); - } - - NBTOutputStream out = new NBTOutputStream(new FileOutputStream(nbt)); - for(Tag tag : this.tags) { - out.writeTag(tag); - } - - return out; - } catch(IOException e) { - System.out.println("Failed to create NBTOutputStream for file \"" + file + "\" from a TagBuilder."); - e.printStackTrace(); - } - - return null; - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/TagRegisterException.java b/src/main/java/ch/spacebase/opennbt/TagRegisterException.java new file mode 100644 index 0000000..66f0985 --- /dev/null +++ b/src/main/java/ch/spacebase/opennbt/TagRegisterException.java @@ -0,0 +1,14 @@ +package ch.spacebase.opennbt; + +/** + * An exception thrown when an error occurs while registering a tag class. + */ +public class TagRegisterException extends Exception { + + private static final long serialVersionUID = -2022049594558041160L; + + public TagRegisterException(Throwable cause) { + super("Failed to register tag.", cause); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/TagRegistry.java b/src/main/java/ch/spacebase/opennbt/TagRegistry.java new file mode 100644 index 0000000..65bde7e --- /dev/null +++ b/src/main/java/ch/spacebase/opennbt/TagRegistry.java @@ -0,0 +1,120 @@ +package ch.spacebase.opennbt; + +import java.util.HashMap; +import java.util.Map; + +import ch.spacebase.opennbt.tag.ByteArrayTag; +import ch.spacebase.opennbt.tag.ByteTag; +import ch.spacebase.opennbt.tag.CompoundTag; +import ch.spacebase.opennbt.tag.DoubleTag; +import ch.spacebase.opennbt.tag.FloatTag; +import ch.spacebase.opennbt.tag.IntArrayTag; +import ch.spacebase.opennbt.tag.IntTag; +import ch.spacebase.opennbt.tag.ListTag; +import ch.spacebase.opennbt.tag.LongTag; +import ch.spacebase.opennbt.tag.ShortTag; +import ch.spacebase.opennbt.tag.StringTag; +import ch.spacebase.opennbt.tag.Tag; +import ch.spacebase.opennbt.tag.custom.DoubleArrayTag; +import ch.spacebase.opennbt.tag.custom.FloatArrayTag; +import ch.spacebase.opennbt.tag.custom.LongArrayTag; +import ch.spacebase.opennbt.tag.custom.SerializableArrayTag; +import ch.spacebase.opennbt.tag.custom.SerializableTag; +import ch.spacebase.opennbt.tag.custom.ShortArrayTag; +import ch.spacebase.opennbt.tag.custom.StringArrayTag; + +/** + * A registry containing different tag classes. + */ +public class TagRegistry { + + private static final Map> tags = new HashMap>(); + private static boolean registered = false; + + protected static void registerDefaultTags() throws TagRegisterException { + if(registered) { + return; + } + + registered = true; + register(CompoundTag.class); + register(ListTag.class); + + register(SerializableTag.class); + register(StringTag.class); + register(ByteTag.class); + register(DoubleTag.class); + register(FloatTag.class); + register(IntTag.class); + register(LongTag.class); + register(ShortTag.class); + + register(SerializableArrayTag.class); + register(StringArrayTag.class); + register(ByteArrayTag.class); + register(DoubleArrayTag.class); + register(FloatArrayTag.class); + register(IntArrayTag.class); + register(LongArrayTag.class); + register(ShortArrayTag.class); + } + + /** + * Registers a tag class. + * @param tag Tag class to register. + * @throws TagRegisterException If an error occurs while registering the tag. + */ + public static void register(Class tag) throws TagRegisterException { + try { + Tag t = tag.getDeclaredConstructor(String.class).newInstance(""); + tags.put(t.getId(), tag); + } catch(Exception e) { + throw new TagRegisterException(e); + } + } + + /** + * Gets the tag class with the given id. + * @param id Id of the tag. + * @return The tag class with the given id. + */ + public static Class getClassFor(int id) { + return tags.get(id); + } + + /** + * Gets the id of the given tag class. + * @param clazz The tag class to get the id for. + * @return The id of the given tag class, or -1 if it cannot be found. + */ + public static int getIdFor(Class clazz) { + for(int id : tags.keySet()) { + if(tags.get(id) == clazz) { + return id; + } + } + + return -1; + } + + /** + * Creates an instance of the tag with the given id, using the String constructor. + * @param id Id of the tag. + * @param tagName Name to give the tag. + * @return The created tag, or null if it could not be created or the type does not exist. + */ + public static Tag createInstance(int id, String tagName) { + if(!tags.containsKey(id)) { + return null; + } + + Class clazz = tags.get(id); + try { + return clazz.getDeclaredConstructor(String.class).newInstance(tagName); + } catch(Exception e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/exception/InvalidNBTException.java b/src/main/java/ch/spacebase/opennbt/exception/InvalidNBTException.java deleted file mode 100644 index acaea1b..0000000 --- a/src/main/java/ch/spacebase/opennbt/exception/InvalidNBTException.java +++ /dev/null @@ -1,17 +0,0 @@ -package ch.spacebase.opennbt.exception; - -import java.io.IOException; - -public class InvalidNBTException extends IOException { - - private static final long serialVersionUID = 1L; - - public InvalidNBTException() { - super("Invalid NBT file!"); - } - - public InvalidNBTException(String message) { - super(message); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/stream/NBTInputStream.java b/src/main/java/ch/spacebase/opennbt/stream/NBTInputStream.java deleted file mode 100644 index c30ff09..0000000 --- a/src/main/java/ch/spacebase/opennbt/stream/NBTInputStream.java +++ /dev/null @@ -1,303 +0,0 @@ -package ch.spacebase.opennbt.stream; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the OpenNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -import java.io.Closeable; -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; -import java.util.zip.GZIPInputStream; - -import ch.spacebase.opennbt.NBTConstants; -import ch.spacebase.opennbt.NBTUtils; -import ch.spacebase.opennbt.tag.ByteArrayTag; -import ch.spacebase.opennbt.tag.ByteTag; -import ch.spacebase.opennbt.tag.CompoundTag; -import ch.spacebase.opennbt.tag.DoubleTag; -import ch.spacebase.opennbt.tag.EndTag; -import ch.spacebase.opennbt.tag.FloatTag; -import ch.spacebase.opennbt.tag.IntArrayTag; -import ch.spacebase.opennbt.tag.IntTag; -import ch.spacebase.opennbt.tag.ListTag; -import ch.spacebase.opennbt.tag.LongTag; -import ch.spacebase.opennbt.tag.ShortTag; -import ch.spacebase.opennbt.tag.StringTag; -import ch.spacebase.opennbt.tag.Tag; -import ch.spacebase.opennbt.tag.custom.DoubleArrayTag; -import ch.spacebase.opennbt.tag.custom.FloatArrayTag; -import ch.spacebase.opennbt.tag.custom.LongArrayTag; -import ch.spacebase.opennbt.tag.custom.ObjectArrayTag; -import ch.spacebase.opennbt.tag.custom.ObjectTag; -import ch.spacebase.opennbt.tag.custom.ShortArrayTag; -import ch.spacebase.opennbt.tag.custom.StringArrayTag; -import ch.spacebase.opennbt.tag.custom.UnknownTag; - -/** - *

This class reads NBT, or - * Named Binary Tag streams, and produces an object graph of - * subclasses of the Tag object.

- * - *

The NBT format was created by Markus Persson, and the specification may - * be found at - * http://www.minecraft.net/docs/NBT.txt.

- */ -public final class NBTInputStream implements Closeable { - - private static final Logger logger = Logger.getLogger("NBTInputStream"); - - /** - * The data input stream. - */ - private final DataInputStream is; - - /** - * Creates a new NBTInputStream, which will source its data - * from the specified input stream. - * @param is The input stream. - * @throws IOException if an I/O error occurs. - */ - public NBTInputStream(InputStream is) throws IOException { - this.is = new DataInputStream(new GZIPInputStream(is)); - } - - /** - * Reads an NBT tag from the stream. - * @return The tag that was read. - * @throws IOException if an I/O error occurs. - */ - public Tag readTag() throws IOException { - return readTag(0); - } - - /** - * Reads an NBT from the stream. - * @param depth The depth of this tag. - * @return The tag that was read. - * @throws IOException if an I/O error occurs. - */ - private Tag readTag(int depth) throws IOException { - int type = is.readByte() & 0xFF; - - String name; - if(type != NBTConstants.TYPE_END) { - int nameLength = is.readShort() & 0xFFFF; - byte[] nameBytes = new byte[nameLength]; - is.readFully(nameBytes); - name = new String(nameBytes, NBTConstants.CHARSET); - } else { - name = ""; - } - - return readTagPayload(type, name, depth); - } - - /** - * Reads the payload of a tag, given the name and type. - * @param type The type. - * @param name The name. - * @param depth The depth. - * @return The tag. - * @throws IOException if an I/O error occurs. - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - private Tag readTagPayload(int type, String name, int depth) throws IOException { - switch(type) { - case NBTConstants.TYPE_END: - if(depth == 0) { - throw new IOException("TAG_End found without a TAG_Compound/TAG_List tag preceding it."); - } else { - return new EndTag(); - } - case NBTConstants.TYPE_BYTE: - return new ByteTag(name, is.readByte()); - case NBTConstants.TYPE_SHORT: - return new ShortTag(name, is.readShort()); - case NBTConstants.TYPE_INT: - return new IntTag(name, is.readInt()); - case NBTConstants.TYPE_LONG: - return new LongTag(name, is.readLong()); - case NBTConstants.TYPE_FLOAT: - return new FloatTag(name, is.readFloat()); - case NBTConstants.TYPE_DOUBLE: - return new DoubleTag(name, is.readDouble()); - case NBTConstants.TYPE_BYTE_ARRAY: - int length = is.readInt(); - byte[] bytes = new byte[length]; - is.readFully(bytes); - return new ByteArrayTag(name, bytes); - case NBTConstants.TYPE_STRING: - length = is.readShort(); - bytes = new byte[length]; - is.readFully(bytes); - return new StringTag(name, new String(bytes, NBTConstants.CHARSET)); - case NBTConstants.TYPE_LIST: - int childType = is.readByte(); - length = is.readInt(); - - Class oclass = NBTUtils.getTypeClass(childType); - - List tagList = new ArrayList(); - for(int i = 0; i < length; i++) { - Tag tag = readTagPayload(childType, "", depth + 1); - - if(tag instanceof EndTag) { - throw new IOException("TAG_End not permitted in a list."); - } else if(!oclass.isInstance(tag)) { - throw new IOException("Mixed types within a list."); - } - - tagList.add(tag); - } - - return new ListTag(name, oclass, tagList); - case NBTConstants.TYPE_COMPOUND: - Map tagMap = new HashMap(); - while(true) { - Tag tag = readTag(depth + 1); - if(tag instanceof EndTag) { - break; - } else { - tagMap.put(tag.getName(), tag); - } - } - - return new CompoundTag(name, tagMap); - case NBTConstants.TYPE_INT_ARRAY: - length = is.readInt(); - int[] data = new int[length]; - - for (int i = 0; i < length; i++) { - data[i] = is.readInt(); - } - - return new IntArrayTag(name, data); - case NBTConstants.TYPE_DOUBLE_ARRAY: - length = is.readInt(); - double[] dat = new double[length]; - - for (int i = 0; i < length; i++) { - dat[i] = is.readDouble(); - } - - return new DoubleArrayTag(name, dat); - case NBTConstants.TYPE_FLOAT_ARRAY: - length = is.readInt(); - float[] floats = new float[length]; - - for (int i = 0; i < length; i++) { - floats[i] = is.readFloat(); - } - - return new FloatArrayTag(name, floats); - case NBTConstants.TYPE_LONG_ARRAY: - length = is.readInt(); - long[] longs = new long[length]; - - for (int i = 0; i < length; i++) { - longs[i] = is.readLong(); - } - - return new LongArrayTag(name, longs); - case NBTConstants.TYPE_OBJECT_ARRAY: - length = is.readInt(); - Object[] objs = new Object[length]; - - ObjectInputStream str = new ObjectInputStream(is); - - for(int i = 0; i < length; i++) { - try { - objs[i] = str.readObject(); - } catch (ClassNotFoundException e) { - logger.severe("Class not found while reading ObjectTag!"); - e.printStackTrace(); - continue; - } - } - - return new ObjectArrayTag(name, objs); - case NBTConstants.TYPE_OBJECT: - str = new ObjectInputStream(is); - Object o = null; - - try { - o = str.readObject(); - } catch (ClassNotFoundException e) { - logger.severe("Class not found while reading ObjectTag!"); - e.printStackTrace(); - return null; - } - - return new ObjectTag(name, o); - case NBTConstants.TYPE_SHORT_ARRAY: - long time = System.currentTimeMillis(); - length = is.readInt(); - short[] shorts = new short[length]; - - for (int i = 0; i < length; i++) { - shorts[i] = is.readShort(); - } - - System.out.println("Took " + (System.currentTimeMillis() - time) + "ms to read a short array."); - return new ShortArrayTag(name, shorts); - case NBTConstants.TYPE_STRING_ARRAY: - length = is.readInt(); - String[] strings = new String[length]; - - for(int i = 0; i < length; i++) { - int size = is.readShort(); - bytes = new byte[size]; - is.readFully(bytes); - strings[i] = new String(bytes, NBTConstants.CHARSET); - } - - return new StringArrayTag(name, strings); - default: - logger.warning("Unknown tag found while reading."); - return new UnknownTag(name); - } - } - - @Override - public void close() throws IOException { - is.close(); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java b/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java deleted file mode 100644 index 6a73006..0000000 --- a/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java +++ /dev/null @@ -1,419 +0,0 @@ -package ch.spacebase.opennbt.stream; - -import java.io.Closeable; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.io.OutputStream; -import java.util.logging.Logger; -import java.util.zip.GZIPOutputStream; - -import ch.spacebase.opennbt.NBTConstants; -import ch.spacebase.opennbt.NBTUtils; -import ch.spacebase.opennbt.tag.ByteArrayTag; -import ch.spacebase.opennbt.tag.ByteTag; -import ch.spacebase.opennbt.tag.CompoundTag; -import ch.spacebase.opennbt.tag.DoubleTag; -import ch.spacebase.opennbt.tag.EndTag; -import ch.spacebase.opennbt.tag.FloatTag; -import ch.spacebase.opennbt.tag.IntArrayTag; -import ch.spacebase.opennbt.tag.IntTag; -import ch.spacebase.opennbt.tag.ListTag; -import ch.spacebase.opennbt.tag.LongTag; -import ch.spacebase.opennbt.tag.ShortTag; -import ch.spacebase.opennbt.tag.StringTag; -import ch.spacebase.opennbt.tag.Tag; -import ch.spacebase.opennbt.tag.custom.DoubleArrayTag; -import ch.spacebase.opennbt.tag.custom.FloatArrayTag; -import ch.spacebase.opennbt.tag.custom.LongArrayTag; -import ch.spacebase.opennbt.tag.custom.ObjectArrayTag; -import ch.spacebase.opennbt.tag.custom.ObjectTag; -import ch.spacebase.opennbt.tag.custom.ShortArrayTag; -import ch.spacebase.opennbt.tag.custom.StringArrayTag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the OpenNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - *

This class writes NBT, or - * Named Binary Tag Tag objects to an underlying - * OutputStream.

- * - *

The NBT format was created by Markus Persson, and the specification may - * be found at - * http://www.minecraft.net/docs/NBT.txt.

- */ -public final class NBTOutputStream implements Closeable { - - private static final Logger logger = Logger.getLogger("NBTOutputStream"); - - /** - * The output stream. - */ - private final DataOutputStream os; - - /** - * Creates a new NBTOutputStream, which will write data to the - * specified underlying output stream. - * @param os The output stream. - * @throws IOException if an I/O error occurs. - */ - public NBTOutputStream(OutputStream os) throws IOException { - this.os = new DataOutputStream(new GZIPOutputStream(os)); - } - - /** - * Writes a tag. - * @param tag The tag to write. - * @throws IOException if an I/O error occurs. - */ - public void writeTag(Tag tag) throws IOException { - int type = NBTUtils.getTypeCode(tag.getClass()); - if(type == NBTConstants.TYPE_UNKNOWN) { - logger.warning("Unknown tag found while writing, ignoring..."); - } - - String name = tag.getName(); - byte[] nameBytes = name.getBytes(NBTConstants.CHARSET); - - os.writeByte(type); - os.writeShort(nameBytes.length); - os.write(nameBytes); - - if(type == NBTConstants.TYPE_END) { - throw new IOException("Named TAG_End not permitted."); - } - - writeTagPayload(tag); - } - - /** - * Writes tag payload. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeTagPayload(Tag tag) throws IOException { - int type = NBTUtils.getTypeCode(tag.getClass()); - switch(type) { - case NBTConstants.TYPE_END: - writeEndTagPayload((EndTag) tag); - break; - case NBTConstants.TYPE_BYTE: - writeByteTagPayload((ByteTag) tag); - break; - case NBTConstants.TYPE_SHORT: - writeShortTagPayload((ShortTag) tag); - break; - case NBTConstants.TYPE_INT: - writeIntTagPayload((IntTag) tag); - break; - case NBTConstants.TYPE_LONG: - writeLongTagPayload((LongTag) tag); - break; - case NBTConstants.TYPE_FLOAT: - writeFloatTagPayload((FloatTag) tag); - break; - case NBTConstants.TYPE_DOUBLE: - writeDoubleTagPayload((DoubleTag) tag); - break; - case NBTConstants.TYPE_BYTE_ARRAY: - writeByteArrayTagPayload((ByteArrayTag) tag); - break; - case NBTConstants.TYPE_STRING: - writeStringTagPayload((StringTag) tag); - break; - case NBTConstants.TYPE_LIST: - writeListTagPayload((ListTag) tag); - break; - case NBTConstants.TYPE_COMPOUND: - writeCompoundTagPayload((CompoundTag) tag); - break; - case NBTConstants.TYPE_INT_ARRAY: - writeIntArrayTagPayload((IntArrayTag) tag); - break; - case NBTConstants.TYPE_DOUBLE_ARRAY: - writeDoubleArrayTagPayload((DoubleArrayTag) tag); - break; - case NBTConstants.TYPE_FLOAT_ARRAY: - writeFloatArrayTagPayload((FloatArrayTag) tag); - break; - case NBTConstants.TYPE_LONG_ARRAY: - writeLongArrayTagPayload((LongArrayTag) tag); - break; - case NBTConstants.TYPE_OBJECT_ARRAY: - writeObjectArrayTagPayload((ObjectArrayTag) tag); - break; - case NBTConstants.TYPE_OBJECT: - writeObjectTagPayload((ObjectTag) tag); - break; - case NBTConstants.TYPE_SHORT_ARRAY: - writeShortArrayTagPayload((ShortArrayTag) tag); - break; - case NBTConstants.TYPE_STRING_ARRAY: - writeStringArrayTagPayload((StringArrayTag) tag); - break; - default: - logger.warning("Unknown tag found while writing, ignoring..."); - } - } - - /** - * Writes a TAG_Byte tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeByteTagPayload(ByteTag tag) throws IOException { - os.writeByte(tag.getValue()); - } - - /** - * Writes a TAG_Byte_Array tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeByteArrayTagPayload(ByteArrayTag tag) throws IOException { - byte[] bytes = tag.getValue(); - os.writeInt(bytes.length); - os.write(bytes); - } - - /** - * Writes a TAG_Compound tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeCompoundTagPayload(CompoundTag tag) throws IOException { - for(Tag childTag : tag.values()) { - writeTag(childTag); - } - os.writeByte((byte) 0); // end tag - better way? - } - - /** - * Writes a TAG_List tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeListTagPayload(ListTag tag) throws IOException { - Class clazz = tag.getType(); - int size = tag.size(); - - os.writeByte(NBTUtils.getTypeCode(clazz)); - os.writeInt(size); - for(Tag t : tag) { - this.writeTagPayload(t); - } - } - - /** - * Writes a TAG_String tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeStringTagPayload(StringTag tag) throws IOException { - byte[] bytes = tag.getValue() != null ? tag.getValue().getBytes(NBTConstants.CHARSET) : new byte[0]; - os.writeShort(bytes.length); - os.write(bytes); - } - - /** - * Writes a TAG_Double tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeDoubleTagPayload(DoubleTag tag) throws IOException { - os.writeDouble(tag.getValue()); - } - - /** - * Writes a TAG_Float tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeFloatTagPayload(FloatTag tag) throws IOException { - os.writeFloat(tag.getValue()); - } - - /** - * Writes a TAG_Long tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeLongTagPayload(LongTag tag) throws IOException { - os.writeLong(tag.getValue()); - } - - /** - * Writes a TAG_Int tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeIntTagPayload(IntTag tag) throws IOException { - os.writeInt(tag.getValue()); - } - - /** - * Writes a TAG_Short tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeShortTagPayload(ShortTag tag) throws IOException { - os.writeShort(tag.getValue()); - } - - /** - * Writes a TAG_Empty tag. - * @param tag The tag. - * @throws IOException if an I/O error occurs. - */ - private void writeEndTagPayload(EndTag tag) { - /* empty */ - } - - /** Writes a TAG_Int_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException { - int[] data = tag.getValue(); - - os.writeInt(data.length); - - for (int i = 0; i < data.length; i++) { - os.writeInt(data[i]); - } - } - - /** Writes a TAG_Double_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeDoubleArrayTagPayload(DoubleArrayTag tag) throws IOException { - double[] data = tag.getValue(); - - os.writeInt(data.length); - - for (int i = 0; i < data.length; i++) { - os.writeDouble(data[i]); - } - } - - /** Writes a TAG_Float_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeFloatArrayTagPayload(FloatArrayTag tag) throws IOException { - float[] data = tag.getValue(); - - os.writeInt(data.length); - - for (int i = 0; i < data.length; i++) { - os.writeFloat(data[i]); - } - } - - /** Writes a TAG_Long_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException { - long[] data = tag.getValue(); - - os.writeInt(data.length); - - for (int i = 0; i < data.length; i++) { - os.writeLong(data[i]); - } - } - - /** Writes a TAG_Object_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeObjectArrayTagPayload(ObjectArrayTag tag) throws IOException { - Object[] data = tag.getValue(); - - os.writeInt(data.length); - ObjectOutputStream str = new ObjectOutputStream(os); - - for (int i = 0; i < data.length; i++) { - str.writeObject(data[i]); - } - } - - /** Writes a TAG_Object tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeObjectTagPayload(ObjectTag tag) throws IOException { - (new ObjectOutputStream(os)).writeObject(tag.getValue()); - } - - /** Writes a TAG_Short_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeShortArrayTagPayload(ShortArrayTag tag) throws IOException { - short[] data = tag.getValue(); - - os.writeInt(data.length); - - for (int i = 0; i < data.length; i++) { - os.writeShort(data[i]); - } - } - - /** Writes a TAG_String_Array tag. - * @param tag The tag - * @throws IOException if an I/O error occurs. - */ - private void writeStringArrayTagPayload(StringArrayTag tag) throws IOException { - String[] data = tag.getValue(); - - os.writeInt(data.length); - byte[] bytes; - - for (int i = 0; i < data.length; i++) { - bytes = data[i].getBytes(NBTConstants.CHARSET); - os.writeShort(bytes.length); - os.write(bytes); - } - } - - @Override - public void close() throws IOException { - os.close(); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java index 32a0d64..59bb469 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java @@ -1,55 +1,28 @@ package ch.spacebase.opennbt.tag; -import java.util.Arrays; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the OpenNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Byte_Array tag. + * A tag containing a byte array. */ -public final class ByteArrayTag extends Tag { +public class ByteArrayTag extends Tag { + + private byte[] value; /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final byte[] value; + public ByteArrayTag(String name) { + this(name, new byte[0]); + } /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public ByteArrayTag(String name, byte[] value) { super(name); @@ -58,34 +31,67 @@ public final class ByteArrayTag extends Tag { @Override public byte[] getValue() { - return value; + return this.value.clone(); } - @Override - public String toString() { - String name = getName(); - String append = ""; - - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(byte[] value) { + if(value == null) { + return; } - return "TAG_Byte_Array" + append + ": " + Arrays.toString(value); + 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 byte 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, byte 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 boolean equals(Object obj) { - if(!(obj instanceof ByteArrayTag)) return false; - - ByteArrayTag tag = (ByteArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 7; + } + + @Override + public void read(DataInputStream in) throws IOException { + this.value = new byte[in.readInt()]; + in.readFully(this.value); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + out.write(this.value); } + @Override public ByteArrayTag clone() { - byte[] clonedArray = this.getValue().clone(); - - return new ByteArrayTag(this.getName(), clonedArray); + return new ByteArrayTag(this.getName(), this.getValue()); } } diff --git a/src/main/java/ch/spacebase/opennbt/tag/ByteTag.java b/src/main/java/ch/spacebase/opennbt/tag/ByteTag.java index 264b4d1..1af94b7 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ByteTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/ByteTag.java @@ -1,53 +1,28 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the OpenNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Byte tag. + * A tag containing a byte. */ -public final class ByteTag extends Tag { +public class ByteTag extends Tag { - /** - * The value. - */ - private final byte value; + private byte value; /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + */ + public ByteTag(String name) { + this(name, (byte) 0); + } + + /** + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public ByteTag(String name, byte value) { super(name); @@ -56,19 +31,33 @@ public final class ByteTag extends Tag { @Override public Byte getValue() { - return value; + return this.value; + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(byte value) { + this.value = value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Byte" + append + ": " + value; + public int getId() { + return 1; + } + + @Override + public void read(DataInputStream in) throws IOException { + this.value = in.readByte(); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeByte(this.value); } + @Override public ByteTag clone() { return new ByteTag(this.getName(), this.getValue()); } diff --git a/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java b/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java index f7eadc1..5d95249 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java @@ -1,126 +1,131 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.Collection; -import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Map.Entry; -import ch.spacebase.opennbt.NBTUtils; +import ch.spacebase.opennbt.NBTIO; /** - * The TAG_Compound tag. + * A compound tag containing other tags. */ -public final class CompoundTag extends Tag { +public class CompoundTag extends Tag { + + private Map value; /** - * The value. - */ - private final Map value; - - /** - * Creates the tag. - * @param name The name. + * Creates a tag with the specified name. + * @param name The name of the tag. */ public CompoundTag(String name) { - this(name, new HashMap()); + this(name, new LinkedHashMap()); } /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public CompoundTag(String name, Map value) { super(name); - this.value = value; + this.value = new LinkedHashMap(value); } @Override public Map getValue() { - return new HashMap(value); + return new LinkedHashMap(this.value); } + /** + * Gets the tag with the specified name. + * @param tagName Name of the tag. + * @return The tag with the specified name. + */ public Tag get(String tagName) { return this.value.get(tagName); } - public Tag put(String tagName, Tag tag) { - return this.value.put(tagName, tag); + /** + * Puts the tag into this compound tag. + * @param tag Tag to put into this compound tag. + * @return The previous tag associated with its name, or null if there wasn't one. + */ + public Tag put(Tag tag) { + return this.value.put(tag.getName(), tag); } + /** + * Removes a tag from this compound tag. + * @param tagName Name of the tag to remove. + * @return The removed tag. + */ public Tag remove(String tagName) { return this.value.remove(tagName); } + /** + * Gets a set of keys in this compound tag. + * @return The compound tag's key set. + */ public Set keySet() { return this.value.keySet(); } + /** + * Gets a collection of tags in this compound tag. + * @return This compound tag's tags. + */ public Collection values() { return this.value.values(); } + /** + * Gets the number of tags in this compound tag. + * @return This compound tag's size. + */ public int size() { return this.value.size(); } + /** + * Clears all tags from this compound tag. + */ public void clear() { this.value.clear(); } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - StringBuilder bldr = new StringBuilder(); - bldr.append("TAG_Compound" + append + ": " + value.size() + " entries\r\n{\r\n"); - for(Map.Entry entry : value.entrySet()) { - bldr.append(" " + entry.getValue().toString().replaceAll("\r\n", "\r\n ") + "\r\n"); - } - bldr.append("}"); - return bldr.toString(); + public int getId() { + return 10; } - public CompoundTag clone() { - Map newMap = NBTUtils.cloneMap(this.getValue()); + @Override + public void read(DataInputStream in) throws IOException { + List tags = NBTIO.readUntilEndTag(in); + for(Tag tag : tags) { + this.put(tag); + } + } + @Override + public void write(DataOutputStream out) throws IOException { + NBTIO.writeTags(out, this.value.values()); + out.writeByte(0); + } + + @Override + public CompoundTag clone() { + Map newMap = new LinkedHashMap(); + for(Entry entry : this.value.entrySet()) { + newMap.put(entry.getKey(), entry.getValue().clone()); + } + return new CompoundTag(this.getName(), newMap); } diff --git a/src/main/java/ch/spacebase/opennbt/tag/DoubleTag.java b/src/main/java/ch/spacebase/opennbt/tag/DoubleTag.java index bfa8719..c379c70 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/DoubleTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/DoubleTag.java @@ -1,76 +1,65 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Double tag. + * A tag containing a double. */ -public final class DoubleTag extends Tag { +public class DoubleTag extends Tag { + + private double value; /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final double value; - + public DoubleTag(String name) { + this(name, 0); + } + /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public DoubleTag(String name, double value) { super(name); this.value = value; } - + @Override public Double getValue() { - return value; + return this.value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Double" + append + ": " + value; + public int getId() { + return 6; } + @Override + public void read(DataInputStream in) throws IOException { + this.value = in.readDouble(); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeDouble(this.value); + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(double value) { + this.value = value; + } + + @Override public DoubleTag clone() { return new DoubleTag(this.getName(), this.getValue()); } - + } diff --git a/src/main/java/ch/spacebase/opennbt/tag/EndTag.java b/src/main/java/ch/spacebase/opennbt/tag/EndTag.java deleted file mode 100644 index b0d0d81..0000000 --- a/src/main/java/ch/spacebase/opennbt/tag/EndTag.java +++ /dev/null @@ -1,63 +0,0 @@ -package ch.spacebase.opennbt.tag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * The TAG_End tag. - */ -public final class EndTag extends Tag { - - /** - * Creates the tag. - */ - public EndTag() { - super(""); - } - - @Override - public Object getValue() { - return null; - } - - @Override - public String toString() { - return "TAG_End"; - } - - public EndTag clone() { - return new EndTag(); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/tag/FloatTag.java b/src/main/java/ch/spacebase/opennbt/tag/FloatTag.java index 2632cf4..98c61c8 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/FloatTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/FloatTag.java @@ -1,76 +1,65 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Float tag. + * A tag containing a float. */ -public final class FloatTag extends Tag { +public class FloatTag extends Tag { - /** - * The value. - */ - private final float value; + private float value; /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + */ + public FloatTag(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 FloatTag(String name, float value) { super(name); this.value = value; } - + @Override public Float getValue() { - return value; + return this.value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Float" + append + ": " + value; + public int getId() { + return 5; } + @Override + public void read(DataInputStream in) throws IOException { + this.value = in.readFloat(); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeFloat(this.value); + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(float value) { + this.value = value; + } + + @Override public FloatTag clone() { return new FloatTag(this.getName(), this.getValue()); } - + } diff --git a/src/main/java/ch/spacebase/opennbt/tag/IntArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/IntArrayTag.java index a4f120c..4f7d6df 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/IntArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/IntArrayTag.java @@ -1,109 +1,101 @@ package ch.spacebase.opennbt.tag; -import java.util.Arrays; - -import ch.spacebase.opennbt.tag.Tag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Int_Array tag. + * A tag containing an integer array. */ -public final class IntArrayTag extends Tag { +public class IntArrayTag extends Tag { + private int[] value; + /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final int[] value; - + public IntArrayTag(String name) { + this(name, new int[0]); + } + /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public IntArrayTag(String name, int[] value) { super(name); this.value = value; } - + @Override public int[] getValue() { - return value; - } - - @Override - public String toString() { - StringBuilder hex = new StringBuilder(); - - for (int curr : value) { - String hexDigits = Integer.toHexString(curr).toUpperCase(); - - if (hexDigits.length() == 1) { - hex.append("0"); - } - - hex.append(hexDigits).append(" "); - } - - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - - return "TAG_Int_Array" + append + ": " + hex.toString(); + return this.value.clone(); } @Override - public boolean equals(Object obj) { - if(!(obj instanceof IntArrayTag)) return false; - - IntArrayTag tag = (IntArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 11; + } + + @Override + public void read(DataInputStream in) throws IOException { + this.value = new int[in.readInt()]; + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readInt(); + } } + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + for(int index = 0; index < this.value.length; index++) { + out.writeInt(this.value[index]); + } + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(int[] 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 int 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, int 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 IntArrayTag clone() { - int[] clonedArray = this.getValue().clone(); - - return new IntArrayTag(this.getName(), clonedArray); + return new IntArrayTag(this.getName(), this.getValue()); } -} \ No newline at end of file +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/IntTag.java b/src/main/java/ch/spacebase/opennbt/tag/IntTag.java index 151af16..995c95b 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/IntTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/IntTag.java @@ -1,53 +1,28 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Int tag. + * A tag containing an integer. */ -public final class IntTag extends Tag { +public class IntTag extends Tag { - /** - * The value. - */ - private final int value; + private int value; /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + */ + public IntTag(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 IntTag(String name, int value) { super(name); @@ -56,21 +31,35 @@ public final class IntTag extends Tag { @Override public Integer getValue() { - return value; + return this.value; + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(int value) { + this.value = value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Int" + append + ": " + value; + public int getId() { + return 3; } + @Override + public void read(DataInputStream in) throws IOException { + this.value = in.readInt(); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value); + } + + @Override public IntTag clone() { return new IntTag(this.getName(), this.getValue()); } - + } diff --git a/src/main/java/ch/spacebase/opennbt/tag/ListTag.java b/src/main/java/ch/spacebase/opennbt/tag/ListTag.java index 50d13dc..407406c 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ListTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/ListTag.java @@ -1,74 +1,45 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import ch.spacebase.opennbt.NBTUtils; +import ch.spacebase.opennbt.TagRegistry; /** - * The TAG_List tag. + * A tag containing a list of tags. */ -public final class ListTag extends Tag implements Iterable { +public class ListTag extends Tag implements Iterable { - /** - * The type. - */ - private final Class type; + private Class type; + private List value; /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final List value; + public ListTag(String name) { + super(name); + this.value = new ArrayList(); + } /** - * Creates the tag. - * @param name The name. - * @param type The type of item in the list. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param type Tag type of the list. */ public ListTag(String name, Class type) { this(name, type, new ArrayList()); } /** - * Creates the tag. - * @param name The name. - * @param type The type of item in the list. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param type Tag type of the list. + * @param value The value of the tag. */ public ListTag(String name, Class type, List value) { super(name); @@ -76,64 +47,99 @@ public final class ListTag extends Tag implements Iterable { this.value = value; } - /** - * Gets the type of item in this list. - * @return The type of item in this list. - */ - public Class getType() { - return type; - } - @Override public List getValue() { - return new ArrayList(value); + return new ArrayList(this.value); } - public boolean add(T value) { - return this.value.add(value); + /** + * Adds a tag to this list tag. + * @param tag Tag to add. + * @return If the list was changed as a result. + */ + public boolean add(T tag) { + return this.value.add(tag); } - public boolean remove(T value) { - return this.value.remove(value); + /** + * Removes a tag from this list tag. + * @param tag Tag to remove. + * @return If the list contained the tag. + */ + public boolean remove(T tag) { + return this.value.remove(tag); } + /** + * Gets the tag at the given index of this list tag. + * @param index Index of the tag. + * @return The tag at the given index. + */ public T get(int index) { return this.value.get(index); } - public Iterator iterator() { - return this.value.iterator(); - } - + /** + * Gets the number of tags in this list tag. + * @return The size of this list tag. + */ public int size() { return this.value.size(); } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + public Iterator iterator() { + return this.value.iterator(); + } + + @Override + public int getId() { + return 9; + } + + @SuppressWarnings("unchecked") + @Override + public void read(DataInputStream in) throws IOException { + int id = in.readByte() & 0xFF; + this.type = (Class) TagRegistry.getClassFor(id); + if(this.type == null) { + throw new IOException("Unknown tag ID in ListTag: " + id); } - StringBuilder bldr = new StringBuilder(); - bldr.append("TAG_List" + append + ": " + value.size() + " entries of type " + NBTUtils.getTypeName(type) + "\r\n{\r\n"); - for(Tag t : value) { - bldr.append(" " + t.toString().replaceAll("\r\n", "\r\n ") + "\r\n"); + + int count = in.readInt(); + for(int index = 0; index < count; index++) { + Tag tag = TagRegistry.createInstance(id, ""); + if(tag == null) { + throw new IOException("Tag could not be created: \"" + this.type.getSimpleName() + "\" (" + id + ")"); + } + + tag.read(in); + this.add((T) tag); + } + } + + @Override + public void write(DataOutputStream out) throws IOException { + int id = TagRegistry.getIdFor(this.type); + if(id == -1) { + throw new IOException("ListTag contains unregistered tag class."); + } + + out.writeByte(id); + out.writeInt(this.value.size()); + for(Tag tag : this.value) { + tag.write(out); } - bldr.append("}"); - return bldr.toString(); } @SuppressWarnings("unchecked") public ListTag clone() { List newList = new ArrayList(); - - for(T value : this.getValue()) { + for(T value : this.value) { newList.add((T) value.clone()); } - return new ListTag(this.getName(), this.getType(), newList); + return new ListTag(this.getName(), this.type, newList); } } diff --git a/src/main/java/ch/spacebase/opennbt/tag/LongTag.java b/src/main/java/ch/spacebase/opennbt/tag/LongTag.java index a964506..331c828 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/LongTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/LongTag.java @@ -1,76 +1,65 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Long tag. + * A tag containing a long. */ -public final class LongTag extends Tag { +public class LongTag extends Tag { - /** - * The value. - */ - private final long value; + private long value; /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + */ + public LongTag(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 LongTag(String name, long value) { super(name); this.value = value; } - + @Override public Long getValue() { - return value; + return this.value; + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(long value) { + this.value = value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Long" + append + ": " + value; + public int getId() { + return 4; } + @Override + public void read(DataInputStream in) throws IOException { + this.value = in.readLong(); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeLong(this.value); + } + + @Override public LongTag clone() { return new LongTag(this.getName(), this.getValue()); } - + } diff --git a/src/main/java/ch/spacebase/opennbt/tag/ShortTag.java b/src/main/java/ch/spacebase/opennbt/tag/ShortTag.java index 551f931..e220491 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ShortTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/ShortTag.java @@ -1,76 +1,65 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; /** - * The TAG_Short tag. + * A tag containing a short. */ -public final class ShortTag extends Tag { +public class ShortTag extends Tag { - /** - * The value. - */ - private final short value; + private short value; /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + */ + public ShortTag(String name) { + this(name, (short) 0); + } + + /** + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public ShortTag(String name, short value) { super(name); this.value = value; } - + @Override public Short getValue() { - return value; + return this.value; + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(short value) { + this.value = value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Short" + append + ": " + value; + public int getId() { + return 2; } + @Override + public void read(DataInputStream in) throws IOException { + this.value = in.readShort(); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeShort(this.value); + } + + @Override public ShortTag clone() { return new ShortTag(this.getName(), this.getValue()); } - + } diff --git a/src/main/java/ch/spacebase/opennbt/tag/StringTag.java b/src/main/java/ch/spacebase/opennbt/tag/StringTag.java index d5632ce..666df1a 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/StringTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/StringTag.java @@ -1,53 +1,30 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import ch.spacebase.opennbt.NBTIO; /** - * The TAG_String tag. + * A tag containing a string. */ -public final class StringTag extends Tag { - - /** - * The value. - */ - private final String value; +public class StringTag extends Tag { + + private String value; /** - * Creates the tag. - * @param name The name. - * @param value The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + */ + public StringTag(String name) { + this(name, ""); + } + + /** + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public StringTag(String name, String value) { super(name); @@ -56,19 +33,37 @@ public final class StringTag extends Tag { @Override public String getValue() { - return value; + return this.value; + } + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(String value) { + this.value = value; } @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_String" + append + ": " + value; + public int getId() { + return 8; } + @Override + public void read(DataInputStream in) throws IOException { + byte[] bytes = new byte[in.readShort()]; + in.readFully(bytes); + this.value = new String(bytes, NBTIO.CHARSET); + } + + @Override + public void write(DataOutputStream out) throws IOException { + byte[] bytes = this.value.getBytes(NBTIO.CHARSET); + out.writeShort(bytes.length); + out.write(bytes); + } + + @Override public StringTag clone() { return new StringTag(this.getName(), this.getValue()); } diff --git a/src/main/java/ch/spacebase/opennbt/tag/Tag.java b/src/main/java/ch/spacebase/opennbt/tag/Tag.java index 1983419..005cf12 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/Tag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/Tag.java @@ -1,51 +1,22 @@ package ch.spacebase.opennbt.tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.lang.reflect.Array; /** - * Represents a single NBT tag. + * Represents an NBT tag. + * + * All tags must have a constructor with a single string parameter for reading tags. + * Tags should also have setter methods specific to their value types. */ public abstract class Tag implements Cloneable { + + private String name; /** - * The name of this tag. - */ - private final String name; - - /** - * Creates the tag with the specified name. + * Creates a tag with the specified name. * @param name The name. */ public Tag(String name) { @@ -57,7 +28,7 @@ public abstract class Tag implements Cloneable { * @return The name of this tag. */ public final String getName() { - return name; + return this.name; } /** @@ -66,15 +37,89 @@ public abstract class Tag implements Cloneable { */ public abstract Object getValue(); + /** + * Gets the type id of this tag. + * @return The tag's id. + */ + public abstract int getId(); + + /** + * Reads this tag from an input stream. + * @param in Stream to write to. + * @throws IOException If an I/O error occurs. + */ + public abstract void read(DataInputStream in) throws IOException; + + /** + * Writes this tag to an output stream. + * @param out Stream to write to. + * @throws IOException If an I/O error occurs. + */ + public abstract void write(DataOutputStream out) throws IOException; + @Override public boolean equals(Object obj) { - if(!(obj instanceof Tag)) return false; + if(!(obj instanceof Tag)) { + return false; + } Tag tag = (Tag) obj; + if(!this.getName().equals(tag.getName())) { + return false; + } - return this.getValue().equals(tag.getValue()) && this.getName().equals(tag.getName()); + if(this.getValue() == null) { + return tag.getValue() == null; + } else if(tag.getValue() == null) { + return false; + } + + if(this.getValue().getClass().isArray() && tag.getValue().getClass().isArray()) { + int length = Array.getLength(this.getValue()); + if(Array.getLength(tag.getValue()) != length) { + return false; + } + + for(int index = 0; index < length; index++) { + Object o = Array.get(this.getValue(), index); + Object other = Array.get(tag.getValue(), index); + if(o == null && other != null || o != null && !o.equals(other)) { + return false; + } + } + + return true; + } + + return this.getValue().equals(tag.getValue()); } + @Override + public String toString() { + String name = this.getName() != null && !this.getName().equals("") ? "(" + this.getName() + ")" : ""; + String value = ""; + if(this.getValue() != null) { + value = this.getValue().toString(); + if(this.getValue().getClass().isArray()) { + StringBuilder build = new StringBuilder(); + build.append("["); + for(int index = 0; index < Array.getLength(this.getValue()); index++) { + if(index > 0) { + build.append(", "); + } + + build.append(Array.get(this.getValue(), index)); + } + + build.append("]"); + value = build.toString(); + } + } + + return this.getClass().getSimpleName() + name + " { " + value + " }"; + } + + @Override public abstract Tag clone(); } diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/DoubleArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/DoubleArrayTag.java index cf57e77..f745dac 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/DoubleArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/DoubleArrayTag.java @@ -1,97 +1,103 @@ package ch.spacebase.opennbt.tag.custom; -import java.util.Arrays; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import ch.spacebase.opennbt.tag.Tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - /** - * The TAG_Double_Array tag. + * A tag containing a double array. */ -public final class DoubleArrayTag extends Tag { +public class DoubleArrayTag extends Tag { + private double[] value; + /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final double[] value; - + public DoubleArrayTag(String name) { + this(name, new double[0]); + } + /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public DoubleArrayTag(String name, double[] value) { super(name); this.value = value; } - + @Override public double[] getValue() { - return value; + return this.value.clone(); } - - @Override - public String toString() { - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(double[] value) { + if(value == null) { + return; } - - return "TAG_Double_Array" + append + ": " + Arrays.toString(value); + + 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 double 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, double 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 boolean equals(Object obj) { - if(!(obj instanceof DoubleArrayTag)) return false; - - DoubleArrayTag tag = (DoubleArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 60; } @Override - public DoubleArrayTag clone() { - double[] clonedArray = this.getValue().clone(); - - return new DoubleArrayTag(this.getName(), clonedArray); + public void read(DataInputStream in) throws IOException { + this.value = new double[in.readInt()]; + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readDouble(); + } } -} \ No newline at end of file + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + for(int index = 0; index < this.value.length; index++) { + out.writeDouble(this.value[index]); + } + } + + @Override + public DoubleArrayTag clone() { + return new DoubleArrayTag(this.getName(), this.getValue()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/FloatArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/FloatArrayTag.java index 81ed9e3..e4a3b84 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/FloatArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/FloatArrayTag.java @@ -1,97 +1,103 @@ package ch.spacebase.opennbt.tag.custom; -import java.util.Arrays; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import ch.spacebase.opennbt.tag.Tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - /** - * The TAG_Float_Array tag. + * A tag containing a float array. */ -public final class FloatArrayTag extends Tag { +public class FloatArrayTag extends Tag { + private float[] value; + /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final float[] value; - + public FloatArrayTag(String name) { + this(name, new float[0]); + } + /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public FloatArrayTag(String name, float[] value) { super(name); this.value = value; } - + @Override public float[] getValue() { - return value; + return this.value.clone(); } - - @Override - public String toString() { - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(float[] value) { + if(value == null) { + return; } - - return "TAG_Float_Array" + append + ": " + Arrays.toString(value); + + 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 float 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, float 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 boolean equals(Object obj) { - if(!(obj instanceof FloatArrayTag)) return false; - - FloatArrayTag tag = (FloatArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 61; } @Override - public FloatArrayTag clone() { - float[] clonedArray = this.getValue().clone(); - - return new FloatArrayTag(this.getName(), clonedArray); + public void read(DataInputStream in) throws IOException { + this.value = new float[in.readInt()]; + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readFloat(); + } } -} \ No newline at end of file + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + for(int index = 0; index < this.value.length; index++) { + out.writeFloat(this.value[index]); + } + } + + @Override + public FloatArrayTag clone() { + return new FloatArrayTag(this.getName(), this.getValue()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/LongArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/LongArrayTag.java index b17b7be..b210ff6 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/LongArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/LongArrayTag.java @@ -1,97 +1,103 @@ package ch.spacebase.opennbt.tag.custom; -import java.util.Arrays; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import ch.spacebase.opennbt.tag.Tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - /** - * The TAG_Long_Array tag. + * A tag containing a long array. */ -public final class LongArrayTag extends Tag { +public class LongArrayTag extends Tag { + private long[] value; + /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final long[] value; - + public LongArrayTag(String name) { + this(name, new long[0]); + } + /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public LongArrayTag(String name, long[] value) { super(name); this.value = value; } - + @Override public long[] getValue() { - return value; + return this.value.clone(); } - - @Override - public String toString() { - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(long[] value) { + if(value == null) { + return; } - - return "TAG_Long_Array" + append + ": " + Arrays.toString(value); + + 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 long 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, long 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 boolean equals(Object obj) { - if(!(obj instanceof LongArrayTag)) return false; - - LongArrayTag tag = (LongArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 62; } @Override - public LongArrayTag clone() { - long[] clonedArray = this.getValue().clone(); - - return new LongArrayTag(this.getName(), clonedArray); + public void read(DataInputStream in) throws IOException { + this.value = new long[in.readInt()]; + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readLong(); + } } -} \ No newline at end of file + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + for(int index = 0; index < this.value.length; index++) { + out.writeLong(this.value[index]); + } + } + + @Override + public LongArrayTag clone() { + return new LongArrayTag(this.getName(), this.getValue()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/ObjectArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/ObjectArrayTag.java deleted file mode 100644 index 219e696..0000000 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/ObjectArrayTag.java +++ /dev/null @@ -1,96 +0,0 @@ -package ch.spacebase.opennbt.tag.custom; - -import java.util.Arrays; - -import ch.spacebase.opennbt.tag.Tag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * The TAG_Object_Array tag. - */ -public final class ObjectArrayTag extends Tag { - - /** - * The value. - */ - private final Object[] value; - - /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. - */ - public ObjectArrayTag(String name, Object[] value) { - super(name); - this.value = value; - } - - @Override - public Object[] getValue() { - return value; - } - - @Override - public String toString() { - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - - return "TAG_Object_Array" + append + ": " + Arrays.toString(value); - } - - @Override - public boolean equals(Object obj) { - if(!(obj instanceof ObjectArrayTag)) return false; - - ObjectArrayTag tag = (ObjectArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); - } - - @Override - public ObjectArrayTag clone() { - Object[] clonedArray = this.getValue().clone(); - return new ObjectArrayTag(this.getName(), clonedArray); - } - -} \ No newline at end of file diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/ObjectTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/ObjectTag.java deleted file mode 100644 index d2c77ff..0000000 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/ObjectTag.java +++ /dev/null @@ -1,78 +0,0 @@ -package ch.spacebase.opennbt.tag.custom; - -import ch.spacebase.opennbt.tag.Tag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * The TAG_Object tag. - */ -public final class ObjectTag extends Tag { - - /** - * The value. - */ - private final Object value; - - /** - * Creates the tag. - * @param name The name. - * @param value The value. - */ - public ObjectTag(String name, Object value) { - super(name); - this.value = value; - } - - @Override - public Object getValue() { - return value; - } - - @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Object" + append + ": " + value.toString(); - } - - public ObjectTag clone() { - return new ObjectTag(this.getName(), this.getValue()); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableArrayTag.java new file mode 100644 index 0000000..d7bf658 --- /dev/null +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableArrayTag.java @@ -0,0 +1,112 @@ +package ch.spacebase.opennbt.tag.custom; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import ch.spacebase.opennbt.tag.Tag; + +/** + * 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 int getId() { + return 63; + } + + @Override + public void read(DataInputStream in) throws IOException { + this.value = new Serializable[in.readInt()]; + ObjectInputStream str = new ObjectInputStream(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(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + ObjectOutputStream str = new ObjectOutputStream(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()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableTag.java new file mode 100644 index 0000000..74f5099 --- /dev/null +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableTag.java @@ -0,0 +1,76 @@ +package ch.spacebase.opennbt.tag.custom; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import ch.spacebase.opennbt.tag.Tag; + +/** + * 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 int getId() { + return 64; + } + + @Override + public void read(DataInputStream in) throws IOException { + ObjectInputStream str = new ObjectInputStream(in); + try { + this.value = (Serializable) str.readObject(); + } catch (ClassNotFoundException e) { + throw new IOException("Class not found while reading SerializableTag!", e); + } + } + + @Override + public void write(DataOutputStream out) throws IOException { + ObjectOutputStream str = new ObjectOutputStream(out); + str.writeObject(this.value); + } + + @Override + public SerializableTag clone() { + return new SerializableTag(this.getName(), this.getValue()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/ShortArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/ShortArrayTag.java index 4928a10..b8d7f25 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/ShortArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/ShortArrayTag.java @@ -1,97 +1,103 @@ package ch.spacebase.opennbt.tag.custom; -import java.util.Arrays; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import ch.spacebase.opennbt.tag.Tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - /** - * The TAG_Short_Array tag. + * A tag containing a short array. */ -public final class ShortArrayTag extends Tag { +public class ShortArrayTag extends Tag { + private short[] value; + /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final short[] value; - + public ShortArrayTag(String name) { + this(name, new short[0]); + } + /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. + * @param value The value of the tag. */ public ShortArrayTag(String name, short[] value) { super(name); this.value = value; } - + @Override public short[] getValue() { - return value; + return this.value.clone(); } - - @Override - public String toString() { - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(short[] value) { + if(value == null) { + return; } - - return "TAG_Short_Array" + append + ": " + Arrays.toString(value); + + 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 short 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, short 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 boolean equals(Object obj) { - if(!(obj instanceof ShortArrayTag)) return false; - - ShortArrayTag tag = (ShortArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 65; } @Override - public ShortArrayTag clone() { - short[] clonedArray = this.getValue().clone(); - - return new ShortArrayTag(this.getName(), clonedArray); + public void read(DataInputStream in) throws IOException { + this.value = new short[in.readInt()]; + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readShort(); + } } -} \ No newline at end of file + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + for(int index = 0; index < this.value.length; index++) { + out.writeShort(this.value[index]); + } + } + + @Override + public ShortArrayTag clone() { + return new ShortArrayTag(this.getName(), this.getValue()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/StringArrayTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/StringArrayTag.java index 7e7a6fe..e18acad 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/StringArrayTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/custom/StringArrayTag.java @@ -1,97 +1,108 @@ package ch.spacebase.opennbt.tag.custom; -import java.util.Arrays; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import ch.spacebase.opennbt.NBTIO; import ch.spacebase.opennbt.tag.Tag; -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - /** - * The TAG_String_Array tag. + * A tag containing a string array. */ -public final class StringArrayTag extends Tag { - +public class StringArrayTag extends Tag { + + private String[] value; + /** - * The value. + * Creates a tag with the specified name. + * @param name The name of the tag. */ - private final String[] value; - + public StringArrayTag(String name) { + this(name, new String[0]); + } + /** - * Creates the tag. - * - * @param name - * The name. - * @param value - * The value. + * 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 value; + return this.value.clone(); } - - @Override - public String toString() { - String name = getName(); - String append = ""; - - if (name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; + + /** + * Sets the value of this tag. + * @param value New value of this tag. + */ + public void setValue(String[] value) { + if(value == null) { + return; } - - return "TAG_String_Array" + append + ": " + Arrays.toString(value); + + 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 boolean equals(Object obj) { - if(!(obj instanceof StringArrayTag)) return false; - - StringArrayTag tag = (StringArrayTag) obj; - - return Arrays.equals(this.getValue(), tag.getValue()) && this.getName().equals(tag.getName()); + public int getId() { + return 66; + } + + @Override + public void read(DataInputStream in) throws IOException { + this.value = new String[in.readInt()]; + for(int index = 0; index < this.value.length; index++) { + byte[] bytes = new byte[in.readShort()]; + in.readFully(bytes); + this.value[index] = new String(bytes, NBTIO.CHARSET); + } } + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(this.value.length); + for(int index = 0; index < this.value.length; index++) { + byte[] bytes = this.value[index].getBytes(NBTIO.CHARSET); + out.writeShort(bytes.length); + out.write(bytes); + } + } + @Override public StringArrayTag clone() { - String[] clonedArray = this.getValue().clone(); - - return new StringArrayTag(this.getName(), clonedArray); + return new StringArrayTag(this.getName(), this.getValue()); } -} \ No newline at end of file +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/custom/UnknownTag.java b/src/main/java/ch/spacebase/opennbt/tag/custom/UnknownTag.java deleted file mode 100644 index af80b70..0000000 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/UnknownTag.java +++ /dev/null @@ -1,81 +0,0 @@ -package ch.spacebase.opennbt.tag.custom; - -import ch.spacebase.opennbt.tag.Tag; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * The TAG_Unknown tag. - */ -public class UnknownTag extends Tag { - - /** - * Creates the tag. - * @param name The name. - * @param value The value. - */ - public UnknownTag(String name) { - super(name); - } - - @Override - public Object getValue() { - return null; - } - - @Override - public String toString() { - String name = getName(); - String append = ""; - if(name != null && !name.equals("")) { - append = "(\"" + this.getName() + "\")"; - } - return "TAG_Unknown" + append; - } - - @Override - public boolean equals(Object obj) { - if(!(obj instanceof UnknownTag)) return false; - - UnknownTag tag = (UnknownTag) obj; - - return tag.getName().equals(this.getName()); - } - - public UnknownTag clone() { - return new UnknownTag(this.getName()); - } - -} diff --git a/src/main/java/ch/spacebase/opennbt/utils/DoubleIntHashMap.java b/src/main/java/ch/spacebase/opennbt/utils/DoubleIntHashMap.java deleted file mode 100644 index fc30f3d..0000000 --- a/src/main/java/ch/spacebase/opennbt/utils/DoubleIntHashMap.java +++ /dev/null @@ -1,78 +0,0 @@ -package ch.spacebase.opennbt.utils; - -import java.util.HashMap; - -/* - * OpenNBT License - * - * JNBT Copyright (c) 2010 Graham Edgecombe - * OpenNBT Copyright(c) 2012 Steveice10 - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of the JNBT team nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * A hashmap using two ints as keys. - */ -public class DoubleIntHashMap extends HashMap { - - private static final long serialVersionUID = 1L; - - public DoubleIntHashMap() { - super(100); - } - - public DoubleIntHashMap(int capacity) { - super(capacity); - } - - public T put(int key1, int key2, T value) { - int key = key(key1, key2); - return super.put(key, value); - } - - public T get(int key1, int key2) { - int key = key(key1, key2); - return super.get(key); - } - - public boolean containsKey(int key1, int key2) { - int key = key(key1, key2); - return super.containsKey(key); - } - - public T remove(int key1, int key2) { - int key = key(key1, key2); - return super.remove(key); - } - - private static final int key(int x, int z) { - return (x & 0xF) << 11 | (z & 0xF) << 7; - } - -}