Refactored ch.spacebase to org.spacehq, reformatted code and line endings.

This commit is contained in:
Steveice10 2014-03-01 16:46:32 -08:00
parent d7f2398e00
commit 7f7acc4193
27 changed files with 1404 additions and 1336 deletions

10
.gitignore vendored
View File

@ -1,6 +1,12 @@
bin bin
.git lib
.settings
.classpath .classpath
.project .project
.settings
*.iml
.idea
target target

View File

@ -1,7 +1,7 @@
Copyright (C) 2013-2014 Steveice10 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: 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 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. 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.

View File

@ -1,22 +1,22 @@
<b><center><h1>OpenNBT</h></center></b> <b><center><h1>OpenNBT</h></center></b>
============== ==============
<b>About OpenNBT</b> <b>About OpenNBT</b>
-------------- --------------
OpenNBT is a library for reading and writing NBT files, with some extra custom tags added to allow the storage of more data types. OpenNBT is a library for reading and writing NBT files, with some extra custom tags added to allow the storage of more data types.
<b>Building the Source</b> <b>Building the Source</b>
-------------- --------------
OpenNBT uses Maven to manage dependencies. Simply run 'mvn clean install' in the source's directory. OpenNBT uses Maven to manage dependencies. Simply run 'mvn clean install' in the source's directory.
You can also download a build <b>[here](http://build.spacebase.ch/job/OpenNBT/)</b>. You can also download a build <b>[here](http://build.spacehq.org/job/OpenNBT/)</b>.
<b>License</b> <b>License</b>
-------------- --------------
OpenNBT is licensed under the <b>[MIT license](http://www.opensource.org/licenses/mit-license.html)</b>. OpenNBT is licensed under the <b>[MIT license](http://www.opensource.org/licenses/mit-license.html)</b>.

135
pom.xml
View File

@ -1,75 +1,76 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>ch.spacebase</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>opennbt</artifactId> <groupId>org.spacehq</groupId>
<version>1.3-SNAPSHOT</version> <artifactId>opennbt</artifactId>
<packaging>jar</packaging> <version>1.3</version>
<packaging>jar</packaging>
<name>OpenNBT</name> <name>OpenNBT</name>
<description>A library for reading and writing NBT files, written in Java.</description> <description>A library for reading and writing NBT files, written in Java.</description>
<url>http://github.com/Steveice10/OpenNBT/</url> <url>http://github.com/Steveice10/OpenNBT/</url>
<licenses> <licenses>
<license> <license>
<name>MIT</name> <name>MIT</name>
<url>http://www.opensource.org/licenses/mit-license.html</url> <url>http://www.opensource.org/licenses/mit-license.html</url>
<distribution>repo</distribution> <distribution>repo</distribution>
</license> </license>
</licenses> </licenses>
<scm> <scm>
<connection>scm:git:git@github.com:Steveice10/OpenNBT.git</connection> <connection>scm:git:git@github.com:Steveice10/OpenNBT.git</connection>
<developerConnection>scm:git:git@github.com:Steveice10/OpenNBT.git</developerConnection> <developerConnection>scm:git:git@github.com:Steveice10/OpenNBT.git</developerConnection>
<url>http://github.com/Steveice10/OpenNBT/</url> <url>http://github.com/Steveice10/OpenNBT/</url>
</scm> </scm>
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>spacebase</id> <id>spacehq</id>
<name>spacebase-releases</name> <name>spacehq-releases</name>
<url>http://repo.spacebase.ch/content/repositories/release/</url> <url>http://repo.spacehq.org/content/repositories/release/</url>
</repository> </repository>
<snapshotRepository> <snapshotRepository>
<id>spacebase</id> <id>spacehq</id>
<name>spacebase-snapshots</name> <name>spacehq-snapshots</name>
<url>http://repo.spacebase.ch/content/repositories/snapshots/</url> <url>http://repo.spacehq.org/content/repositories/snapshots/</url>
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
<developers> <developers>
<developer> <developer>
<id>steveice10</id> <id>steveice10</id>
<name>Steveice10</name> <name>Steveice10</name>
<email>Steveice10@gmail.com</email> <email>Steveice10@gmail.com</email>
</developer> </developer>
</developers> </developers>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<build> <build>
<defaultGoal>clean install</defaultGoal> <defaultGoal>clean install</defaultGoal>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory> <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<!-- Resources --> <!-- Resources -->
<resources> <resources>
<resource> <resource>
<targetPath>.</targetPath> <targetPath>.</targetPath>
<filtering>true</filtering> <filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory> <directory>${basedir}/src/main/resources</directory>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -1,127 +1,128 @@
package ch.spacebase.opennbt; package org.spacehq.opennbt;
import java.io.DataInputStream; import org.spacehq.opennbt.tag.CompoundTag;
import java.io.DataOutputStream; import org.spacehq.opennbt.tag.Tag;
import java.io.File;
import java.io.FileInputStream; import java.io.*;
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.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import ch.spacebase.opennbt.tag.CompoundTag;
import ch.spacebase.opennbt.tag.Tag;
public class NBTFileIO { public class NBTFileIO {
/** /**
* Reads the root CompoundTag from the given file. * Reads the root CompoundTag from the given file.
*
* @param path Path of the file. * @param path Path of the file.
* @return The read compound tag. * @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 { public static CompoundTag readFile(String path) throws IOException {
return readFile(new File(path)); return readFile(new File(path));
} }
/** /**
* Reads the root CompoundTag from the given file. * Reads the root CompoundTag from the given file.
*
* @param file File to read from. * @param file File to read from.
* @return The read compound tag. * @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 { public static CompoundTag readFile(File file) throws IOException {
return readFile(file, true); return readFile(file, true);
} }
/** /**
* Reads the root CompoundTag from the given file. * 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. * @param compressed Whether the NBT file is compressed.
* @return The read compound tag. * @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 { public static CompoundTag readFile(String path, boolean compressed) throws IOException {
return readFile(new File(path), compressed); return readFile(new File(path), compressed);
} }
/** /**
* Reads the root CompoundTag from the given file. * 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. * @param compressed Whether the NBT file is compressed.
* @return The read compound tag. * @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 { public static CompoundTag readFile(File file, boolean compressed) throws IOException {
InputStream in = new FileInputStream(file); InputStream in = new FileInputStream(file);
if(compressed) { if(compressed) {
in = new GZIPInputStream(in); in = new GZIPInputStream(in);
} }
Tag tag = NBTIO.readTag(new DataInputStream(in)); Tag tag = NBTIO.readTag(new DataInputStream(in));
if(!(tag instanceof CompoundTag)) { if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a CompoundTag!"); throw new IOException("Root tag is not a CompoundTag!");
} }
return (CompoundTag) tag; return (CompoundTag) tag;
} }
/** /**
* Writes the given root CompoundTag to the given file. * 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. * @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 { public static void writeFile(CompoundTag tag, String path) throws IOException {
writeFile(tag, new File(path)); writeFile(tag, new File(path));
} }
/** /**
* Writes the given root CompoundTag to the given file. * 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. * @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 { public static void writeFile(CompoundTag tag, File file) throws IOException {
writeFile(tag, file, true); writeFile(tag, file, true);
} }
/** /**
* Writes the given root CompoundTag to the given file. * 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. * @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 { public static void writeFile(CompoundTag tag, String path, boolean compressed) throws IOException {
writeFile(tag, new File(path), compressed); writeFile(tag, new File(path), compressed);
} }
/** /**
* Writes the given root CompoundTag to the given file. * 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. * @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 { public static void writeFile(CompoundTag tag, File file, boolean compressed) throws IOException {
if(!file.exists()) { if(!file.exists()) {
if(file.getParentFile() != null && !file.getParentFile().exists()) { if(file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
file.createNewFile(); file.createNewFile();
} }
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
if(compressed) { if(compressed) {
out = new GZIPOutputStream(out); out = new GZIPOutputStream(out);
} }
NBTIO.writeTag(new DataOutputStream(out), tag); NBTIO.writeTag(new DataOutputStream(out), tag);
out.close(); out.close();
} }
} }

View File

@ -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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -9,20 +11,19 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import ch.spacebase.opennbt.tag.Tag;
/** /**
* A class containing methods for reading/writing NBT tags. * A class containing methods for reading/writing NBT tags.
*/ */
public class NBTIO { public class NBTIO {
public static final Charset CHARSET = Charset.forName("UTF-8"); public static final Charset CHARSET = Charset.forName("UTF-8");
/** /**
* Reads NBT tags until an end tag is reached. * Reads NBT tags until an end tag is reached.
*
* @param in Input stream to read from. * @param in Input stream to read from.
* @return The read tags. * @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<Tag> readUntilEndTag(DataInputStream in) throws IOException { public static List<Tag> readUntilEndTag(DataInputStream in) throws IOException {
List<Tag> ret = new ArrayList<Tag>(); List<Tag> ret = new ArrayList<Tag>();
@ -34,22 +35,23 @@ public class NBTIO {
} catch(EOFException e) { } catch(EOFException e) {
throw new IOException("Closing EndTag was not found!"); throw new IOException("Closing EndTag was not found!");
} }
return ret; return ret;
} }
/** /**
* Reads an NBT tag. * Reads an NBT tag.
*
* @param in Input stream to read from. * @param in Input stream to read from.
* @return The read tag, or null if the tag is an end tag. * @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 { public static Tag readTag(DataInputStream in) throws IOException {
int id = in.readByte() & 0xFF; int id = in.readByte() & 0xFF;
if(id == 0) { if(id == 0) {
return null; return null;
} }
byte[] nameBytes = new byte[in.readShort() & 0xFFFF]; byte[] nameBytes = new byte[in.readShort() & 0xFFFF];
in.readFully(nameBytes); in.readFully(nameBytes);
String name = new String(nameBytes, CHARSET); String name = new String(nameBytes, CHARSET);
@ -57,28 +59,30 @@ public class NBTIO {
if(tag == null) { if(tag == null) {
throw new IOException("Invalid tag: " + id); throw new IOException("Invalid tag: " + id);
} }
tag.read(in); tag.read(in);
return tag; return tag;
} }
/** /**
* Writes a collection of tags to an output stream. * 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. * @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<Tag> tags) throws IOException { public static void writeTags(DataOutputStream out, Collection<Tag> tags) throws IOException {
for(Tag tag : tags) { for(Tag tag : tags) {
writeTag(out, tag); writeTag(out, tag);
} }
} }
/** /**
* Writes a tag to an output stream. * Writes a tag to an output stream.
*
* @param out Output stream to write to. * @param out Output stream to write to.
* @param tag Tag to write. * @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 { public static void writeTag(DataOutputStream out, Tag tag) throws IOException {
byte[] nameBytes = tag.getName().getBytes(CHARSET); byte[] nameBytes = tag.getName().getBytes(CHARSET);
@ -87,5 +91,5 @@ public class NBTIO {
out.write(nameBytes); out.write(nameBytes);
tag.write(out); tag.write(out);
} }
} }

View File

@ -1,14 +1,14 @@
package ch.spacebase.opennbt; package org.spacehq.opennbt;
/** /**
* An exception thrown when an error occurs while registering a tag class. * An exception thrown when an error occurs while registering a tag class.
*/ */
public class TagRegisterException extends Exception { public class TagRegisterException extends Exception {
private static final long serialVersionUID = -2022049594558041160L; private static final long serialVersionUID = -2022049594558041160L;
public TagRegisterException(Throwable cause) { public TagRegisterException(Throwable cause) {
super("Failed to register tag.", cause); super("Failed to register tag.", cause);
} }
} }

View File

@ -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.HashMap;
import java.util.Map; 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. * A registry containing different tag classes.
*/ */
public class TagRegistry { public class TagRegistry {
private static final Map<Integer, Class<? extends Tag>> tags = new HashMap<Integer, Class<? extends Tag>>(); private static final Map<Integer, Class<? extends Tag>> tags = new HashMap<Integer, Class<? extends Tag>>();
private static boolean registered = false;
static { static {
try { 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) { } catch(TagRegisterException e) {
throw new RuntimeException("Failed to register default tags.", 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. * Registers a tag class.
*
* @param tag Tag class to register. * @param tag Tag class to register.
* @throws TagRegisterException If an error occurs while registering the tag. * @throws TagRegisterException If an error occurs while registering the tag.
*/ */
@ -80,18 +54,20 @@ public class TagRegistry {
throw new TagRegisterException(e); throw new TagRegisterException(e);
} }
} }
/** /**
* Gets the tag class with the given id. * Gets the tag class with the given id.
*
* @param id Id of the tag. * @param id Id of the tag.
* @return The tag class with the given id. * @return The tag class with the given id.
*/ */
public static Class<? extends Tag> getClassFor(int id) { public static Class<? extends Tag> getClassFor(int id) {
return tags.get(id); return tags.get(id);
} }
/** /**
* Gets the id of the given tag class. * Gets the id of the given tag class.
*
* @param clazz The tag class to get the id for. * @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. * @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 id;
} }
} }
return -1; return -1;
} }
/** /**
* Creates an instance of the tag with the given id, using the String constructor. * 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. * @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. * @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)) { if(!tags.containsKey(id)) {
return null; return null;
} }
Class<? extends Tag> clazz = tags.get(id); Class<? extends Tag> clazz = tags.get(id);
try { try {
return clazz.getDeclaredConstructor(String.class).newInstance(tagName); return clazz.getDeclaredConstructor(String.class).newInstance(tagName);
@ -124,5 +101,5 @@ public class TagRegistry {
return null; return null;
} }
} }
} }

View File

@ -1,97 +1,103 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing a byte array. * A tag containing a byte array.
*/ */
public class ByteArrayTag extends Tag { public class ByteArrayTag extends Tag {
private byte[] value; private byte[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public ByteArrayTag(String name) { */
this(name, new byte[0]); public ByteArrayTag(String name) {
} this(name, new byte[0]);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public ByteArrayTag(String name, byte[] value) { * @param value The value of the tag.
super(name); */
this.value = value; public ByteArrayTag(String name, byte[] value) {
} super(name);
this.value = value;
@Override }
public byte[] getValue() {
return this.value.clone(); @Override
} public byte[] getValue() {
return this.value.clone();
/** }
* Sets the value of this tag.
* @param value New value of this tag. /**
*/ * Sets the value of this tag.
public void setValue(byte[] value) { *
if(value == null) { * @param value New value of this tag.
return; */
} public void setValue(byte[] value) {
if(value == null) {
this.value = value.clone(); 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. /**
*/ * Gets a value in this tag's array.
public byte getValue(int index) { *
return this.value[index]; * @param index Index of the value.
} * @return The value at the given index.
*/
/** public byte getValue(int index) {
* Sets a value in this tag's array. return this.value[index];
* @param index Index of the value. }
* @param value Value to set.
*/ /**
public void setValue(int index, byte value) { * Sets a value in this tag's array.
this.value[index] = value; *
} * @param index Index of the value.
* @param value Value to set.
/** */
* Gets the length of this tag's array. public void setValue(int index, byte value) {
* @return This tag's array length. this.value[index] = value;
*/ }
public int length() {
return this.value.length; /**
} * Gets the length of this tag's array.
*
@Override * @return This tag's array length.
public int getId() { */
return 7; public int length() {
} return this.value.length;
}
@Override
public void read(DataInputStream in) throws IOException { @Override
this.value = new byte[in.readInt()]; public int getId() {
in.readFully(this.value); return 7;
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void read(DataInputStream in) throws IOException {
out.writeInt(this.value.length); this.value = new byte[in.readInt()];
out.write(this.value); in.readFully(this.value);
} }
@Override @Override
public ByteArrayTag clone() { public void write(DataOutputStream out) throws IOException {
return new ByteArrayTag(this.getName(), this.getValue()); out.writeInt(this.value.length);
} out.write(this.value);
}
}
@Override
public ByteArrayTag clone() {
return new ByteArrayTag(this.getName(), this.getValue());
}
}

View File

@ -1,65 +1,68 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing a byte. * A tag containing a byte.
*/ */
public class ByteTag extends Tag { public class ByteTag extends Tag {
private byte value; private byte value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public ByteTag(String name) { */
this(name, (byte) 0); public ByteTag(String name) {
} this(name, (byte) 0);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public ByteTag(String name, byte value) { * @param value The value of the tag.
super(name); */
this.value = value; public ByteTag(String name, byte value) {
} super(name);
this.value = value;
@Override }
public Byte getValue() {
return this.value; @Override
} public Byte getValue() {
return this.value;
/** }
* Sets the value of this tag.
* @param value New value of this tag. /**
*/ * Sets the value of this tag.
public void setValue(byte value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(byte value) {
@Override this.value = value;
public int getId() { }
return 1;
} @Override
public int getId() {
@Override return 1;
public void read(DataInputStream in) throws IOException { }
this.value = in.readByte();
} @Override
public void read(DataInputStream in) throws IOException {
@Override this.value = in.readByte();
public void write(DataOutputStream out) throws IOException { }
out.writeByte(this.value);
} @Override
public void write(DataOutputStream out) throws IOException {
@Override out.writeByte(this.value);
public ByteTag clone() { }
return new ByteTag(this.getName(), this.getValue());
} @Override
public ByteTag clone() {
} return new ByteTag(this.getName(), this.getValue());
}
}

View File

@ -1,132 +1,136 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import org.spacehq.opennbt.NBTIO;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.DataInputStream;
import java.util.Collection; import java.io.DataOutputStream;
import java.util.LinkedHashMap; import java.io.IOException;
import java.util.List; import java.util.*;
import java.util.Map; import java.util.Map.Entry;
import java.util.Set;
import java.util.Map.Entry; /**
* A compound tag containing other tags.
import ch.spacebase.opennbt.NBTIO; */
public class CompoundTag extends Tag {
/**
* A compound tag containing other tags. private Map<String, Tag> value;
*/
public class CompoundTag extends Tag { /**
* Creates a tag with the specified name.
private Map<String, Tag> value; *
* @param name The name of the tag.
/** */
* Creates a tag with the specified name. public CompoundTag(String name) {
* @param name The name of the tag. this(name, new LinkedHashMap<String, Tag>());
*/ }
public CompoundTag(String name) {
this(name, new LinkedHashMap<String, Tag>()); /**
} * Creates a tag with the specified name.
*
/** * @param name The name of the tag.
* Creates a tag with the specified name. * @param value The value of the tag.
* @param name The name of the tag. */
* @param value The value of the tag. public CompoundTag(String name, Map<String, Tag> value) {
*/ super(name);
public CompoundTag(String name, Map<String, Tag> value) { this.value = new LinkedHashMap<String, Tag>(value);
super(name); }
this.value = new LinkedHashMap<String, Tag>(value);
} @Override
public Map<String, Tag> getValue() {
@Override return new LinkedHashMap<String, Tag>(this.value);
public Map<String, Tag> getValue() { }
return new LinkedHashMap<String, Tag>(this.value);
} /**
* Gets the tag with the specified name.
/** *
* Gets the tag with the specified name. * @param tagName Name of the tag.
* @param tagName Name of the tag. * @return The tag with the specified name.
* @return The tag with the specified name. */
*/ public Tag get(String tagName) {
public Tag get(String tagName) { return this.value.get(tagName);
return this.value.get(tagName); }
}
/**
/** * Puts the tag into this compound tag.
* Puts the tag into this compound tag. *
* @param tag Tag to put 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. * @return The previous tag associated with its name, or null if there wasn't one.
*/ */
public Tag put(Tag tag) { public Tag put(Tag tag) {
return this.value.put(tag.getName(), tag); return this.value.put(tag.getName(), tag);
} }
/** /**
* Removes a tag from this compound tag. * Removes a tag from this compound tag.
* @param tagName Name of the tag to remove. *
* @return The removed tag. * @param tagName Name of the tag to remove.
*/ * @return The removed tag.
public Tag remove(String tagName) { */
return this.value.remove(tagName); 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. * Gets a set of keys in this compound tag.
*/ *
public Set<String> keySet() { * @return The compound tag's key set.
return this.value.keySet(); */
} public Set<String> keySet() {
return this.value.keySet();
/** }
* Gets a collection of tags in this compound tag.
* @return This compound tag's tags. /**
*/ * Gets a collection of tags in this compound tag.
public Collection<Tag> values() { *
return this.value.values(); * @return This compound tag's tags.
} */
public Collection<Tag> values() {
/** return this.value.values();
* Gets the number of tags in this compound tag. }
* @return This compound tag's size.
*/ /**
public int size() { * Gets the number of tags in this compound tag.
return this.value.size(); *
} * @return This compound tag's size.
*/
/** public int size() {
* Clears all tags from this compound tag. return this.value.size();
*/ }
public void clear() {
this.value.clear(); /**
} * Clears all tags from this compound tag.
*/
@Override public void clear() {
public int getId() { this.value.clear();
return 10; }
}
@Override
@Override public int getId() {
public void read(DataInputStream in) throws IOException { return 10;
List<Tag> tags = NBTIO.readUntilEndTag(in); }
for(Tag tag : tags) {
this.put(tag); @Override
} public void read(DataInputStream in) throws IOException {
} List<Tag> tags = NBTIO.readUntilEndTag(in);
for(Tag tag : tags) {
@Override this.put(tag);
public void write(DataOutputStream out) throws IOException { }
NBTIO.writeTags(out, this.value.values()); }
out.writeByte(0);
} @Override
public void write(DataOutputStream out) throws IOException {
@Override NBTIO.writeTags(out, this.value.values());
public CompoundTag clone() { out.writeByte(0);
Map<String, Tag> newMap = new LinkedHashMap<String, Tag>(); }
for(Entry<String, Tag> entry : this.value.entrySet()) {
newMap.put(entry.getKey(), entry.getValue().clone()); @Override
} public CompoundTag clone() {
Map<String, Tag> newMap = new LinkedHashMap<String, Tag>();
return new CompoundTag(this.getName(), newMap); for(Entry<String, Tag> entry : this.value.entrySet()) {
} newMap.put(entry.getKey(), entry.getValue().clone());
}
}
return new CompoundTag(this.getName(), newMap);
}
}

View File

@ -1,65 +1,68 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing a double. * A tag containing a double.
*/ */
public class DoubleTag extends Tag { public class DoubleTag extends Tag {
private double value; private double value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public DoubleTag(String name) { */
this(name, 0); public DoubleTag(String name) {
} this(name, 0);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public DoubleTag(String name, double value) { * @param value The value of the tag.
super(name); */
this.value = value; public DoubleTag(String name, double value) {
} super(name);
this.value = value;
@Override }
public Double getValue() {
return this.value; @Override
} public Double getValue() {
return this.value;
@Override }
public int getId() {
return 6; @Override
} public int getId() {
return 6;
@Override }
public void read(DataInputStream in) throws IOException {
this.value = in.readDouble(); @Override
} public void read(DataInputStream in) throws IOException {
this.value = in.readDouble();
@Override }
public void write(DataOutputStream out) throws IOException {
out.writeDouble(this.value); @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. /**
*/ * Sets the value of this tag.
public void setValue(double value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(double value) {
@Override this.value = value;
public DoubleTag clone() { }
return new DoubleTag(this.getName(), this.getValue());
} @Override
public DoubleTag clone() {
} return new DoubleTag(this.getName(), this.getValue());
}
}

View File

@ -1,65 +1,68 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing a float. * A tag containing a float.
*/ */
public class FloatTag extends Tag { public class FloatTag extends Tag {
private float value; private float value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public FloatTag(String name) { */
this(name, 0); public FloatTag(String name) {
} this(name, 0);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public FloatTag(String name, float value) { * @param value The value of the tag.
super(name); */
this.value = value; public FloatTag(String name, float value) {
} super(name);
this.value = value;
@Override }
public Float getValue() {
return this.value; @Override
} public Float getValue() {
return this.value;
@Override }
public int getId() {
return 5; @Override
} public int getId() {
return 5;
@Override }
public void read(DataInputStream in) throws IOException {
this.value = in.readFloat(); @Override
} public void read(DataInputStream in) throws IOException {
this.value = in.readFloat();
@Override }
public void write(DataOutputStream out) throws IOException {
out.writeFloat(this.value); @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. /**
*/ * Sets the value of this tag.
public void setValue(float value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(float value) {
@Override this.value = value;
public FloatTag clone() { }
return new FloatTag(this.getName(), this.getValue());
} @Override
public FloatTag clone() {
} return new FloatTag(this.getName(), this.getValue());
}
}

View File

@ -1,4 +1,4 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -10,41 +10,43 @@ import java.io.IOException;
public class IntArrayTag extends Tag { public class IntArrayTag extends Tag {
private int[] value; private int[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public IntArrayTag(String name) { public IntArrayTag(String name) {
this(name, new int[0]); this(name, new int[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public IntArrayTag(String name, int[] value) { public IntArrayTag(String name, int[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public int[] getValue() { public int[] getValue() {
return this.value.clone(); return this.value.clone();
} }
@Override @Override
public int getId() { public int getId() {
return 11; return 11;
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new int[in.readInt()]; this.value = new int[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readInt(); this.value[index] = in.readInt();
} }
} }
@Override @Override
@ -54,45 +56,49 @@ public class IntArrayTag extends Tag {
out.writeInt(this.value[index]); out.writeInt(this.value[index]);
} }
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(int[] value) { public void setValue(int[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public int getValue(int index) { public int getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, int value) { public void setValue(int index, int value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public IntArrayTag clone() { public IntArrayTag clone() {
return new IntArrayTag(this.getName(), this.getValue()); return new IntArrayTag(this.getName(), this.getValue());

View File

@ -1,65 +1,68 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing an integer. * A tag containing an integer.
*/ */
public class IntTag extends Tag { public class IntTag extends Tag {
private int value; private int value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public IntTag(String name) { */
this(name, 0); public IntTag(String name) {
} this(name, 0);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public IntTag(String name, int value) { * @param value The value of the tag.
super(name); */
this.value = value; public IntTag(String name, int value) {
} super(name);
this.value = value;
@Override }
public Integer getValue() {
return this.value; @Override
} public Integer getValue() {
return this.value;
/** }
* Sets the value of this tag.
* @param value New value of this tag. /**
*/ * Sets the value of this tag.
public void setValue(int value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(int value) {
@Override this.value = value;
public int getId() { }
return 3;
} @Override
public int getId() {
@Override return 3;
public void read(DataInputStream in) throws IOException { }
this.value = in.readInt();
} @Override
public void read(DataInputStream in) throws IOException {
@Override this.value = in.readInt();
public void write(DataOutputStream out) throws IOException { }
out.writeInt(this.value);
} @Override
public void write(DataOutputStream out) throws IOException {
@Override out.writeInt(this.value);
public IntTag clone() { }
return new IntTag(this.getName(), this.getValue());
} @Override
public IntTag clone() {
} return new IntTag(this.getName(), this.getValue());
}
}

View File

@ -1,146 +1,153 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import org.spacehq.opennbt.TagRegistry;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.DataInputStream;
import java.util.ArrayList; import java.io.DataOutputStream;
import java.util.Iterator; import java.io.IOException;
import java.util.List; import java.util.ArrayList;
import java.util.Iterator;
import ch.spacebase.opennbt.TagRegistry; import java.util.List;
/** /**
* A tag containing a list of tags. * A tag containing a list of tags.
*/ */
public class ListTag<T extends Tag> extends Tag implements Iterable<T> { public class ListTag<T extends Tag> extends Tag implements Iterable<T> {
private Class<T> type; private Class<T> type;
private List<T> value; private List<T> value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public ListTag(String name) { */
super(name); public ListTag(String name) {
this.value = new ArrayList<T>(); super(name);
} this.value = new ArrayList<T>();
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param type Tag type of the list. *
*/ * @param name The name of the tag.
public ListTag(String name, Class<T> type) { * @param type Tag type of the list.
this(name, type, new ArrayList<T>()); */
} public ListTag(String name, Class<T> type) {
this(name, type, new ArrayList<T>());
/** }
* Creates a tag with the specified name.
* @param name The name of the tag. /**
* @param type Tag type of the list. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public ListTag(String name, Class<T> type, List<T> value) { * @param type Tag type of the list.
super(name); * @param value The value of the tag.
this.type = type; */
this.value = value; public ListTag(String name, Class<T> type, List<T> value) {
} super(name);
this.type = type;
@Override this.value = value;
public List<T> getValue() { }
return new ArrayList<T>(this.value);
} @Override
public List<T> getValue() {
/** return new ArrayList<T>(this.value);
* Adds a tag to this list tag. }
* @param tag Tag to add.
* @return If the list was changed as a result. /**
*/ * Adds a tag to this list tag.
public boolean add(T tag) { *
return this.value.add(tag); * @param tag Tag to add.
} * @return If the list was changed as a result.
*/
/** public boolean add(T tag) {
* Removes a tag from this list tag. return this.value.add(tag);
* @param tag Tag to remove. }
* @return If the list contained the tag.
*/ /**
public boolean remove(T tag) { * Removes a tag from this list tag.
return this.value.remove(tag); *
} * @param tag Tag to remove.
* @return If the list contained the tag.
/** */
* Gets the tag at the given index of this list tag. public boolean remove(T tag) {
* @param index Index of the tag. return this.value.remove(tag);
* @return The tag at the given index. }
*/
public T get(int index) { /**
return this.value.get(index); * Gets the tag at the given index of this list tag.
} *
* @param index Index of the tag.
/** * @return The tag at the given index.
* Gets the number of tags in this list tag. */
* @return The size of this list tag. public T get(int index) {
*/ return this.value.get(index);
public int size() { }
return this.value.size();
} /**
* Gets the number of tags in this list tag.
@Override *
public Iterator<T> iterator() { * @return The size of this list tag.
return this.value.iterator(); */
} public int size() {
return this.value.size();
@Override }
public int getId() {
return 9; @Override
} public Iterator<T> iterator() {
return this.value.iterator();
@SuppressWarnings("unchecked") }
@Override
public void read(DataInputStream in) throws IOException { @Override
int id = in.readByte() & 0xFF; public int getId() {
this.type = (Class<T>) TagRegistry.getClassFor(id); return 9;
if(this.type == null) { }
throw new IOException("Unknown tag ID in ListTag: " + id);
} @SuppressWarnings("unchecked")
@Override
int count = in.readInt(); public void read(DataInputStream in) throws IOException {
for(int index = 0; index < count; index++) { int id = in.readByte() & 0xFF;
Tag tag = TagRegistry.createInstance(id, ""); this.type = (Class<T>) TagRegistry.getClassFor(id);
if(tag == null) { if(this.type == null) {
throw new IOException("Tag could not be created: \"" + this.type.getSimpleName() + "\" (" + id + ")"); throw new IOException("Unknown tag ID in ListTag: " + id);
} }
tag.read(in); int count = in.readInt();
this.add((T) tag); 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 + ")");
@Override }
public void write(DataOutputStream out) throws IOException {
int id = TagRegistry.getIdFor(this.type); tag.read(in);
if(id == -1) { this.add((T) tag);
throw new IOException("ListTag contains unregistered tag class."); }
} }
out.writeByte(id); @Override
out.writeInt(this.value.size()); public void write(DataOutputStream out) throws IOException {
for(Tag tag : this.value) { int id = TagRegistry.getIdFor(this.type);
tag.write(out); if(id == -1) {
} throw new IOException("ListTag contains unregistered tag class.");
} }
@SuppressWarnings("unchecked") out.writeByte(id);
@Override out.writeInt(this.value.size());
public ListTag<T> clone() { for(Tag tag : this.value) {
List<T> newList = new ArrayList<T>(); tag.write(out);
for(T value : this.value) { }
newList.add((T) value.clone()); }
}
@SuppressWarnings("unchecked")
return new ListTag<T>(this.getName(), this.type, newList); @Override
} public ListTag<T> clone() {
List<T> newList = new ArrayList<T>();
} for(T value : this.value) {
newList.add((T) value.clone());
}
return new ListTag<T>(this.getName(), this.type, newList);
}
}

View File

@ -1,65 +1,68 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing a long. * A tag containing a long.
*/ */
public class LongTag extends Tag { public class LongTag extends Tag {
private long value; private long value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public LongTag(String name) { */
this(name, 0); public LongTag(String name) {
} this(name, 0);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public LongTag(String name, long value) { * @param value The value of the tag.
super(name); */
this.value = value; public LongTag(String name, long value) {
} super(name);
this.value = value;
@Override }
public Long getValue() {
return this.value; @Override
} public Long getValue() {
return this.value;
/** }
* Sets the value of this tag.
* @param value New value of this tag. /**
*/ * Sets the value of this tag.
public void setValue(long value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(long value) {
@Override this.value = value;
public int getId() { }
return 4;
} @Override
public int getId() {
@Override return 4;
public void read(DataInputStream in) throws IOException { }
this.value = in.readLong();
} @Override
public void read(DataInputStream in) throws IOException {
@Override this.value = in.readLong();
public void write(DataOutputStream out) throws IOException { }
out.writeLong(this.value);
} @Override
public void write(DataOutputStream out) throws IOException {
@Override out.writeLong(this.value);
public LongTag clone() { }
return new LongTag(this.getName(), this.getValue());
} @Override
public LongTag clone() {
} return new LongTag(this.getName(), this.getValue());
}
}

View File

@ -1,65 +1,68 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* A tag containing a short. * A tag containing a short.
*/ */
public class ShortTag extends Tag { public class ShortTag extends Tag {
private short value; private short value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public ShortTag(String name) { */
this(name, (short) 0); public ShortTag(String name) {
} this(name, (short) 0);
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public ShortTag(String name, short value) { * @param value The value of the tag.
super(name); */
this.value = value; public ShortTag(String name, short value) {
} super(name);
this.value = value;
@Override }
public Short getValue() {
return this.value; @Override
} public Short getValue() {
return this.value;
/** }
* Sets the value of this tag.
* @param value New value of this tag. /**
*/ * Sets the value of this tag.
public void setValue(short value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(short value) {
@Override this.value = value;
public int getId() { }
return 2;
} @Override
public int getId() {
@Override return 2;
public void read(DataInputStream in) throws IOException { }
this.value = in.readShort();
} @Override
public void read(DataInputStream in) throws IOException {
@Override this.value = in.readShort();
public void write(DataOutputStream out) throws IOException { }
out.writeShort(this.value);
} @Override
public void write(DataOutputStream out) throws IOException {
@Override out.writeShort(this.value);
public ShortTag clone() { }
return new ShortTag(this.getName(), this.getValue());
} @Override
public ShortTag clone() {
} return new ShortTag(this.getName(), this.getValue());
}
}

View File

@ -1,71 +1,74 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import org.spacehq.opennbt.NBTIO;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.DataInputStream;
import java.io.DataOutputStream;
import ch.spacebase.opennbt.NBTIO; import java.io.IOException;
/** /**
* A tag containing a string. * A tag containing a string.
*/ */
public class StringTag extends Tag { public class StringTag extends Tag {
private String value; private String value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name of the tag. *
*/ * @param name The name of the tag.
public StringTag(String name) { */
this(name, ""); public StringTag(String name) {
} this(name, "");
}
/**
* Creates a tag with the specified name. /**
* @param name The name of the tag. * Creates a tag with the specified name.
* @param value The value of the tag. *
*/ * @param name The name of the tag.
public StringTag(String name, String value) { * @param value The value of the tag.
super(name); */
this.value = value; public StringTag(String name, String value) {
} super(name);
this.value = value;
@Override }
public String getValue() {
return this.value; @Override
} public String getValue() {
return this.value;
/** }
* Sets the value of this tag.
* @param value New value of this tag. /**
*/ * Sets the value of this tag.
public void setValue(String value) { *
this.value = value; * @param value New value of this tag.
} */
public void setValue(String value) {
@Override this.value = value;
public int getId() { }
return 8;
} @Override
public int getId() {
@Override return 8;
public void read(DataInputStream in) throws IOException { }
byte[] bytes = new byte[in.readShort()];
in.readFully(bytes); @Override
this.value = new String(bytes, NBTIO.CHARSET); public void read(DataInputStream in) throws IOException {
} byte[] bytes = new byte[in.readShort()];
in.readFully(bytes);
@Override this.value = new String(bytes, NBTIO.CHARSET);
public void write(DataOutputStream out) throws IOException { }
byte[] bytes = this.value.getBytes(NBTIO.CHARSET);
out.writeShort(bytes.length); @Override
out.write(bytes); public void write(DataOutputStream out) throws IOException {
} byte[] bytes = this.value.getBytes(NBTIO.CHARSET);
out.writeShort(bytes.length);
@Override out.write(bytes);
public StringTag clone() { }
return new StringTag(this.getName(), this.getValue());
} @Override
public StringTag clone() {
} return new StringTag(this.getName(), this.getValue());
}
}

View File

@ -1,125 +1,131 @@
package ch.spacebase.opennbt.tag; package org.spacehq.opennbt.tag;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Array; import java.lang.reflect.Array;
/** /**
* Represents an NBT tag. * Represents an NBT tag.
* * <p/>
* All tags must have a constructor with a single string parameter for reading tags. * 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. * Tags should also have setter methods specific to their value types.
*/ */
public abstract class Tag implements Cloneable { public abstract class Tag implements Cloneable {
private String name; private String name;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
* @param name The name. *
*/ * @param name The name.
public Tag(String name) { */
this.name = name; public Tag(String name) {
} this.name = name;
}
/**
* Gets the name of this tag. /**
* @return The name of this tag. * Gets the name of this tag.
*/ *
public final String getName() { * @return The name of this tag.
return this.name; */
} public final String getName() {
return this.name;
/** }
* Gets the value of this tag.
* @return The value of this tag. /**
*/ * Gets the value of this tag.
public abstract Object getValue(); *
* @return The value of this tag.
/** */
* Gets the type id of this tag. public abstract Object getValue();
* @return The tag's id.
*/ /**
public abstract int getId(); * Gets the type id of this tag.
*
/** * @return The tag's id.
* Reads this tag from an input stream. */
* @param in Stream to write to. public abstract int getId();
* @throws IOException If an I/O error occurs.
*/ /**
public abstract void read(DataInputStream in) throws IOException; * Reads this tag from an input stream.
*
/** * @param in Stream to write to.
* Writes this tag to an output stream. * @throws java.io.IOException If an I/O error occurs.
* @param out Stream to write to. */
* @throws IOException If an I/O error occurs. public abstract void read(DataInputStream in) throws IOException;
*/
public abstract void write(DataOutputStream out) throws IOException; /**
* Writes this tag to an output stream.
@Override *
public boolean equals(Object obj) { * @param out Stream to write to.
if(!(obj instanceof Tag)) { * @throws java.io.IOException If an I/O error occurs.
return false; */
} public abstract void write(DataOutputStream out) throws IOException;
Tag tag = (Tag) obj; @Override
if(!this.getName().equals(tag.getName())) { public boolean equals(Object obj) {
return false; if(!(obj instanceof Tag)) {
} return false;
}
if(this.getValue() == null) {
return tag.getValue() == null; Tag tag = (Tag) obj;
} else if(tag.getValue() == null) { if(!this.getName().equals(tag.getName())) {
return false; return false;
} }
if(this.getValue().getClass().isArray() && tag.getValue().getClass().isArray()) { if(this.getValue() == null) {
int length = Array.getLength(this.getValue()); return tag.getValue() == null;
if(Array.getLength(tag.getValue()) != length) { } else if(tag.getValue() == null) {
return false; return false;
} }
for(int index = 0; index < length; index++) { if(this.getValue().getClass().isArray() && tag.getValue().getClass().isArray()) {
Object o = Array.get(this.getValue(), index); int length = Array.getLength(this.getValue());
Object other = Array.get(tag.getValue(), index); if(Array.getLength(tag.getValue()) != length) {
if(o == null && other != null || o != null && !o.equals(other)) { return false;
return false; }
}
} for(int index = 0; index < length; index++) {
Object o = Array.get(this.getValue(), index);
return true; Object other = Array.get(tag.getValue(), index);
} if(o == null && other != null || o != null && !o.equals(other)) {
return false;
return this.getValue().equals(tag.getValue()); }
} }
@Override return true;
public String toString() { }
String name = this.getName() != null && !this.getName().equals("") ? "(" + this.getName() + ")" : "";
String value = ""; return this.getValue().equals(tag.getValue());
if(this.getValue() != null) { }
value = this.getValue().toString();
if(this.getValue().getClass().isArray()) { @Override
StringBuilder build = new StringBuilder(); public String toString() {
build.append("["); String name = this.getName() != null && !this.getName().equals("") ? "(" + this.getName() + ")" : "";
for(int index = 0; index < Array.getLength(this.getValue()); index++) { String value = "";
if(index > 0) { if(this.getValue() != null) {
build.append(", "); value = this.getValue().toString();
} if(this.getValue().getClass().isArray()) {
StringBuilder build = new StringBuilder();
build.append(Array.get(this.getValue(), index)); build.append("[");
} for(int index = 0; index < Array.getLength(this.getValue()); index++) {
if(index > 0) {
build.append("]"); build.append(", ");
value = build.toString(); }
}
} build.append(Array.get(this.getValue(), index));
}
return this.getClass().getSimpleName() + name + " { " + value + " }";
} build.append("]");
value = build.toString();
@Override }
public abstract Tag clone(); }
} return this.getClass().getSimpleName() + name + " { " + value + " }";
}
@Override
public abstract Tag clone();
}

View File

@ -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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import ch.spacebase.opennbt.tag.Tag;
/** /**
* A tag containing a double array. * A tag containing a double array.
*/ */
public class DoubleArrayTag extends Tag { public class DoubleArrayTag extends Tag {
private double[] value; private double[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public DoubleArrayTag(String name) { public DoubleArrayTag(String name) {
this(name, new double[0]); this(name, new double[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public DoubleArrayTag(String name, double[] value) { public DoubleArrayTag(String name, double[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public double[] getValue() { public double[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(double[] value) { public void setValue(double[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public double getValue(int index) { public double getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, double value) { public void setValue(int index, double value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public int getId() { public int getId() {
return 60; return 60;
@ -82,9 +88,9 @@ public class DoubleArrayTag extends Tag {
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new double[in.readInt()]; this.value = new double[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readDouble(); this.value[index] = in.readDouble();
} }
} }
@Override @Override
@ -94,7 +100,7 @@ public class DoubleArrayTag extends Tag {
out.writeDouble(this.value[index]); out.writeDouble(this.value[index]);
} }
} }
@Override @Override
public DoubleArrayTag clone() { public DoubleArrayTag clone() {
return new DoubleArrayTag(this.getName(), this.getValue()); return new DoubleArrayTag(this.getName(), this.getValue());

View File

@ -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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import ch.spacebase.opennbt.tag.Tag;
/** /**
* A tag containing a float array. * A tag containing a float array.
*/ */
public class FloatArrayTag extends Tag { public class FloatArrayTag extends Tag {
private float[] value; private float[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public FloatArrayTag(String name) { public FloatArrayTag(String name) {
this(name, new float[0]); this(name, new float[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public FloatArrayTag(String name, float[] value) { public FloatArrayTag(String name, float[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public float[] getValue() { public float[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(float[] value) { public void setValue(float[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public float getValue(int index) { public float getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, float value) { public void setValue(int index, float value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public int getId() { public int getId() {
return 61; return 61;
@ -82,9 +88,9 @@ public class FloatArrayTag extends Tag {
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new float[in.readInt()]; this.value = new float[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readFloat(); this.value[index] = in.readFloat();
} }
} }
@Override @Override
@ -94,7 +100,7 @@ public class FloatArrayTag extends Tag {
out.writeFloat(this.value[index]); out.writeFloat(this.value[index]);
} }
} }
@Override @Override
public FloatArrayTag clone() { public FloatArrayTag clone() {
return new FloatArrayTag(this.getName(), this.getValue()); return new FloatArrayTag(this.getName(), this.getValue());

View File

@ -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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import ch.spacebase.opennbt.tag.Tag;
/** /**
* A tag containing a long array. * A tag containing a long array.
*/ */
public class LongArrayTag extends Tag { public class LongArrayTag extends Tag {
private long[] value; private long[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public LongArrayTag(String name) { public LongArrayTag(String name) {
this(name, new long[0]); this(name, new long[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public LongArrayTag(String name, long[] value) { public LongArrayTag(String name, long[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public long[] getValue() { public long[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(long[] value) { public void setValue(long[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public long getValue(int index) { public long getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, long value) { public void setValue(int index, long value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public int getId() { public int getId() {
return 62; return 62;
@ -82,9 +88,9 @@ public class LongArrayTag extends Tag {
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new long[in.readInt()]; this.value = new long[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readLong(); this.value[index] = in.readLong();
} }
} }
@Override @Override
@ -94,7 +100,7 @@ public class LongArrayTag extends Tag {
out.writeLong(this.value[index]); out.writeLong(this.value[index]);
} }
} }
@Override @Override
public LongArrayTag clone() { public LongArrayTag clone() {
return new LongArrayTag(this.getName(), this.getValue()); return new LongArrayTag(this.getName(), this.getValue());

View File

@ -1,13 +1,8 @@
package ch.spacebase.opennbt.tag.custom; package org.spacehq.opennbt.tag.custom;
import java.io.DataInputStream; import org.spacehq.opennbt.tag.Tag;
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; import java.io.*;
/** /**
* A tag containing an array of serializable objects. * A tag containing an array of serializable objects.
@ -15,95 +10,101 @@ import ch.spacebase.opennbt.tag.Tag;
public class SerializableArrayTag extends Tag { public class SerializableArrayTag extends Tag {
private Serializable[] value; private Serializable[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public SerializableArrayTag(String name) { public SerializableArrayTag(String name) {
this(name, new Serializable[0]); this(name, new Serializable[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public SerializableArrayTag(String name, Serializable[] value) { public SerializableArrayTag(String name, Serializable[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public Serializable[] getValue() { public Serializable[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(Serializable[] value) { public void setValue(Serializable[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public Serializable getValue(int index) { public Serializable getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, Serializable value) { public void setValue(int index, Serializable value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public int getId() { public int getId() {
return 63; return 63;
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new Serializable[in.readInt()]; this.value = new Serializable[in.readInt()];
ObjectInputStream str = new ObjectInputStream(in); ObjectInputStream str = new ObjectInputStream(in);
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
try { try {
this.value[index] = (Serializable) str.readObject(); this.value[index] = (Serializable) str.readObject();
} catch (ClassNotFoundException e) { } catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableArrayTag!", e); throw new IOException("Class not found while reading SerializableArrayTag!", e);
} }
} }
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutputStream out) throws IOException {
out.writeInt(this.value.length); out.writeInt(this.value.length);
ObjectOutputStream str = new ObjectOutputStream(out); ObjectOutputStream str = new ObjectOutputStream(out);
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
str.writeObject(this.value[index]); str.writeObject(this.value[index]);
} }
} }
@Override @Override
public SerializableArrayTag clone() { public SerializableArrayTag clone() {
return new SerializableArrayTag(this.getName(), this.getValue()); return new SerializableArrayTag(this.getName(), this.getValue());

View File

@ -1,13 +1,8 @@
package ch.spacebase.opennbt.tag.custom; package org.spacehq.opennbt.tag.custom;
import java.io.DataInputStream; import org.spacehq.opennbt.tag.Tag;
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; import java.io.*;
/** /**
* A tag containing a serializable object. * A tag containing a serializable object.
@ -15,18 +10,20 @@ import ch.spacebase.opennbt.tag.Tag;
public class SerializableTag extends Tag { public class SerializableTag extends Tag {
private Serializable value; private Serializable value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public SerializableTag(String name) { public SerializableTag(String name) {
this(name, 0); this(name, 0);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public SerializableTag(String name, Serializable value) { public SerializableTag(String name, Serializable value) {
@ -38,39 +35,40 @@ public class SerializableTag extends Tag {
public Serializable getValue() { public Serializable getValue() {
return this.value; return this.value;
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(Serializable value) { public void setValue(Serializable value) {
this.value = value; this.value = value;
} }
@Override @Override
public int getId() { public int getId() {
return 64; return 64;
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
ObjectInputStream str = new ObjectInputStream(in); ObjectInputStream str = new ObjectInputStream(in);
try { try {
this.value = (Serializable) str.readObject(); this.value = (Serializable) str.readObject();
} catch (ClassNotFoundException e) { } catch(ClassNotFoundException e) {
throw new IOException("Class not found while reading SerializableTag!", e); throw new IOException("Class not found while reading SerializableTag!", e);
} }
} }
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutputStream out) throws IOException {
ObjectOutputStream str = new ObjectOutputStream(out); ObjectOutputStream str = new ObjectOutputStream(out);
str.writeObject(this.value); str.writeObject(this.value);
} }
@Override @Override
public SerializableTag clone() { public SerializableTag clone() {
return new SerializableTag(this.getName(), this.getValue()); return new SerializableTag(this.getName(), this.getValue());
} }
} }

View File

@ -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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import ch.spacebase.opennbt.tag.Tag;
/** /**
* A tag containing a short array. * A tag containing a short array.
*/ */
public class ShortArrayTag extends Tag { public class ShortArrayTag extends Tag {
private short[] value; private short[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public ShortArrayTag(String name) { public ShortArrayTag(String name) {
this(name, new short[0]); this(name, new short[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public ShortArrayTag(String name, short[] value) { public ShortArrayTag(String name, short[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public short[] getValue() { public short[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(short[] value) { public void setValue(short[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public short getValue(int index) { public short getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, short value) { public void setValue(int index, short value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public int getId() { public int getId() {
return 65; return 65;
@ -82,9 +88,9 @@ public class ShortArrayTag extends Tag {
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new short[in.readInt()]; this.value = new short[in.readInt()];
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
this.value[index] = in.readShort(); this.value[index] = in.readShort();
} }
} }
@Override @Override
@ -94,7 +100,7 @@ public class ShortArrayTag extends Tag {
out.writeShort(this.value[index]); out.writeShort(this.value[index]);
} }
} }
@Override @Override
public ShortArrayTag clone() { public ShortArrayTag clone() {
return new ShortArrayTag(this.getName(), this.getValue()); return new ShortArrayTag(this.getName(), this.getValue());

View File

@ -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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import ch.spacebase.opennbt.NBTIO;
import ch.spacebase.opennbt.tag.Tag;
/** /**
* A tag containing a string array. * A tag containing a string array.
*/ */
public class StringArrayTag extends Tag { public class StringArrayTag extends Tag {
private String[] value; private String[] value;
/** /**
* Creates a tag with the specified name. * Creates a tag with the specified name.
*
* @param name The name of the tag. * @param name The name of the tag.
*/ */
public StringArrayTag(String name) { public StringArrayTag(String name) {
this(name, new String[0]); this(name, new String[0]);
} }
/** /**
* Creates a tag with the specified name. * 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. * @param value The value of the tag.
*/ */
public StringArrayTag(String name, String[] value) { public StringArrayTag(String name, String[] value) {
super(name); super(name);
this.value = value; this.value = value;
} }
@Override @Override
public String[] getValue() { public String[] getValue() {
return this.value.clone(); return this.value.clone();
} }
/** /**
* Sets the value of this tag. * Sets the value of this tag.
*
* @param value New value of this tag. * @param value New value of this tag.
*/ */
public void setValue(String[] value) { public void setValue(String[] value) {
if(value == null) { if(value == null) {
return; return;
} }
this.value = value.clone(); this.value = value.clone();
} }
/** /**
* Gets a value in this tag's array. * Gets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @return The value at the given index. * @return The value at the given index.
*/ */
public String getValue(int index) { public String getValue(int index) {
return this.value[index]; return this.value[index];
} }
/** /**
* Sets a value in this tag's array. * Sets a value in this tag's array.
*
* @param index Index of the value. * @param index Index of the value.
* @param value Value to set. * @param value Value to set.
*/ */
public void setValue(int index, String value) { public void setValue(int index, String value) {
this.value[index] = value; this.value[index] = value;
} }
/** /**
* Gets the length of this tag's array. * Gets the length of this tag's array.
*
* @return This tag's array length. * @return This tag's array length.
*/ */
public int length() { public int length() {
return this.value.length; return this.value.length;
} }
@Override @Override
public int getId() { public int getId() {
return 66; return 66;
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
this.value = new String[in.readInt()]; this.value = new String[in.readInt()];
@ -92,14 +98,14 @@ public class StringArrayTag extends Tag {
@Override @Override
public void write(DataOutputStream out) throws IOException { public void write(DataOutputStream out) throws IOException {
out.writeInt(this.value.length); out.writeInt(this.value.length);
for(int index = 0; index < this.value.length; index++) { for(int index = 0; index < this.value.length; index++) {
byte[] bytes = this.value[index].getBytes(NBTIO.CHARSET); byte[] bytes = this.value[index].getBytes(NBTIO.CHARSET);
out.writeShort(bytes.length); out.writeShort(bytes.length);
out.write(bytes); out.write(bytes);
} }
} }
@Override @Override
public StringArrayTag clone() { public StringArrayTag clone() {
return new StringArrayTag(this.getName(), this.getValue()); return new StringArrayTag(this.getName(), this.getValue());