From 7f7acc4193775840600ec2182bc83a057eebe2fc Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Sat, 1 Mar 2014 16:46:32 -0800 Subject: [PATCH] Refactored ch.spacebase to org.spacehq, reformatted code and line endings. --- .gitignore | 10 +- LICENSE.txt | 12 +- README.md | 42 +-- pom.xml | 135 ++++---- .../spacehq}/opennbt/NBTFileIO.java | 83 ++--- .../spacehq}/opennbt/NBTIO.java | 36 ++- .../opennbt/TagRegisterException.java | 6 +- .../spacehq}/opennbt/TagRegistry.java | 97 +++--- .../spacehq}/opennbt/tag/ByteArrayTag.java | 200 ++++++------ .../spacehq}/opennbt/tag/ByteTag.java | 133 ++++---- .../spacehq}/opennbt/tag/CompoundTag.java | 268 ++++++++-------- .../spacehq}/opennbt/tag/DoubleTag.java | 133 ++++---- .../spacehq}/opennbt/tag/FloatTag.java | 133 ++++---- .../spacehq}/opennbt/tag/IntArrayTag.java | 38 ++- .../spacehq}/opennbt/tag/IntTag.java | 133 ++++---- .../spacehq}/opennbt/tag/ListTag.java | 299 +++++++++--------- .../spacehq}/opennbt/tag/LongTag.java | 133 ++++---- .../spacehq}/opennbt/tag/ShortTag.java | 133 ++++---- .../spacehq}/opennbt/tag/StringTag.java | 145 ++++----- .../spacehq}/opennbt/tag/Tag.java | 256 +++++++-------- .../opennbt/tag/custom/DoubleArrayTag.java | 40 ++- .../opennbt/tag/custom/FloatArrayTag.java | 40 ++- .../opennbt/tag/custom/LongArrayTag.java | 40 ++- .../tag/custom/SerializableArrayTag.java | 63 ++-- .../opennbt/tag/custom/SerializableTag.java | 40 ++- .../opennbt/tag/custom/ShortArrayTag.java | 40 ++- .../opennbt/tag/custom/StringArrayTag.java | 52 +-- 27 files changed, 1404 insertions(+), 1336 deletions(-) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/NBTFileIO.java (72%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/NBTIO.java (84%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/TagRegisterException.java (89%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/TagRegistry.java (51%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/ByteArrayTag.java (90%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/ByteTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/CompoundTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/DoubleTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/FloatTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/IntArrayTag.java (89%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/IntTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/ListTag.java (90%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/LongTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/ShortTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/StringTag.java (82%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/Tag.java (90%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/DoubleArrayTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/FloatArrayTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/LongArrayTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/SerializableArrayTag.java (69%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/SerializableTag.java (68%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/ShortArrayTag.java (87%) rename src/main/java/{ch/spacebase => org/spacehq}/opennbt/tag/custom/StringArrayTag.java (81%) diff --git a/.gitignore b/.gitignore index e112f2f..13f6839 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,12 @@ bin -.git +lib + +.settings .classpath .project -.settings + +*.iml +.idea + target + diff --git a/LICENSE.txt b/LICENSE.txt index 075fd67..4137cf4 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,7 +1,7 @@ -Copyright (C) 2013-2014 Steveice10 - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - +Copyright (C) 2013-2014 Steveice10 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index b47ddd3..a1c3464 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,22 @@ -

OpenNBT

-============== - - - -About OpenNBT --------------- - -OpenNBT is a library for reading and writing NBT files, with some extra custom tags added to allow the storage of more data types. - - -Building the Source --------------- - -OpenNBT uses Maven to manage dependencies. Simply run 'mvn clean install' in the source's directory. -You can also download a build [here](http://build.spacebase.ch/job/OpenNBT/). - - -License --------------- - +

OpenNBT

+============== + + + +About OpenNBT +-------------- + +OpenNBT is a library for reading and writing NBT files, with some extra custom tags added to allow the storage of more data types. + + +Building the Source +-------------- + +OpenNBT uses Maven to manage dependencies. Simply run 'mvn clean install' in the source's directory. +You can also download a build [here](http://build.spacehq.org/job/OpenNBT/). + + +License +-------------- + OpenNBT is licensed under the [MIT license](http://www.opensource.org/licenses/mit-license.html). \ No newline at end of file diff --git a/pom.xml b/pom.xml index 631e433..f050968 100644 --- a/pom.xml +++ b/pom.xml @@ -1,75 +1,76 @@ - - 4.0.0 - ch.spacebase - opennbt - 1.3-SNAPSHOT - jar + + 4.0.0 + org.spacehq + opennbt + 1.3 + jar - OpenNBT - A library for reading and writing NBT files, written in Java. - http://github.com/Steveice10/OpenNBT/ + OpenNBT + A library for reading and writing NBT files, written in Java. + http://github.com/Steveice10/OpenNBT/ - - - MIT - http://www.opensource.org/licenses/mit-license.html - repo - - + + + MIT + http://www.opensource.org/licenses/mit-license.html + repo + + - - scm:git:git@github.com:Steveice10/OpenNBT.git - scm:git:git@github.com:Steveice10/OpenNBT.git - http://github.com/Steveice10/OpenNBT/ - + + scm:git:git@github.com:Steveice10/OpenNBT.git + scm:git:git@github.com:Steveice10/OpenNBT.git + http://github.com/Steveice10/OpenNBT/ + - - - spacebase - spacebase-releases - http://repo.spacebase.ch/content/repositories/release/ - - - spacebase - spacebase-snapshots - http://repo.spacebase.ch/content/repositories/snapshots/ - - + + + spacehq + spacehq-releases + http://repo.spacehq.org/content/repositories/release/ + + + spacehq + spacehq-snapshots + http://repo.spacehq.org/content/repositories/snapshots/ + + - - - steveice10 - Steveice10 - Steveice10@gmail.com - - + + + steveice10 + Steveice10 + Steveice10@gmail.com + + - - UTF-8 - + + UTF-8 + - - clean install - ${basedir}/src/main/java - - - - . - true - ${basedir}/src/main/resources - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - - org.apache.maven.plugins - maven-jar-plugin - 2.3.2 - - - + + clean install + ${basedir}/src/main/java + + + + . + true + ${basedir}/src/main/resources + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.2 + + + diff --git a/src/main/java/ch/spacebase/opennbt/NBTFileIO.java b/src/main/java/org/spacehq/opennbt/NBTFileIO.java similarity index 72% rename from src/main/java/ch/spacebase/opennbt/NBTFileIO.java rename to src/main/java/org/spacehq/opennbt/NBTFileIO.java index 3c87fd6..662ef20 100644 --- a/src/main/java/ch/spacebase/opennbt/NBTFileIO.java +++ b/src/main/java/org/spacehq/opennbt/NBTFileIO.java @@ -1,127 +1,128 @@ -package ch.spacebase.opennbt; +package org.spacehq.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 org.spacehq.opennbt.tag.CompoundTag; +import org.spacehq.opennbt.tag.Tag; + +import java.io.*; 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. + * @throws java.io.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. + * @throws java.io.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 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. + * @throws java.io.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 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. + * @throws java.io.IOException If an I/O error occurs. */ public static CompoundTag readFile(File file, boolean compressed) throws IOException { 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 tag Tag to write. * @param path Path to write to. - * @throws IOException If an I/O error occurs. + * @throws java.io.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 tag Tag to write. * @param file File to write to. - * @throws IOException If an I/O error occurs. + * @throws java.io.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 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. + * @throws java.io.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 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. + * @throws java.io.IOException If an I/O error occurs. */ public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException { 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/org/spacehq/opennbt/NBTIO.java similarity index 84% rename from src/main/java/ch/spacebase/opennbt/NBTIO.java rename to src/main/java/org/spacehq/opennbt/NBTIO.java index d913212..7e23a75 100644 --- a/src/main/java/ch/spacebase/opennbt/NBTIO.java +++ b/src/main/java/org/spacehq/opennbt/NBTIO.java @@ -1,4 +1,6 @@ -package ch.spacebase.opennbt; +package org.spacehq.opennbt; + +import org.spacehq.opennbt.tag.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -9,20 +11,19 @@ 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. + * @throws java.io.IOException If an I/O error occurs. */ public static List readUntilEndTag(DataInputStream in) throws IOException { List ret = new ArrayList(); @@ -34,22 +35,23 @@ public class NBTIO { } 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. + * @throws java.io.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); @@ -57,28 +59,30 @@ public class NBTIO { 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 out Output stream to write to. * @param tags Tags to write. - * @throws IOException If an I/O error occurs. + * @throws java.io.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. + * @throws java.io.IOException If an I/O error occurs. */ public static void writeTag(DataOutputStream out, Tag tag) throws IOException { byte[] nameBytes = tag.getName().getBytes(CHARSET); @@ -87,5 +91,5 @@ public class NBTIO { out.write(nameBytes); tag.write(out); } - + } diff --git a/src/main/java/ch/spacebase/opennbt/TagRegisterException.java b/src/main/java/org/spacehq/opennbt/TagRegisterException.java similarity index 89% rename from src/main/java/ch/spacebase/opennbt/TagRegisterException.java rename to src/main/java/org/spacehq/opennbt/TagRegisterException.java index 66f0985..5ce38b1 100644 --- a/src/main/java/ch/spacebase/opennbt/TagRegisterException.java +++ b/src/main/java/org/spacehq/opennbt/TagRegisterException.java @@ -1,14 +1,14 @@ -package ch.spacebase.opennbt; +package org.spacehq.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/org/spacehq/opennbt/TagRegistry.java similarity index 51% rename from src/main/java/ch/spacebase/opennbt/TagRegistry.java rename to src/main/java/org/spacehq/opennbt/TagRegistry.java index 74debd4..cdff153 100644 --- a/src/main/java/ch/spacebase/opennbt/TagRegistry.java +++ b/src/main/java/org/spacehq/opennbt/TagRegistry.java @@ -1,74 +1,48 @@ -package ch.spacebase.opennbt; +package org.spacehq.opennbt; + +import org.spacehq.opennbt.tag.*; +import org.spacehq.opennbt.tag.custom.*; 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; - + static { try { - registerDefaultTags(); + 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); } catch(TagRegisterException e) { throw new RuntimeException("Failed to register default tags.", e); } } - - 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. */ @@ -80,18 +54,20 @@ public class TagRegistry { 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. */ @@ -101,13 +77,14 @@ public class TagRegistry { 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 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. */ @@ -115,7 +92,7 @@ public class TagRegistry { if(!tags.containsKey(id)) { return null; } - + Class clazz = tags.get(id); try { return clazz.getDeclaredConstructor(String.class).newInstance(tagName); @@ -124,5 +101,5 @@ public class TagRegistry { return null; } } - + } diff --git a/src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/ByteArrayTag.java similarity index 90% rename from src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/ByteArrayTag.java index 59bb469..970f786 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ByteArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/ByteArrayTag.java @@ -1,97 +1,103 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing a byte array. - */ -public class ByteArrayTag extends Tag { - - private byte[] value; - - /** - * Creates a tag with the specified name. - * @param name The name of the tag. - */ - public ByteArrayTag(String name) { - this(name, new byte[0]); - } - - /** - * 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); - this.value = value; - } - - @Override - public byte[] getValue() { - return this.value.clone(); - } - - /** - * Sets the value of this tag. - * @param value New value of this tag. - */ - public void setValue(byte[] 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 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 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() { - return new ByteArrayTag(this.getName(), this.getValue()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a byte array. + */ +public class ByteArrayTag extends Tag { + + private byte[] value; + + /** + * Creates a tag with the specified name. + * + * @param name The name of the tag. + */ + public ByteArrayTag(String name) { + this(name, new byte[0]); + } + + /** + * 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); + this.value = value; + } + + @Override + public byte[] getValue() { + return this.value.clone(); + } + + /** + * Sets the value of this tag. + * + * @param value New value of this tag. + */ + public void setValue(byte[] 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 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 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() { + return new ByteArrayTag(this.getName(), this.getValue()); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/ByteTag.java b/src/main/java/org/spacehq/opennbt/tag/ByteTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/ByteTag.java rename to src/main/java/org/spacehq/opennbt/tag/ByteTag.java index 1af94b7..0862736 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ByteTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/ByteTag.java @@ -1,65 +1,68 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing a byte. - */ -public class ByteTag extends Tag { - - private byte 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); - this.value = value; - } - - @Override - public Byte getValue() { - 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 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()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a byte. + */ +public class ByteTag extends Tag { + + private byte 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); + this.value = value; + } + + @Override + public Byte getValue() { + 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 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/org/spacehq/opennbt/tag/CompoundTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java rename to src/main/java/org/spacehq/opennbt/tag/CompoundTag.java index 5d95249..c046d65 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/CompoundTag.java @@ -1,132 +1,136 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.Collection; -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.NBTIO; - -/** - * A compound tag containing other tags. - */ -public class CompoundTag extends Tag { - - private Map value; - - /** - * Creates a tag with the specified name. - * @param name The name of the tag. - */ - public CompoundTag(String name) { - this(name, new LinkedHashMap()); - } - - /** - * 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 = new LinkedHashMap(value); - } - - @Override - public Map getValue() { - 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); - } - - /** - * 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 int getId() { - return 10; - } - - @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); - } - -} +package org.spacehq.opennbt.tag; + +import org.spacehq.opennbt.NBTIO; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.*; +import java.util.Map.Entry; + +/** + * A compound tag containing other tags. + */ +public class CompoundTag extends Tag { + + private Map value; + + /** + * Creates a tag with the specified name. + * + * @param name The name of the tag. + */ + public CompoundTag(String name) { + this(name, new LinkedHashMap()); + } + + /** + * 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 = new LinkedHashMap(value); + } + + @Override + public Map getValue() { + 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); + } + + /** + * 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 int getId() { + return 10; + } + + @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/org/spacehq/opennbt/tag/DoubleTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/DoubleTag.java rename to src/main/java/org/spacehq/opennbt/tag/DoubleTag.java index c379c70..6279cbd 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/DoubleTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/DoubleTag.java @@ -1,65 +1,68 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing a double. - */ -public class DoubleTag extends Tag { - - private double value; - - /** - * Creates a tag with the specified name. - * @param name The name of the tag. - */ - public DoubleTag(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 DoubleTag(String name, double value) { - super(name); - this.value = value; - } - - @Override - public Double getValue() { - return this.value; - } - - @Override - 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()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a double. + */ +public class DoubleTag extends Tag { + + private double value; + + /** + * Creates a tag with the specified name. + * + * @param name The name of the tag. + */ + public DoubleTag(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 DoubleTag(String name, double value) { + super(name); + this.value = value; + } + + @Override + public Double getValue() { + return this.value; + } + + @Override + 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/FloatTag.java b/src/main/java/org/spacehq/opennbt/tag/FloatTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/FloatTag.java rename to src/main/java/org/spacehq/opennbt/tag/FloatTag.java index 98c61c8..6c276d3 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/FloatTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/FloatTag.java @@ -1,65 +1,68 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing a float. - */ -public class FloatTag extends Tag { - - private float 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 this.value; - } - - @Override - 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()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a float. + */ +public class FloatTag extends Tag { + + private float 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 this.value; + } + + @Override + 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/org/spacehq/opennbt/tag/IntArrayTag.java similarity index 89% rename from src/main/java/ch/spacebase/opennbt/tag/IntArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/IntArrayTag.java index 4f7d6df..36f735d 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/IntArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/IntArrayTag.java @@ -1,4 +1,4 @@ -package ch.spacebase.opennbt.tag; +package org.spacehq.opennbt.tag; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -10,41 +10,43 @@ import java.io.IOException; public class IntArrayTag extends Tag { private int[] value; - + /** * Creates a tag with the specified name. + * * @param name The name of the tag. */ public IntArrayTag(String name) { this(name, new int[0]); } - + /** * Creates a tag with the specified name. - * @param name The name of the tag. + * + * @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 this.value.clone(); } - + @Override 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(); - } + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readInt(); + } } @Override @@ -54,45 +56,49 @@ public class IntArrayTag extends Tag { 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() { return new IntArrayTag(this.getName(), this.getValue()); diff --git a/src/main/java/ch/spacebase/opennbt/tag/IntTag.java b/src/main/java/org/spacehq/opennbt/tag/IntTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/IntTag.java rename to src/main/java/org/spacehq/opennbt/tag/IntTag.java index 995c95b..3fbfde5 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/IntTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/IntTag.java @@ -1,65 +1,68 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing an integer. - */ -public class IntTag extends Tag { - - private int 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); - this.value = value; - } - - @Override - public Integer getValue() { - 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 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()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing an integer. + */ +public class IntTag extends Tag { + + private int 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); + this.value = value; + } + + @Override + public Integer getValue() { + 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 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/org/spacehq/opennbt/tag/ListTag.java similarity index 90% rename from src/main/java/ch/spacebase/opennbt/tag/ListTag.java rename to src/main/java/org/spacehq/opennbt/tag/ListTag.java index 03f217a..af3b4d8 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ListTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/ListTag.java @@ -1,146 +1,153 @@ -package ch.spacebase.opennbt.tag; - -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.TagRegistry; - -/** - * A tag containing a list of tags. - */ -public class ListTag extends Tag implements Iterable { - - private Class type; - private List value; - - /** - * Creates a tag with the specified name. - * @param name The name of the tag. - */ - public ListTag(String name) { - super(name); - this.value = new ArrayList(); - } - - /** - * 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 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); - this.type = type; - this.value = value; - } - - @Override - public List getValue() { - return new ArrayList(this.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); - } - - /** - * 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); - } - - /** - * 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 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); - } - - 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); - } - } - - @SuppressWarnings("unchecked") - @Override - public ListTag clone() { - List newList = new ArrayList(); - for(T value : this.value) { - newList.add((T) value.clone()); - } - - return new ListTag(this.getName(), this.type, newList); - } - -} +package org.spacehq.opennbt.tag; + +import org.spacehq.opennbt.TagRegistry; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * A tag containing a list of tags. + */ +public class ListTag extends Tag implements Iterable { + + private Class type; + private List value; + + /** + * Creates a tag with the specified name. + * + * @param name The name of the tag. + */ + public ListTag(String name) { + super(name); + this.value = new ArrayList(); + } + + /** + * 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 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); + this.type = type; + this.value = value; + } + + @Override + public List getValue() { + return new ArrayList(this.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); + } + + /** + * 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); + } + + /** + * 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 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); + } + + 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); + } + } + + @SuppressWarnings("unchecked") + @Override + public ListTag clone() { + List newList = new ArrayList(); + for(T value : this.value) { + newList.add((T) value.clone()); + } + + return new ListTag(this.getName(), this.type, newList); + } + +} diff --git a/src/main/java/ch/spacebase/opennbt/tag/LongTag.java b/src/main/java/org/spacehq/opennbt/tag/LongTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/LongTag.java rename to src/main/java/org/spacehq/opennbt/tag/LongTag.java index 331c828..a9c2569 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/LongTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/LongTag.java @@ -1,65 +1,68 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing a long. - */ -public class LongTag extends Tag { - - private long 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 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 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()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a long. + */ +public class LongTag extends Tag { + + private long 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 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 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/org/spacehq/opennbt/tag/ShortTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/ShortTag.java rename to src/main/java/org/spacehq/opennbt/tag/ShortTag.java index e220491..432d713 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ShortTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/ShortTag.java @@ -1,65 +1,68 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -/** - * A tag containing a short. - */ -public class ShortTag extends Tag { - - private short 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 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 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()); - } - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a short. + */ +public class ShortTag extends Tag { + + private short 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 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 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/org/spacehq/opennbt/tag/StringTag.java similarity index 82% rename from src/main/java/ch/spacebase/opennbt/tag/StringTag.java rename to src/main/java/org/spacehq/opennbt/tag/StringTag.java index 666df1a..2d533fa 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/StringTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/StringTag.java @@ -1,71 +1,74 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import ch.spacebase.opennbt.NBTIO; - -/** - * A tag containing a string. - */ -public class StringTag extends Tag { - - private String 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); - this.value = value; - } - - @Override - public String getValue() { - 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 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()); - } - -} +package org.spacehq.opennbt.tag; + +import org.spacehq.opennbt.NBTIO; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A tag containing a string. + */ +public class StringTag extends Tag { + + private String 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); + this.value = value; + } + + @Override + public String getValue() { + 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 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/org/spacehq/opennbt/tag/Tag.java similarity index 90% rename from src/main/java/ch/spacebase/opennbt/tag/Tag.java rename to src/main/java/org/spacehq/opennbt/tag/Tag.java index 005cf12..ab0d262 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/Tag.java +++ b/src/main/java/org/spacehq/opennbt/tag/Tag.java @@ -1,125 +1,131 @@ -package ch.spacebase.opennbt.tag; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.lang.reflect.Array; - -/** - * 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; - - /** - * Creates a tag with the specified name. - * @param name The name. - */ - public Tag(String name) { - this.name = name; - } - - /** - * Gets the name of this tag. - * @return The name of this tag. - */ - public final String getName() { - return this.name; - } - - /** - * Gets the value of this tag. - * @return The value of this tag. - */ - 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; - } - - Tag tag = (Tag) obj; - if(!this.getName().equals(tag.getName())) { - return false; - } - - 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(); - -} +package org.spacehq.opennbt.tag; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.lang.reflect.Array; + +/** + * 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; + + /** + * Creates a tag with the specified name. + * + * @param name The name. + */ + public Tag(String name) { + this.name = name; + } + + /** + * Gets the name of this tag. + * + * @return The name of this tag. + */ + public final String getName() { + return this.name; + } + + /** + * Gets the value of this tag. + * + * @return The value of this tag. + */ + 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 java.io.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 java.io.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; + } + + Tag tag = (Tag) obj; + if(!this.getName().equals(tag.getName())) { + return false; + } + + 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/org/spacehq/opennbt/tag/custom/DoubleArrayTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/custom/DoubleArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/DoubleArrayTag.java index f745dac..a073689 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/DoubleArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/DoubleArrayTag.java @@ -1,79 +1,85 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.opennbt.tag.custom; + +import org.spacehq.opennbt.tag.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import ch.spacebase.opennbt.tag.Tag; - /** * A tag containing a double array. */ public class DoubleArrayTag extends Tag { private double[] value; - + /** * Creates a tag with the specified name. + * * @param name The name of the tag. */ public DoubleArrayTag(String name) { this(name, new double[0]); } - + /** * Creates a tag with the specified name. - * @param name The name of the tag. + * + * @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 this.value.clone(); } - + /** * Sets the value of this tag. + * * @param value New value of this tag. */ public void setValue(double[] 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 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 int getId() { return 60; @@ -82,9 +88,9 @@ public class DoubleArrayTag extends Tag { @Override 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(); - } + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readDouble(); + } } @Override @@ -94,7 +100,7 @@ public class DoubleArrayTag extends Tag { 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/org/spacehq/opennbt/tag/custom/FloatArrayTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/custom/FloatArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/FloatArrayTag.java index e4a3b84..c785398 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/FloatArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/FloatArrayTag.java @@ -1,79 +1,85 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.opennbt.tag.custom; + +import org.spacehq.opennbt.tag.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import ch.spacebase.opennbt.tag.Tag; - /** * A tag containing a float array. */ public class FloatArrayTag extends Tag { private float[] value; - + /** * Creates a tag with the specified name. + * * @param name The name of the tag. */ public FloatArrayTag(String name) { this(name, new float[0]); } - + /** * Creates a tag with the specified name. - * @param name The name of the tag. + * + * @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 this.value.clone(); } - + /** * Sets the value of this tag. + * * @param value New value of this tag. */ public void setValue(float[] 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 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 int getId() { return 61; @@ -82,9 +88,9 @@ public class FloatArrayTag extends Tag { @Override 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(); - } + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readFloat(); + } } @Override @@ -94,7 +100,7 @@ public class FloatArrayTag extends Tag { 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/org/spacehq/opennbt/tag/custom/LongArrayTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/custom/LongArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/LongArrayTag.java index b210ff6..98913bb 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/LongArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/LongArrayTag.java @@ -1,79 +1,85 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.opennbt.tag.custom; + +import org.spacehq.opennbt.tag.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import ch.spacebase.opennbt.tag.Tag; - /** * A tag containing a long array. */ public class LongArrayTag extends Tag { private long[] value; - + /** * Creates a tag with the specified name. + * * @param name The name of the tag. */ public LongArrayTag(String name) { this(name, new long[0]); } - + /** * Creates a tag with the specified name. - * @param name The name of the tag. + * + * @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 this.value.clone(); } - + /** * Sets the value of this tag. + * * @param value New value of this tag. */ public void setValue(long[] 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 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 int getId() { return 62; @@ -82,9 +88,9 @@ public class LongArrayTag extends Tag { @Override 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(); - } + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readLong(); + } } @Override @@ -94,7 +100,7 @@ public class LongArrayTag extends Tag { 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/SerializableArrayTag.java b/src/main/java/org/spacehq/opennbt/tag/custom/SerializableArrayTag.java similarity index 69% rename from src/main/java/ch/spacebase/opennbt/tag/custom/SerializableArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/SerializableArrayTag.java index d7bf658..7c83388 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/SerializableArrayTag.java @@ -1,13 +1,8 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.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 org.spacehq.opennbt.tag.Tag; -import ch.spacebase.opennbt.tag.Tag; +import java.io.*; /** * A tag containing an array of serializable objects. @@ -15,95 +10,101 @@ import ch.spacebase.opennbt.tag.Tag; 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 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 = 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) { + } 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]); - } + 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/org/spacehq/opennbt/tag/custom/SerializableTag.java similarity index 68% rename from src/main/java/ch/spacebase/opennbt/tag/custom/SerializableTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/SerializableTag.java index 74f5099..da7c58b 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/SerializableTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/SerializableTag.java @@ -1,13 +1,8 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.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 org.spacehq.opennbt.tag.Tag; -import ch.spacebase.opennbt.tag.Tag; +import java.io.*; /** * A tag containing a serializable object. @@ -15,18 +10,20 @@ import ch.spacebase.opennbt.tag.Tag; 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 name The name of the tag. * @param value The value of the tag. */ public SerializableTag(String name, Serializable value) { @@ -38,39 +35,40 @@ public class SerializableTag extends Tag { 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 { + ObjectInputStream str = new ObjectInputStream(in); + try { this.value = (Serializable) str.readObject(); - } catch (ClassNotFoundException e) { + } 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); + 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/org/spacehq/opennbt/tag/custom/ShortArrayTag.java similarity index 87% rename from src/main/java/ch/spacebase/opennbt/tag/custom/ShortArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/ShortArrayTag.java index b8d7f25..e796334 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/ShortArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/ShortArrayTag.java @@ -1,79 +1,85 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.opennbt.tag.custom; + +import org.spacehq.opennbt.tag.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import ch.spacebase.opennbt.tag.Tag; - /** * A tag containing a short array. */ public class ShortArrayTag extends Tag { private short[] value; - + /** * Creates a tag with the specified name. + * * @param name The name of the tag. */ public ShortArrayTag(String name) { this(name, new short[0]); } - + /** * Creates a tag with the specified name. - * @param name The name of the tag. + * + * @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 this.value.clone(); } - + /** * Sets the value of this tag. + * * @param value New value of this tag. */ public void setValue(short[] 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 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 int getId() { return 65; @@ -82,9 +88,9 @@ public class ShortArrayTag extends Tag { @Override 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(); - } + for(int index = 0; index < this.value.length; index++) { + this.value[index] = in.readShort(); + } } @Override @@ -94,7 +100,7 @@ public class ShortArrayTag extends Tag { 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/org/spacehq/opennbt/tag/custom/StringArrayTag.java similarity index 81% rename from src/main/java/ch/spacebase/opennbt/tag/custom/StringArrayTag.java rename to src/main/java/org/spacehq/opennbt/tag/custom/StringArrayTag.java index e18acad..8635613 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/custom/StringArrayTag.java +++ b/src/main/java/org/spacehq/opennbt/tag/custom/StringArrayTag.java @@ -1,85 +1,91 @@ -package ch.spacebase.opennbt.tag.custom; +package org.spacehq.opennbt.tag.custom; + +import org.spacehq.opennbt.NBTIO; +import org.spacehq.opennbt.tag.Tag; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import ch.spacebase.opennbt.NBTIO; -import ch.spacebase.opennbt.tag.Tag; - /** * A tag containing a string array. */ public class StringArrayTag extends Tag { - + private String[] value; - + /** * Creates a tag with the specified name. + * * @param name The name of the tag. */ public StringArrayTag(String name) { this(name, new String[0]); } - + /** * Creates a tag with the specified name. - * @param name The name of the tag. + * + * @param name The name of the tag. * @param value The value of the tag. */ public StringArrayTag(String name, String[] value) { super(name); this.value = value; } - + @Override public String[] getValue() { return this.value.clone(); } - + /** * Sets the value of this tag. + * * @param value New value of this tag. */ public void setValue(String[] value) { if(value == null) { return; } - + this.value = value.clone(); } - + /** * Gets a value in this tag's array. + * * @param index Index of the value. * @return The value at the given index. */ public String getValue(int index) { return this.value[index]; } - + /** * Sets a value in this tag's array. + * * @param index Index of the value. * @param value Value to set. */ public void setValue(int index, String value) { this.value[index] = value; } - + /** * Gets the length of this tag's array. + * * @return This tag's array length. */ public int length() { return this.value.length; } - + @Override public int getId() { return 66; } - + @Override public void read(DataInputStream in) throws IOException { this.value = new String[in.readInt()]; @@ -92,14 +98,14 @@ public class StringArrayTag extends Tag { @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); - } + 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() { return new StringArrayTag(this.getName(), this.getValue());