diff --git a/src/main/java/com/flowpowered/nbt/LongArrayTag.java b/src/main/java/com/flowpowered/nbt/LongArrayTag.java new file mode 100644 index 0000000..7560ae6 --- /dev/null +++ b/src/main/java/com/flowpowered/nbt/LongArrayTag.java @@ -0,0 +1,95 @@ +/* + * This file is part of Flow NBT, licensed under the MIT License (MIT). + * + * Copyright (c) 2011 Flow Powered + * + * 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. + */ +package com.flowpowered.nbt; + +import java.util.Arrays; + +public class LongArrayTag extends Tag { + /** + * The value. + */ + private final long[] value; + + /** + * Creates the tag. + * + * @param name The name. + * @param value The value. + */ + public LongArrayTag(String name, long[] value) { + super(TagType.TAG_LONG_ARRAY, name); + this.value = value; + } + + @Override + public long[] getValue() { + return value; + } + + @Override + public String toString() { + StringBuilder hex = new StringBuilder(); + for (long s : value) { + String hexDigits = Long.toHexString(s).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_Long_Array" + append + ": " + hex.toString(); + } + + public LongArrayTag clone() { + long[] clonedArray = cloneArray(value); + + return new LongArrayTag(getName(), clonedArray); + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof LongArrayTag)) { + return false; + } + + LongArrayTag tag = (LongArrayTag) other; + return Arrays.equals(value, tag.value) && getName().equals(tag.getName()); + } + + private long[] cloneArray(long[] longArray) { + if (longArray == null) { + return null; + } else { + int length = longArray.length; + byte[] newArray = new byte[length]; + System.arraycopy(longArray, 0, newArray, 0, length); + return longArray; + } + } +} diff --git a/src/main/java/com/flowpowered/nbt/NBTConstants.java b/src/main/java/com/flowpowered/nbt/NBTConstants.java index 4400597..6903cbd 100644 --- a/src/main/java/com/flowpowered/nbt/NBTConstants.java +++ b/src/main/java/com/flowpowered/nbt/NBTConstants.java @@ -49,6 +49,7 @@ public final class NBTConstants { TYPE_LIST = TagType.TAG_LIST.getId(), TYPE_COMPOUND = TagType.TAG_COMPOUND.getId(), TYPE_INT_ARRAY = TagType.TAG_INT_ARRAY.getId(), + TYPE_LONG_ARRAY = TagType.TAG_LONG_ARRAY.getId(), TYPE_SHORT_ARRAY = TagType.TAG_SHORT_ARRAY.getId(); /** diff --git a/src/main/java/com/flowpowered/nbt/TagType.java b/src/main/java/com/flowpowered/nbt/TagType.java index 96beede..330cdf7 100644 --- a/src/main/java/com/flowpowered/nbt/TagType.java +++ b/src/main/java/com/flowpowered/nbt/TagType.java @@ -40,6 +40,7 @@ public enum TagType { // Java generics, y u so suck TAG_COMPOUND(CompoundTag.class, "TAG_Compound", 10), TAG_INT_ARRAY(IntArrayTag.class, "TAG_Int_Array", 11), + TAG_LONG_ARRAY(LongArrayTag.class, "TAG_Long_Array", 12), TAG_SHORT_ARRAY(ShortArrayTag.class, "TAG_Short_Array", 100),; private static final Map>, TagType> BY_CLASS = new HashMap>, TagType>(); private static final Map BY_NAME = new HashMap(); diff --git a/src/main/java/com/flowpowered/nbt/stream/NBTInputStream.java b/src/main/java/com/flowpowered/nbt/stream/NBTInputStream.java index c87515b..e73b669 100644 --- a/src/main/java/com/flowpowered/nbt/stream/NBTInputStream.java +++ b/src/main/java/com/flowpowered/nbt/stream/NBTInputStream.java @@ -41,6 +41,7 @@ import com.flowpowered.nbt.IntArrayTag; import com.flowpowered.nbt.IntTag; import com.flowpowered.nbt.ListTag; +import com.flowpowered.nbt.LongArrayTag; import com.flowpowered.nbt.LongTag; import com.flowpowered.nbt.NBTConstants; import com.flowpowered.nbt.ShortArrayTag; @@ -215,6 +216,14 @@ private Tag readTagPayload(TagType type, String name, int depth) throws IOExcept } return new IntArrayTag(name, ints); + case TAG_LONG_ARRAY: + length = is.readInt(); + long[] longs = new long[length]; + for (int i = 0; i < length; i++) { + longs[i] = is.readLong(); + } + return new LongArrayTag(name, longs); + case TAG_SHORT_ARRAY: length = is.readInt(); short[] shorts = new short[length]; diff --git a/src/main/java/com/flowpowered/nbt/stream/NBTOutputStream.java b/src/main/java/com/flowpowered/nbt/stream/NBTOutputStream.java index bdb9504..bd7aab4 100644 --- a/src/main/java/com/flowpowered/nbt/stream/NBTOutputStream.java +++ b/src/main/java/com/flowpowered/nbt/stream/NBTOutputStream.java @@ -39,6 +39,7 @@ import com.flowpowered.nbt.IntArrayTag; import com.flowpowered.nbt.IntTag; import com.flowpowered.nbt.ListTag; +import com.flowpowered.nbt.LongArrayTag; import com.flowpowered.nbt.LongTag; import com.flowpowered.nbt.NBTConstants; import com.flowpowered.nbt.ShortArrayTag; @@ -167,6 +168,10 @@ private void writeTagPayload(Tag tag) throws IOException { writeIntArrayTagPayload((IntArrayTag) tag); break; + case TAG_LONG_ARRAY: + writeLongArrayTagPayload((LongArrayTag) tag); + break; + case TAG_SHORT_ARRAY: writeShortArrayTagPayload((ShortArrayTag) tag); break; @@ -306,6 +311,20 @@ private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException { } } + /** + * Writes a {@code TAG_Long_Array} tag. + * + * @param tag The tag. + * @throws java.io.IOException if an I/O error occurs. + */ + private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException { + long[] longs = tag.getValue(); + os.writeInt(longs.length); + for (int i = 0; i < longs.length; i++) { + os.writeLong(longs[i]); + } + } + /** * Writes a {@code TAG_Short_Array} tag. *