From 0cb99feedf7e9acdcadd4bbb39dbe7fa15766e9c Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Wed, 1 Jun 2022 22:00:37 +0200 Subject: [PATCH] A new ImageInputStream adapter for InputStream. --- .../stream/DirectImageInputStream.java | 133 ++++++ .../stream/BufferedImageInputStreamTest.java | 3 +- .../stream/DirectImageInputStreamTest.java | 380 ++++++++++++++++++ 3 files changed, 515 insertions(+), 1 deletion(-) create mode 100644 imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/DirectImageInputStream.java create mode 100755 imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/DirectImageInputStreamTest.java diff --git a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/DirectImageInputStream.java b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/DirectImageInputStream.java new file mode 100644 index 00000000..bff04e3b --- /dev/null +++ b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/DirectImageInputStream.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2022, Harald Kuhr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.twelvemonkeys.imageio.stream; + +import javax.imageio.stream.ImageInputStreamImpl; +import java.io.IOException; +import java.io.InputStream; + +import static com.twelvemonkeys.lang.Validate.isTrue; +import static com.twelvemonkeys.lang.Validate.notNull; + +/** + * An {@code ImageInputStream} that adapts an {@code InputSteam}, + * by reading directly from the stream without and form of caching or buffering. + *

+ * Note: This is not a general-purpose {@code ImageInputStream}, and is designed for reading large chunks, + * typically of pixel data, from an {@code InputStream}. + * It does not support backwards seeking, or reading bits. + *

+ */ +public final class DirectImageInputStream extends ImageInputStreamImpl { + private final InputStream stream; + private final long length; + + public DirectImageInputStream(final InputStream stream) { + this(stream, -1L); + } + + public DirectImageInputStream(final InputStream stream, long length) { + this.stream = notNull(stream, "stream"); + this.length = isTrue(length >= 0L || length == -1L, length, "negative length: %d"); + } + + @Override + public int read() throws IOException { + bitOffset = 0; + streamPos++; + return stream.read(); + } + + @Override + public int read(final byte[] bytes, int off, int len) throws IOException { + bitOffset = 0; + + int read = stream.read(bytes, off, len); + if (read > 0) { + streamPos += read; + } + + return read; + } + + @Override + public void seek(long pos) throws IOException { + checkClosed(); + + if (pos < streamPos) { + // Handle as if flushedPos == streamPos at any time + throw new IndexOutOfBoundsException("pos < flushedPos"); + } + + bitOffset = 0; + + while (streamPos < pos) { + long skipped = stream.skip(pos - streamPos); + + if (skipped <= 0) { + break; + } + + streamPos += skipped; + } + } + + @Override + public long getFlushedPosition() { + // Handle as if flushedPos == streamPos at any time + return streamPos; + } + + @Override + public long length() { + return length; + } + + @SuppressWarnings("RedundantThrows") + @Override + public int readBit() throws IOException { + throw new UnsupportedOperationException("Bit reading not supported"); + } + + @SuppressWarnings("RedundantThrows") + @Override + public long readBits(int numBits) throws IOException { + throw new UnsupportedOperationException("Bit reading not supported"); + } + + @Override + public void close() throws IOException { + // We could seek to EOF here, but the usual case + + stream.close(); + super.close(); + } +} diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStreamTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStreamTest.java index 17dab84c..8ddbcc72 100644 --- a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStreamTest.java +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedImageInputStreamTest.java @@ -53,6 +53,7 @@ import static org.mockito.Mockito.*; * @author last modified by $Author: haraldk$ * @version $Id: BufferedImageInputStreamTest.java,v 1.0 Jun 30, 2008 3:07:42 PM haraldk Exp$ */ +@SuppressWarnings("deprecation") public class BufferedImageInputStreamTest { private final Random random = new Random(3450972865211L); @@ -433,7 +434,7 @@ public class BufferedImageInputStreamTest { * and {@code pFirstOffset == pSecondOffset}. * Otherwise {@code false}. */ - static boolean rangeEquals(byte[] pFirst, int pFirstOffset, byte[] pSecond, int pSecondOffset, int pLength) { + public static boolean rangeEquals(byte[] pFirst, int pFirstOffset, byte[] pSecond, int pSecondOffset, int pLength) { if (pFirst == pSecond && pFirstOffset == pSecondOffset) { return true; } diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/DirectImageInputStreamTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/DirectImageInputStreamTest.java new file mode 100755 index 00000000..15467730 --- /dev/null +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/DirectImageInputStreamTest.java @@ -0,0 +1,380 @@ +/* + * Copyright (c) 2022, Harald Kuhr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.twelvemonkeys.imageio.stream; + +import org.junit.Ignore; +import org.junit.Test; +import org.junit.function.ThrowingRunnable; + +import javax.imageio.stream.ImageInputStream; +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Random; + +import static com.twelvemonkeys.imageio.stream.BufferedImageInputStreamTest.rangeEquals; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +/** + * NonSeekableImageInputStreamTest + * + * @author Harald Kuhr + * @author last modified by $Author: haraldk$ + * @version $Id: NonSeekableImageInputStreamTest.java,v 1.0 Apr 21, 2009 10:58:48 AM haraldk Exp$ + */ +public class DirectImageInputStreamTest { + private final Random random = new Random(170984354357234566L); + + private InputStream randomData(byte[] data) { + random.nextBytes(data); + + return new ByteArrayInputStream(data); + } + + @Test + public void testCreate() throws IOException { + try (DirectImageInputStream stream = new DirectImageInputStream(new ByteArrayInputStream(new byte[0]), 0)) { + assertEquals("Data length should be same as stream length", 0, stream.length()); + } + } + + @Test + public void testCreateNullFile() throws IOException { + try (@SuppressWarnings("unused") DirectImageInputStream stream = new DirectImageInputStream(null)) { + fail("Expected IllegalArgumentException"); + } + catch (IllegalArgumentException expected) { + assertNotNull("Null exception message", expected.getMessage()); + String message = expected.getMessage().toLowerCase(); + assertTrue("Exception message does not contain parameter name", message.contains("stream")); + assertTrue("Exception message does not contain null", message.contains("null")); + } + } + + @Test + public void testRead() throws IOException { + byte[] data = new byte[1024 * 1024]; + InputStream input = randomData(data); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + for (byte value : data) { + assertEquals("Wrong data read", value & 0xff, stream.read()); + } + } + } + + @Test + public void testReadArray() throws IOException { + byte[] data = new byte[1024 * 10]; + InputStream input = randomData(data); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + byte[] result = new byte[1024]; + + for (int i = 0; i < data.length / result.length; i++) { + stream.readFully(result); + assertTrue("Wrong data read: " + i, rangeEquals(data, i * result.length, result, 0, result.length)); + } + } + } + + @Test + public void testReadSkip() throws IOException { + byte[] data = new byte[1024 * 14]; + InputStream input = randomData(data); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + byte[] result = new byte[7]; + + for (int i = 0; i < data.length / result.length; i += 2) { + stream.readFully(result); + stream.skipBytes(result.length); + assertTrue("Wrong data read: " + i, rangeEquals(data, i * result.length, result, 0, result.length)); + } + } + } + + @Test + public void testReadSeek() throws IOException { + byte[] data = new byte[24 * 18]; + InputStream input = randomData(data); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + byte[] result = new byte[9]; + + for (int i = 0; i < data.length / (2 * result.length); i++) { + long newPos = i * 2 * result.length; + stream.seek(newPos); + assertEquals("Wrong stream position", newPos, stream.getStreamPosition()); + stream.readFully(result); + assertTrue("Wrong data read: " + i, rangeEquals(data, (int) newPos, result, 0, result.length)); + } + } + } + + @SuppressWarnings("ConstantConditions") + @Ignore("Bit reading requires backwards seek or buffer...") + @Test + public void testReadBitRandom() throws IOException { + byte[] bytes = new byte[8]; + InputStream input = randomData(bytes); + long value = ByteBuffer.wrap(bytes).getLong(); + + // Create stream + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + for (int i = 1; i <= 64; i++) { + assertEquals(String.format("bit %d differ", i), (value << (i - 1L)) >>> 63L, stream.readBit()); + } + } + } + + @SuppressWarnings("ConstantConditions") + @Ignore("Bit reading requires backwards seek or buffer...") + @Test + public void testReadBitsRandom() throws IOException { + byte[] bytes = new byte[8]; + InputStream input = randomData(bytes); + long value = ByteBuffer.wrap(bytes).getLong(); + + // Create stream + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + for (int i = 1; i <= 64; i++) { + stream.seek(0); + assertEquals(String.format("bit %d differ", i), value >>> (64L - i), stream.readBits(i)); + assertEquals(i % 8, stream.getBitOffset()); + } + } + } + + @SuppressWarnings("ConstantConditions") + @Ignore("Bit reading requires backwards seek or buffer...") + @Test + public void testReadBitsRandomOffset() throws IOException { + byte[] bytes = new byte[8]; + InputStream input = randomData(bytes); + long value = ByteBuffer.wrap(bytes).getLong(); + + // Create stream + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + for (int i = 1; i <= 60; i++) { + stream.seek(0); + stream.setBitOffset(i % 8); + assertEquals(String.format("bit %d differ", i), (value << (i % 8)) >>> (64L - i), stream.readBits(i)); + assertEquals(i * 2L % 8, stream.getBitOffset()); + } + } + } + + @Test + public void testReadShort() throws IOException { + byte[] bytes = new byte[31]; + InputStream input = randomData(bytes); + + ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + stream.setByteOrder(ByteOrder.BIG_ENDIAN); + + for (int i = 0; i < bytes.length / 2; i++) { + assertEquals(buffer.getShort(), stream.readShort()); + } + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readShort(); + } + }); + } + + try (DirectImageInputStream stream = new DirectImageInputStream(new ByteArrayInputStream(bytes))) { + stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); + buffer.position(0); + buffer.order(ByteOrder.LITTLE_ENDIAN); + + for (int i = 0; i < bytes.length / 2; i++) { + assertEquals(buffer.getShort(), stream.readShort()); + } + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readShort(); + } + }); + } + } + + @Test + public void testReadInt() throws IOException { + byte[] bytes = new byte[31]; + InputStream input = randomData(bytes); + ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); + + // Create stream + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + stream.setByteOrder(ByteOrder.BIG_ENDIAN); + + for (int i = 0; i < bytes.length / 4; i++) { + assertEquals(buffer.getInt(), stream.readInt()); + } + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readInt(); + } + }); + } + + try (DirectImageInputStream stream = new DirectImageInputStream(new ByteArrayInputStream(bytes))) { + stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); + buffer.position(0); + buffer.order(ByteOrder.LITTLE_ENDIAN); + + for (int i = 0; i < bytes.length / 4; i++) { + assertEquals(buffer.getInt(), stream.readInt()); + } + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readInt(); + } + }); + } + } + + @Test + public void testReadLong() throws IOException { + byte[] bytes = new byte[8743]; // Slightly more than one buffer size + InputStream input = randomData(bytes); + ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + stream.setByteOrder(ByteOrder.BIG_ENDIAN); + + for (int i = 0; i < bytes.length / 8; i++) { + assertEquals(buffer.getLong(), stream.readLong()); + } + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readLong(); + } + }); + } + + try (DirectImageInputStream stream = new DirectImageInputStream(new ByteArrayInputStream(bytes))) { + stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); + buffer.position(0); + buffer.order(ByteOrder.LITTLE_ENDIAN); + + for (int i = 0; i < bytes.length / 8; i++) { + assertEquals(buffer.getLong(), stream.readLong()); + } + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readLong(); + } + }); + } + } + + @Test + public void testSeekPastEOF() throws IOException { + byte[] bytes = new byte[9]; + InputStream input = randomData(bytes); + + try (DirectImageInputStream stream = new DirectImageInputStream(input)) { + stream.seek(1000); + + assertEquals(-1, stream.read()); + assertEquals(-1, stream.read(new byte[1], 0, 1)); + + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readFully(new byte[1]); + } + }); + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readByte(); + } + }); + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readShort(); + } + }); + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readInt(); + } + }); + assertThrows(EOFException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stream.readLong(); + } + }); + } + + try (DirectImageInputStream stream = new DirectImageInputStream(new ByteArrayInputStream(bytes))) { + for (byte value : bytes) { + assertEquals(value, stream.readByte()); + } + + assertEquals(-1, stream.read()); + } + } + + @Test + public void testClose() throws IOException { + // Create wrapper stream + InputStream input = mock(InputStream.class); + ImageInputStream stream = new DirectImageInputStream(input); + + stream.close(); + verify(input, only()).close(); + } +}