sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1486988 - in /sis/branches/JDK7/storage/sis-storage/src: main/java/org/apache/sis/internal/storage/ test/java/org/apache/sis/internal/storage/ test/java/org/apache/sis/test/suite/
Date Tue, 28 May 2013 16:21:37 GMT
Author: desruisseaux
Date: Tue May 28 16:21:37 2013
New Revision: 1486988

URL: http://svn.apache.org/r1486988
Log:
Moved ChannelImageInputStream in main code and added test case. This is a little bit cleaner
than the strange beast living in the test directory that we had, and will be needed anyway
when we will use some part of the javax.imageio package.

Added:
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
      - copied, changed from r1486860, sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java
  (with props)
Removed:
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelDataInputCompleted.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
Modified:
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelDataInputTest.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java?rev=1486988&r1=1486987&r2=1486988&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
[UTF-8] Tue May 28 16:21:37 2013
@@ -53,13 +53,9 @@ import java.nio.channels.SeekableByteCha
  * This class API is compatibly with the {@link java.io.DataInput} interface, so subclasses
can implement that
  * interface if they wish. This class does not implement {@code DataInput} itself because
it is not needed for
  * SIS purposes, and because {@code DataInput} has undesirable methods ({@code readLine()}
and {@code readUTF()}).
- * However the {@code ChannelDataInputCompleted} class in the test directory implements the
{@code DataInput}
- * interface, both for testing API compatibility and in case we choose to implement that
interface after all
- * in a future SIS version.
- *
- * <p>The API of this class is also compatible with {@link javax.imageio.stream.ImageInputStream}.
- * See {@code ChannelImageInputStream} in the test directory if a channel-based implementation
of
- * image input stream is desired in a future SIS version.</p>
+ * However the {@link ChannelImageInputStream} class implements the {@code DataInput} interface,
together with
+ * the {@link javax.imageio.stream.ImageInputStream} one, mostly for situations when inter-operability
with
+ * {@link javax.imageio} is needed.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.07)
@@ -697,6 +693,21 @@ public class ChannelDataInput {
     }
 
     /**
+     * Sets the current byte position of the stream. This method does <strong>not</strong>
seeks the stream;
+     * this method only modifies the value to be returned by {@link #getStreamPosition()}.
This method can
+     * be invoked when some external code has performed some work with the {@linkplain #channel}
and wants
+     * to inform this {@code ChannelDataInput} about the new position resulting from this
work.
+     *
+     * <p>This method does not need to be invoked when only the {@linkplain Buffer#position()
buffer position}
+     * has changed.</p>
+     *
+     * @param position The new position of the stream.
+     */
+    public final void setStreamPosition(final long position) {
+        bufferOffset = position - buffer.position();
+    }
+
+    /**
      * Returns the current byte position of the stream.
      *
      * @return The position of the stream.

Copied: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
(from r1486860, sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStream.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java?p2=sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java&p1=sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStream.java&r1=1486860&r2=1486988&rev=1486988&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
[UTF-8] Tue May 28 16:21:37 2013
@@ -16,12 +16,14 @@
  */
 package org.apache.sis.internal.storage;
 
+import java.io.DataInputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.channels.ReadableByteChannel;
 import javax.imageio.stream.IIOByteBuffer;
 import javax.imageio.stream.ImageInputStream;
+import org.apache.sis.util.resources.Errors;
 
 import static org.apache.sis.util.ArgumentChecks.ensureBetween;
 
@@ -31,10 +33,7 @@ import java.nio.channels.SeekableByteCha
 
 /**
  * Adds the missing methods in {@code ChannelDataInput} for implementing the {@code ImageInputStream}
interface.
- * This class is provided for testing the compatibility of {@code ChannelDataInput} API with
{@code ImageInputStream},
- * and as a placeholder in case we want to move this implementation in the main code in a
future SIS version.
- *
- * <p>Note that the JDK approach for creating an image input stream from a channel
would be as below:</p>
+ * The JDK approach for creating an image input stream from a channel would be as below:
  *
  * {@preformat java
  *     ReadableByteChannel channel = ...;
@@ -43,13 +42,10 @@ import java.nio.channels.SeekableByteCha
  *
  * However the standard {@link javax.imageio.stream.ImageInputStreamImpl} implementation
performs many work by itself,
  * including supporting various {@linkplain ByteOrder byte order}, which could be more efficiently
done by NIO.
+ * Furthermore, this class allows us to reuse an existing buffer (especially direct buffer,
which are costly to create)
+ * and allow subclasses to store additional information, for example the file path.
  *
- * <p>This class is provided as a <em>proof of concept</em> only - it is
not intended to be used in the main SIS code.
- * This class lives in the {@code test} directory for that reason, where the "test" is to
ensure that the API of the
- * {@link ChannelDataInput} base class is compatible with the {@link ImageInputStream} API.</p>
- *
- * <p>Note in particular that the {@link #readBit()} and {@link #readBits(int)} methods
are not strictly compliant
- * to the {@code ImageInputStream} contract.</p>
+ * <p>This class is used when compatibility with {@link javax.imageio.ImageReader}
is needed.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.07)
@@ -60,16 +56,29 @@ import java.nio.channels.SeekableByteCha
  * @see javax.imageio.ImageIO#createImageInputStream(Object)
  * @see java.nio.channels.Channels#newInputStream(ReadableByteChannel)
  */
