mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-02 11:05:29 -04:00
Added test for BufferedIIS. Still some issues with direct reading.
This commit is contained in:
parent
2b823d82dd
commit
a493cebe13
49
twelvemonkeys-imageio/core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStream.java
Executable file → Normal file
49
twelvemonkeys-imageio/core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStream.java
Executable file → Normal file
@ -26,14 +26,15 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
|
|||||||
private int mBufferPos = 0;
|
private int mBufferPos = 0;
|
||||||
private int mBufferLength = 0;
|
private int mBufferLength = 0;
|
||||||
|
|
||||||
public BufferedImageInputStream(final ImageInputStream pStream) {
|
public BufferedImageInputStream(final ImageInputStream pStream) throws IOException {
|
||||||
this(pStream, DEFAULT_BUFFER_SIZE);
|
this(pStream, DEFAULT_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BufferedImageInputStream(final ImageInputStream pStream, final int pBufferSize) {
|
private BufferedImageInputStream(final ImageInputStream pStream, final int pBufferSize) throws IOException {
|
||||||
Validate.notNull(pStream, "stream");
|
Validate.notNull(pStream, "stream");
|
||||||
|
|
||||||
mStream = pStream;
|
mStream = pStream;
|
||||||
|
streamPos = pStream.getStreamPosition();
|
||||||
mBuffer = new byte[pBufferSize];
|
mBuffer = new byte[pBufferSize];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,39 +67,55 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
|
|||||||
public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
|
public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
|
||||||
bitOffset = 0;
|
bitOffset = 0;
|
||||||
|
|
||||||
boolean bypassBuffer = false;
|
// TODO: Consider fixing the bypass buffer code...
|
||||||
|
|
||||||
if (!isBufferValid()) {
|
if (!isBufferValid()) {
|
||||||
// Bypass cache if cache is empty for reads longer than buffer
|
// Bypass cache if cache is empty for reads longer than buffer
|
||||||
if (pLength >= mBuffer.length) {
|
if (pLength >= mBuffer.length) {
|
||||||
bypassBuffer = true;
|
return readDirect(pBuffer, pOffset, pLength);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fillBuffer();
|
fillBuffer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bypassBuffer && mBufferLength <= 0) {
|
return readBuffered(pBuffer, pOffset, pLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int readDirect(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
|
||||||
|
// System.err.println("BEFORE streamPos: " + streamPos);
|
||||||
|
// System.err.println("BEFORE mStream.getStreamPosition(): " + mStream.getStreamPosition());
|
||||||
|
|
||||||
|
int read = mStream.read(pBuffer, pOffset, Math.min(mBuffer.length, pLength));
|
||||||
|
|
||||||
|
if (read > 0) {
|
||||||
|
streamPos += read;
|
||||||
|
}
|
||||||
|
|
||||||
|
// System.err.println("AFTER streamPos: " + streamPos);
|
||||||
|
// System.err.println("AFTER mStream.getStreamPosition(): " + mStream.getStreamPosition());
|
||||||
|
// System.err.println();
|
||||||
|
|
||||||
|
|
||||||
|
mBufferStart = mStream.getStreamPosition();
|
||||||
|
mBufferLength = 0;
|
||||||
|
|
||||||
|
return read;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int readBuffered(final byte[] pBuffer, final int pOffset, final int pLength) {
|
||||||
|
if (mBufferLength <= 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read as much as possible from buffer
|
// Read as much as possible from buffer
|
||||||
int length = bypassBuffer ? 0 : Math.min(mBufferLength - mBufferPos, pLength);
|
int length = Math.min(mBufferLength - mBufferPos, pLength);
|
||||||
|
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
System.arraycopy(mBuffer, mBufferPos, pBuffer, pOffset, length);
|
System.arraycopy(mBuffer, mBufferPos, pBuffer, pOffset, length);
|
||||||
mBufferPos += length;
|
mBufferPos += length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read rest directly from stream, if longer than buffer
|
|
||||||
if (pLength - length >= mBuffer.length) {
|
|
||||||
int read = mStream.read(pBuffer, pOffset + length, pLength - length);
|
|
||||||
|
|
||||||
if (read > 0) {
|
|
||||||
length += read;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
streamPos += length;
|
streamPos += length;
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
|
35
twelvemonkeys-imageio/core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStreamTestCase.java
Executable file → Normal file
35
twelvemonkeys-imageio/core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStreamTestCase.java
Executable file → Normal file
@ -17,14 +17,14 @@ import java.util.Random;
|
|||||||
* @author last modified by $Author: haraldk$
|
* @author last modified by $Author: haraldk$
|
||||||
* @version $Id: BufferedImageInputStreamTestCase.java,v 1.0 Jun 30, 2008 3:07:42 PM haraldk Exp$
|
* @version $Id: BufferedImageInputStreamTestCase.java,v 1.0 Jun 30, 2008 3:07:42 PM haraldk Exp$
|
||||||
*/
|
*/
|
||||||
public class BufferedImageInputStreamTestCase extends TestCase{
|
public class BufferedImageInputStreamTestCase extends TestCase {
|
||||||
protected final Random mRandom = new Random();
|
protected final Random mRandom = new Random();
|
||||||
|
|
||||||
public void testCreate() {
|
public void testCreate() throws IOException {
|
||||||
new BufferedImageInputStream(new ByteArrayImageInputStream(new byte[0]));
|
new BufferedImageInputStream(new ByteArrayImageInputStream(new byte[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCreateNull() {
|
public void testCreateNull() throws IOException {
|
||||||
try {
|
try {
|
||||||
new BufferedImageInputStream(null);
|
new BufferedImageInputStream(null);
|
||||||
fail("Expected IllegalArgumentException");
|
fail("Expected IllegalArgumentException");
|
||||||
@ -76,6 +76,35 @@ public class BufferedImageInputStreamTestCase extends TestCase{
|
|||||||
assertTrue(rangeEquals(bytes, size, result, 0, size));
|
assertTrue(rangeEquals(bytes, size, result, 0, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testBufferPositionCorrect() throws IOException {
|
||||||
|
// Fill bytes
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
mRandom.nextBytes(bytes);
|
||||||
|
|
||||||
|
ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes);
|
||||||
|
|
||||||
|
input.readByte();
|
||||||
|
input.readByte();
|
||||||
|
input.skipBytes(124);
|
||||||
|
input.readByte();
|
||||||
|
input.readByte();
|
||||||
|
|
||||||
|
// Sanity check
|
||||||
|
assertEquals(128, input.getStreamPosition());
|
||||||
|
|
||||||
|
BufferedImageInputStream stream = new BufferedImageInputStream(input);
|
||||||
|
|
||||||
|
assertEquals(input.getStreamPosition(), stream.getStreamPosition());
|
||||||
|
|
||||||
|
stream.skipBytes(128);
|
||||||
|
|
||||||
|
//assertTrue(256 <= input.getStreamPosition());
|
||||||
|
assertEquals(256, stream.getStreamPosition());
|
||||||
|
|
||||||
|
stream.seek(1020);
|
||||||
|
assertEquals(1020, stream.getStreamPosition());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test two arrays for range equality. That is, they contain the same elements for some specified range.
|
* Test two arrays for range equality. That is, they contain the same elements for some specified range.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user