1111 * Stream reader for PGP objects
1212 */
1313public class BCPGInputStream
14- extends InputStream implements PacketTags
14+ extends InputStream
15+ implements PacketTags
1516{
1617 /**
1718 * If the argument is a {@link BCPGInputStream}, return it.
@@ -29,19 +30,19 @@ public static BCPGInputStream wrap(InputStream in)
2930 return new BCPGInputStream (in );
3031 }
3132
32- InputStream in ;
33- boolean next = false ;
34- int nextB ;
33+ InputStream in ;
34+ boolean next = false ;
35+ int nextB ;
3536
36- boolean mNext = false ;
37- int mNextB ;
37+ boolean mNext = false ;
38+ int mNextB ;
3839
3940 public BCPGInputStream (
40- InputStream in )
41+ InputStream in )
4142 {
4243 this .in = in ;
4344 }
44-
45+
4546 public int available ()
4647 throws IOException
4748 {
@@ -113,9 +114,9 @@ public int read(
113114 }
114115
115116 public void readFully (
116- byte [] buf ,
117- int off ,
118- int len )
117+ byte [] buf ,
118+ int off ,
119+ int len )
119120 throws IOException
120121 {
121122 if (Streams .readFully (this , buf , off , len ) < len )
@@ -131,7 +132,7 @@ public byte[] readAll()
131132 }
132133
133134 public void readFully (
134- byte [] buf )
135+ byte [] buf )
135136 throws IOException
136137 {
137138 readFully (buf , 0 , buf .length );
@@ -141,7 +142,6 @@ public void readFully(
141142 * Obtains the tag of the next packet in the stream.
142143 *
143144 * @return the {@link PacketTags tag number}.
144- *
145145 * @throws IOException if an error occurs reading the tag from the stream.
146146 */
147147 public int nextPacketTag ()
@@ -176,12 +176,13 @@ public int nextPacketTag()
176176
177177 /**
178178 * Reads the next packet from the stream.
179+ *
179180 * @throws IOException
180181 */
181182 public Packet readPacket ()
182183 throws IOException
183184 {
184- int hdr = this .read ();
185+ int hdr = this .read ();
185186
186187 if (hdr < 0 )
187188 {
@@ -193,36 +194,17 @@ public Packet readPacket()
193194 throw new IOException ("invalid header encountered" );
194195 }
195196
196- boolean newPacket = (hdr & 0x40 ) != 0 ;
197- int tag = 0 ;
198- int bodyLen = 0 ;
199- boolean partial = false ;
197+ boolean newPacket = (hdr & 0x40 ) != 0 ;
198+ int tag = 0 ;
199+ int bodyLen = 0 ;
200+ boolean partial = false ;
200201
201202 if (newPacket )
202203 {
203204 tag = hdr & 0x3f ;
204-
205- int l = this .read ();
206-
207- if (l < 192 )
208- {
209- bodyLen = l ;
210- }
211- else if (l <= 223 )
212- {
213- int b = this .read ();
214-
215- bodyLen = ((l - 192 ) << 8 ) + (b ) + 192 ;
216- }
217- else if (l == 255 )
218- {
219- bodyLen = StreamUtil .read4OctetLength (this );
220- }
221- else
222- {
223- partial = true ;
224- bodyLen = 1 << (l & 0x1f );
225- }
205+ boolean [] flags = new boolean [3 ];
206+ bodyLen = StreamUtil .readBodyLen (this , this , flags );
207+ partial = flags [2 ];
226208 }
227209 else
228210 {
@@ -249,7 +231,7 @@ else if (l == 255)
249231 }
250232 }
251233
252- BCPGInputStream objStream ;
234+ BCPGInputStream objStream ;
253235
254236 if (bodyLen == 0 && partial )
255237 {
@@ -260,7 +242,7 @@ else if (l == 255)
260242 objStream = new BCPGInputStream (
261243 new BufferedInputStream (new PartialInputStream (this , partial , bodyLen )));
262244 }
263-
245+
264246 switch (tag )
265247 {
266248 case RESERVED :
@@ -314,9 +296,9 @@ else if (l == 255)
314296 }
315297
316298 /**
317- * @deprecated use skipMarkerAndPaddingPackets
318299 * @return the tag for the next non-marker/padding packet
319300 * @throws IOException on a parsing issue.
301+ * @deprecated use skipMarkerAndPaddingPackets
320302 */
321303 public int skipMarkerPackets ()
322304 throws IOException
@@ -326,6 +308,7 @@ public int skipMarkerPackets()
326308
327309 /**
328310 * skip any marker and padding packets found in the stream.
311+ *
329312 * @return the tag for the next non-marker/padding packet
330313 * @throws IOException on a parsing issue.
331314 */
@@ -334,7 +317,7 @@ public int skipMarkerAndPaddingPackets()
334317 {
335318 int tag ;
336319 while ((tag = nextPacketTag ()) == PacketTags .MARKER
337- || tag == PacketTags .PADDING )
320+ || tag == PacketTags .PADDING )
338321 {
339322 readPacket ();
340323 }
@@ -350,20 +333,20 @@ public void close()
350333
351334 /**
352335 * a stream that overlays our input stream, allowing the user to only read a segment of it.
353- *
336+ * <p>
354337 * NB: dataLength will be negative if the segment length is in the upper range above 2**31.
355338 */
356339 private static class PartialInputStream
357340 extends InputStream
358341 {
359- private BCPGInputStream in ;
360- private boolean partial ;
361- private int dataLength ;
342+ private BCPGInputStream in ;
343+ private boolean partial ;
344+ private int dataLength ;
362345
363346 PartialInputStream (
364- BCPGInputStream in ,
365- boolean partial ,
366- int dataLength )
347+ BCPGInputStream in ,
348+ boolean partial ,
349+ int dataLength )
367350 {
368351 this .in = in ;
369352 this .partial = partial ;
@@ -392,32 +375,13 @@ public int available()
392375 private int loadDataLength ()
393376 throws IOException
394377 {
395- int l = in . read () ;
396-
397- if (l < 0 )
378+ boolean [] flags = new boolean [ 3 ] ;
379+ dataLength = StreamUtil . readBodyLen ( in , in , flags );
380+ if (flags [ 0 ] )
398381 {
399382 return -1 ;
400383 }
401-
402- partial = false ;
403- if (l < 192 )
404- {
405- dataLength = l ;
406- }
407- else if (l <= 223 )
408- {
409- dataLength = ((l - 192 ) << 8 ) + (in .read ()) + 192 ;
410- }
411- else if (l == 255 )
412- {
413- dataLength = StreamUtil .read4OctetLength (in );
414- }
415- else
416- {
417- partial = true ;
418- dataLength = 1 << (l & 0x1f );
419- }
420-
384+ partial = flags [2 ];
421385 return dataLength ;
422386 }
423387
0 commit comments