-public class ChannelImageInputStream extends ChannelDataInputCompleted implements ImageInputStream
{
+public class ChannelImageInputStream extends ChannelDataInput implements ImageInputStream
{
     /**
-     * The current bit offset within the stream.
+     * Number of bits needed for storing the bit offset in {@link #bitPosition}.
+     * The following condition must hold:
+     *
+     * {@preformat java
+     *     (1 << BIT_OFFSET_SIZE) == Byte.SIZE
+     * }
+     */
+    private static final int BIT_OFFSET_SIZE = 3;
+
+    /**
+     * The current bit position within the stream. The 3 lowest bits are the bit offset,
+     * and the remaining of the {@code long} value is the stream position where the bit
+     * offset is valid.
      *
      * @see #getBitOffset()
      */
-    private int bitOffset;
+    private long bitPosition;
 
     /**
      * The most recent mark, or {@code null} if none.
+     * This is the tail of a chained list of marks.
      */
     private Mark mark;
 
@@ -158,74 +167,47 @@ public class ChannelImageInputStream ext
     /**
      * Returns the current bit offset, as an integer between 0 and 7 inclusive.
      *
-     * {@section Contract violation}
-     * According {@link ImageInputStream} contract, the bit offset shall be reset to 0 by
every call to
-     * any {@code read} method except {@code readBit()} and {@link #readBits(int)}. This
is not done in
-     * this {@code ChannelImageInputStream} class because the {@code read} methods are final,
and we
-     * don't want to alter the main SIS code just for this "proof of concept" class.
+     * <p>According {@link ImageInputStream} contract, the bit offset shall be reset
to 0 by every call to
+     * any {@code read} method except {@code readBit()} and {@link #readBits(int)}.</p>
      *
      * @return The bit offset of the stream.
-     * @throws IOException if an I/O error occurs.
      */
     @Override
-    public final int getBitOffset() throws IOException {
-        return bitOffset;
+    public final int getBitOffset() {
+        final long currentPosition = getStreamPosition();
+        if ((bitPosition >>> BIT_OFFSET_SIZE) != currentPosition) {
+            bitPosition = currentPosition << BIT_OFFSET_SIZE;
+        }
+        return (int) (bitPosition & (Byte.SIZE - 1));
     }
 
     /**
      * Sets the bit offset to the given value.
      *
-     * {@section Contract violation}
-     * According {@link ImageInputStream} contract, the bit offset shall be reset to 0 by
every call to
-     * any {@code read} method except {@code readBit()} and {@link #readBits(int)}. This
is not done in
-     * this {@code ChannelImageInputStream} class because the {@code read} methods are final,
and we
-     * don't want to alter the main SIS code just for this "proof of concept" class.
-     *
      * @param bitOffset The new bit offset of the stream.
-     * @throws IOException if an I/O error occurs.
      */
     @Override
-    public final void setBitOffset(final int bitOffset) throws IOException {
-        ensureBetween("bitOffset", 0, Byte.SIZE-1, bitOffset);
-        this.bitOffset = bitOffset;
+    public final void setBitOffset(final int bitOffset) {
+        ensureBetween("bitOffset", 0, Byte.SIZE - 1, bitOffset);
+        bitPosition = (getStreamPosition() << BIT_OFFSET_SIZE) | bitOffset;
     }
 
     /**
      * Reads a single bit from the stream. The bit to be read depends on the
      * {@linkplain #getBitOffset() current bit offset}.
      *
-     * {@section Contract violation}
-     * According {@link ImageInputStream} contract, the bit offset shall be reset to 0 by
every call to
-     * any {@code read} method except {@code readBit()} and {@link #readBits(int)}. This
is not done in
-     * this {@code ChannelImageInputStream} class because the {@code read} methods are final,
and we
-     * don't want to alter the main SIS code just for this "proof of concept" class.
-     *
      * @return The value of the next bit from the stream.
      * @throws IOException If an error occurred while reading (including EOF).
      */
     @Override
     public final int readBit() throws IOException {
-        int value = readUnsignedByte();
-        final int toShift = (Byte.SIZE - ++bitOffset);
-        if (toShift == 0) {
-            bitOffset = 0;
-        } else {
-            pushBack();
-            value >>= toShift;
-        }
-        return value & 1;
+        return (int) (readBits(1) & 1);
     }
 
     /**
      * Reads many bits from the stream. The first bit to be read depends on the
      * {@linkplain #getBitOffset() current bit offset}.
      *
-     * {@section Contract violation}
-     * According {@link ImageInputStream} contract, the bit offset shall be reset to 0 by
every call to
-     * any {@code read} method except {@code readBit()} and {@link #readBits(int)}. This
is not done in
-     * this {@code ChannelImageInputStream} class because the {@code read} methods are final,
and we
-     * don't want to alter the main SIS code just for this "proof of concept" class.
-     *
      * @param  numBits The number of bits to read.
      * @return The value of the next bits from the stream.
      * @throws IOException If an error occurred while reading (including EOF).
@@ -241,6 +223,7 @@ public class ChannelImageInputStream ext
          * and compute the number of bits that still need to be read. That number may
          * be negative if we have read too many bits.
          */
+        final int bitOffset = getBitOffset();
         long value = readByte() & (0xFF >>> bitOffset);
         numBits -= (Byte.SIZE - bitOffset);
         while (numBits > 0) {
@@ -249,30 +232,90 @@ public class ChannelImageInputStream ext
         }
         if (numBits != 0) {
             value >>>= (-numBits); // Discard the unwanted bits.
-            bitOffset = Byte.SIZE + numBits;
+            numBits += Byte.SIZE;
             pushBack();
-        } else {
-            bitOffset = 0;
         }
+        setBitOffset(numBits);
         return value;
     }
 
     /**
-     * Reads up to {@code length} bytes from the stream, and modifies the supplied
-     * {@code IIOByteBuffer} to indicate the byte array, offset, and length where
-     * the data may be found.
+     * Reads a byte from the stream and returns a {@code true} if it is nonzero, {@code false}
otherwise.
+     * The implementation is as below:
      *
-     * @param  dest The buffer to be written to.
-     * @param  length The maximum number of bytes to read.
+     * {@preformat java
+     *     return readByte() != 0;
+     * }
+     *
+     * @return The value of the next boolean from the stream.
+     * @throws IOException If an error (including EOF) occurred while reading the stream.
+     */
+    @Override
+    public final boolean readBoolean() throws IOException {
+        return readByte() != 0;
+    }
+
+    /**
+     * Reads in a string that has been encoded using a UTF-8 string.
+     *
+     * @return The string reads from the stream.
+     * @throws IOException If an error (including EOF) occurred while reading the stream.
+     */
+    @Override
+    public final String readUTF() throws IOException {
+        final ByteOrder oldOrder = buffer.order();
+        buffer.order(ByteOrder.BIG_ENDIAN);
+        try {
+            return DataInputStream.readUTF(this);
+        } finally {
+            buffer.order(oldOrder);
+        }
+    }
+
+    /**
+     * Reads the new bytes until the next EOL. This method can read only US-ASCII strings.
+     * This method is provided for compliance with the {@link DataInput} interface,
+     * but is generally not recommended.
+     *
+     * @return The next line, or {@code null} if the EOF has been reached.
      * @throws IOException If an error occurred while reading.
      */
     @Override
-    public final void readBytes(final IIOByteBuffer dest, int length) throws IOException
{
-        final byte[] data = new byte[length];
-        length = read(data);
-        dest.setData(data);
-        dest.setOffset(0);
-        dest.setLength(length);
+    public final String readLine() throws IOException {
+        int c = read();
+        if (c < 0) {
+            return null;
+        }
+        StringBuilder line = new StringBuilder();
+        line.append((char) c);
+loop:   while ((c = read()) >= 0) {
+            switch (c) {
+                case '\r': {
+                    c = read();
+                    if (c >= 0 && c != '\n') {
+                        pushBack();
+                    }
+                    break loop;
+                }
+                case '\n': {
+                    break loop;
+                }
+            }
+            line.append((char) c);
+        }
+        return line.toString();
+    }
+
+    /**
+     * Returns the next byte from the stream as an unsigned integer between 0 and 255,
+     * or -1 if we reached the end of stream.
+     *
+     * @return The next byte as an unsigned integer, or -1 on end of stream.
+     * @throws IOException If an error occurred while reading the stream.
+     */
+    @Override
+    public final int read() throws IOException {
+        return hasRemaining() ? buffer.get() & 0xFF : -1;
     }
 
     /**
@@ -308,7 +351,6 @@ public class ChannelImageInputStream ext
         if (!hasRemaining()) {
             return -1;
         }
-        bitOffset = 0;
         final int requested = length;
         while (length != 0 && hasRemaining()) {
             final int n = Math.min(buffer.remaining(), length);
@@ -320,20 +362,55 @@ public class ChannelImageInputStream ext
     }
 
     /**
+     * Reads up to {@code length} bytes from the stream, and modifies the supplied
+     * {@code IIOByteBuffer} to indicate the byte array, offset, and length where
+     * the data may be found.
+     *
+     * @param  dest The buffer to be written to.
+     * @param  length The maximum number of bytes to read.
+     * @throws IOException If an error occurred while reading.
+     */
+    @Override
+    public final void readBytes(final IIOByteBuffer dest, int length) throws IOException
{
+        final byte[] data = new byte[length];
+        length = read(data);
+        dest.setData(data);
+        dest.setOffset(0);
+        dest.setLength(length);
+    }
+
+    /**
+     * Skips over <var>n</var> bytes of data from the input stream.
+     * This implementation does not skip more bytes than the buffer capacity.
+     *
+     * @param  n Maximal number of bytes to skip.
+     * @return Number of bytes actually skipped.
+     * @throws IOException If an error occurred while reading.
+     */
+    @Override
+    public final int skipBytes(int n) throws IOException {
+        if (!hasRemaining()) {
+            return 0;
+        }
+        int r = buffer.remaining();
+        if (n >= r) {
+            n = r;
+        }
+        buffer.position(buffer.position() + n);
+        return n;
+    }
+
+    /**
      * Advances the current stream position by the given amount of bytes.
      * The bit offset is reset to 0 by this method.
      *
      * @param  n The number of bytes to seek forward.
      * @return The number of bytes skipped.
-     * @throws IOException If an error occurred while skiping.
+     * @throws IOException If an error occurred while skipping.
      */
     @Override
     public final long skipBytes(final long n) throws IOException {
-        bitOffset = 0;
-        if (n > Integer.MAX_VALUE) {
-            throw new UnsupportedOperationException();
-        }
-        return skipBytes((int) n);
+        return skipBytes(Math.min((int) n, Integer.MAX_VALUE));
     }
 
     /**
@@ -341,7 +418,7 @@ public class ChannelImageInputStream ext
      */
     @Override
     public final void mark() {
-        mark = new Mark(getStreamPosition(), bitOffset, mark);
+        mark = new Mark(getStreamPosition(), getBitOffset(), mark);
     }
 
     /**
@@ -357,7 +434,7 @@ public class ChannelImageInputStream ext
             throw new IOException("No marked position.");
         }
         seek(mark.position);
-        bitOffset = mark.bitOffset;
+        setBitOffset(mark.bitOffset);
         mark = mark.next;
     }
 
@@ -381,12 +458,23 @@ public class ChannelImageInputStream ext
      * Attempting to {@linkplain #seek(long) seek} to an offset within the flushed
      * portion of the stream will result in an {@link IndexOutOfBoundsException}.
      *
-     * @param  pos The length of the stream prefix that may be flushed.
+     * @param  position The length of the stream prefix that may be flushed.
      * @throws IOException If an I/O error occurred.
      */
     @Override
-    public final void flushBefore(long pos) throws IOException {
-        // No-op for now.
+    public final void flushBefore(final long position) throws IOException {
+        final long bufferOffset    = getFlushedPosition();
+        final long currentPosition = getStreamPosition();
+        if (position < bufferOffset || position > currentPosition) {
+            throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.ValueOutOfRange_4,
+                    "position", bufferOffset, currentPosition, position));
+        }
+        final int n = (int) (position - bufferOffset);
+        final int p = buffer.position() - n;
+        final int r = buffer.limit() - n;
+        buffer.position(n); // Number of bytes to forget.
+        buffer.compact().position(p).limit(r);
+        setStreamPosition(currentPosition);
     }
 
     /**

Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelDataInputTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelDataInputTest.java?rev=1486988&r1=1486987&r2=1486988&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelDataInputTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelDataInputTest.java
[UTF-8] Tue May 28 16:21:37 2013
@@ -173,7 +173,7 @@ public final strictfp class ChannelDataI
      * @param length The length of the array to create.
      * @param random The random number generator to use.
      */
-    private static byte[] createRandomArray(final int length, final Random random) {
+    static byte[] createRandomArray(final int length, final Random random) {
         final byte[] array = new byte[length];
         for (int i=0; i<length; i++) {
             array[i] = (byte) random.nextInt(256);

Added: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java?rev=1486988&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java
[UTF-8] Tue May 28 16:21:37 2013
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.sis.internal.storage;
+
+import java.util.Arrays;
+import java.util.Random;
+import java.nio.ByteOrder;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.imageio.stream.ImageInputStream;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.apache.sis.test.TestUtilities;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link ChannelImageInputStream}. A buffer is filled with random data
+ * and a standard {@link ImageInputStream} is used for comparison purpose.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.07)
+ * @version 0.3
+ * @module
+ */
+@DependsOn(ChannelDataInputTest.class)
+public final strictfp class ChannelImageInputStreamTest extends TestCase {
+    /**
+     * The maximal size of the arrays to be used for the tests, in bytes.
+     */
+    private static final int ARRAY_MAX_SIZE = 512;
+
+    /**
+     * Fills a buffer with random data and compares the result with a standard image input
stream.
+     * We will allocate a small buffer for the {@code ChannelImageInputStream} in order to
force
+     * frequent interactions between the buffer and the channel.
+     *
+     * @throws IOException Should never happen.
+     */
+    @Test
+    public void testWithRandomData() throws IOException {
+        long position = 0;
+        int bitOffset = 0;
+        int operation = 0;
+        final ByteBuffer buffer = ByteBuffer.allocate(128);
+        final Random random = TestUtilities.createRandomNumberGenerator("testWithRandomData");
+        final ByteOrder byteOrder = random.nextBoolean() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
+        final byte[] data = ChannelDataInputTest.createRandomArray(512 * 1024, random);
+        try (ImageInputStream r = ImageIO.createImageInputStream(new ByteArrayInputStream(data));
+             ImageInputStream t = new ChannelImageInputStream("Test data",
+                     Channels.newChannel(new ByteArrayInputStream(data)), buffer, false))
+        {
+            r.setByteOrder(byteOrder);
+            t.setByteOrder(byteOrder);
+            while ((position = r.getStreamPosition()) < data.length - ARRAY_MAX_SIZE)
{
+                bitOffset = r.getBitOffset();
+                operation = random.nextInt(24);
+                switch (operation) {
+                    default: throw new AssertionError(operation);
+                    case  0: assertEquals("read()",              r.read(),              t.read());
             break;
+                    case  1: assertEquals("readBoolean()",       r.readBoolean(),       t.readBoolean());
      break;
+                    case  2: assertEquals("readChar()",          r.readChar(),          t.readChar());
         break;
+                    case  3: assertEquals("readByte()",          r.readByte(),          t.readByte());
         break;
+                    case  4: assertEquals("readShort()",         r.readShort(),         t.readShort());
        break;
+                    case  5: assertEquals("readUnsignedShort()", r.readUnsignedShort(), t.readUnsignedShort());
break;
+                    case  6: assertEquals("readInt()",           r.readInt(),           t.readInt());
          break;
+                    case  7: assertEquals("readUnsignedInt()",   r.readUnsignedInt(),   t.readUnsignedInt());
  break;
+                    case  8: assertEquals("readLong()",          r.readLong(),          t.readLong());
         break;
+                    case  9: assertEquals("readFloat()",         r.readFloat(),         t.readFloat(),
 0f);    break;
+                    case 10: assertEquals("readDouble()",        r.readDouble(),        t.readDouble(),
0d);    break;
+                    case 11: assertEquals("readBit()",           r.readBit(),           t.readBit());
          break;
+                    case 12: {
+                        final int n = random.nextInt(Long.SIZE + 1);
+                        assertEquals("readBits(" + n + ')', r.readBits(n), t.readBits(n));
+                        break;
+                    }
+                    case 13: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE);
+                        final byte[] actual = new byte[length];
+                        final int n = t.read(actual);
+                        assertFalse("Reached EOF", n < 0);
+                        final byte[] expected = new byte[n];
+                        r.readFully(expected);
+                        assertArrayEquals("read(byte[])", expected, actual);
+                        break;
+                    }
+                    case 14: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE);
+                        final byte[] expected = new byte[length]; r.readFully(expected);
+                        final byte[] actual   = new byte[length]; t.readFully(actual);
+                        assertArrayEquals("readFully(byte[])", expected, actual);
+                        break;
+                    }
+                    case 15: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE * Byte.SIZE / Character.SIZE);
+                        final char[] expected = new char[length]; r.readFully(expected, 0,
length);
+                        final char[] actual   = new char[length]; t.readFully(actual,   0,
length);
+                        assertArrayEquals("readFully(char[])", expected, actual);
+                        break;
+                    }
+                    case 16: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE * Byte.SIZE / Short.SIZE);
+                        final short[] expected = new short[length]; r.readFully(expected,
0, length);
+                        final short[] actual   = new short[length]; t.readFully(actual, 
 0, length);
+                        assertArrayEquals("readFully(short[])", expected, actual);
+                        break;
+                    }
+                    case 17: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE * Byte.SIZE / Integer.SIZE);
+                        final int[] expected = new int[length]; r.readFully(expected, 0,
length);
+                        final int[] actual   = new int[length]; t.readFully(actual,   0,
length);
+                        assertArrayEquals("readFully(int[])", expected, actual);
+                        break;
+                    }
+                    case 18: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE * Byte.SIZE / Long.SIZE);
+                        final long[] expected = new long[length]; r.readFully(expected, 0,
length);
+                        final long[] actual   = new long[length]; t.readFully(actual,   0,
length);
+                        assertArrayEquals("readFully(long[])", expected, actual);
+                        break;
+                    }
+                    case 19: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE * Byte.SIZE / Float.SIZE);
+                        final float[] expected = new float[length]; r.readFully(expected,
0, length);
+                        final float[] actual   = new float[length]; t.readFully(actual, 
 0, length);
