Initial commit

This commit is contained in:
Flare Microsystems
2022-02-03 20:04:49 -08:00
commit a97769e934
36 changed files with 758 additions and 0 deletions
@@ -0,0 +1,35 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSBinaryDataType<T> extends SDSObject<byte[]>{
public SDSBinaryDataType()
{
}
public SDSBinaryDataType(byte[] data)
{
this.value = data;
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeInt(value.length);
out.write(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException, SDSUndefinedException
{
value = new byte[in.readInt()];
return this;
}
@Override
public int getByteLength() {
return 4 + value.length;
}
}
@@ -0,0 +1,32 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSBooleanType extends SDSObject<Boolean>{
public SDSBooleanType() {
super();
}
public SDSBooleanType(Boolean value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeBoolean(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readBoolean();
return this;
}
@Override
public int getByteLength() {
return 1;
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSByteType extends SDSObject<Byte>{
public SDSByteType() {
super();
}
public SDSByteType(Byte value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeByte(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readByte();
return this;
}
@Override
public int getByteLength() {
return 1;
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSCharType extends SDSObject<Character>{
public SDSCharType() {
super();
}
public SDSCharType(Character value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeChar(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readChar();
return this;
}
@Override
public int getByteLength() {
return 2;
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSDoubleType extends SDSObject<Double>{
public SDSDoubleType() {
super();
}
public SDSDoubleType(Double value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeDouble(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readDouble();
return this;
}
@Override
public int getByteLength() {
return 8;
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSFloatType extends SDSObject<Float>{
public SDSFloatType() {
super();
}
public SDSFloatType(Float value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeFloat(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readFloat();
return this;
}
@Override
public int getByteLength() {
return 4;
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSIntType extends SDSObject<Integer>{
public SDSIntType() {
super();
}
public SDSIntType(Integer value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeInt(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readInt();
return this;
}
@Override
public int getByteLength() {
return 4;
}
}
@@ -0,0 +1,14 @@
package com.flaremicrosystems.sds;
public class SDSInvalidSizeException extends RuntimeException {
public SDSInvalidSizeException(String string) {
super(string);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
@@ -0,0 +1,71 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class SDSListType<T> extends SDSObject<ArrayList<SDSObject<T>>> implements Iterable<T> {
public SDSListType() {
this.value = new ArrayList<SDSObject<T>>();
}
public void store(DataOutputStream out) throws IOException {
super.store(out);
out.writeInt(value.size());
Iterator<SDSObject<T>> keyIter = value.iterator();
while (keyIter.hasNext()) {
SDSObject<T> k = keyIter.next();
k.store(out);
}
}
@SuppressWarnings("unchecked")
public SDSObject<?> load(DataInputStream in) throws IOException {
int len = in.readInt();
for (int i = 0; i < len; i++) {
try {
SDSObject<?> value = SDSObject.readObject(in);
this.value.add((SDSObject<T>) value);
} catch (SDSUndefinedException e) {
e.printStackTrace();
}
}
return this;
}
@Override
public int getByteLength() {
int length = 4;
for (SDSObject<?> obj : value)
length += OPCODE_BYTES + LENGTH_BYTES + obj.getByteLength();
return length;
}
public void add(SDSObject<T> val) {
value.add(val);
}
@SuppressWarnings("unchecked")
public Iterator<T> iterator() {
return (Iterator<T>) value.iterator();
}
public int size() {
return value.size();
}
public SDSObject<T> get(int index) {
return value.get(index);
}
public T[] asArray(T[] container) {
for(int i = 0; i < container.length && i < value.size(); i++)
{
container[i] = value.get(i).value;
}
return container;
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSLongType extends SDSObject<Long>{
public SDSLongType() {
super();
}
public SDSLongType(Long value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeLong(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readLong();
return this;
}
@Override
public int getByteLength() {
return 8;
}
}
@@ -0,0 +1,191 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.TreeMap;
public class SDSNameMapType extends SDSObject<TreeMap<String, SDSObject<?>>>{
public SDSNameMapType()
{
this.value = new TreeMap<String, SDSObject<?>>();
}
public Boolean getBooleanValue(String key, Boolean fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSBooleanType))
return fallback;
else return (Boolean) obj.value;
}
public Byte getByteValue(String key, Byte fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSByteType))
return fallback;
else return (Byte) obj.value;
}
public Short getShortValue(String key, Short fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSShortType))
return fallback;
else return (Short) obj.value;
}
public Integer getIntValue(String key, Integer fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSIntType))
return fallback;
else return (Integer) obj.value;
}
public Character getCharacterValue(String key, Character fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSCharType))
return fallback;
else return (Character) obj.value;
}
public Long getLongValue(String key, Long fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSLongType))
return fallback;
else return (Long) obj.value;
}
public Float getFloatValue(String key, Float fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSFloatType))
return fallback;
else return (Float) obj.value;
}
public Double getDoubleValue(String key, Double fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSDoubleType))
return fallback;
else return (Double) obj.value;
}
public String getStringValue(String key, String fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSStringType))
return fallback;
else return (String) obj.value;
}
public SDSListType<?> getArrayValue(String key, SDSListType<?> fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSListType))
return fallback;
else return (SDSListType<?>)obj;
}
public SDSNameMapType getNameMapping(String key, SDSNameMapType fallback)
{
SDSObject<?> obj = this.value.get(key);
if(obj == null || !(obj instanceof SDSNameMapType))
return fallback;
else return (SDSNameMapType)obj;
}
public SDSObject<?> getSDSObject(String key)
{
return this.value.get(key);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeInt(value.size());
Iterator<String> keyIter = value.keySet().iterator();
while(keyIter.hasNext())
{
String k = keyIter.next();
SDSObject<?> v = value.get(k);
out.writeUTF(k);
v.store(out);
}
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
int len = in.readInt();
for(int i = 0; i < len; i++)
{
String key = in.readUTF();
try {
SDSObject<?> value = SDSObject.readObject(in);
this.value.put(key, value);
} catch (SDSUndefinedException e) {
e.printStackTrace();
}
}
return this;
}
public void put(String name, SDSObject<?> sdsObject) {
if(sdsObject == null)
value.remove(name);
else value.put(name, sdsObject);
}
@Override
public int getByteLength() {
int length = 4;
Iterator<String> keyIter = value.keySet().iterator();
while(keyIter.hasNext())
{
String k = keyIter.next();
try {
length += k.getBytes("UTF-8").length;
} catch (UnsupportedEncodingException e) {
length += k.getBytes().length;
}
SDSObject<?> v = value.get(k);
length += OPCODE_BYTES + LENGTH_BYTES + v.getByteLength();
}
return length;
}
public void putBoolean(String name, boolean val) {
this.put(name, new SDSBooleanType(val));
}
public void putByte(String name, byte val) {
this.put(name, new SDSByteType(val));
}
public void putShort(String name, short val) {
this.put(name, new SDSShortType(val));
}
public void putChar(String name, char val) {
this.put(name, new SDSCharType(val));
}
public void putInt(String name, int val) {
this.put(name, new SDSIntType(val));
}
public void putLong(String name, long val) {
this.put(name, new SDSLongType(val));
}
public void putFloat(String name, float val) {
this.put(name, new SDSFloatType(val));
}
public void putDouble(String name, double val) {
this.put(name, new SDSDoubleType(val));
}
public boolean hasOfType(String name, Class<?> tester) {
return this.value.containsKey(name) && tester.isInstance(this.value.get(name));
}
}
@@ -0,0 +1,13 @@
package com.flaremicrosystems.sds;
public class SDSNullType<T> extends SDSObject<T>{
@Override
public int getByteLength() {
return 0;
}
@Override
public boolean isNull()
{
return true;
}
}
@@ -0,0 +1,98 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public abstract class SDSObject<T> {
T value;
static ArrayList<Integer> ids = new ArrayList<Integer>();
static ArrayList<Class<?>> objects = new ArrayList<Class<?>>();
static ArrayList<Class<?>> dataTypes = new ArrayList<Class<?>>();
static final int OPCODE_BYTES = 4;
static final int LENGTH_BYTES = 4;
static {
registerDataStore(0, SDSBooleanType.class, Boolean.class);
registerDataStore(1, SDSByteType.class, Byte.class);
registerDataStore(2, SDSShortType.class, Short.class);
registerDataStore(3, SDSIntType.class, Integer.class);
registerDataStore(4, SDSCharType.class, Character.class);
registerDataStore(5, SDSLongType.class, Long.class);
registerDataStore(6, SDSFloatType.class, Float.class);
registerDataStore(7, SDSDoubleType.class, Double.class);
registerDataStore(8, SDSStringType.class, String.class);
registerDataStore(9, SDSListType.class, ArrayList.class);
registerDataStore(10, SDSNameMapType.class, HashMap.class);
registerDataStore(11, SDSBinaryDataType.class, byte[].class);
registerDataStore(12, SDSNullType.class, null);
}
public SDSObject() {
}
public SDSObject(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public boolean isNull()
{
return false;
}
public abstract int getByteLength();
public void setValue(T newVal) {
value = newVal;
}
@Override
public String toString()
{
return this.value.toString();
}
public void store(DataOutputStream out) throws IOException {
int id = ids.get(objects.indexOf(this.getClass()));
out.writeInt(id);
int length = getByteLength();
out.writeInt(length);
}
public static void registerDataStore(int id, Class<?> obj, Class<?> dataType) {
ids.add(id);
objects.add(obj);
dataTypes.add(dataType);
}
public SDSObject<?> load(DataInputStream in) throws IOException, SDSUndefinedException {
return this;
}
public static SDSObject<?> readObject(DataInputStream in) throws IOException, SDSUndefinedException {
int id = in.readInt();
int length = in.readInt();
if (!ids.contains(id))
{
in.read(new byte[length]);
throw new SDSUndefinedException("Unknown identifier: " + id);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
Class<SDSObject> obj = (Class<SDSObject>) objects.get(ids.get(id));
try {
return obj.newInstance().load(in);
} catch (InstantiationException e) {
e.printStackTrace();
throw new SDSTotalFailureException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new SDSTotalFailureException(e);
}
}
}
@@ -0,0 +1,31 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SDSShortType extends SDSObject<Short>{
public SDSShortType() {
super();
}
public SDSShortType(Short value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeShort(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readShort();
return this;
}
@Override
public int getByteLength() {
return 2;
}
}
@@ -0,0 +1,36 @@
package com.flaremicrosystems.sds;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class SDSStringType extends SDSObject<String>{
public SDSStringType() {
super();
}
public SDSStringType(String value) {
super(value);
}
public void store(DataOutputStream out) throws IOException
{
super.store(out);
out.writeUTF(value);
}
public SDSObject<?> load(DataInputStream in) throws IOException
{
value = in.readUTF();
return this;
}
@Override
public int getByteLength() {
try {
return value.getBytes("UTF-8").length;
} catch (UnsupportedEncodingException e) {
return value.getBytes().length;
}
}
}
@@ -0,0 +1,14 @@
package com.flaremicrosystems.sds;
public class SDSTotalFailureException extends RuntimeException {
public SDSTotalFailureException(Exception e) {
super(e);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
@@ -0,0 +1,14 @@
package com.flaremicrosystems.sds;
public class SDSUndefinedException extends Exception {
public SDSUndefinedException(String string) {
super(string);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}