From 1c1713d8b3d52128ac113be5263bd789acfff1f8 Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Sun, 11 Mar 2012 21:07:52 -0700 Subject: [PATCH] Added IntArrayTag --- src/opennbt/NBTConstants.java | 3 +- src/opennbt/NBTUtils.java | 52 ++++++++++++- src/opennbt/stream/NBTInputStream.java | 10 +++ src/opennbt/stream/NBTOutputStream.java | 18 +++++ src/opennbt/tag/ByteArrayTag.java | 2 +- src/opennbt/tag/IntArrayTag.java | 99 +++++++++++++++++++++++++ 6 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 src/opennbt/tag/IntArrayTag.java diff --git a/src/opennbt/NBTConstants.java b/src/opennbt/NBTConstants.java index 7b6f4a7..dde6dcd 100644 --- a/src/opennbt/NBTConstants.java +++ b/src/opennbt/NBTConstants.java @@ -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. diff --git a/src/opennbt/NBTUtils.java b/src/opennbt/NBTUtils.java index 0086617..2567a7f 100644 --- a/src/opennbt/NBTUtils.java +++ b/src/opennbt/NBTUtils.java @@ -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 getChildTag(Map items, String key, Class 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. */ diff --git a/src/opennbt/stream/NBTInputStream.java b/src/opennbt/stream/NBTInputStream.java index 5cb02b5..19be00f 100644 --- a/src/opennbt/stream/NBTInputStream.java +++ b/src/opennbt/stream/NBTInputStream.java @@ -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 + "."); } diff --git a/src/opennbt/stream/NBTOutputStream.java b/src/opennbt/stream/NBTOutputStream.java index 9c846d8..e5b2e1a 100644 --- a/src/opennbt/stream/NBTOutputStream.java +++ b/src/opennbt/stream/NBTOutputStream.java @@ -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 + "."); } @@ -264,6 +268,20 @@ public final class NBTOutputStream implements Closeable { private void writeEndTagPayload(EndTag tag) { /* empty */ } + + /** Writes a TAG_Int_Array tag. + * @param tag The tag + * @throws IOException if an I/O error occurs. + */ + private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException { + int[] data = tag.getValue(); + + os.writeInt(data.length); + + for (int i = 0; i < data.length; i++) { + os.writeInt(data[i]); + } + } @Override public void close() throws IOException { diff --git a/src/opennbt/tag/ByteArrayTag.java b/src/opennbt/tag/ByteArrayTag.java index 2e76d79..6267e59 100644 --- a/src/opennbt/tag/ByteArrayTag.java +++ b/src/opennbt/tag/ByteArrayTag.java @@ -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); } diff --git a/src/opennbt/tag/IntArrayTag.java b/src/opennbt/tag/IntArrayTag.java new file mode 100644 index 0000000..674cf0c --- /dev/null +++ b/src/opennbt/tag/IntArrayTag.java @@ -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 TAG_Int_Array 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); + } + +} \ No newline at end of file