package mightypork.utils.ion;
import java.io.Closeable;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import mightypork.utils.exceptions.CorruptDataException;
/**
* Ion input stream
*
* @author Ondřej Hruška (MightyPork)
*/
public class IonInput implements Closeable {
private final DataInput in;
private final InputStream stream;
@SuppressWarnings("resource")
public IonInput(File inFile) throws FileNotFoundException
{
this(new FileInputStream(inFile));
}
public IonInput(InputStream in)
{
this.stream = in;
this.in = new DataInputStream(in);
}
/**
* Read int 0-255. Suitable when the int was written using
* writeIntByte()
method.
*
* @return int
* @throws IOException
*/
public int readIntByte() throws IOException
{
return in.readUnsignedByte();
}
/**
* Read an int 0-65535. Suitable when the int was written using
* writeIntShort()
method.
*
* @return int
* @throws IOException
*/
public int readIntShort() throws IOException
{
return in.readUnsignedShort();
}
public boolean readBoolean() throws IOException
{
return in.readBoolean();
}
public byte readByte() throws IOException
{
return in.readByte();
}
public short readShort() throws IOException
{
return in.readShort();
}
public char readChar() throws IOException
{
return in.readChar();
}
public int readInt() throws IOException
{
return in.readInt();
}
public long readLong() throws IOException
{
return in.readLong();
}
public float readFloat() throws IOException
{
return in.readFloat();
}
public double readDouble() throws IOException
{
return in.readDouble();
}
public String readString() throws IOException
{
return in.readUTF();
}
public boolean[] readBooleans() throws IOException
{
final int length = readLength();
final boolean[] arr = new boolean[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readBoolean();
}
return arr;
}
public byte[] readBytes() throws IOException
{
final int length = readLength();
final byte[] arr = new byte[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readByte();
}
return arr;
}
public char[] readChars() throws IOException
{
final int length = readLength();
final char[] arr = new char[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readChar();
}
return arr;
}
public short[] readShorts() throws IOException
{
final int length = readLength();
final short[] arr = new short[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readShort();
}
return arr;
}
public int[] readInts() throws IOException
{
final int length = readLength();
final int[] arr = new int[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readInt();
}
return arr;
}
public long[] readLongs() throws IOException
{
final int length = readLength();
final long[] arr = new long[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readLong();
}
return arr;
}
public float[] readFloats() throws IOException
{
final int length = readLength();
final float[] arr = new float[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readFloat();
}
return arr;
}
public double[] readDoubles() throws IOException
{
final int length = readLength();
final double[] arr = new double[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readDouble();
}
return arr;
}
public String[] readStrings() throws IOException
{
final int length = readLength();
final String[] arr = new String[length];
for (int i = 0; i < length; i++) {
arr[i] = in.readUTF();
}
return arr;
}
public Object[] readObjects() throws IOException
{
final int length = readLength();
final Object[] arr = new Object[length];
for (int i = 0; i < length; i++) {
arr[i] = readObject();
}
return arr;
}
/**
* Read bundle without a mark
*/
public IonDataBundle readBundle() throws IOException
{
final IonDataBundle ib = new IonDataBundle();
ib.load(this);
return ib;
}
/**
* Read bundle without a mark, load into a provided one
*/
public void readBundle(IonDataBundle filled) throws IOException
{
filled.clear();
filled.load(this);
}
private int readMark() throws IOException
{
return readIntByte();
}
private int readLength() throws IOException
{
return readInt();
}
/**
*
* Read object based on mark; if null mark is found, returns default value. *
** If, however, an object of invalid or different type is found, an * exception will be thrown. *
* * @param def default value. * @return the loaded object * @throws CorruptDataException */ @SuppressWarnings("unchecked") public