package common.tags;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;

class TagByteArray extends Tag {
	private byte[] data;

	TagByteArray() {
	}

	public TagByteArray(byte[] data) {
		this.data = data;
	}

	void write(DataOutput output) throws IOException {
		output.writeInt(this.data.length);
		output.write(this.data);
	}

	void read(DataInput input, int depth, SizeTracker tracker) throws IOException {
		tracker.read(24);
		int len = input.readInt();
		tracker.read(len);
		this.data = new byte[len];
		input.readFully(this.data);
	}

	TagType getType() {
		return TagType.BYTE_ARRAY;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder("[0x");
		for(int z = 0; z < this.data.length; z++) {
			sb.append(String.format("%02x", this.data[z]));
		}
		return sb.append("]").toString();
	}

	public Tag copy() {
		byte[] data = new byte[this.data.length];
		System.arraycopy(this.data, 0, data, 0, this.data.length);
		return new TagByteArray(data);
	}

	public boolean equals(Object other) {
		return super.equals(other) && Arrays.equals(this.data, ((TagByteArray)other).data);
	}

	public int hashCode() {
		return super.hashCode() ^ Arrays.hashCode(this.data);
	}

	public byte[] getByteArray() {
		return this.data;
	}
}