+                        assertTrue("readFully(float[])", Arrays.equals(expected, actual));
+                        break;
+                    }
+                    case 20: {
+                        final int length = random.nextInt(ARRAY_MAX_SIZE * Byte.SIZE / Double.SIZE);
+                        final double[] expected = new double[length]; r.readFully(expected,
0, length);
+                        final double[] actual   = new double[length]; t.readFully(actual,
  0, length);
+                        assertTrue("readFully(double[])", Arrays.equals(expected, actual));
+                        break;
+                    }
+                    case 21: {
+                        final long length = random.nextInt(ARRAY_MAX_SIZE);
+                        final long n = t.skipBytes(length);
+                        assertFalse("Reached EOF", n < 0);
+                        r.readFully(new byte[(int) n]);
+                        assertEquals("skipBytes(int)", r.getStreamPosition(), t.getStreamPosition());
+                        break;
+                    }
+                    case 22: {
+                        long flushedPosition = StrictMath.max(r.getFlushedPosition(), t.getFlushedPosition());
+                        flushedPosition += random.nextInt(1 + (int) (r.getStreamPosition()
- flushedPosition));
+                        r.flushBefore(flushedPosition);
+                        t.flushBefore(flushedPosition);
+                        break;
+                    }
+                    case 23: {
+                        r.flush();
+                        t.flush();
+                        break;
+                    }
+                }
+                assertEquals("getStreamPosition()", r.getStreamPosition(), t.getStreamPosition());
+                assertEquals("getBitOffset()",      r.getBitOffset(),      t.getBitOffset());
+            }
+        } catch (AssertionError | RuntimeException e) {
+            out.println("Position:    " + position);
+            out.println("Bit offset:  " + bitOffset);
+            out.println("Byte order:  " + byteOrder);
+            out.println("Operation:   " + operation);
+            out.println("Exception:   " + e.getLocalizedMessage());
+            throw e;
+        }
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ChannelImageInputStreamTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java?rev=1486988&r1=1486987&r2=1486988&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] Tue May 28 16:21:37 2013
@@ -30,7 +30,8 @@ import org.junit.BeforeClass;
  * @module
  */
 @Suite.SuiteClasses({
-    org.apache.sis.internal.storage.ChannelDataInputTest.class
+    org.apache.sis.internal.storage.ChannelDataInputTest.class,
+    org.apache.sis.internal.storage.ChannelImageInputStreamTest.class
 })
 public final strictfp class StorageTestSuite extends TestSuite {
     /**



Mime
View raw message