Mainly new code standard.

A few changes that should have been committed earlier.. :-/
This commit is contained in:
Harald Kuhr
2011-02-17 12:40:49 +01:00
parent 41b8080683
commit 20b87d155d
40 changed files with 951 additions and 593 deletions
@@ -37,9 +37,7 @@ import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
@@ -61,7 +59,7 @@ public abstract class ImageReaderBase extends ImageReader {
* For convenience. Only set if the input is an {@code ImageInputStream}.
* @see #setInput(Object, boolean, boolean)
*/
protected ImageInputStream mImageInput;
protected ImageInputStream imageInput;
/**
* Constructs an {@code ImageReader} and sets its
@@ -102,7 +100,7 @@ public abstract class ImageReaderBase extends ImageReader {
resetMembers();
super.setInput(pInput, pSeekForwardOnly, pIgnoreMetadata);
if (pInput instanceof ImageInputStream) {
mImageInput = (ImageInputStream) pInput;
imageInput = (ImageInputStream) pInput;
}
}
@@ -229,7 +227,7 @@ public abstract class ImageReaderBase extends ImageReader {
if (dest != null) {
boolean found = false;
// NOTE: This is bad, as it relies on implementation details of super method...
// NOTE: This is bad, as it relies on implementation details of "super" method...
// We know that the iterator has not been touched if explicit destination..
while (pTypes.hasNext()) {
ImageTypeSpecifier specifier = pTypes.next();
@@ -318,7 +316,27 @@ public abstract class ImageReaderBase extends ImageReader {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JFrame frame = new JFrame(pTitle);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getRootPane().getActionMap().put("window-close", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Window window = SwingUtilities.getWindowAncestor((Component) e.getSource());
window.setVisible(false);
window.dispose();
}
});
frame.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "window-close");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
Window[] windows = Window.getWindows();
if (windows == null || windows.length == 0) {
System.exit(0);
}
}
});
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
JPanel pane = new JPanel(new BorderLayout());
JScrollPane scroll = new JScrollPane(new ImageLabel(pImage));
@@ -338,6 +356,10 @@ public abstract class ImageReaderBase extends ImageReader {
}
}
protected static boolean hasExplicitDestination(final ImageReadParam pParam) {
return (pParam != null && (pParam.getDestination() != null || pParam.getDestinationType() != null || pParam.getDestinationOffset() != null));
}
private static class ImageLabel extends JLabel {
Paint mBackground;
@@ -52,7 +52,7 @@ public abstract class ImageWriterBase extends ImageWriter {
* For convenience. Only set if the output is an {@code ImageInputStream}.
* @see #setOutput(Object)
*/
protected ImageOutputStream mImageOutput;
protected ImageOutputStream imageOutput;
/**
* Constructs an {@code ImageWriter} and sets its
@@ -80,7 +80,7 @@ public abstract class ImageWriterBase extends ImageWriter {
super.setOutput(pOutput);
if (pOutput instanceof ImageOutputStream) {
mImageOutput = (ImageOutputStream) pOutput;
imageOutput = (ImageOutputStream) pOutput;
}
}
@@ -16,9 +16,9 @@ public class ProviderInfo {
// TODO: Consider reading the META-INF/MANIFEST.MF from the class path using java.util.jar.Manifest.
// Use the manifest that is located in the same class path folder as the package.
private final String mTitle;
private final String mVendorName;
private final String mVersion;
private final String title;
private final String vendorName;
private final String version;
/**
* Creates a provider information instance based on the given package.
@@ -32,13 +32,13 @@ public class ProviderInfo {
Validate.notNull(pPackage, "package");
String title = pPackage.getImplementationTitle();
mTitle = title != null ? title : pPackage.getName();
this.title = title != null ? title : pPackage.getName();
String vendor = pPackage.getImplementationVendor();
mVendorName = vendor != null ? vendor : fakeVendor(pPackage);
vendorName = vendor != null ? vendor : fakeVendor(pPackage);
String version = pPackage.getImplementationVersion();
mVersion = version != null ? version : fakeVersion(pPackage);
this.version = version != null ? version : fakeVersion(pPackage);
}
private static String fakeVendor(final Package pPackage) {
@@ -60,7 +60,7 @@ public class ProviderInfo {
* @return the implementation title
*/
final String getImplementationTitle() {
return mTitle;
return title;
}
/**
@@ -72,7 +72,7 @@ public class ProviderInfo {
* @return the vendor name.
*/
public final String getVendorName() {
return mVendorName;
return vendorName;
}
/**
@@ -83,11 +83,11 @@ public class ProviderInfo {
* @return the vendor name.
*/
public final String getVersion() {
return mVersion;
return version;
}
@Override
public String toString() {
return mTitle + ", " + mVersion + " by " + mVendorName;
return title + ", " + version + " by " + vendorName;
}
}
@@ -23,12 +23,12 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
static final int DEFAULT_BUFFER_SIZE = 8192;
private ImageInputStream mStream;
private ImageInputStream stream;
private byte[] mBuffer;
private long mBufferStart = 0;
private int mBufferPos = 0;
private int mBufferLength = 0;
private byte[] buffer;
private long bufferStart = 0;
private int bufferPos = 0;
private int bufferLength = 0;
public BufferedImageInputStream(final ImageInputStream pStream) throws IOException {
this(pStream, DEFAULT_BUFFER_SIZE);
@@ -37,19 +37,19 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
private BufferedImageInputStream(final ImageInputStream pStream, final int pBufferSize) throws IOException {
Validate.notNull(pStream, "stream");
mStream = pStream;
stream = pStream;
streamPos = pStream.getStreamPosition();
mBuffer = new byte[pBufferSize];
buffer = new byte[pBufferSize];
}
private void fillBuffer() throws IOException {
mBufferStart = streamPos;
mBufferLength = mStream.read(mBuffer, 0, mBuffer.length);
mBufferPos = 0;
bufferStart = streamPos;
bufferLength = stream.read(buffer, 0, buffer.length);
bufferPos = 0;
}
private boolean isBufferValid() throws IOException {
return mBufferPos < mBufferLength && mBufferStart == mStream.getStreamPosition() - mBufferLength;
return bufferPos < bufferLength && bufferStart == stream.getStreamPosition() - bufferLength;
}
@Override
@@ -58,14 +58,14 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
fillBuffer();
}
if (mBufferLength <= 0) {
if (bufferLength <= 0) {
return -1;
}
bitOffset = 0;
streamPos++;
return mBuffer[mBufferPos++] & 0xff;
return buffer[bufferPos++] & 0xff;
}
@Override
@@ -74,7 +74,7 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
if (!isBufferValid()) {
// Bypass cache if cache is empty for reads longer than buffer
if (pLength >= mBuffer.length) {
if (pLength >= buffer.length) {
return readDirect(pBuffer, pOffset, pLength);
}
else {
@@ -87,30 +87,30 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
private int readDirect(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
// TODO: Figure out why reading more than the buffer length causes alignment issues...
int read = mStream.read(pBuffer, pOffset, Math.min(mBuffer.length, pLength));
int read = stream.read(pBuffer, pOffset, Math.min(buffer.length, pLength));
if (read > 0) {
streamPos += read;
}
mBufferStart = mStream.getStreamPosition();
mBufferLength = 0;
bufferStart = stream.getStreamPosition();
bufferLength = 0;
return read;
}
private int readBuffered(final byte[] pBuffer, final int pOffset, final int pLength) {
if (mBufferLength <= 0) {
if (bufferLength <= 0) {
return -1;
}
// Read as much as possible from buffer
int length = Math.min(mBufferLength - mBufferPos, pLength);
int length = Math.min(bufferLength - bufferPos, pLength);
if (length > 0) {
System.arraycopy(mBuffer, mBufferPos, pBuffer, pOffset, length);
mBufferPos += length;
System.arraycopy(buffer, bufferPos, pBuffer, pOffset, length);
bufferPos += length;
}
streamPos += length;
@@ -121,42 +121,42 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
@Override
public void seek(long pPosition) throws IOException {
// TODO: Could probably be optimized to not invalidate buffer if new position is within current buffer
mStream.seek(pPosition);
mBufferLength = 0; // Will invalidate buffer
streamPos = mStream.getStreamPosition();
stream.seek(pPosition);
bufferLength = 0; // Will invalidate buffer
streamPos = stream.getStreamPosition();
}
@Override
public void flushBefore(long pos) throws IOException {
mStream.flushBefore(pos);
stream.flushBefore(pos);
}
@Override
public long getFlushedPosition() {
return mStream.getFlushedPosition();
return stream.getFlushedPosition();
}
@Override
public boolean isCached() {
return mStream.isCached();
return stream.isCached();
}
@Override
public boolean isCachedMemory() {
return mStream.isCachedMemory();
return stream.isCachedMemory();
}
@Override
public boolean isCachedFile() {
return mStream.isCachedFile();
return stream.isCachedFile();
}
@Override
public void close() throws IOException {
if (mStream != null) {
//mStream.close();
mStream = null;
mBuffer = null;
if (stream != null) {
//stream.close();
stream = null;
buffer = null;
}
super.close();
}
@@ -170,7 +170,7 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
public long length() {
// WTF?! This method is allowed to throw IOException in the interface...
try {
return mStream.length();
return stream.length();
}
catch (IOException ignore) {
}
@@ -13,35 +13,39 @@ import java.io.IOException;
* @version $Id: ByteArrayImageInputStream.java,v 1.0 May 15, 2008 2:12:12 PM haraldk Exp$
*/
public final class ByteArrayImageInputStream extends ImageInputStreamImpl {
private final byte[] mData;
private final byte[] data;
public ByteArrayImageInputStream(final byte[] pData) {
Validate.notNull(pData, "data");
mData = pData;
data = pData;
}
public int read() throws IOException {
if (streamPos >= mData.length) {
if (streamPos >= data.length) {
return -1;
}
bitOffset = 0;
return mData[((int) streamPos++)] & 0xff;
return data[((int) streamPos++)] & 0xff;
}
public int read(byte[] pBuffer, int pOffset, int pLength) throws IOException {
if (streamPos >= mData.length) {
if (streamPos >= data.length) {
return -1;
}
int length = (int) Math.min(mData.length - streamPos, pLength);
int length = (int) Math.min(data.length - streamPos, pLength);
bitOffset = 0;
System.arraycopy(mData, (int) streamPos, pBuffer, pOffset, length);
System.arraycopy(data, (int) streamPos, pBuffer, pOffset, length);
streamPos += length;
return length;
}
@Override
public long length() {
return mData.length;
return data.length;
}
@Override
@@ -16,9 +16,9 @@ import java.io.IOException;
public final class SubImageInputStream extends ImageInputStreamImpl {
// NOTE: This class is based on com.sun.imageio.plugins.common.SubImageInputStream, but fixes some of its bugs.
private final ImageInputStream mStream;
private final long mStartPos;
private final long mLength;
private final ImageInputStream stream;
private final long startPos;
private final long length;
/**
* Creates a {@link ImageInputStream}, reading up to a maximum number of bytes from the underlying stream.
@@ -32,21 +32,19 @@ public final class SubImageInputStream extends ImageInputStreamImpl {
*/
public SubImageInputStream(final ImageInputStream pStream, final long pLength) throws IOException {
Validate.notNull(pStream, "stream");
if (pLength < 0) {
throw new IllegalArgumentException("length < 0");
}
Validate.isTrue(pLength >= 0, pLength, "length < 0: %d");
mStream = pStream;
mStartPos = pStream.getStreamPosition();
mLength = pLength;
stream = pStream;
startPos = pStream.getStreamPosition();
length = pLength;
}
public int read() throws IOException {
if (streamPos >= mLength) { // Local EOF
if (streamPos >= length) { // Local EOF
return -1;
}
else {
int read = mStream.read();
int read = stream.read();
if (read >= 0) {
streamPos++;
@@ -57,13 +55,13 @@ public final class SubImageInputStream extends ImageInputStreamImpl {
}
public int read(final byte[] pBytes, final int pOffset, final int pLength) throws IOException {
if (streamPos >= mLength) { // Local EOF
if (streamPos >= length) { // Local EOF
return -1;
}
// Safe cast, as pLength can never cause int overflow
int length = (int) Math.min(pLength, mLength - streamPos);
int count = mStream.read(pBytes, pOffset, length);
int length = (int) Math.min(pLength, this.length - streamPos);
int count = stream.read(pBytes, pOffset, length);
if (count >= 0) {
streamPos += count;
@@ -75,8 +73,8 @@ public final class SubImageInputStream extends ImageInputStreamImpl {
@Override
public long length() {
try {
long length = mStream.length();
return length < 0 ? -1 : Math.min(length - mStartPos, mLength);
long length = stream.length();
return length < 0 ? -1 : Math.min(length - startPos, this.length);
}
catch (IOException ignore) {
}
@@ -90,7 +88,7 @@ public final class SubImageInputStream extends ImageInputStreamImpl {
throw new IndexOutOfBoundsException("pos < flushedPosition");
}
mStream.seek(mStartPos + pPosition);
stream.seek(startPos + pPosition);
streamPos = pPosition;
}
@@ -43,10 +43,10 @@ import java.io.InputStream;
* @version $Id: IIOInputStreamAdapter.java,v 1.0 Sep 26, 2007 11:35:59 AM haraldk Exp$
*/
class IIOInputStreamAdapter extends InputStream {
private ImageInputStream mInput;
private final boolean mHasLength;
private long mLeft;
private long mMarkPosition;
private ImageInputStream input;
private final boolean hasLength;
private long left;
private long markPosition;
// TODO: Enforce stream boundaries!
// TODO: Stream start position....
@@ -82,9 +82,9 @@ class IIOInputStreamAdapter extends InputStream {
throw new IllegalArgumentException("length < 0");
}
mInput = pInput;
mHasLength = pHasLength;
mLeft = pLength;
input = pInput;
hasLength = pHasLength;
left = pLength;
}
@@ -93,17 +93,17 @@ class IIOInputStreamAdapter extends InputStream {
* This implementation does <em>not</em> close the underlying stream.
*/
public void close() throws IOException {
if (mHasLength) {
mInput.seek(mInput.getStreamPosition() + mLeft);
if (hasLength) {
input.seek(input.getStreamPosition() + left);
}
mLeft = 0;
mInput = null;
left = 0;
input = null;
}
public int available() throws IOException {
if (mHasLength) {
return mLeft > 0 ? (int) Math.min(Integer.MAX_VALUE, mLeft) : 0;
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.
}
@@ -115,7 +115,7 @@ class IIOInputStreamAdapter extends InputStream {
public void mark(int pReadLimit) {
try {
mMarkPosition = mInput.getStreamPosition();
markPosition = input.getStreamPosition();
}
catch (IOException e) {
// Let's hope this never happens, because it's not possible to reset then...
@@ -124,17 +124,17 @@ class IIOInputStreamAdapter extends InputStream {
}
public void reset() throws IOException {
long diff = mInput.getStreamPosition() - mMarkPosition;
mInput.seek(mMarkPosition);
mLeft += diff;
long diff = input.getStreamPosition() - markPosition;
input.seek(markPosition);
left += diff;
}
public int read() throws IOException {
if (mHasLength && mLeft-- <= 0) {
mLeft = 0;
if (hasLength && left-- <= 0) {
left = 0;
return -1;
}
return mInput.read();
return input.read();
}
public final int read(byte[] pBytes) throws IOException {
@@ -142,13 +142,13 @@ class IIOInputStreamAdapter extends InputStream {
}
public int read(final byte[] pBytes, final int pOffset, final int pLength) throws IOException {
if (mHasLength && mLeft <= 0) {
if (hasLength && left <= 0) {
return -1;
}
int read = mInput.read(pBytes, pOffset, (int) findMaxLen(pLength));
if (mHasLength) {
mLeft = read < 0 ? 0 : mLeft - read;
int read = input.read(pBytes, pOffset, (int) findMaxLen(pLength));
if (hasLength) {
left = read < 0 ? 0 : left - read;
}
return read;
}
@@ -161,8 +161,8 @@ class IIOInputStreamAdapter extends InputStream {
* @return the maximum number of bytes to read
*/
private long findMaxLen(long pLength) {
if (mHasLength && mLeft < pLength) {
return Math.max(mLeft, 0);
if (hasLength && left < pLength) {
return Math.max(left, 0);
}
else {
return Math.max(pLength, 0);
@@ -170,8 +170,8 @@ class IIOInputStreamAdapter extends InputStream {
}
public long skip(long pLength) throws IOException {
long skipped = mInput.skipBytes(findMaxLen(pLength)); // Skips 0 or more, never -1
mLeft -= skipped;
long skipped = input.skipBytes(findMaxLen(pLength)); // Skips 0 or more, never -1
left -= skipped;
return skipped;
}
}
@@ -40,34 +40,34 @@ import java.io.OutputStream;
* @version $Id: IIOOutputStreamAdapter.java,v 1.0 Sep 26, 2007 11:50:38 AM haraldk Exp$
*/
class IIOOutputStreamAdapter extends OutputStream {
private ImageOutputStream mOutput;
private ImageOutputStream output;
public IIOOutputStreamAdapter(final ImageOutputStream pOutput) {
mOutput = pOutput;
output = pOutput;
}
@Override
public void write(final byte[] pBytes) throws IOException {
mOutput.write(pBytes);
output.write(pBytes);
}
@Override
public void write(final byte[] pBytes, final int pOffset, final int pLength) throws IOException {
mOutput.write(pBytes, pOffset, pLength);
output.write(pBytes, pOffset, pLength);
}
@Override
public void write(final int pByte) throws IOException {
mOutput.write(pByte);
output.write(pByte);
}
@Override
public void flush() throws IOException {
mOutput.flush();
output.flush();
}
@Override
public void close() throws IOException {
mOutput = null;
output = null;
}
}
@@ -47,15 +47,15 @@ import java.util.Map;
* @version $Id: ReaderFileSuffixFilter.java,v 1.0 11.okt.2006 20:05:36 haku Exp$
*/
public final class ReaderFileSuffixFilter extends FileFilter implements java.io.FileFilter {
private final String mDescription;
private final Map<String, Boolean> mKnownSuffixes = new HashMap<String, Boolean>(32);
private final String description;
private final Map<String, Boolean> knownSuffixes = new HashMap<String, Boolean>(32);
public ReaderFileSuffixFilter() {
this("Images (all supported input formats)");
}
public ReaderFileSuffixFilter(String pDescription) {
mDescription = pDescription;
description = pDescription;
}
public boolean accept(File pFile) {
@@ -71,19 +71,20 @@ public final class ReaderFileSuffixFilter extends FileFilter implements java.io.
}
private boolean hasReaderForSuffix(String pSuffix) {
if (mKnownSuffixes.get(pSuffix) == Boolean.TRUE) {
if (knownSuffixes.get(pSuffix) == Boolean.TRUE) {
return true;
}
try {
// Cahce lookup
Iterator iterator = ImageIO.getImageReadersBySuffix(pSuffix);
if (iterator.hasNext()) {
mKnownSuffixes.put(pSuffix, Boolean.TRUE);
knownSuffixes.put(pSuffix, Boolean.TRUE);
return true;
}
else {
mKnownSuffixes.put(pSuffix, Boolean.FALSE);
knownSuffixes.put(pSuffix, Boolean.FALSE);
return false;
}
}
@@ -93,6 +94,6 @@ public final class ReaderFileSuffixFilter extends FileFilter implements java.io.
}
public String getDescription() {
return mDescription;
return description;
}
}
@@ -47,15 +47,15 @@ import java.util.Map;
* @version $Id: WriterFileSuffixFilter.java,v 1.0 11.okt.2006 20:05:36 haku Exp$
*/
public final class WriterFileSuffixFilter extends FileFilter implements java.io.FileFilter {
private final String mDescription;
private Map<String, Boolean>mKnownSuffixes = new HashMap<String, Boolean>(32);
private final String description;
private Map<String, Boolean> knownSuffixes = new HashMap<String, Boolean>(32);
public WriterFileSuffixFilter() {
this("Images (all supported output formats)");
}
public WriterFileSuffixFilter(String pDescription) {
mDescription = pDescription;
description = pDescription;
}
public boolean accept(File pFile) {
@@ -66,24 +66,25 @@ public final class WriterFileSuffixFilter extends FileFilter implements java.io.
// Test if we have an ImageWriter for this suffix
String suffix = FileUtil.getExtension(pFile);
return !StringUtil.isEmpty(suffix) && hasWriterForSuffix(suffix);
return !StringUtil.isEmpty(suffix) && hasWriterForSuffix(suffix);
}
private boolean hasWriterForSuffix(String pSuffix) {
if (mKnownSuffixes.get(pSuffix) == Boolean.TRUE) {
if (knownSuffixes.get(pSuffix) == Boolean.TRUE) {
return true;
}
try {
// Cahce lookup
Iterator iterator = ImageIO.getImageWritersBySuffix(pSuffix);
if (iterator.hasNext()) {
mKnownSuffixes.put(pSuffix, Boolean.TRUE);
knownSuffixes.put(pSuffix, Boolean.TRUE);
return true;
}
else {
mKnownSuffixes.put(pSuffix, Boolean.FALSE);
knownSuffixes.put(pSuffix, Boolean.FALSE);
return false;
}
}
@@ -93,6 +94,6 @@ public final class WriterFileSuffixFilter extends FileFilter implements java.io.
}
public String getDescription() {
return mDescription;
return description;
}
}
@@ -18,7 +18,7 @@ import java.util.Random;
* @version $Id: BufferedImageInputStreamTestCase.java,v 1.0 Jun 30, 2008 3:07:42 PM haraldk Exp$
*/
public class BufferedImageInputStreamTestCase extends TestCase {
protected final Random mRandom = new Random();
protected final Random random = new Random();
public void testCreate() throws IOException {
new BufferedImageInputStream(new ByteArrayImageInputStream(new byte[0]));
@@ -58,7 +58,7 @@ public class BufferedImageInputStreamTestCase extends TestCase {
// Fill bytes
byte[] bytes = new byte[size * 2];
mRandom.nextBytes(bytes);
random.nextBytes(bytes);
// Create wrapper stream
BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes));
@@ -79,7 +79,7 @@ public class BufferedImageInputStreamTestCase extends TestCase {
public void testBufferPositionCorrect() throws IOException {
// Fill bytes
byte[] bytes = new byte[1024];
mRandom.nextBytes(bytes);
random.nextBytes(bytes);
ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes);
@@ -1,11 +1,12 @@
package com.twelvemonkeys.imageio.stream;
import static com.twelvemonkeys.imageio.stream.BufferedImageInputStreamTestCase.rangeEquals;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Random;
import static com.twelvemonkeys.imageio.stream.BufferedImageInputStreamTestCase.rangeEquals;
/**
* ByteArrayImageInputStreamTestCase
*
@@ -14,7 +15,7 @@ import java.util.Random;
* @version $Id: ByteArrayImageInputStreamTestCase.java,v 1.0 Apr 21, 2009 10:58:48 AM haraldk Exp$
*/
public class ByteArrayImageInputStreamTestCase extends TestCase {
protected final Random mRandom = new Random();
protected final Random random = new Random();
public void testCreate() {
ByteArrayImageInputStream stream = new ByteArrayImageInputStream(new byte[0]);
@@ -36,7 +37,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
public void testRead() throws IOException {
byte[] data = new byte[1024 * 1024];
mRandom.nextBytes(data);
random.nextBytes(data);
ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data);
@@ -49,7 +50,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
public void testReadArray() throws IOException {
byte[] data = new byte[1024 * 1024];
mRandom.nextBytes(data);
random.nextBytes(data);
ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data);
@@ -65,7 +66,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
public void testReadSkip() throws IOException {
byte[] data = new byte[1024 * 14];
mRandom.nextBytes(data);
random.nextBytes(data);
ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data);
@@ -82,7 +83,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
public void testReadSeek() throws IOException {
byte[] data = new byte[1024 * 18];
mRandom.nextBytes(data);
random.nextBytes(data);
ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data);
@@ -19,12 +19,12 @@ import java.util.Random;
*/
public class SubImageInputStreamTestCase extends TestCase {
// TODO: Extract super test case for all stream tests
private final Random mRandom = new Random(837468l);
private final Random random = new Random(837468l);
private ImageInputStream createStream(final int pSize) {
byte[] bytes = new byte[pSize];
mRandom.nextBytes(bytes);
random.nextBytes(bytes);
return new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)) {
@Override
@@ -116,6 +116,10 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
protected abstract List<String> getMIMETypes();
protected boolean allowsNullRawImageType() {
return false;
}
protected void assertProviderInstalledForName(final String pFormat, final Class<? extends ImageReader> pReaderClass) {
assertProviderInstalled0(pFormat.toUpperCase(), pReaderClass, ImageIO.getImageReadersByFormatName(pFormat.toUpperCase()));
assertProviderInstalled0(pFormat.toLowerCase(), pReaderClass, ImageIO.getImageReadersByFormatName(pFormat.toLowerCase()));
@@ -1111,30 +1115,34 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
public void testGetTypeSpecifiers() throws IOException {
final ImageReader reader = createReader();
TestData data = getTestData().get(0);
reader.setInput(data.getInputStream());
for (TestData data : getTestData()) {
reader.setInput(data.getInputStream());
ImageTypeSpecifier rawType = reader.getRawImageType(0);
assertNotNull(rawType);
Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
assertNotNull(types);
assertTrue(types.hasNext());
// TODO: This might fail even though the specifiers are obviously equal, if the
// color spaces they use are not the SAME instance, as ColorSpace uses identity equals
// and Interleaved ImageTypeSpecifiers are only equal if color spaces are equal...
boolean rawFound = false;
while (types.hasNext()) {
ImageTypeSpecifier type = types.next();
if (type.equals(rawType)) {
rawFound = true;
break;
ImageTypeSpecifier rawType = reader.getRawImageType(0);
if (rawType == null && allowsNullRawImageType()) {
continue;
}
}
assertNotNull(rawType);
assertTrue("ImageTypeSepcifier from getRawImageType should be in the iterator from getImageTypes", rawFound);
Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
assertNotNull(types);
assertTrue(types.hasNext());
// TODO: This might fail even though the specifiers are obviously equal, if the
// color spaces they use are not the SAME instance, as ColorSpace uses identity equals
// and Interleaved ImageTypeSpecifiers are only equal if color spaces are equal...
boolean rawFound = false;
while (types.hasNext()) {
ImageTypeSpecifier type = types.next();
if (type.equals(rawType)) {
rawFound = true;
break;
}
}
assertTrue("ImageTypeSepcifier from getRawImageType should be in the iterator from getImageTypes", rawFound);
}
}
public void testSetDestination() throws IOException {
@@ -1164,12 +1172,18 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
ImageReadParam param = reader.getDefaultReadParam();
ImageTypeSpecifier type = reader.getRawImageType(0);
BufferedImage destination = type.createBufferedImage(reader.getWidth(0), reader.getHeight(0));
param.setDestination(destination);
BufferedImage result = reader.read(0, param);
if (type != null) {
BufferedImage destination = type.createBufferedImage(reader.getWidth(0), reader.getHeight(0));
param.setDestination(destination);
assertSame(destination, result);
BufferedImage result = reader.read(0, param);
assertSame(destination, result);
}
else {
System.err.println("WARNING: Test skipped due to reader.getRawImageType(0) returning null");
}
}
public void testSetDestinationIllegal() throws IOException {
@@ -1258,7 +1272,7 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
boolean removed = illegalTypes.remove(valid);
// TODO: 4BYTE_ABGR (6) and 4BYTE_ABGR_PRE (7) is essentially the same type...
// !#$#§%$! ImageTypeSpecifier.equals is not well-defined
// !#$#%$! ImageTypeSpecifier.equals is not well-defined
if (!removed) {
for (Iterator<ImageTypeSpecifier> iterator = illegalTypes.iterator(); iterator.hasNext();) {
ImageTypeSpecifier illegalType = iterator.next();