diff options
Diffstat (limited to 'java/com/xagasoft/gats/GatsInputStream.java')
| -rw-r--r-- | java/com/xagasoft/gats/GatsInputStream.java | 96 |
1 files changed, 96 insertions, 0 deletions
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 @@ | |||
| 1 | package com.xagasoft.gats; | ||
| 2 | |||
| 3 | import java.io.InputStream; | ||
| 4 | import java.io.ByteArrayOutputStream; | ||
| 5 | import java.io.DataInputStream; | ||
| 6 | |||
| 7 | public class GatsInputStream | ||
| 8 | { | ||
| 9 | private InputStream is; | ||
| 10 | private ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| 11 | private int iVer = 0; | ||
| 12 | private int iSize; | ||
| 13 | |||
| 14 | public GatsInputStream( InputStream is ) | ||
| 15 | { | ||
| 16 | this.is = is; | ||
| 17 | } | ||
| 18 | |||
| 19 | public GatsObject readObject() | ||
| 20 | { | ||
| 21 | do | ||
| 22 | { | ||
| 23 | if( baos.size() < 5 ) | ||
| 24 | { | ||
| 25 | byte aBuf[] = new byte[5-baos.size()]; | ||
| 26 | int iRead = is.read( aBuf ); | ||
| 27 | baos.write( ret, iRead ); | ||
| 28 | |||
| 29 | if( baos.size() < 5 ) | ||
| 30 | return null; | ||
| 31 | } | ||
| 32 | } while( !skipReadNulls() ); | ||
| 33 | |||
| 34 | if( iVer == 0 ) | ||
| 35 | { | ||
| 36 | ByteArrayInputStream bais = new ByteArrayInputStream( | ||
| 37 | baos.toByteArray() | ||
| 38 | ); | ||
| 39 | DataInputStream dis( bais ); | ||
| 40 | iVer = dis.readUnsignedByte(); | ||
| 41 | iSize = dis.readInt(); | ||
| 42 | } | ||
| 43 | |||
| 44 | byte aBuf[] = new byte[1500]; | ||
| 45 | while( baos.size() < iSize ) | ||
| 46 | { | ||
| 47 | int iGoal = iSize-baos.size(); | ||
| 48 | if( iGoal > 1500 ) | ||
| 49 | iGoal = 1500; | ||
| 50 | |||
| 51 | int iRead = is.read( aBuf ); | ||
| 52 | baos.write( abuf, 0, iRead ); | ||
| 53 | |||
| 54 | if( iRead < iGoal ) | ||
| 55 | return null; | ||
| 56 | } | ||
| 57 | |||
| 58 | if( baos.size() < iSize ) | ||
| 59 | return null; | ||
| 60 | |||
| 61 | ByteArrayInputStream bais = new ByteArrayInputStream( | ||
| 62 | baos.toByteArray() | ||
| 63 | ); | ||
| 64 | bais.skip( 5 ); | ||
| 65 | |||
| 66 | GatsObject goRet = GatsObject::read( bais ); | ||
| 67 | iVer = 0; | ||
| 68 | |||
| 69 | return goRet; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | private bool skipReadNulls() | ||
| 74 | { | ||
| 75 | if( baos.size() == 0 ) | ||
| 76 | return false; | ||
| 77 | |||
| 78 | byte aBuf[] = baos.toByteArray(); | ||
| 79 | if( aBuf[0] != 0 ) | ||
| 80 | return true; | ||
| 81 | |||
| 82 | for( int j = 1; j < aBuf.size; j++ ) | ||
| 83 | { | ||
| 84 | if( aBuf[j] != 0 ) | ||
| 85 | { | ||
| 86 | baos.clear(); | ||
| 87 | baos.write( aBuf, j, aBuf.size-j ); | ||
| 88 | return true; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | baos.clear(); | ||
| 93 | return true; | ||
| 94 | } | ||
| 95 | }; | ||
| 96 | |||
