From 83cbf329465fd0f60179d471ef1cd0fcf54b9863 Mon Sep 17 00:00:00 2001 From: nagoya0 Date: Thu, 9 Aug 2012 18:48:18 +0900 Subject: [PATCH] sidestep Android compatibility issues. --- .../org/msgpack/io/LinkedBufferInput.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/msgpack/io/LinkedBufferInput.java b/src/main/java/org/msgpack/io/LinkedBufferInput.java index a8a3cc247..df78feb93 100644 --- a/src/main/java/org/msgpack/io/LinkedBufferInput.java +++ b/src/main/java/org/msgpack/io/LinkedBufferInput.java @@ -21,6 +21,7 @@ import java.io.EOFException; import java.util.LinkedList; import java.util.Iterator; +import java.util.NoSuchElementException; import java.nio.ByteBuffer; public class LinkedBufferInput extends AbstractInput { @@ -50,7 +51,7 @@ public int read(byte[] b, int off, int len) throws EOFException { } int olen = len; while (true) { - ByteBuffer bb = link.peekFirst(); + ByteBuffer bb = link.getFirst(); if (len < bb.remaining()) { bb.get(b, off, len); incrReadByteCount(len); @@ -69,7 +70,10 @@ public int read(byte[] b, int off, int len) throws EOFException { } public boolean tryRefer(BufferReferer ref, int len) throws IOException { - ByteBuffer bb = link.peekFirst(); + ByteBuffer bb = null; + try { + bb = link.getFirst(); + } catch(NoSuchElementException e) {} if (bb == null) { throw new EndOfBufferException(); } else if (bb.remaining() < len) { @@ -98,7 +102,10 @@ public boolean tryRefer(BufferReferer ref, int len) throws IOException { } public byte readByte() throws EOFException { - ByteBuffer bb = link.peekFirst(); + ByteBuffer bb = null; + try { + bb = link.getFirst(); + } catch(NoSuchElementException e) {} if (bb == null || bb.remaining() == 0) { throw new EndOfBufferException(); } @@ -117,7 +124,7 @@ public void advance() { int len = nextAdvance; ByteBuffer bb; while (true) { - bb = link.peekFirst(); + bb = link.getFirst(); if (len < bb.remaining()) { bb.position(bb.position() + len); break; @@ -169,7 +176,10 @@ private void requireMore(int n) throws EOFException { } private ByteBuffer require(int n) throws EOFException { - ByteBuffer bb = link.peekFirst(); + ByteBuffer bb = null; + try { + bb = link.getFirst(); + } catch(NoSuchElementException e) {} if (bb == null) { throw new EndOfBufferException(); } @@ -227,7 +237,7 @@ public void feed(byte[] b, int off, int len) { public void feed(byte[] b, int off, int len, boolean reference) { if (reference) { - if (writable > 0 && link.peekLast().remaining() == 0) { + if (writable > 0 && link.getLast().remaining() == 0) { link.add(link.size()-1, ByteBuffer.wrap(b, off, len)); return; } @@ -236,7 +246,10 @@ public void feed(byte[] b, int off, int len, boolean reference) { return; } - ByteBuffer bb = link.peekLast(); + ByteBuffer bb = null; + try { + bb = link.getLast(); + } catch(NoSuchElementException e) {} if (len <= writable) { int pos = bb.position(); bb.position(bb.limit()); @@ -273,7 +286,7 @@ public void feed(ByteBuffer b) { public void feed(ByteBuffer buf, boolean reference) { if (reference) { - if (writable > 0 && link.peekLast().remaining() == 0) { + if (writable > 0 && link.getLast().remaining() == 0) { link.add(link.size()-1, buf); return; } @@ -284,7 +297,10 @@ public void feed(ByteBuffer buf, boolean reference) { int rem = buf.remaining(); - ByteBuffer bb = link.peekLast(); + ByteBuffer bb = null; + try { + bb = link.getLast(); + } catch(NoSuchElementException e) {} if (rem <= writable) { int pos = bb.position(); bb.position(bb.limit());