2121import java .io .EOFException ;
2222import java .util .LinkedList ;
2323import java .util .Iterator ;
24+ import java .util .NoSuchElementException ;
2425import java .nio .ByteBuffer ;
2526
2627public class LinkedBufferInput extends AbstractInput {
@@ -50,7 +51,7 @@ public int read(byte[] b, int off, int len) throws EOFException {
5051 }
5152 int olen = len ;
5253 while (true ) {
53- ByteBuffer bb = link .peekFirst ();
54+ ByteBuffer bb = link .getFirst ();
5455 if (len < bb .remaining ()) {
5556 bb .get (b , off , len );
5657 incrReadByteCount (len );
@@ -69,7 +70,10 @@ public int read(byte[] b, int off, int len) throws EOFException {
6970 }
7071
7172 public boolean tryRefer (BufferReferer ref , int len ) throws IOException {
72- ByteBuffer bb = link .peekFirst ();
73+ ByteBuffer bb = null ;
74+ try {
75+ bb = link .getFirst ();
76+ } catch (NoSuchElementException e ) {}
7377 if (bb == null ) {
7478 throw new EndOfBufferException ();
7579 } else if (bb .remaining () < len ) {
@@ -98,7 +102,10 @@ public boolean tryRefer(BufferReferer ref, int len) throws IOException {
98102 }
99103
100104 public byte readByte () throws EOFException {
101- ByteBuffer bb = link .peekFirst ();
105+ ByteBuffer bb = null ;
106+ try {
107+ bb = link .getFirst ();
108+ } catch (NoSuchElementException e ) {}
102109 if (bb == null || bb .remaining () == 0 ) {
103110 throw new EndOfBufferException ();
104111 }
@@ -117,7 +124,7 @@ public void advance() {
117124 int len = nextAdvance ;
118125 ByteBuffer bb ;
119126 while (true ) {
120- bb = link .peekFirst ();
127+ bb = link .getFirst ();
121128 if (len < bb .remaining ()) {
122129 bb .position (bb .position () + len );
123130 break ;
@@ -169,7 +176,10 @@ private void requireMore(int n) throws EOFException {
169176 }
170177
171178 private ByteBuffer require (int n ) throws EOFException {
172- ByteBuffer bb = link .peekFirst ();
179+ ByteBuffer bb = null ;
180+ try {
181+ bb = link .getFirst ();
182+ } catch (NoSuchElementException e ) {}
173183 if (bb == null ) {
174184 throw new EndOfBufferException ();
175185 }
@@ -227,7 +237,7 @@ public void feed(byte[] b, int off, int len) {
227237
228238 public void feed (byte [] b , int off , int len , boolean reference ) {
229239 if (reference ) {
230- if (writable > 0 && link .peekLast ().remaining () == 0 ) {
240+ if (writable > 0 && link .getLast ().remaining () == 0 ) {
231241 link .add (link .size ()-1 , ByteBuffer .wrap (b , off , len ));
232242 return ;
233243 }
@@ -236,7 +246,10 @@ public void feed(byte[] b, int off, int len, boolean reference) {
236246 return ;
237247 }
238248
239- ByteBuffer bb = link .peekLast ();
249+ ByteBuffer bb = null ;
250+ try {
251+ bb = link .getLast ();
252+ } catch (NoSuchElementException e ) {}
240253 if (len <= writable ) {
241254 int pos = bb .position ();
242255 bb .position (bb .limit ());
@@ -273,7 +286,7 @@ public void feed(ByteBuffer b) {
273286
274287 public void feed (ByteBuffer buf , boolean reference ) {
275288 if (reference ) {
276- if (writable > 0 && link .peekLast ().remaining () == 0 ) {
289+ if (writable > 0 && link .getLast ().remaining () == 0 ) {
277290 link .add (link .size ()-1 , buf );
278291 return ;
279292 }
@@ -284,7 +297,10 @@ public void feed(ByteBuffer buf, boolean reference) {
284297
285298 int rem = buf .remaining ();
286299
287- ByteBuffer bb = link .peekLast ();
300+ ByteBuffer bb = null ;
301+ try {
302+ bb = link .getLast ();
303+ } catch (NoSuchElementException e ) {}
288304 if (rem <= writable ) {
289305 int pos = bb .position ();
290306 bb .position (bb .limit ());
0 commit comments