VoidData.java
// <editor-fold defaultstate="collapsed" desc="license">
/*
* Copyright (c) 2014, Karl H. Beckers
* 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 <ORGANIZATION> 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.
**/
// </editor-fold>
package net.jarre_de_the.griffin.types.data;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import net.jarre_de_the.griffin.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of VOID. The GFF VOID data type contains variable-length
* arbitrary data. The first element is a DWORD that stores the length of the
* actual data as a number of bytes.
*
* @author charly4711
*/
public class VoidData extends AbstractData
implements Cloneable {
private static final Logger LOGGER = LoggerFactory.getLogger(VoidData.class);
private byte[] value;
/*
*
* constructors
*
*/
protected VoidData() {
}
/**
* Creates a new instance of {@code VoidData} from an array of arbitrary
* bytes.
*
* @param buf The value to set.
* @see #setValue(byte[] buf)
*/
public VoidData(byte[] buf) {
setValue(buf);
}
/*
*
* read from file
*
*/
public static VoidData read(RandomAccessFile in)
throws IOException {
LOGGER.debug("Reading VoidData from file.");
DWordData len = DWordData.read(in);
LOGGER.trace("Need to read " + len.getValueAsNumber() + " bytes.");
// we're just hoping we'll never encounter a GFF file where a
// single buffer is > MAXINT
byte[] buf = new byte[len.getValueAsNumber().intValue()];
in.readFully(buf);
LOGGER.trace("Read " + new String(buf, Util.CHARSET_US_ASCII));
return new VoidData(buf);
}
/*
*
* setter
*
*/
/**
* Sets this instance's value.
*
* @param buf The value to set. This input is copied internally, so don't
* expect to be able to retain a reference to the array and change it later
* on.
*/
private void setValue(byte[] buf) {
if (buf != null) {
this.value = new byte[buf.length];
System.arraycopy(buf, 0, this.value, 0, buf.length);
}
}
/*
*
* getter
*
*/
/**
* Retrieves a copy (!) of this instance's value.
*
* @return A copy of this instance's value. Changing the copy will NOT
* change the original.
*/
public byte[] getValueAsByteArray() {
byte[] buf;
if (null != value) {
buf = new byte[value.length];
System.arraycopy(value, 0, buf, 0, value.length);
} else {
buf = new byte[0];
}
return buf;
}
/*
*
* utility
*
*/
/**
* Overrides the {@code java.lang.Object} method to ensure we always get
* back an instance of {@code VoidData} rather than just {@code Object}.
*
* @return A deep copy of this object.
* @throws java.lang.CloneNotSupportedException
*/
@Override
public VoidData clone() throws CloneNotSupportedException {
LOGGER.debug("Cloning object");
VoidData clone = (VoidData) super.clone();
clone.setValue(value);
return clone;
}
/**
* Overrides the {@code java.lang.Object} method to make test for equality
* possible for this class.
*
* @param compare The Object to compare this object with.
* @return True if the objects have the same value, false if not.
*/
@Override
public boolean equals(Object compare) {
if (compare == this) {
return true;
}
if (!(compare instanceof VoidData)) {
return false;
}
VoidData dw = (VoidData) compare;
return Arrays.equals(dw.getValueAsByteArray(), this.getValueAsByteArray());
}
/**
* Overrides the {@code java.lang.Object} method to ensure we have a hash
* code consistent with the test for equality.
*
* @return The hash returned is based on the value.
*/
@Override
public int hashCode() {
return Arrays.hashCode(value);
}
}