diff options
Diffstat (limited to 'java/com/xagasoft/gats/GatsInputStream.java')
-rw-r--r-- | java/com/xagasoft/gats/GatsInputStream.java | 110 |
1 files changed, 24 insertions, 86 deletions
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 @@ | |||
1 | package com.xagasoft.gats; | 1 | package com.xagasoft.gats; |
2 | 2 | ||
3 | import java.io.InputStream; | 3 | import java.io.InputStream; |
4 | import java.io.ByteArrayOutputStream; | ||
5 | import java.io.ByteArrayInputStream; | ||
6 | import java.io.DataInputStream; | 4 | import java.io.DataInputStream; |
7 | 5 | ||
6 | /** | ||
7 | * Facilitates reading GatsObjects from an InputStream. This doesn't really | ||
8 | * inherit from InputStream, so maybe it would make more sense to call it | ||
9 | * something else. Use the readObject function to read an entire object from | ||
10 | * the InputStream. | ||
11 | * <p> | ||
12 | * At the moment this class will require all data to be available in continuous | ||
13 | * read operations from teh provided InputStream. This means that only complete | ||
14 | * packets can be read from files on the disk, or that if a socket is provided | ||
15 | * it is in blocking or synchronous I/O mode. In java, this should rarely be | ||
16 | * an issue. | ||
17 | * <p> | ||
18 | * Each call to readObject returns a new GatsObject read from the InputStream or | ||
19 | * null if nothing else could be read. While reading, all zero bytes discovered | ||
20 | * in between packets will be considered padding and will be ignored. | ||
21 | * <p> | ||
22 | * Like with the GatsOutputStream, there are generally many small reads | ||
23 | * performed during a single readObject operation, so it is a good idea to | ||
24 | * provide some sort of buffered input stream. | ||
25 | *@see com.xagasoft.gats.GatsOutputStream | ||
26 | */ | ||
8 | public class GatsInputStream | 27 | public class GatsInputStream |
9 | { | 28 | { |
10 | private InputStream is; | 29 | private InputStream is; |
11 | private ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
12 | private int iVer = 0; | 30 | private int iVer = 0; |
13 | private int iSize; | 31 | private int iSize; |
14 | 32 | ||
@@ -17,6 +35,9 @@ public class GatsInputStream | |||
17 | this.is = is; | 35 | this.is = is; |
18 | } | 36 | } |
19 | 37 | ||
38 | /** | ||
39 | * Reads an object from the input and returns it. | ||
40 | */ | ||
20 | public GatsObject readObject() throws java.io.IOException | 41 | public GatsObject readObject() throws java.io.IOException |
21 | { | 42 | { |
22 | do | 43 | do |
@@ -34,88 +55,5 @@ public class GatsInputStream | |||
34 | 55 | ||
35 | return null; | 56 | return null; |
36 | } | 57 | } |
37 | |||
38 | /* | ||
39 | public GatsObject readObject() throws java.io.IOException | ||
40 | { | ||
41 | do | ||
42 | { | ||
43 | if( baos.size() < 5 ) | ||
44 | { | ||
45 | byte aBuf[] = new byte[5-baos.size()]; | ||
46 | int iRead = is.read( aBuf ); | ||
47 | baos.write( aBuf, 0, iRead ); | ||
48 | |||
49 | if( baos.size() < 5 ) | ||
50 | return null; | ||
51 | } | ||
52 | } while( !skipReadNulls() ); | ||
53 | |||
54 | if( iVer == 0 ) | ||
55 | { | ||
56 | ByteArrayInputStream bais = new ByteArrayInputStream( | ||
57 | baos.toByteArray() | ||
58 | ); | ||
59 | DataInputStream dis = new DataInputStream( bais ); | ||
60 | iVer = dis.readUnsignedByte(); | ||
61 | iSize = dis.readInt(); | ||
62 | } | ||
63 | |||
64 | byte aBuf[] = new byte[1500]; | ||
65 | while( baos.size() < iSize ) | ||
66 | { | ||
67 | int iGoal = iSize-baos.size(); | ||
68 | if( iGoal > 1500 ) | ||
69 | iGoal = 1500; | ||
70 | |||
71 | int iRead = is.read( aBuf, 0, iGoal ); | ||
72 | baos.write( aBuf, 0, iRead ); | ||
73 | |||
74 | if( iRead < iGoal ) | ||
75 | return null; | ||
76 | } | ||
77 | |||
78 | if( baos.size() < iSize ) | ||
79 | return null; | ||
80 | |||
81 | byte aTmp[] = baos.toByteArray(); | ||
82 | ByteArrayInputStream bais = new ByteArrayInputStream( | ||
83 | aTmp | ||
84 | ); | ||
85 | bais.skip( 5 ); | ||
86 | |||
87 | GatsObject goRet = GatsObject.read( bais ); | ||
88 | |||
89 | baos.reset(); | ||
90 | baos.write( aTmp, iSize, aTmp.length-iSize ); | ||
91 | |||
92 | iVer = 0; | ||
93 | |||
94 | return goRet; | ||
95 | } | ||
96 | |||
97 | private boolean skipReadNulls() | ||
98 | { | ||
99 | if( baos.size() == 0 ) | ||
100 | return false; | ||
101 | |||
102 | byte aBuf[] = baos.toByteArray(); | ||
103 | if( aBuf[0] != 0 ) | ||
104 | return true; | ||
105 | |||
106 | for( int j = 1; j < aBuf.length; j++ ) | ||
107 | { | ||
108 | if( aBuf[j] != 0 ) | ||
109 | { | ||
110 | baos.reset(); | ||
111 | baos.write( aBuf, j, aBuf.length-j ); | ||
112 | return true; | ||
113 | } | ||
114 | } | ||
115 | |||
116 | baos.reset(); | ||
117 | return true; | ||
118 | } | ||
119 | */ | ||
120 | }; | 58 | }; |
121 | 59 | ||