Added IntArrayTag

This commit is contained in:
Steveice10 2012-03-11 21:07:52 -07:00
parent 598ca75826
commit 1c1713d8b3
6 changed files with 181 additions and 3 deletions

View File

@ -59,7 +59,8 @@ public final class NBTConstants {
TYPE_BYTE_ARRAY = 7,
TYPE_STRING = 8,
TYPE_LIST = 9,
TYPE_COMPOUND = 10;
TYPE_COMPOUND = 10,
TYPE_INT_ARRAY = 11;
/**
* Default private constructor.

View File

@ -4,12 +4,15 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.sun.media.sound.InvalidFormatException;
import opennbt.tag.ByteArrayTag;
import opennbt.tag.ByteTag;
import opennbt.tag.CompoundTag;
import opennbt.tag.DoubleTag;
import opennbt.tag.EndTag;
import opennbt.tag.FloatTag;
import opennbt.tag.IntArrayTag;
import opennbt.tag.IntTag;
import opennbt.tag.ListTag;
import opennbt.tag.LongTag;
@ -85,6 +88,8 @@ public final class NBTUtils {
return "TAG_Short";
} else if(clazz.equals(StringTag.class)) {
return "TAG_String";
} else if (clazz.equals(IntArrayTag.class)) {
return "TAG_Int_Array";
} else {
throw new IllegalArgumentException("Invalid tag classs (" + clazz.getName() + ").");
}
@ -119,6 +124,8 @@ public final class NBTUtils {
return NBTConstants.TYPE_SHORT;
} else if(clazz.equals(StringTag.class)) {
return NBTConstants.TYPE_STRING;
} else if(clazz.equals(IntArrayTag.class)) {
return NBTConstants.TYPE_INT_ARRAY;
} else {
throw new IllegalArgumentException("Invalid tag classs (" + clazz.getName() + ").");
}
@ -154,6 +161,8 @@ public final class NBTUtils {
return ListTag.class;
case NBTConstants.TYPE_COMPOUND:
return CompoundTag.class;
case NBTConstants.TYPE_INT_ARRAY:
return IntArrayTag.class;
default:
throw new IllegalArgumentException("Invalid tag type : " + type + ".");
}
@ -179,7 +188,7 @@ public final class NBTUtils {
* @param array to clone
* @return clone of array
*/
public static byte[] cloneArray(byte[] array) {
public static byte[] cloneByteArray(byte[] array) {
if(array == null) {
return null;
} else {
@ -192,6 +201,47 @@ public final class NBTUtils {
}
}
/**
* Clones an int array
* @param array to clone
* @return clone of array
*/
public static int[] cloneIntArray(int[] array) {
if(array == null) {
return null;
} else {
int size = array.length;
int[] newArray = new int[size];
System.arraycopy(array, 0, newArray, 0, size);
return newArray;
}
}
/**
* Get child tag of a NBT structure.
*
* @param items
* @param key
* @param expected
* @return child tag
* @throws InvalidFormatException
*/
public static <T extends Tag> T getChildTag(Map<String,Tag> items, String key, Class<T> expected) throws InvalidFormatException {
if (!items.containsKey(key)) {
throw new InvalidFormatException("Missing a \"" + key + "\" tag");
}
Tag tag = items.get(key);
if (!expected.isInstance(tag)) {
throw new InvalidFormatException(key + " tag is not of tag type " + expected.getName());
}
return expected.cast(tag);
}
/**
* Default private constructor.
*/

View File

@ -52,6 +52,7 @@ import opennbt.tag.CompoundTag;
import opennbt.tag.DoubleTag;
import opennbt.tag.EndTag;
import opennbt.tag.FloatTag;
import opennbt.tag.IntArrayTag;
import opennbt.tag.IntTag;
import opennbt.tag.ListTag;
import opennbt.tag.LongTag;
@ -188,6 +189,15 @@ public final class NBTInputStream implements Closeable {
}
return new CompoundTag(name, tagMap);
case NBTConstants.TYPE_INT_ARRAY:
length = is.readInt();
int[] data = new int[length];
for (int i = 0; i < length; i++) {
data[i] = is.readInt();
}
return new IntArrayTag(name, data);
default:
throw new IOException("Invalid tag type: " + type + ".");
}

View File

@ -15,6 +15,7 @@ import opennbt.tag.CompoundTag;
import opennbt.tag.DoubleTag;
import opennbt.tag.EndTag;
import opennbt.tag.FloatTag;
import opennbt.tag.IntArrayTag;
import opennbt.tag.IntTag;
import opennbt.tag.ListTag;
import opennbt.tag.LongTag;
@ -145,6 +146,9 @@ public final class NBTOutputStream implements Closeable {
case NBTConstants.TYPE_COMPOUND:
writeCompoundTagPayload((CompoundTag) tag);
break;
case NBTConstants.TYPE_INT_ARRAY:
writeIntArrayTagPayload((IntArrayTag) tag);
break;
default:
throw new IOException("Invalid tag type: " + type + ".");
}
@ -265,6 +269,20 @@ public final class NBTOutputStream implements Closeable {
/* empty */
}
/** Writes a <code>TAG_Int_Array<code> tag.
* @param tag The tag
* @throws IOException if an I/O error occurs.
*/
private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException {
int[] data = tag.getValue();
os.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
os.writeInt(data[i]);
}
}
@Override
public void close() throws IOException {
os.close();

View File

@ -92,7 +92,7 @@ public final class ByteArrayTag extends Tag {
}
public Tag clone() {
byte[] clonedArray = NBTUtils.cloneArray(this.getValue());
byte[] clonedArray = NBTUtils.cloneByteArray(this.getValue());
return new ByteArrayTag(this.getName(), clonedArray);
}

View File

@ -0,0 +1,99 @@
package opennbt.tag;
import opennbt.NBTUtils;
import opennbt.tag.Tag;
/*
* OpenNBT License
*
* JNBT Copyright (c) 2010 Graham Edgecombe
* OpenNBT Copyright(c) 2012 Steveice10
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the JNBT team nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* The <code>TAG_Int_Array</code> tag.
*/
public final class IntArrayTag extends Tag {
/**
* The value.
*/
private final int[] value;
/**
* Creates the tag.
*
* @param name
* The name.
* @param value
* The value.
*/
public IntArrayTag(String name, int[] value) {
super(name);
this.value = value;
}
@Override
public int[] getValue() {
return value;
}
@Override
public String toString() {
StringBuilder hex = new StringBuilder();
for (int curr : value) {
String hexDigits = Integer.toHexString(curr).toUpperCase();
if (hexDigits.length() == 1) {
hex.append("0");
}
hex.append(hexDigits).append(" ");
}
String name = getName();
String append = "";
if (name != null && !name.equals("")) {
append = "(\"" + this.getName() + "\")";
}
return "TAG_Int_Array" + append + ": " + hex.toString();
}
@Override
public Tag clone() {
int[] clonedArray = NBTUtils.cloneIntArray(this.getValue());
return new IntArrayTag(this.getName(), clonedArray);
}
}