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
.git
lib
.settings
.classpath
.project
.settings
*.iml
.idea
target

View File

@ -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.

View File

@ -1,22 +1,22 @@
<b><center><h1>OpenNBT</h></center></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.
<b>Building the Source</b>
--------------
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>.
<b>License</b>
--------------
<b><center><h1>OpenNBT</h></center></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.
<b>Building the Source</b>
--------------
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.spacehq.org/job/OpenNBT/)</b>.
<b>License</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">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.spacebase</groupId>
<artifactId>opennbt</artifactId>
<version>1.3-SNAPSHOT</version>
<packaging>jar</packaging>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.spacehq</groupId>
<artifactId>opennbt</artifactId>
<version>1.3</version>
<packaging>jar</packaging>
<name>OpenNBT</name>
<description>A library for reading and writing NBT files, written in Java.</description>
<url>http://github.com/Steveice10/OpenNBT/</url>
<name>OpenNBT</name>
<description>A library for reading and writing NBT files, written in Java.</description>
<url>http://github.com/Steveice10/OpenNBT/</url>
<licenses>
<license>
<name>MIT</name>
<url>http://www.opensource.org/licenses/mit-license.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<licenses>
<license>
<name>MIT</name>
<url>http://www.opensource.org/licenses/mit-license.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:git:git@github.com:Steveice10/OpenNBT.git</connection>
<developerConnection>scm:git:git@github.com:Steveice10/OpenNBT.git</developerConnection>
<url>http://github.com/Steveice10/OpenNBT/</url>
</scm>
<scm>
<connection>scm:git:git@github.com:Steveice10/OpenNBT.git</connection>
<developerConnection>scm:git:git@github.com:Steveice10/OpenNBT.git</developerConnection>
<url>http://github.com/Steveice10/OpenNBT/</url>
</scm>
<distributionManagement>
<repository>
<id>spacebase</id>
<name>spacebase-releases</name>
<url>http://repo.spacebase.ch/content/repositories/release/</url>
</repository>
<snapshotRepository>
<id>spacebase</id>
<name>spacebase-snapshots</name>
<url>http://repo.spacebase.ch/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<distributionManagement>
<repository>
<id>spacehq</id>
<name>spacehq-releases</name>
<url>http://repo.spacehq.org/content/repositories/release/</url>
</repository>
<snapshotRepository>
<id>spacehq</id>
<name>spacehq-snapshots</name>
<url>http://repo.spacehq.org/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<developers>
<developer>
<id>steveice10</id>
<name>Steveice10</name>
<email>Steveice10@gmail.com</email>
</developer>
</developers>
<developers>
<developer>
<id>steveice10</id>
<name>Steveice10</name>
<email>Steveice10@gmail.com</email>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean install</defaultGoal>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<!-- Resources -->
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build>
<build>
<defaultGoal>clean install</defaultGoal>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<!-- Resources -->
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -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();
}
}

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.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<Tag> readUntilEndTag(DataInputStream in) throws IOException {
List<Tag> ret = new ArrayList<Tag>();
@ -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<Tag> 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);
}
}

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.
*/
public class TagRegisterException extends Exception {
private static final long serialVersionUID = -2022049594558041160L;
public TagRegisterException(Throwable 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.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<Integer, Class<? extends Tag>> tags = new HashMap<Integer, Class<? extends Tag>>();
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<? extends Tag> 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<? extends Tag> clazz = tags.get(id);
try {
return clazz.getDeclaredConstructor(String.class).newInstance(tagName);
@ -124,5 +101,5 @@ public class TagRegistry {
return null;
}
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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<String, Tag> value;
/**
* Creates a tag with the specified name.
* @param name The name of the 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.
* @param value The value of the tag.
*/
public CompoundTag(String name, Map<String, Tag> value) {
super(name);
this.value = new LinkedHashMap<String, Tag>(value);
}
@Override
public Map<String, Tag> getValue() {
return new LinkedHashMap<String, Tag>(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<String> keySet() {
return this.value.keySet();
}
/**
* Gets a collection of tags in this compound tag.
* @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() {
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<Tag> 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<String, Tag> newMap = new LinkedHashMap<String, Tag>();
for(Entry<String, Tag> 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<String, Tag> value;
/**
* Creates a tag with the specified name.
*
* @param name The name of the 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.
* @param value The value of the tag.
*/
public CompoundTag(String name, Map<String, Tag> value) {
super(name);
this.value = new LinkedHashMap<String, Tag>(value);
}
@Override
public Map<String, Tag> getValue() {
return new LinkedHashMap<String, Tag>(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<String> keySet() {
return this.value.keySet();
}
/**
* Gets a collection of tags in this compound tag.
*
* @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() {
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<Tag> 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<String, Tag> newMap = new LinkedHashMap<String, Tag>();
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;
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());
}
}

View File

@ -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());
}
}

View File

@ -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());

View File

@ -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());
}
}

View File

@ -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<T extends Tag> extends Tag implements Iterable<T> {
private Class<T> type;
private List<T> 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<T>();
}
/**
* 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<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.
* @param value The value of the tag.
*/
public ListTag(String name, Class<T> type, List<T> value) {
super(name);
this.type = type;
this.value = 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.
*/
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<T> 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<T>) 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<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);
}
}
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<T extends Tag> extends Tag implements Iterable<T> {
private Class<T> type;
private List<T> 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<T>();
}
/**
* 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<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.
* @param value The value of the tag.
*/
public ListTag(String name, Class<T> type, List<T> value) {
super(name);
this.type = type;
this.value = 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.
*/
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<T> 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<T>) 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<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;
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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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.
* <p/>
* 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();
}

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.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());

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.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());

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.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());

View File

@ -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());

View File

@ -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());
}
}

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.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());

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.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());