Moved general purpose stuff into util, renamed utils->util
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* Ionizable Arraylist
|
||||
*
|
||||
* @author MightyPork
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractIonList<T> extends ArrayList<T> implements Ionizable {
|
||||
|
||||
@Override
|
||||
public void ionRead(InputStream in) throws IonException
|
||||
{
|
||||
try {
|
||||
while (true) {
|
||||
final byte b = BinaryUtils.readByte(in);
|
||||
|
||||
if (b == IonMarks.ENTRY) {
|
||||
final T value = (T) Ion.readObject(in);
|
||||
add(value);
|
||||
} else if (b == IonMarks.END) {
|
||||
break;
|
||||
} else {
|
||||
throw new IonException("Unexpected mark in IonList: " + Integer.toHexString(b));
|
||||
}
|
||||
}
|
||||
ionReadCustomData(in);
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Error reading ion list", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void ionWrite(OutputStream out) throws IonException
|
||||
{
|
||||
try {
|
||||
for (final T entry : this) {
|
||||
if (entry instanceof IonizableOptional && !((IonizableOptional) entry).ionShouldSave()) continue;
|
||||
BinaryUtils.writeByte(out, IonMarks.ENTRY);
|
||||
Ion.writeObject(out, entry);
|
||||
}
|
||||
BinaryUtils.writeByte(out, IonMarks.END);
|
||||
ionWriteCustomData(out);
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Error reading ion map", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read custom data of this AbstractIonList implementation
|
||||
*
|
||||
* @param in input stream
|
||||
*/
|
||||
public void ionReadCustomData(InputStream in)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write custom data of this AbstractIonList implementation
|
||||
*
|
||||
* @param out output stream
|
||||
*/
|
||||
public void ionWriteCustomData(OutputStream out)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public abstract byte ionMark();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
|
||||
/**
|
||||
* Ionizable HashMap
|
||||
*
|
||||
* @author MightyPork
|
||||
* @param <V>
|
||||
*/
|
||||
public abstract class AbstractIonMap<V> extends LinkedHashMap<String, V> implements Ionizable {
|
||||
|
||||
@Override
|
||||
public V get(Object key)
|
||||
{
|
||||
return super.get(key);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V put(String key, V value)
|
||||
{
|
||||
return super.put(key, value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void ionRead(InputStream in) throws IonException
|
||||
{
|
||||
try {
|
||||
while (true) {
|
||||
final byte b = BinaryUtils.readByte(in);
|
||||
if (b == IonMarks.ENTRY) {
|
||||
final String key = BinaryUtils.readString(in);
|
||||
|
||||
final V value = (V) Ion.readObject(in);
|
||||
put(key, value);
|
||||
|
||||
} else if (b == IonMarks.END) {
|
||||
break;
|
||||
} else {
|
||||
throw new RuntimeException("Unexpected mark in IonMap: " + Integer.toHexString(b));
|
||||
}
|
||||
}
|
||||
ionReadCustomData(in);
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Error reading ion map", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void ionWrite(OutputStream out) throws IonException
|
||||
{
|
||||
try {
|
||||
for (final java.util.Map.Entry<String, V> entry : entrySet()) {
|
||||
BinaryUtils.writeByte(out, IonMarks.ENTRY);
|
||||
BinaryUtils.writeString(out, entry.getKey());
|
||||
Ion.writeObject(out, entry.getValue());
|
||||
}
|
||||
BinaryUtils.writeByte(out, IonMarks.END);
|
||||
ionWriteCustomData(out);
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Error reading ion map", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read custom data of this AbstractIonMap implementation
|
||||
*
|
||||
* @param in input stream
|
||||
*/
|
||||
public void ionReadCustomData(InputStream in)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write custom data of this AbstractIonMap implementation
|
||||
*
|
||||
* @param out output stream
|
||||
*/
|
||||
public void ionWriteCustomData(OutputStream out)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte ionMark()
|
||||
{
|
||||
return IonMarks.MAP;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
|
||||
/**
|
||||
* Utilities to store and load objects to streams.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class BinaryUtils {
|
||||
|
||||
private static ByteBuffer bi = ByteBuffer.allocate(Integer.SIZE / 8);
|
||||
private static ByteBuffer bd = ByteBuffer.allocate(Double.SIZE / 8);
|
||||
private static ByteBuffer bf = ByteBuffer.allocate(Float.SIZE / 8);
|
||||
private static ByteBuffer bc = ByteBuffer.allocate(Character.SIZE / 8);
|
||||
private static ByteBuffer bl = ByteBuffer.allocate(Long.SIZE / 8);
|
||||
private static ByteBuffer bs = ByteBuffer.allocate(Short.SIZE / 8);
|
||||
|
||||
private static byte[] ai = new byte[Integer.SIZE / 8];
|
||||
private static byte[] ad = new byte[Double.SIZE / 8];
|
||||
private static byte[] af = new byte[Float.SIZE / 8];
|
||||
private static byte[] ac = new byte[Character.SIZE / 8];
|
||||
private static byte[] al = new byte[Long.SIZE / 8];
|
||||
private static byte[] as = new byte[Short.SIZE / 8];
|
||||
|
||||
|
||||
// CONVERSIONS
|
||||
|
||||
public static byte[] getBytesBool(boolean bool)
|
||||
{
|
||||
return new byte[] { (byte) (bool ? 1 : 0) };
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesByte(byte num)
|
||||
{
|
||||
return new byte[] { num };
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesChar(char num)
|
||||
{
|
||||
bc.clear();
|
||||
bc.putChar(num);
|
||||
return bc.array();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesShort(short num)
|
||||
{
|
||||
bs.clear();
|
||||
bs.putShort(num);
|
||||
return bs.array();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesInt(int num)
|
||||
{
|
||||
bi.clear();
|
||||
bi.putInt(num);
|
||||
return bi.array();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesLong(long num)
|
||||
{
|
||||
bl.clear();
|
||||
bl.putLong(num);
|
||||
return bl.array();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesFloat(float num)
|
||||
{
|
||||
bf.clear();
|
||||
bf.putFloat(num);
|
||||
return bf.array();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesDouble(double num)
|
||||
{
|
||||
bd.clear();
|
||||
bd.putDouble(num);
|
||||
return bd.array();
|
||||
}
|
||||
|
||||
|
||||
public static byte[] getBytesString(String str)
|
||||
{
|
||||
final char[] chars = str.toCharArray();
|
||||
|
||||
final ByteBuffer bstr = ByteBuffer.allocate((Character.SIZE / 8) * chars.length + (Character.SIZE / 8));
|
||||
for (final char c : chars) {
|
||||
bstr.putChar(c);
|
||||
}
|
||||
|
||||
bstr.putChar((char) 0);
|
||||
|
||||
return bstr.array();
|
||||
}
|
||||
|
||||
|
||||
public static void writeBoolean(OutputStream out, boolean num) throws IOException
|
||||
{
|
||||
out.write(getBytesBool(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeByte(OutputStream out, byte num) throws IOException
|
||||
{
|
||||
out.write(getBytesByte(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeChar(OutputStream out, char num) throws IOException
|
||||
{
|
||||
out.write(getBytesChar(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeShort(OutputStream out, short num) throws IOException
|
||||
{
|
||||
out.write(getBytesShort(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeInt(OutputStream out, int num) throws IOException
|
||||
{
|
||||
out.write(getBytesInt(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeLong(OutputStream out, long num) throws IOException
|
||||
{
|
||||
out.write(getBytesLong(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeFloat(OutputStream out, float num) throws IOException
|
||||
{
|
||||
out.write(getBytesFloat(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeDouble(OutputStream out, double num) throws IOException
|
||||
{
|
||||
out.write(getBytesDouble(num));
|
||||
}
|
||||
|
||||
|
||||
public static void writeString(OutputStream out, String str) throws IOException
|
||||
{
|
||||
out.write(getBytesString(str));
|
||||
}
|
||||
|
||||
|
||||
// READING
|
||||
|
||||
public static boolean readBoolean(InputStream in) throws IOException
|
||||
{
|
||||
return in.read() > 0;
|
||||
}
|
||||
|
||||
|
||||
public static byte readByte(InputStream in) throws IOException
|
||||
{
|
||||
return (byte) in.read();
|
||||
}
|
||||
|
||||
|
||||
public static char readChar(InputStream in) throws IOException
|
||||
{
|
||||
if (-1 == in.read(ac, 0, ac.length)) throw new IOException("End of stream.");
|
||||
final ByteBuffer buf = ByteBuffer.wrap(ac);
|
||||
return buf.getChar();
|
||||
}
|
||||
|
||||
|
||||
public static short readShort(InputStream in) throws IOException
|
||||
{
|
||||
if (-1 == in.read(as, 0, as.length)) throw new IOException("End of stream.");
|
||||
final ByteBuffer buf = ByteBuffer.wrap(as);
|
||||
return buf.getShort();
|
||||
}
|
||||
|
||||
|
||||
public static long readLong(InputStream in) throws IOException
|
||||
{
|
||||
if (-1 == in.read(al, 0, al.length)) throw new IOException("End of stream.");
|
||||
|
||||
final ByteBuffer buf = ByteBuffer.wrap(al);
|
||||
return buf.getLong();
|
||||
}
|
||||
|
||||
|
||||
public static int readInt(InputStream in) throws IOException
|
||||
{
|
||||
if (-1 == in.read(ai, 0, ai.length)) throw new IOException("End of stream.");
|
||||
final ByteBuffer buf = ByteBuffer.wrap(ai);
|
||||
return buf.getInt();
|
||||
}
|
||||
|
||||
|
||||
public static float readFloat(InputStream in) throws IOException
|
||||
{
|
||||
if (-1 == in.read(af, 0, af.length)) throw new IOException("End of stream.");
|
||||
final ByteBuffer buf = ByteBuffer.wrap(af);
|
||||
return buf.getFloat();
|
||||
}
|
||||
|
||||
|
||||
public static double readDouble(InputStream in) throws IOException
|
||||
{
|
||||
if (-1 == in.read(ad, 0, ad.length)) throw new IOException("End of stream.");
|
||||
final ByteBuffer buf = ByteBuffer.wrap(ad);
|
||||
return buf.getDouble();
|
||||
}
|
||||
|
||||
|
||||
public static String readString(InputStream in) throws IOException
|
||||
{
|
||||
String s = "";
|
||||
char c;
|
||||
while ((c = readChar(in)) > 0) {
|
||||
s += c;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import mightypork.util.math.Calc;
|
||||
|
||||
|
||||
/**
|
||||
* Universal data storage system
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class Ion {
|
||||
|
||||
/** Ionizables<Mark, Class> */
|
||||
private static Map<Byte, Class<?>> customIonizables = new HashMap<>();
|
||||
|
||||
// register default ionizables
|
||||
static {
|
||||
try {
|
||||
registerIonizable(IonMarks.MAP, IonMap.class);
|
||||
registerIonizable(IonMarks.LIST, IonList.class);
|
||||
} catch (final IonException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register new Ionizable for direct reconstructing.
|
||||
*
|
||||
* @param mark byte mark to be used, see {@link IonMarks} for reference.
|
||||
* @param objClass class of the registered Ionizable
|
||||
* @throws IonException
|
||||
*/
|
||||
public static void registerIonizable(byte mark, Class<?> objClass) throws IonException
|
||||
{
|
||||
if (customIonizables.containsKey(mark)) {
|
||||
throw new IonException("IonMark " + mark + " is already used.");
|
||||
}
|
||||
customIonizables.put(mark, objClass);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load Ion object from file.
|
||||
*
|
||||
* @param file file path
|
||||
* @return the loaded object
|
||||
* @throws IonException
|
||||
*/
|
||||
public static Object fromFile(String file) throws IonException
|
||||
{
|
||||
return fromFile(new File(file));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load Ion object from file.
|
||||
*
|
||||
* @param file file
|
||||
* @return the loaded object
|
||||
* @throws IonException on failure
|
||||
*/
|
||||
public static Object fromFile(File file) throws IonException
|
||||
{
|
||||
try (InputStream in = new FileInputStream(file)) {
|
||||
|
||||
final Object obj = fromStream(in);
|
||||
return obj;
|
||||
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Error loading ION file.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load Ion object from stream.
|
||||
*
|
||||
* @param in input stream
|
||||
* @return the loaded object
|
||||
* @throws IonException
|
||||
*/
|
||||
public static Object fromStream(InputStream in) throws IonException
|
||||
{
|
||||
return readObject(in);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store Ion object to file.
|
||||
*
|
||||
* @param path file path
|
||||
* @param obj object to store
|
||||
* @throws IonException
|
||||
*/
|
||||
public static void toFile(String path, Object obj) throws IonException
|
||||
{
|
||||
toFile(new File(path), obj);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store Ion object to file.
|
||||
*
|
||||
* @param path file path
|
||||
* @param obj object to store
|
||||
* @throws IonException
|
||||
*/
|
||||
public static void toFile(File path, Object obj) throws IonException
|
||||
{
|
||||
try (OutputStream out = new FileOutputStream(path)) {
|
||||
final String f = path.toString();
|
||||
final File dir = new File(f.substring(0, f.lastIndexOf(File.separator)));
|
||||
|
||||
if (!dir.mkdirs()) throw new IOException("Could not create file.");
|
||||
|
||||
toStream(out, obj);
|
||||
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (final Exception e) {
|
||||
throw new IonException("Error writing to ION file.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store Ion object to output stream.
|
||||
*
|
||||
* @param out output stream *
|
||||
* @param obj object to store
|
||||
* @throws IonException
|
||||
*/
|
||||
public static void toStream(OutputStream out, Object obj) throws IonException
|
||||
{
|
||||
writeObject(out, obj);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read single ionizable or primitive object from input stream
|
||||
*
|
||||
* @param in input stream
|
||||
* @return the loaded object
|
||||
* @throws IonException
|
||||
*/
|
||||
public static Object readObject(InputStream in) throws IonException
|
||||
{
|
||||
try {
|
||||
final int bi = in.read();
|
||||
if (bi == -1) throw new IonException("Unexpected end of stream.");
|
||||
final byte b = (byte) bi;
|
||||
if (customIonizables.containsKey(b)) {
|
||||
Ionizable ion;
|
||||
try {
|
||||
ion = ((Ionizable) customIonizables.get(b).newInstance());
|
||||
} catch (final InstantiationException e) {
|
||||
throw new IonException("Cound not instantiate " + customIonizables.get(b).getSimpleName(), e);
|
||||
} catch (final IllegalAccessException e) {
|
||||
throw new IonException("Cound not instantiate " + customIonizables.get(b).getSimpleName(), e);
|
||||
}
|
||||
ion.ionRead(in);
|
||||
return ion;
|
||||
}
|
||||
|
||||
switch (b) {
|
||||
case IonMarks.BOOLEAN:
|
||||
return BinaryUtils.readBoolean(in);
|
||||
case IonMarks.BYTE:
|
||||
return BinaryUtils.readByte(in);
|
||||
case IonMarks.CHAR:
|
||||
return BinaryUtils.readChar(in);
|
||||
case IonMarks.SHORT:
|
||||
return BinaryUtils.readShort(in);
|
||||
case IonMarks.INT:
|
||||
return BinaryUtils.readInt(in);
|
||||
case IonMarks.LONG:
|
||||
return BinaryUtils.readLong(in);
|
||||
case IonMarks.FLOAT:
|
||||
return BinaryUtils.readFloat(in);
|
||||
case IonMarks.DOUBLE:
|
||||
return BinaryUtils.readDouble(in);
|
||||
case IonMarks.STRING:
|
||||
final String s = BinaryUtils.readString(in);
|
||||
return s;
|
||||
default:
|
||||
throw new IonException("Invalid Ion mark " + Integer.toHexString(bi));
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Error loading ION file: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write single ionizable or primitive object to output stream
|
||||
*
|
||||
* @param out output stream
|
||||
* @param obj stored object
|
||||
* @throws IonException
|
||||
*/
|
||||
public static void writeObject(OutputStream out, Object obj) throws IonException
|
||||
{
|
||||
try {
|
||||
if (obj instanceof Ionizable) {
|
||||
out.write(((Ionizable) obj).ionMark());
|
||||
((Ionizable) obj).ionWrite(out);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Boolean) {
|
||||
out.write(IonMarks.BOOLEAN);
|
||||
BinaryUtils.writeBoolean(out, (Boolean) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Byte) {
|
||||
out.write(IonMarks.BYTE);
|
||||
BinaryUtils.writeByte(out, (Byte) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Character) {
|
||||
out.write(IonMarks.CHAR);
|
||||
BinaryUtils.writeChar(out, (Character) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Short) {
|
||||
out.write(IonMarks.SHORT);
|
||||
BinaryUtils.writeShort(out, (Short) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Integer) {
|
||||
out.write(IonMarks.INT);
|
||||
BinaryUtils.writeInt(out, (Integer) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Long) {
|
||||
out.write(IonMarks.LONG);
|
||||
BinaryUtils.writeLong(out, (Long) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Float) {
|
||||
out.write(IonMarks.FLOAT);
|
||||
BinaryUtils.writeFloat(out, (Float) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof Double) {
|
||||
out.write(IonMarks.DOUBLE);
|
||||
BinaryUtils.writeDouble(out, (Double) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj instanceof String) {
|
||||
out.write(IonMarks.STRING);
|
||||
BinaryUtils.writeString(out, (String) obj);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new IonException(Calc.cname(obj) + " can't be stored in Ion storage.");
|
||||
|
||||
} catch (final IOException e) {
|
||||
throw new IonException("Could not store " + obj, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
public class IonException extends Exception {
|
||||
|
||||
public IonException() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
public IonException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
|
||||
public IonException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
||||
public IonException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
/**
|
||||
* Ionizable Arraylist
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class IonList extends AbstractIonList<Object> {
|
||||
|
||||
public Ionizable getIonizable(int index) throws IonException
|
||||
{
|
||||
return (Ionizable) getCheckType(index, Ionizable.class);
|
||||
}
|
||||
|
||||
|
||||
public boolean getBoolean(int index) throws IonException
|
||||
{
|
||||
return (Boolean) getCheckType(index, Boolean.class);
|
||||
}
|
||||
|
||||
|
||||
public byte getByte(int index) throws IonException
|
||||
{
|
||||
return (Byte) getCheckType(index, Byte.class);
|
||||
}
|
||||
|
||||
|
||||
public char getChar(int index) throws IonException
|
||||
{
|
||||
return (Character) getCheckType(index, Character.class);
|
||||
}
|
||||
|
||||
|
||||
public short getShort(int index) throws IonException
|
||||
{
|
||||
return (Short) getCheckType(index, Short.class);
|
||||
}
|
||||
|
||||
|
||||
public int getInt(int index) throws IonException
|
||||
{
|
||||
return (Integer) getCheckType(index, Integer.class);
|
||||
}
|
||||
|
||||
|
||||
public long getLong(int index) throws IonException
|
||||
{
|
||||
return (Long) getCheckType(index, Long.class);
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(int index) throws IonException
|
||||
{
|
||||
return (Float) getCheckType(index, Float.class);
|
||||
}
|
||||
|
||||
|
||||
public double getDouble(int index) throws IonException
|
||||
{
|
||||
return (Double) getCheckType(index, Double.class);
|
||||
}
|
||||
|
||||
|
||||
public String getString(int index) throws IonException
|
||||
{
|
||||
return (String) getCheckType(index, String.class);
|
||||
}
|
||||
|
||||
|
||||
public void addIonizable(Ionizable o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addBoolean(boolean o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addByte(byte o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addChar(char o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addShort(short o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addInt(int o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addLong(long o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addFloat(float o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addDouble(double o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public void addString(String o) throws IonException
|
||||
{
|
||||
assertNotNull(o);
|
||||
}
|
||||
|
||||
|
||||
public Object getCheckType(int index, Class<?> type) throws IonException
|
||||
{
|
||||
try {
|
||||
final Object o = super.get(index);
|
||||
if (o == null || !o.getClass().isAssignableFrom(type)) {
|
||||
throw new IonException("Incorrect object type");
|
||||
}
|
||||
return o;
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
throw new IonException("Out of bounds");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void assertNotNull(Object o) throws IonException
|
||||
{
|
||||
if (o == null) throw new IonException("Cannot store null");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte ionMark()
|
||||
{
|
||||
return IonMarks.LIST;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
/**
|
||||
* Ionizable HashMap
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class IonMap extends AbstractIonMap<Object> {
|
||||
|
||||
public boolean getBoolean(String key)
|
||||
{
|
||||
return (Boolean) get(key);
|
||||
}
|
||||
|
||||
|
||||
public boolean getBool(String key)
|
||||
{
|
||||
return (Boolean) get(key);
|
||||
}
|
||||
|
||||
|
||||
public byte getByte(String key)
|
||||
{
|
||||
return (Byte) get(key);
|
||||
}
|
||||
|
||||
|
||||
public char getChar(String key)
|
||||
{
|
||||
return (Character) get(key);
|
||||
}
|
||||
|
||||
|
||||
public short getShort(String key)
|
||||
{
|
||||
return (Short) get(key);
|
||||
}
|
||||
|
||||
|
||||
public int getInt(String key)
|
||||
{
|
||||
return (Integer) get(key);
|
||||
}
|
||||
|
||||
|
||||
public long getLong(String key)
|
||||
{
|
||||
return (Long) get(key);
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(String key)
|
||||
{
|
||||
return (Float) get(key);
|
||||
}
|
||||
|
||||
|
||||
public double getDouble(String key)
|
||||
{
|
||||
return (Double) get(key);
|
||||
}
|
||||
|
||||
|
||||
public String getString(String key)
|
||||
{
|
||||
return (String) get(key);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object get(Object arg0)
|
||||
{
|
||||
return super.get(arg0);
|
||||
}
|
||||
|
||||
|
||||
public void putBoolean(String key, boolean num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putBool(String key, boolean num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putByte(String key, int num)
|
||||
{
|
||||
put(key, (byte) num);
|
||||
}
|
||||
|
||||
|
||||
public void putChar(String key, char num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putCharacter(String key, char num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putShort(String key, int num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putInt(String key, int num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putInteger(String key, int num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putLong(String key, long num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putFloat(String key, double num)
|
||||
{
|
||||
put(key, (float) num);
|
||||
}
|
||||
|
||||
|
||||
public void putDouble(String key, double num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putString(String key, String num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte ionMark()
|
||||
{
|
||||
return IonMarks.MAP;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
/**
|
||||
* Byte marks used to structure data in Ion files.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class IonMarks {
|
||||
|
||||
/** Null value */
|
||||
public static final byte NULL = 0;
|
||||
|
||||
/** Boolean value */
|
||||
public static final byte BOOLEAN = 1;
|
||||
|
||||
/** Byte value */
|
||||
public static final byte BYTE = 2;
|
||||
|
||||
/** Character value */
|
||||
public static final byte CHAR = 3;
|
||||
|
||||
/** Short value */
|
||||
public static final byte SHORT = 4;
|
||||
|
||||
/** Integer value */
|
||||
public static final byte INT = 5;
|
||||
|
||||
/** Long value */
|
||||
public static final byte LONG = 6;
|
||||
|
||||
/** Float value */
|
||||
public static final byte FLOAT = 7;
|
||||
|
||||
/** Double value */
|
||||
public static final byte DOUBLE = 8;
|
||||
|
||||
/** String value */
|
||||
public static final byte STRING = 9;
|
||||
|
||||
/** List value (begin) - contains entries, ends with END */
|
||||
public static final byte LIST = 10;
|
||||
|
||||
/** Map value (begin) - contains entries, ends with END */
|
||||
public static final byte MAP = 11;
|
||||
|
||||
/**
|
||||
* List / Map entry<br>
|
||||
* In list directly followed by entry value. In map followed by (string) key
|
||||
* and the entry value.
|
||||
*/
|
||||
public static final byte ENTRY = 12;
|
||||
|
||||
/** End of List / Map */
|
||||
public static final byte END = 13;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
||||
/**
|
||||
* Object that can be saved to and loaded from Ion file.<br>
|
||||
* All classes implementing Ionizable must be registered to {@link Ion} using
|
||||
* Ion.registerIonizable(obj.class).
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface Ionizable {
|
||||
|
||||
/**
|
||||
* Load data from the input stream. Mark has already been read, begin
|
||||
* reading right after it.
|
||||
*
|
||||
* @param in input stream
|
||||
* @throws IonException
|
||||
*/
|
||||
public void ionRead(InputStream in) throws IonException;
|
||||
|
||||
|
||||
/**
|
||||
* Store data to output stream. mark has already been written, begin right
|
||||
* after it.
|
||||
*
|
||||
* @param out Output stream
|
||||
* @throws IonException
|
||||
*/
|
||||
public void ionWrite(OutputStream out) throws IonException;
|
||||
|
||||
|
||||
/**
|
||||
* Get Ion mark byte.
|
||||
*
|
||||
* @return Ion mark byte.
|
||||
*/
|
||||
public byte ionMark();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
|
||||
/**
|
||||
* Optional ionizable
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface IonizableOptional extends Ionizable {
|
||||
|
||||
/**
|
||||
* Get if this ionizable should be saved to a list
|
||||
*
|
||||
* @return should save
|
||||
*/
|
||||
public boolean ionShouldSave();
|
||||
}
|
||||
Reference in New Issue
Block a user