Further Ion improvements
This commit is contained in:
@@ -97,19 +97,20 @@ public class Ion {
|
||||
* Indicates whether range checking should be performed when registering
|
||||
* marks.
|
||||
*/
|
||||
private static boolean safety;
|
||||
private static boolean markRangeChecking;
|
||||
|
||||
static {
|
||||
// register default ionizables
|
||||
safety = false;
|
||||
markRangeChecking = false;
|
||||
|
||||
registerIonizable(Ion.DATA_BUNDLE, IonDataBundle.class);
|
||||
registerIonizable(Ion.DATA_LIST, IonDataList.class);
|
||||
safety = true;
|
||||
|
||||
markRangeChecking = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register new Ionizable for direct reconstructing.
|
||||
* Register new {@link Ionizable} for direct reconstructing.
|
||||
*
|
||||
* @param mark mark to be used. Numbers 0..99 are reserved.
|
||||
* @param objClass class of the registered Ionizable
|
||||
@@ -118,7 +119,7 @@ public class Ion {
|
||||
{
|
||||
// negative marks are allowed.
|
||||
|
||||
if (safety && mark >= 0 && mark < 100) {
|
||||
if (markRangeChecking && mark >= 0 && mark < 100) {
|
||||
throw new RuntimeException("Marks 0..99 are reserved.");
|
||||
}
|
||||
|
||||
@@ -131,7 +132,7 @@ public class Ion {
|
||||
|
||||
|
||||
/**
|
||||
* Load Ion object from file.
|
||||
* Load an object from file.
|
||||
*
|
||||
* @param file file
|
||||
* @return the loaded object
|
||||
@@ -151,7 +152,7 @@ public class Ion {
|
||||
|
||||
|
||||
/**
|
||||
* Load Ion object from stream.
|
||||
* Load an object from stream.
|
||||
*
|
||||
* @param in input stream
|
||||
* @return the loaded object
|
||||
@@ -164,7 +165,7 @@ public class Ion {
|
||||
|
||||
|
||||
/**
|
||||
* Write Ion object to a stream.
|
||||
* Write an object to a stream.
|
||||
*
|
||||
* @param out output stream
|
||||
* @param obj written object
|
||||
@@ -177,7 +178,7 @@ public class Ion {
|
||||
|
||||
|
||||
/**
|
||||
* Store Ion object to file.
|
||||
* Store an object to file.
|
||||
*
|
||||
* @param path file path
|
||||
* @param obj object to store
|
||||
@@ -198,7 +199,10 @@ public class Ion {
|
||||
|
||||
|
||||
/**
|
||||
* Read single object from input stream
|
||||
* Read single object from input stream, preceded by a mark. If a mark is
|
||||
* not present, the behavior is undefined - in case the read bytes happen to
|
||||
* match one of the registered marks, some garbage will be read. Otherwise
|
||||
* an exception will be be thrown.
|
||||
*
|
||||
* @param in input stream
|
||||
* @return the loaded object
|
||||
@@ -280,7 +284,7 @@ public class Ion {
|
||||
|
||||
|
||||
/**
|
||||
* Write single ionizable or primitive object to output stream
|
||||
* Write a single object to output stream, with a mark.
|
||||
*
|
||||
* @param out output stream
|
||||
* @param obj stored object
|
||||
@@ -444,66 +448,143 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
public static void writeBoolean(OutputStream out, boolean bool) throws IOException
|
||||
/**
|
||||
* Write a boolean (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param b boolean to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeBoolean(OutputStream out, boolean b) throws IOException
|
||||
{
|
||||
out.write(getBytesBool(bool));
|
||||
out.write(getBytesBool(b));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a byte (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param b byte to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeByte(OutputStream out, byte b) throws IOException
|
||||
{
|
||||
out.write(getBytesByte(b));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a char (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param c char to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeChar(OutputStream out, char c) throws IOException
|
||||
{
|
||||
out.write(getBytesChar(c));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a short (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param s short to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeShort(OutputStream out, short s) throws IOException
|
||||
{
|
||||
out.write(getBytesShort(s));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write an integer (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param i integer to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeInt(OutputStream out, int i) throws IOException
|
||||
{
|
||||
out.write(getBytesInt(i));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a long (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param l long to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeLong(OutputStream out, long l) throws IOException
|
||||
{
|
||||
out.write(getBytesLong(l));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a float (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param f float to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeFloat(OutputStream out, float f) throws IOException
|
||||
{
|
||||
out.write(getBytesFloat(f));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a double (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param d double to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeDouble(OutputStream out, double d) throws IOException
|
||||
{
|
||||
out.write(getBytesDouble(d));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a String (without a mark)
|
||||
*
|
||||
* @param out output stream
|
||||
* @param str String to write
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeString(OutputStream out, String str) throws IOException
|
||||
{
|
||||
out.write(getBytesString(str));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a boolean (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return boolean read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean readBoolean(InputStream in) throws IOException
|
||||
{
|
||||
return readByte(in) > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a byte (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return byte read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte readByte(InputStream in) throws IOException
|
||||
{
|
||||
int b = in.read();
|
||||
@@ -512,6 +593,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a char (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return char read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static char readChar(InputStream in) throws IOException
|
||||
{
|
||||
synchronized (ac) {
|
||||
@@ -522,6 +610,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a short (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return short read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static short readShort(InputStream in) throws IOException
|
||||
{
|
||||
synchronized (as) {
|
||||
@@ -532,6 +627,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a long (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return long read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static long readLong(InputStream in) throws IOException
|
||||
{
|
||||
synchronized (al) {
|
||||
@@ -542,6 +644,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read an integer (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return integer read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static int readInt(InputStream in) throws IOException
|
||||
{
|
||||
synchronized (ai) {
|
||||
@@ -552,6 +661,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a float (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return float read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static float readFloat(InputStream in) throws IOException
|
||||
{
|
||||
synchronized (af) {
|
||||
@@ -562,6 +678,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a double (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return double read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static double readDouble(InputStream in) throws IOException
|
||||
{
|
||||
synchronized (ad) {
|
||||
@@ -572,6 +695,13 @@ public class Ion {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a string (without a mark)
|
||||
*
|
||||
* @param in input stream
|
||||
* @return string read
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String readString(InputStream in) throws IOException
|
||||
{
|
||||
String s = "";
|
||||
|
||||
@@ -3,7 +3,8 @@ package mightypork.util.files.ion;
|
||||
|
||||
|
||||
/**
|
||||
* Ionizable HashMap
|
||||
* Ionizable HashMap<String, Object> with getters and setters for individual
|
||||
* supported types.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
@@ -11,140 +12,120 @@ public class IonDataBundle extends IonMap<String, Object> {
|
||||
|
||||
public boolean getBoolean(String key)
|
||||
{
|
||||
return (Boolean) get(key);
|
||||
}
|
||||
|
||||
|
||||
public boolean getBool(String key)
|
||||
{
|
||||
return (Boolean) get(key);
|
||||
return (Boolean) getOfType(key, Boolean.class);
|
||||
}
|
||||
|
||||
|
||||
public byte getByte(String key)
|
||||
{
|
||||
return (Byte) get(key);
|
||||
return (Byte) getOfType(key, Byte.class);
|
||||
}
|
||||
|
||||
|
||||
public char getChar(String key)
|
||||
{
|
||||
return (Character) get(key);
|
||||
return (Character) getOfType(key, Character.class);
|
||||
}
|
||||
|
||||
|
||||
public short getShort(String key)
|
||||
{
|
||||
return (Short) get(key);
|
||||
return (Short) getOfType(key, Short.class);
|
||||
}
|
||||
|
||||
|
||||
public int getInt(String key)
|
||||
{
|
||||
return (Integer) get(key);
|
||||
return (Integer) getOfType(key, Integer.class);
|
||||
}
|
||||
|
||||
|
||||
public long getLong(String key)
|
||||
{
|
||||
return (Long) get(key);
|
||||
return (Long) getOfType(key, Long.class);
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(String key)
|
||||
{
|
||||
return (Float) get(key);
|
||||
return (Float) getOfType(key, Float.class);
|
||||
}
|
||||
|
||||
|
||||
public double getDouble(String key)
|
||||
{
|
||||
return (Double) get(key);
|
||||
return (Double) getOfType(key, Double.class);
|
||||
}
|
||||
|
||||
|
||||
public String getString(String key)
|
||||
{
|
||||
return (String) get(key);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object get(Object arg0)
|
||||
{
|
||||
return super.get(arg0);
|
||||
return (String) getOfType(key, String.class);
|
||||
}
|
||||
|
||||
|
||||
public void putBoolean(String key, boolean num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putBool(String key, boolean num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putByte(String key, int num)
|
||||
{
|
||||
put(key, (byte) num);
|
||||
super.put(key, (byte) num);
|
||||
}
|
||||
|
||||
|
||||
public void putChar(String key, char num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putCharacter(String key, char num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putShort(String key, int num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putInt(String key, int num)
|
||||
{
|
||||
put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putInteger(String key, int num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putLong(String key, long num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putFloat(String key, double num)
|
||||
{
|
||||
put(key, (float) num);
|
||||
super.put(key, (float) num);
|
||||
}
|
||||
|
||||
|
||||
public void putDouble(String key, double num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
public void putString(String key, String num)
|
||||
{
|
||||
put(key, num);
|
||||
super.put(key, num);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Object getOfType(String key, Class<?> type)
|
||||
{
|
||||
final Object o = super.get(key);
|
||||
if (o == null || !o.getClass().isAssignableFrom(type)) {
|
||||
throw new RuntimeException("Incorrect object type");
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package mightypork.util.files.ion;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* Ionizable Arraylist
|
||||
@@ -10,153 +8,133 @@ import java.io.IOException;
|
||||
*/
|
||||
public class IonDataList extends IonList<Object> {
|
||||
|
||||
public Ionizable getIonizable(int index) throws IOException
|
||||
public Ionizable getIonizable(int index)
|
||||
{
|
||||
return (Ionizable) getOfType(index, Ionizable.class);
|
||||
}
|
||||
|
||||
|
||||
public boolean getBoolean(int index) throws IOException
|
||||
public boolean getBoolean(int index)
|
||||
{
|
||||
return (Boolean) getOfType(index, Boolean.class);
|
||||
}
|
||||
|
||||
|
||||
public byte getByte(int index) throws IOException
|
||||
public byte getByte(int index)
|
||||
{
|
||||
return (Byte) getOfType(index, Byte.class);
|
||||
}
|
||||
|
||||
|
||||
public char getChar(int index) throws IOException
|
||||
public char getChar(int index)
|
||||
{
|
||||
return (Character) getOfType(index, Character.class);
|
||||
}
|
||||
|
||||
|
||||
public short getShort(int index) throws IOException
|
||||
public short getShort(int index)
|
||||
{
|
||||
return (Short) getOfType(index, Short.class);
|
||||
}
|
||||
|
||||
|
||||
public int getInt(int index) throws IOException
|
||||
public int getInt(int index)
|
||||
{
|
||||
return (Integer) getOfType(index, Integer.class);
|
||||
}
|
||||
|
||||
|
||||
public long getLong(int index) throws IOException
|
||||
public long getLong(int index)
|
||||
{
|
||||
return (Long) getOfType(index, Long.class);
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(int index) throws IOException
|
||||
public float getFloat(int index)
|
||||
{
|
||||
return (Float) getOfType(index, Float.class);
|
||||
}
|
||||
|
||||
|
||||
public double getDouble(int index) throws IOException
|
||||
public double getDouble(int index)
|
||||
{
|
||||
return (Double) getOfType(index, Double.class);
|
||||
}
|
||||
|
||||
|
||||
public String getString(int index) throws IOException
|
||||
public String getString(int index)
|
||||
{
|
||||
return (String) getOfType(index, String.class);
|
||||
}
|
||||
|
||||
|
||||
public void addIonizable(Ionizable o) throws IOException
|
||||
public void addIonizable(Ionizable o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addBoolean(boolean o) throws IOException
|
||||
public void addBoolean(boolean o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addByte(byte o) throws IOException
|
||||
public void addByte(byte o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addChar(char o) throws IOException
|
||||
public void addChar(char o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addShort(short o) throws IOException
|
||||
public void addShort(short o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addInt(int o) throws IOException
|
||||
public void addInt(int o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addLong(long o) throws IOException
|
||||
public void addLong(long o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addFloat(float o) throws IOException
|
||||
public void addFloat(float o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addDouble(double o) throws IOException
|
||||
public void addDouble(double o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public void addString(String o) throws IOException
|
||||
public void addString(String o)
|
||||
{
|
||||
assertNotNull(o);
|
||||
add(o);
|
||||
super.add(o);
|
||||
}
|
||||
|
||||
|
||||
public Object getOfType(int index, Class<?> type) throws IOException
|
||||
public Object getOfType(int index, Class<?> type)
|
||||
{
|
||||
try {
|
||||
final Object o = super.get(index);
|
||||
if (o == null || !o.getClass().isAssignableFrom(type)) {
|
||||
throw new IOException("Incorrect object type");
|
||||
}
|
||||
return o;
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
throw new IOException("Out of bounds");
|
||||
final Object o = super.get(index);
|
||||
if (o == null || !o.getClass().isAssignableFrom(type)) {
|
||||
throw new RuntimeException("Incorrect object type");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void assertNotNull(Object o) throws IOException
|
||||
{
|
||||
if (o == null) throw new IOException("Cannot store null");
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user