From e3efaf2a9ad82deb1644ccab8c1469719a0c5b65 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 16 Jan 2012 02:31:34 +0000 Subject: Lots of documentation, an example program, and also some visibility cleanup. --- java/com/xagasoft/gats/GatsInputStream.java | 110 ++++++---------------------- 1 file changed, 24 insertions(+), 86 deletions(-) (limited to 'java/com/xagasoft/gats/GatsInputStream.java') diff --git a/java/com/xagasoft/gats/GatsInputStream.java b/java/com/xagasoft/gats/GatsInputStream.java index 5cd830d..2417018 100644 --- a/java/com/xagasoft/gats/GatsInputStream.java +++ b/java/com/xagasoft/gats/GatsInputStream.java @@ -1,14 +1,32 @@ package com.xagasoft.gats; import java.io.InputStream; -import java.io.ByteArrayOutputStream; -import java.io.ByteArrayInputStream; import java.io.DataInputStream; +/** + * Facilitates reading GatsObjects from an InputStream. This doesn't really + * inherit from InputStream, so maybe it would make more sense to call it + * something else. Use the readObject function to read an entire object from + * the InputStream. + *

+ * At the moment this class will require all data to be available in continuous + * read operations from teh provided InputStream. This means that only complete + * packets can be read from files on the disk, or that if a socket is provided + * it is in blocking or synchronous I/O mode. In java, this should rarely be + * an issue. + *

+ * Each call to readObject returns a new GatsObject read from the InputStream or + * null if nothing else could be read. While reading, all zero bytes discovered + * in between packets will be considered padding and will be ignored. + *

+ * Like with the GatsOutputStream, there are generally many small reads + * performed during a single readObject operation, so it is a good idea to + * provide some sort of buffered input stream. + *@see com.xagasoft.gats.GatsOutputStream + */ public class GatsInputStream { private InputStream is; - private ByteArrayOutputStream baos = new ByteArrayOutputStream(); private int iVer = 0; private int iSize; @@ -17,6 +35,9 @@ public class GatsInputStream this.is = is; } + /** + * Reads an object from the input and returns it. + */ public GatsObject readObject() throws java.io.IOException { do @@ -34,88 +55,5 @@ public class GatsInputStream return null; } - - /* - public GatsObject readObject() throws java.io.IOException - { - do - { - if( baos.size() < 5 ) - { - byte aBuf[] = new byte[5-baos.size()]; - int iRead = is.read( aBuf ); - baos.write( aBuf, 0, iRead ); - - if( baos.size() < 5 ) - return null; - } - } while( !skipReadNulls() ); - - if( iVer == 0 ) - { - ByteArrayInputStream bais = new ByteArrayInputStream( - baos.toByteArray() - ); - DataInputStream dis = new DataInputStream( 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, 0, iGoal ); - baos.write( aBuf, 0, iRead ); - - if( iRead < iGoal ) - return null; - } - - if( baos.size() < iSize ) - return null; - - byte aTmp[] = baos.toByteArray(); - ByteArrayInputStream bais = new ByteArrayInputStream( - aTmp - ); - bais.skip( 5 ); - - GatsObject goRet = GatsObject.read( bais ); - - baos.reset(); - baos.write( aTmp, iSize, aTmp.length-iSize ); - - iVer = 0; - - return goRet; - } - - private boolean skipReadNulls() - { - if( baos.size() == 0 ) - return false; - - byte aBuf[] = baos.toByteArray(); - if( aBuf[0] != 0 ) - return true; - - for( int j = 1; j < aBuf.length; j++ ) - { - if( aBuf[j] != 0 ) - { - baos.reset(); - baos.write( aBuf, j, aBuf.length-j ); - return true; - } - } - - baos.reset(); - return true; - } - */ }; -- cgit v1.2.3