Added test case for IIOOutputStreamAdapter + fixed bug in flush method.

Strengthened tests for IIOInputStreamAdapter
Minor clean up of the code.
This commit is contained in:
Harald Kuhr
2011-11-30 12:46:58 +01:00
parent d1e72d1ece
commit 2a282cf8e4
4 changed files with 114 additions and 8 deletions
@@ -28,6 +28,8 @@
package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.lang.Validate;
import javax.imageio.stream.ImageInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -75,16 +77,12 @@ class IIOInputStreamAdapter extends InputStream {
}
private IIOInputStreamAdapter(ImageInputStream pInput, long pLength, boolean pHasLength) {
if (pInput == null) {
throw new IllegalArgumentException("stream == null");
}
if (pHasLength && pLength < 0) {
throw new IllegalArgumentException("length < 0");
}
Validate.notNull(pInput, "stream");
Validate.isTrue(!pHasLength || pLength >= 0, pLength, "length < 0: %f");
input = pInput;
hasLength = pHasLength;
left = pLength;
hasLength = pHasLength;
}
@@ -105,6 +103,7 @@ class IIOInputStreamAdapter extends InputStream {
if (hasLength) {
return left > 0 ? (int) Math.min(Integer.MAX_VALUE, left) : 0;
}
return 0; // We don't really know, so we say 0 to be safe.
}
@@ -28,6 +28,8 @@
package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.lang.Validate;
import javax.imageio.stream.ImageOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -43,27 +45,40 @@ class IIOOutputStreamAdapter extends OutputStream {
private ImageOutputStream output;
public IIOOutputStreamAdapter(final ImageOutputStream pOutput) {
Validate.notNull(pOutput, "stream == null");
output = pOutput;
}
@Override
public void write(final byte[] pBytes) throws IOException {
assertOpen();
output.write(pBytes);
}
@Override
public void write(final byte[] pBytes, final int pOffset, final int pLength) throws IOException {
assertOpen();
output.write(pBytes, pOffset, pLength);
}
@Override
public void write(final int pByte) throws IOException {
assertOpen();
output.write(pByte);
}
@Override
public void flush() throws IOException {
output.flush();
// NOTE: The contract of OutputStream.flush is very different from ImageOutputStream.flush. We can't delegate.
// TODO: Fulfill the contract of OutputStream.flush? This seems to be good enough for now.
assertOpen();
}
private void assertOpen() throws IOException {
if (output == null) {
throw new IOException("stream already closed");
}
}
@Override