From 49216a230c331791b06df1527c3245c88e991c20 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 21 Apr 2011 17:19:19 +0000 Subject: Gats for java is getting closer. --- java/com/xagasoft/gats/Boolean.java | 6 +- java/com/xagasoft/gats/Dictionary.java | 6 +- java/com/xagasoft/gats/Float.java | 6 +- java/com/xagasoft/gats/GatsInputStream.java | 96 ++++++++++++++++++++++++++++ java/com/xagasoft/gats/GatsObject.java | 62 ++++++++++++++++++ java/com/xagasoft/gats/GatsOutputStream.java | 7 ++ java/com/xagasoft/gats/GatsStream.java | 6 -- java/com/xagasoft/gats/Integer.java | 6 +- java/com/xagasoft/gats/List.java | 6 +- java/com/xagasoft/gats/String.java | 6 +- java/com/xagasoft/gats/Type.java | 16 ----- 11 files changed, 183 insertions(+), 40 deletions(-) create mode 100644 java/com/xagasoft/gats/GatsInputStream.java create mode 100644 java/com/xagasoft/gats/GatsObject.java create mode 100644 java/com/xagasoft/gats/GatsOutputStream.java delete mode 100644 java/com/xagasoft/gats/GatsStream.java delete mode 100644 java/com/xagasoft/gats/Type.java (limited to 'java/com') diff --git a/java/com/xagasoft/gats/Boolean.java b/java/com/xagasoft/gats/Boolean.java index eb83ecd..692247a 100644 --- a/java/com/xagasoft/gats/Boolean.java +++ b/java/com/xagasoft/gats/Boolean.java @@ -1,10 +1,10 @@ package com.xagasoft.gats; -public class Boolean extends Type +public class Boolean extends GatsObject { - public int getType() + public int getGatsObject() { - return Type.BOOLEAN; + return GatsObject.BOOLEAN; } }; diff --git a/java/com/xagasoft/gats/Dictionary.java b/java/com/xagasoft/gats/Dictionary.java index 1166899..d96ff76 100644 --- a/java/com/xagasoft/gats/Dictionary.java +++ b/java/com/xagasoft/gats/Dictionary.java @@ -1,10 +1,10 @@ package com.xagasoft.gats; -public class Dictionary extends Type +public class Dictionary extends GatsObject { - public int getType() + public int getGatsObject() { - return Type.DICTIONARY; + return GatsObject.DICTIONARY; } }; diff --git a/java/com/xagasoft/gats/Float.java b/java/com/xagasoft/gats/Float.java index 60d5cf0..d0a9604 100644 --- a/java/com/xagasoft/gats/Float.java +++ b/java/com/xagasoft/gats/Float.java @@ -1,10 +1,10 @@ package com.xagasoft.gats; -public class Float extends Type +public class Float extends GatsObject { - public int getType() + public int getGatsObject() { - return Type.FLOAT; + return GatsObject.FLOAT; } }; diff --git a/java/com/xagasoft/gats/GatsInputStream.java b/java/com/xagasoft/gats/GatsInputStream.java new file mode 100644 index 0000000..e787d56 --- /dev/null +++ b/java/com/xagasoft/gats/GatsInputStream.java @@ -0,0 +1,96 @@ +package com.xagasoft.gats; + +import java.io.InputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; + +public class GatsInputStream +{ + private InputStream is; + private ByteArrayOutputStream baos = new ByteArrayOutputStream(); + private int iVer = 0; + private int iSize; + + public GatsInputStream( InputStream is ) + { + this.is = is; + } + + public GatsObject readObject() + { + do + { + if( baos.size() < 5 ) + { + byte aBuf[] = new byte[5-baos.size()]; + int iRead = is.read( aBuf ); + baos.write( ret, iRead ); + + if( baos.size() < 5 ) + return null; + } + } while( !skipReadNulls() ); + + if( iVer == 0 ) + { + ByteArrayInputStream bais = new ByteArrayInputStream( + baos.toByteArray() + ); + DataInputStream dis( bais ); + iVer = dis.readUnsignedByte(); + iSize = dis.readInt(); + } + + byte aBuf[] = new byte[1500]; + while( baos.size() < iSize ) + { + int iGoal = iSize-baos.size(); + if( iGoal > 1500 ) + iGoal = 1500; + + int iRead = is.read( aBuf ); + baos.write( abuf, 0, iRead ); + + if( iRead < iGoal ) + return null; + } + + if( baos.size() < iSize ) + return null; + + ByteArrayInputStream bais = new ByteArrayInputStream( + baos.toByteArray() + ); + bais.skip( 5 ); + + GatsObject goRet = GatsObject::read( bais ); + iVer = 0; + + return goRet; + } + + + private bool skipReadNulls() + { + if( baos.size() == 0 ) + return false; + + byte aBuf[] = baos.toByteArray(); + if( aBuf[0] != 0 ) + return true; + + for( int j = 1; j < aBuf.size; j++ ) + { + if( aBuf[j] != 0 ) + { + baos.clear(); + baos.write( aBuf, j, aBuf.size-j ); + return true; + } + } + + baos.clear(); + return true; + } +}; + diff --git a/java/com/xagasoft/gats/GatsObject.java b/java/com/xagasoft/gats/GatsObject.java new file mode 100644 index 0000000..63fcf36 --- /dev/null +++ b/java/com/xagasoft/gats/GatsObject.java @@ -0,0 +1,62 @@ +package com.xagasoft.gats; + +import java.io.InputStream; + +public abstract class GatsObject +{ + public final static int INTEGER = 1; + public final static int FLOAT = 2; + public final static int STRING = 3; + public final static int LIST = 4; + public final static int DICTIONARY = 5; + public final static int BOOLEAN = 6; + + public abstract int getType(); + + public abstract void read( InputStream is, char cType ); + + public static GatsObject read( InputStream is ) + { + char type = is.read(); + GatsObject goRet; + switch( type ) + { + case 'i': + goRet = new Integer(); + break; + + case 's': + goRet = new String(); + break; + + case '0': + case '1': + goRet = new Boolean(); + break; + + case 'l': + goRet = new List(); + break; + + case 'd': + goRet = new Dictionary(); + break; + + case 'f': + case 'F': + goRet = new Float(); + break; + + case 'e': + return null; + + default: + throw new Exception("Invalid gats type discovered: " + (char)type ); + } + + goRet.read( is, type ); + + return goRet; + } +}; + diff --git a/java/com/xagasoft/gats/GatsOutputStream.java b/java/com/xagasoft/gats/GatsOutputStream.java new file mode 100644 index 0000000..f3020f4 --- /dev/null +++ b/java/com/xagasoft/gats/GatsOutputStream.java @@ -0,0 +1,7 @@ +package com.xagasoft.gats; + +public class GatsOutputStream +{ + public GatsOutputStream( +}; + diff --git a/java/com/xagasoft/gats/GatsStream.java b/java/com/xagasoft/gats/GatsStream.java deleted file mode 100644 index 0cbd775..0000000 --- a/java/com/xagasoft/gats/GatsStream.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.xagasoft.gats; - -public class GatsStream -{ -}; - diff --git a/java/com/xagasoft/gats/Integer.java b/java/com/xagasoft/gats/Integer.java index 7da12d4..ab3d6de 100644 --- a/java/com/xagasoft/gats/Integer.java +++ b/java/com/xagasoft/gats/Integer.java @@ -3,11 +3,11 @@ package com.xagasoft.gats; import java.io.OutputStream; import java.io.InputStream; -public class Integer extends Type +public class Integer extends GatsObject { - public int getType() + public int getGatsObject() { - return Type.INTEGER; + return GatsObject.INTEGER; }; /** diff --git a/java/com/xagasoft/gats/List.java b/java/com/xagasoft/gats/List.java index 99d10e9..dbc729b 100644 --- a/java/com/xagasoft/gats/List.java +++ b/java/com/xagasoft/gats/List.java @@ -1,10 +1,10 @@ package com.xagasoft.gats; -public class List extends Type +public class List extends GatsObject { - public int getType() + public int getGatsObject() { - return Type.LIST; + return GatsObject.LIST; } }; diff --git a/java/com/xagasoft/gats/String.java b/java/com/xagasoft/gats/String.java index b89d042..301a98a 100644 --- a/java/com/xagasoft/gats/String.java +++ b/java/com/xagasoft/gats/String.java @@ -1,10 +1,10 @@ package com.xagasoft.gats; -public class String extends Type +public class String extends GatsObject { - public int getType() + public int getGatsObject() { - return Type.STRING; + return GatsObject.STRING; } }; diff --git a/java/com/xagasoft/gats/Type.java b/java/com/xagasoft/gats/Type.java deleted file mode 100644 index d6bff4d..0000000 --- a/java/com/xagasoft/gats/Type.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xagasoft.gats; - -public abstract class Type -{ - public final static int INTEGER = 1; - public final static int FLOAT = 2; - public final static int STRING = 3; - public final static int LIST = 4; - public final static int DICTIONARY = 5; - public final static int BOOLEAN = 6; - - public abstract int getType(); - - -}; - -- cgit v1.2.3