Ghidra is a software reverse engineering (SRE) framework
GP-7358 Refactor LinkedByteBuffer
caheckmandea986359246
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/utils/SlaFormat.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/utils/SlaFormat.javaindex 9951d6ae28..7c50d9433b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/utils/SlaFormat.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/utils/SlaFormat.java@@ -39,9 +39,9 @@ public class SlaFormat {public static final int FORMAT_VERSION = 4;/**- * Absolute limit on the number of bytes in a .sla file+ * Absolute limit on the number of bytes in the (uncompressed) .sla file*/- public static final int MAX_FILE_SIZE = 1 << 24; // 16 Megabytes+ public static final long MAX_FILE_SIZE = 1L << 31; // 2 Gigabytes// Attributes// ATTRIB_CONTENT = 1 is reserved@@ -261,7 +261,7 @@ public class SlaFormat {throw new IOException("Missing SLA format header");}PackedDecode decoder = new PackedDecode();- decoder.open(MAX_FILE_SIZE, ".sla file loader");+ decoder.open(MAX_FILE_SIZE, sleighFile.getName() + " (uncompressed)");try (InflaterInputStream inflaterStream = new InflaterInputStream(stream)) {decoder.ingestStream(inflaterStream);diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ByteIngest.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ByteIngest.javaindex e10e8ede5a..dbd7545304 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ByteIngest.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ByteIngest.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -34,7 +34,7 @@ public interface ByteIngest {* @param max is the maximum number of bytes that can be read* @param desc is the description of the byte source*/- public void open(int max, String desc);+ public void open(long max, String desc);/*** Ingest bytes from the stream up to (and including) the first 0 byte. This can be calleddiff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.javaindex 4f7b455929..cbb26793d0 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java@@ -17,6 +17,7 @@ package ghidra.program.model.pcode;import java.io.IOException;import java.io.InputStream;+import java.nio.charset.StandardCharsets;/*** A byte buffer that is stored as a linked list of pages. Each page holds BUFFER_SIZE bytes.@@ -29,12 +30,11 @@ public class LinkedByteBuffer {/*** An iterator into the byte buffer*/- public static class Position {- public LinkedByteBuffer buffer; // The buffer object- public ArrayIter seqIter; // Linked-list node of the current page- public byte[] array; // The byte data of the current page- public int length; // Number of bytes stored in the array- public int current; // Position within page of current byte+ public class Position {+ private ArrayIter seqIter; // Linked-list node of the current page+ private byte[] array; // The byte data of the current page+ private int length; // Number of bytes stored in the array+ private int current; // Position within page of current byte/*** Set this to be a copy of another Position@@ -65,7 +65,7 @@ public class LinkedByteBuffer {if (plus1 == length) {ArrayIter iter = seqIter.next;if (iter == null) {- iter = buffer.readNextPage(seqIter);+ iter = readNextPage(seqIter);}return iter.array[0];}@@ -84,7 +84,7 @@ public class LinkedByteBuffer {return res;}if (seqIter.next == null) {- seqIter = buffer.readNextPage(seqIter);+ seqIter = readNextPage(seqIter);}else {seqIter = seqIter.next;@@ -104,7 +104,7 @@ public class LinkedByteBuffer {while (length - current <= skip) {skip -= (length - current);if (seqIter.next == null) {- seqIter = buffer.readNextPage(seqIter);+ seqIter = readNextPage(seqIter);}else {seqIter = seqIter.next;@@ -115,34 +115,73 @@ public class LinkedByteBuffer {}current += skip;}++ /**+ * Get the next set of number of bytes as a UTF-8 encoded string, advancing this position.+ * @param numBytes is the number of bytes to consume+ * @return the String+ * @throws DecoderException is the end of stream is reached+ */+ public String getNextString(int numBytes) throws DecoderException {+ int curLen = length - current;+ if (curLen >= numBytes) {+ String res = new String(array, current, numBytes, StandardCharsets.UTF_8);+ advancePosition(numBytes);+ return res;+ }+ int size = curLen;+ byte[] buf = new byte[numBytes];+ System.arraycopy(array, current, buf, 0, curLen);+ numBytes -= curLen;+ advancePosition(curLen);+ while (numBytes > 0) {+ curLen = length - current;+ if (curLen > numBytes) {+ curLen = numBytes;+ }+ System.arraycopy(array, current, buf, size, curLen);+ size += curLen;+ numBytes -= curLen;+ advancePosition(curLen);+ }+ return new String(buf, 0, size, StandardCharsets.UTF_8);+ }++ /**+ * Set this position to the start of the buffer+ */+ public void setToStart() {+ array = initialBuffer.array;+ length = initialBuffer.length;+ current = 0;+ seqIter = initialBuffer;+ }}/*** A linked-list page node*/public static class ArrayIter {- public ArrayIter next; // The next-node in the list- public byte[] array; // Byte data contained in this page- public int length; // Number of bytes stored in the buffer+ private ArrayIter next; // The next-node in the list+ private byte[] array; // Byte data contained in this page+ private int length; // Number of bytes stored in the buffer}public final static int BUFFER_SIZE = 1024;private ArrayIter initialBuffer; // The initial page in the linked list- private int byteCount; // Total number of bytes cached- private int maxCount; // Maximum bytes that can be cached+ private long byteCount; // Total number of bytes cached+ private long maxCount; // Maximum bytes that can be cachedprivate ArrayIter currentBuffer; // Last page in the linked listprivate int currentPos; // Last byte successfully cached in last page- private int padValue; // Padding dataprivate InputStream asNeededStream = null; // If non-null, a stream that is read as neededprivate String description; // Describes source of bytes for use in error messages- public LinkedByteBuffer(int max, int pad, String desc) {+ public LinkedByteBuffer(long max, String desc) {initialBuffer = new ArrayIter();currentBuffer = initialBuffer;byteCount = 0;currentPos = 0;- padValue = pad;maxCount = max;initialBuffer.array = new byte[BUFFER_SIZE];initialBuffer.length = BUFFER_SIZE;@@ -151,7 +190,7 @@ public class LinkedByteBuffer {}/**- * Close the "as needed" stream, if configure.+ * Close the "as needed" stream, if configured.* @throws IOException for problems closing the stream*/public void close() throws IOException {@@ -169,16 +208,16 @@ public class LinkedByteBuffer {*/public void ingestStreamAsNeeded(InputStream stream, Position start) throws IOException {asNeededStream = stream;- currentPos = readPage(stream, initialBuffer);+ readToEndOfPage(stream);if (currentPos < BUFFER_SIZE) {endIngest();}- getStartPosition(start);+ start.setToStart();initialBuffer = null; // Let garbage collection pick up pages that are already parsed}/**- * Ingest stream up to the first 0 byte or until maxCount bytes is reached.+ * Ingest stream up to the first 0 byte or until maxCount bytes or end of stream is reached.* Store the bytes on the heap in BUFFER_SIZE chunks.* @param stream is the input* @throws IOException for errors reading from the stream@@ -189,21 +228,25 @@ public class LinkedByteBuffer {return;}for (;;) {- if (byteCount > maxCount) {- throw new IOException("Response buffer size exceeded for: " + description);- }+ long finalPos = currentPos + (maxCount - byteCount);+ int curMax = (finalPos < BUFFER_SIZE) ? (int) finalPos : BUFFER_SIZE;+ int i = currentPos;do {- if (currentPos == BUFFER_SIZE) {+ if (i == curMax) {break;}- currentBuffer.array[currentPos++] = (byte) tok;+ currentBuffer.array[i++] = (byte) tok;tok = stream.read();}while (tok > 0);- byteCount += currentPos;+ byteCount += (i - currentPos);+ currentPos = i;if (tok <= 0) {return; // Reached terminator or end of stream, ingest is finished}+ if (byteCount == maxCount) {+ throw new IOException("Response buffer size exceeded for: " + description);+ }// Set up next buffercurrentBuffer.next = new ArrayIter();currentBuffer = currentBuffer.next;@@ -214,43 +257,38 @@ public class LinkedByteBuffer {}/**- * Read a page of data into the given buffer. The buffer must already be allocated and- * will be entirely filled, unless end-of-stream is reached. The number of bytes- * actually read into the buffer is returned.+ * Read data up to the end of the current buffer. The buffer must already be allocated and+ * will be entirely filled, unless end-of-stream is reached.* @param stream is the stream- * @param buffer is the allocated buffer- * @return the number of bytes read* @throws IOException for problems reading from the stream*/- private int readPage(InputStream stream, ArrayIter buffer) throws IOException {- int len = maxCount - byteCount;- if (len > BUFFER_SIZE) {- len = BUFFER_SIZE;- }- int pos = 0;+ private void readToEndOfPage(InputStream stream) throws IOException {+ int bytesLeft = BUFFER_SIZE - currentPos;+ int len = (maxCount - byteCount >= bytesLeft) ? bytesLeft : (int) (maxCount - byteCount);+ int i = 0;do {- int readLen = stream.read(currentBuffer.array, pos, len - pos);+ int readLen = stream.read(currentBuffer.array, i + currentPos, len - i);if (readLen < 0) {break;}- pos += readLen;+ i += readLen;}- while (pos < len);- byteCount += pos;- return pos;+ while (i < len);+ byteCount += i;+ currentPos += i;}/**- * Read the stream until the end of stream is encountered or until maxCount bytes is reached.+ * Read until the end of stream is encountered or until maxCount bytes is reached.* Store the bytes on the heap in BUFFER_SIZE chunks.+ * If maxCount bytes are read without reaching end of stream, an IOException is thrown.* @param stream is the input* @throws IOException for errors reading from the stream*/public void ingestStream(InputStream stream) throws IOException {while (byteCount < maxCount) {- int pos = readPage(stream, currentBuffer);- if (pos < BUFFER_SIZE) {- currentPos += pos;+ readToEndOfPage(stream);+ if (currentPos < BUFFER_SIZE) {break;}// Set up next buffer@@ -260,6 +298,10 @@ public class LinkedByteBuffer {currentBuffer.length = BUFFER_SIZE;currentPos = 0;}+ if (byteCount == maxCount && stream.read() != -1) {+ throw new IOException(+ description + ": exceeded maximum size of " + maxCount + " bytes");+ }}/**@@ -296,7 +338,6 @@ public class LinkedByteBuffer {*/public void endIngest() {if (currentPos == BUFFER_SIZE) {- byteCount += currentPos;currentBuffer.next = new ArrayIter();currentBuffer = currentBuffer.next;currentBuffer.array = new byte[1];@@ -304,15 +345,12 @@ public class LinkedByteBuffer {currentPos = 0;}- currentBuffer.array[currentPos++] = (byte) padValue;+ currentBuffer.array[currentPos++] = 0; // Add pad valuecurrentBuffer.length = currentPos; // Any bytes after are not in the buffer}- public void getStartPosition(Position position) {- position.array = initialBuffer.array;- position.length = initialBuffer.length;- position.current = 0;- position.seqIter = initialBuffer;+ public Position allocatePosition() {+ return new Position();}/**@@ -331,13 +369,17 @@ public class LinkedByteBuffer {buffer.next = currentBuffer;currentBuffer.array = new byte[BUFFER_SIZE];currentBuffer.length = BUFFER_SIZE;+ currentPos = 0;try {- currentPos = readPage(asNeededStream, currentBuffer);+ readToEndOfPage(asNeededStream);}catch (IOException e) {throw new DecoderException(e.getMessage());}if (currentPos < BUFFER_SIZE) {+ if (currentPos == 0) {+ throw new DecoderException("End of stream");+ }endIngest();}return buffer.next;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.javaindex 6c01a34648..b4ceb19dd1 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java@@ -20,6 +20,7 @@ import java.util.ArrayList;import ghidra.program.model.address.AddressFactory;import ghidra.program.model.address.AddressSpace;+import ghidra.program.model.pcode.LinkedByteBuffer.Position;/** A byte-based decoder designed to marshal info to the decompiler efficiently@@ -78,9 +79,9 @@ public class PackedDecode implements Decoder, Closeable {private AddressFactory addrFactory;protected AddressSpace[] spaces;private LinkedByteBuffer inStream;- private LinkedByteBuffer.Position startPos;- private LinkedByteBuffer.Position curPos;- private LinkedByteBuffer.Position endPos;+ private Position startPos;+ private Position curPos;+ private Position endPos;private boolean attributeRead;/**@@ -90,17 +91,11 @@ public class PackedDecode implements Decoder, Closeable {public PackedDecode() {addrFactory = null;inStream = null;- startPos = new LinkedByteBuffer.Position();- curPos = new LinkedByteBuffer.Position();- endPos = new LinkedByteBuffer.Position();}public PackedDecode(AddressFactory addrFactory) {this.addrFactory = addrFactory;inStream = null;- startPos = new LinkedByteBuffer.Position();- curPos = new LinkedByteBuffer.Position();- endPos = new LinkedByteBuffer.Position();buildAddrSpaceArray();}@@ -116,9 +111,6 @@ public class PackedDecode implements Decoder, Closeable {public PackedDecode(InputStream stream, String desc) throws IOException {addrFactory = null;inStream = null;- startPos = new LinkedByteBuffer.Position();- curPos = new LinkedByteBuffer.Position();- endPos = new LinkedByteBuffer.Position();open(Integer.MAX_VALUE, desc);inStream.ingestStreamAsNeeded(stream, endPos);}@@ -219,11 +211,11 @@ public class PackedDecode implements Decoder, Closeable {}@Override- public void open(int max, String desc) {- inStream = new LinkedByteBuffer(max, ELEMENT_END, desc);- startPos.buffer = inStream;- curPos.buffer = inStream;- endPos.buffer = inStream;+ public void open(long max, String desc) {+ inStream = new LinkedByteBuffer(max, desc);+ startPos = inStream.allocatePosition();+ curPos = inStream.allocatePosition();+ endPos = inStream.allocatePosition();}/**@@ -254,7 +246,7 @@ public class PackedDecode implements Decoder, Closeable {@Overridepublic void endIngest() {inStream.endIngest();- inStream.getStartPosition(endPos);+ endPos.setToStart();}@Override@@ -438,8 +430,7 @@ public class PackedDecode implements Decoder, Closeable {public long readSignedIntegerExpectString(String expect, long expectval)throws DecoderException {long res;- LinkedByteBuffer.Position tmpPos = new LinkedByteBuffer.Position();- tmpPos.buffer = inStream;+ Position tmpPos = inStream.allocatePosition();tmpPos.copy(curPos);byte header1 = tmpPos.getNextByte();if ((header1 & HEADEREXTEND_MASK) != 0) {@@ -514,28 +505,7 @@ public class PackedDecode implements Decoder, Closeable {length = (int) readInteger(length);attributeRead = true;- int curLen = curPos.length - curPos.current;- if (curLen >= length) {- String res = new String(curPos.array, curPos.current, length);- curPos.advancePosition(length);- return res;- }- int size = curLen;- byte[] buf = new byte[length];- System.arraycopy(curPos.array, curPos.current, buf, 0, curLen);- length -= curLen;- curPos.advancePosition(curLen);- while (length > 0) {- curLen = curPos.length - curPos.current;- if (curLen > length) {- curLen = length;- }- System.arraycopy(curPos.array, curPos.current, buf, size, curLen);- size += curLen;- length -= curLen;- curPos.advancePosition(curLen);- }- return new String(buf, 0, size);+ return curPos.getNextString(length);}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/StringIngest.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/StringIngest.javaindex 20183dd05e..53d4fbb718 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/StringIngest.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/StringIngest.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -21,7 +21,7 @@ public class StringIngest implements ByteIngest {private ByteArrayOutputStream outStream;private String description; // Describes the source of bytes, for use in error messages- private int maxBytes; // Maximum number of bytes that can be ingested+ private long maxBytes; // Maximum number of bytes that can be ingestedpublic StringIngest() {outStream = null;@@ -30,7 +30,7 @@ public class StringIngest implements ByteIngest {}@Override- public void open(int max, String desc) {+ public void open(long max, String desc) {maxBytes = max;description = desc;outStream = new ByteArrayOutputStream();diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/pcode/LinkedByteBufferTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/pcode/LinkedByteBufferTest.javanew file mode 100644index 0000000000..0f1fae1f49--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/pcode/LinkedByteBufferTest.java@@ -0,0 +1,171 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.model.pcode;++import static org.junit.Assert.*;++import java.io.ByteArrayInputStream;+import java.io.IOException;++import org.junit.Test;++import generic.test.AbstractGenericTest;+import ghidra.program.model.pcode.LinkedByteBuffer.Position;++public class LinkedByteBufferTest extends AbstractGenericTest {++ private static byte[] allocateArray(int size, int val) {+ byte[] array = new byte[size];+ for (int i = 0; i < size; ++i)+ array[i] = (byte) val;+ return array;+ }++ @Test+ public void testBufferSizeExceeded() {+ LinkedByteBuffer buffer = new LinkedByteBuffer(16, "teststream");+ byte[] array = allocateArray(32, 0xff);+ ByteArrayInputStream inStream = new ByteArrayInputStream(array);+ boolean sawException = false;+ try {+ buffer.ingestStream(inStream);+ }+ catch (IOException e) {+ sawException = true;+ }+ assertTrue(sawException);+ buffer = new LinkedByteBuffer(16, "teststream2");+ inStream = new ByteArrayInputStream(array);+ sawException = false;+ try {+ buffer.ingestStreamToNextTerminator(inStream);+ }+ catch (IOException e) {+ sawException = true;+ }+ assertTrue(sawException);+ }++ @Test+ public void testIngestStream() throws IOException, DecoderException {+ LinkedByteBuffer buffer = new LinkedByteBuffer(1 << 24, "teststream");+ byte[] array = allocateArray(LinkedByteBuffer.BUFFER_SIZE + 30, 0x7f);+ array[0] = 11;+ array[50] = 3;+ array[LinkedByteBuffer.BUFFER_SIZE] = 10;+ ByteArrayInputStream inStream = new ByteArrayInputStream(array);+ buffer.ingestStream(inStream);+ buffer.endIngest();+ Position pos = buffer.allocatePosition();+ pos.setToStart();+ assertTrue(pos.getNextByte() == 11);+ for (int i = 1; i < 50; ++i) {+ byte val = pos.getNextByte();+ assertTrue(val == 0x7f);+ }+ assertTrue(pos.getByte() == 3);+ assertTrue(pos.getNextByte() == 3);+ for (int i = 51; i < LinkedByteBuffer.BUFFER_SIZE; ++i) {+ byte val = pos.getNextByte();+ assertTrue(val == 0x7f);+ }+ assertTrue(pos.getNextByte() == 10);+ pos.advancePosition(28);+ assertTrue(pos.getNextByte() == 0x7f);+ boolean sawEndOfStream = false;+ try {+ pos.getNextByte();+ }+ catch (DecoderException ex) {+ sawEndOfStream = true;+ }+ assertTrue(sawEndOfStream);+ }++ @Test+ public void testIngestToTerminator() throws IOException, DecoderException {+ LinkedByteBuffer buffer = new LinkedByteBuffer(1 << 24, "teststream");+ byte[] array = allocateArray(LinkedByteBuffer.BUFFER_SIZE + 30, 0x7f);+ array[300] = 0;+ ByteArrayInputStream inStream = new ByteArrayInputStream(array);+ buffer.ingestStreamToNextTerminator(inStream);+ buffer.endIngest();+ Position pos = buffer.allocatePosition();+ pos.setToStart();+ pos.advancePosition(300);+ boolean sawEndOfStream = false;+ try {+ pos.getNextByte();+ }+ catch (DecoderException ex) {+ sawEndOfStream = true;+ }+ assertTrue(sawEndOfStream);+ }++ @Test+ public void testIngestAsNeeded() throws IOException, DecoderException {+ LinkedByteBuffer buffer = new LinkedByteBuffer(1 << 24, "teststream");+ Position pos = buffer.allocatePosition();+ byte[] array = allocateArray(LinkedByteBuffer.BUFFER_SIZE + 30, 0x7f);+ array[LinkedByteBuffer.BUFFER_SIZE + 10] = 100;+ ByteArrayInputStream inStream = new ByteArrayInputStream(array);+ buffer.ingestStreamAsNeeded(inStream, pos);+ assertTrue(inStream.available() == 30);+ for (int i = 0; i < LinkedByteBuffer.BUFFER_SIZE + 10; ++i) {+ assertTrue(pos.getNextByte() == 0x7f);+ }+ assertTrue(pos.getNextByte() == 100);+ pos.advancePosition(19);+ boolean sawEndOfStream = false;+ try {+ pos.getNextByte();+ }+ catch (DecoderException ex) {+ sawEndOfStream = true;+ }+ assertTrue(sawEndOfStream);+ }++ @Test+ public void testString() throws IOException, DecoderException {+ LinkedByteBuffer buffer = new LinkedByteBuffer(1 << 24, "teststream");+ Position pos = buffer.allocatePosition();+ byte[] array = allocateArray(LinkedByteBuffer.BUFFER_SIZE + 30, 0x7f);+ int j = LinkedByteBuffer.BUFFER_SIZE - 4;+ array[j++] = 'c';+ array[j++] = 'r';+ array[j++] = 'o';+ array[j++] = 's';+ array[j++] = 's';+ array[j++] = 'i';+ array[j++] = 'n';+ array[j++] = 'g';+ j++;+ array[j++] = 'c';+ array[j++] = 'a';+ array[j++] = 't';+ ByteArrayInputStream inStream = new ByteArrayInputStream(array);+ buffer.ingestStream(inStream);+ pos.setToStart();+ pos.advancePosition(LinkedByteBuffer.BUFFER_SIZE - 4);+ String str = pos.getNextString(8);+ assertTrue(str.equals("crossing"));+ assertTrue(pos.getNextByte() == 0x7f);+ str = pos.getNextString(3);+ assertTrue(str.equals("cat"));+ }+}