You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2014/04/24 13:39:05 UTC
svn commit: r1589674 - in
/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage:
ChannelData.java ChannelDataInput.java ChannelDataOutput.java
ChannelImageInputStream.java
Author: desruisseaux
Date: Thu Apr 24 11:39:05 2014
New Revision: 1589674
URL: http://svn.apache.org/r1589674
Log:
Move more common code to the ChannelData parent class.
Modified:
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java?rev=1589674&r1=1589673&r2=1589674&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java [UTF-8] Thu Apr 24 11:39:05 2014
@@ -19,19 +19,36 @@ package org.apache.sis.internal.storage;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
-import java.nio.channels.SeekableByteChannel;
import org.apache.sis.util.Debug;
+import org.apache.sis.util.resources.Errors;
+
+import static org.apache.sis.util.ArgumentChecks.ensureBetween;
+
+// Related to JDK7
+import java.nio.channels.SeekableByteChannel;
/**
* Properties common to {@link ChannelDataInput} and {@link ChannelDataOutput}.
- * This base class contains a reference to a {@link ByteBuffer} supplied by the user.
+ * Common properties include a reference to a {@link ByteBuffer} supplied by the user and methods for
+ * querying or modifying the stream position. This class does not define any read or write operations.
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.5 (derived from 0.3)
+ * @version 0.5
* @module
*/
-abstract class ChannelData {
+public abstract class ChannelData {
+ /**
+ * 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;
+
/**
* A file identifier used only for formatting error message.
*/
@@ -61,43 +78,93 @@ abstract class ChannelData {
long bufferOffset;
/**
+ * 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 long bitPosition;
+
+ /**
+ * The most recent mark, or {@code null} if none.
+ * This is the tail of a chained list of marks.
+ */
+ private Mark mark;
+
+ /**
+ * A mark pushed by the {@link ChannelDate#mark()} method
+ * and pooled by the {@link ChannelDate#reset()} method.
+ */
+ private static final class Mark {
+ final long position;
+ final byte bitOffset;
+ Mark next;
+
+ Mark(long position, byte bitOffset, Mark next) {
+ this.position = position;
+ this.bitOffset = bitOffset;
+ this.next = next;
+ }
+ }
+
+ /**
* Creates a new instance for the given channel and using the given buffer.
+ * The channel is not stored by this class - it shall be stored by the subclass.
*
* @param filename A file identifier used only for formatting error message.
* @param channel The channel from where data are read or where to wrote.
* @param buffer The buffer where to store the data.
* @throws IOException If an error occurred while reading the channel.
*/
- public ChannelData(final String filename, final Channel channel, final ByteBuffer buffer) throws IOException {
+ ChannelData(final String filename, final Channel channel, final ByteBuffer buffer) throws IOException {
this.filename = filename;
this.buffer = buffer;
this.channelOffset = (channel instanceof SeekableByteChannel) ? ((SeekableByteChannel) channel).position() : 0;
}
/**
- * Invoked when an operation between the channel and the buffer transfered no byte. Note that this is unrelated
- * to end-of-file, in which case {@link java.nio.channels.ReadableByteChannel#read(ByteBuffer)} returns -1.
- * A return value of 0 happen for example if the channel is a socket in non-blocking mode and the socket buffer
- * has not yet transfered new data.
+ * Pushes back the last processed byte. This is used when a call to {@code readBit()} did not
+ * used every bits in a byte, or when {@code readLine()} checked for the Windows-style of EOL.
+ */
+ final void pushBack() {
+ buffer.position(buffer.position() - 1);
+ }
+
+ /**
+ * Returns the current bit offset, as an integer between 0 and 7 inclusive.
*
- * <p>The current implementation sleeps an arbitrary amount of time before to allow a new try.
- * We do that in order to avoid high CPU consumption when data are expected to take more than
- * a few nanoseconds to arrive.</p>
+ * <p>According {@link javax.imageio.stream.ImageInputStream} contract, the bit offset shall be reset to 0
+ * by every call to any {@code read} or {@code write} method except {@code readBit()}, {@code readBits(int)},
+ * {@code writeBit(int)} and {@code writeBits(long, int)}.</p>
*
- * @throws IOException If the implementation chooses to stop the process.
+ * @return The bit offset of the stream.
*/
- protected void onEmptyChannelBuffer() throws IOException {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- /*
- * Someone doesn't want to let us sleep. Stop the reading or writing process. We don't try to go back to
- * work, because the waiting time was short and this method is invoked in loops. Consequently if the user
- * interrupted us, it is probably because he waited for a long time and we still have not transfered any
- * new data.
- */
- throw new IOException(e);
+ 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.
+ *
+ * @param bitOffset The new bit offset of the stream.
+ */
+ public final void setBitOffset(final int bitOffset) {
+ ensureBetween("bitOffset", 0, Byte.SIZE - 1, bitOffset);
+ bitPosition = (getStreamPosition() << BIT_OFFSET_SIZE) | bitOffset;
+ }
+
+ /**
+ * Returns the current byte position of the stream.
+ *
+ * @return The position of the stream.
+ */
+ public final long getStreamPosition() {
+ return bufferOffset + buffer.position();
}
/**
@@ -116,12 +183,109 @@ abstract class ChannelData {
}
/**
- * Returns the current byte position of the stream.
+ * Returns the earliest position in the stream to which {@linkplain #seek(long) seeking}
+ * may be performed.
*
- * @return The position of the stream.
+ * @return the earliest legal position for seeking.
*/
- public final long getStreamPosition() {
- return bufferOffset + buffer.position();
+ public final long getFlushedPosition() {
+ return bufferOffset;
+ }
+
+ /**
+ * Discards the initial portion of the stream prior to the indicated position.
+ * Attempting to {@linkplain #seek(long) seek} to an offset within the flushed
+ * portion of the stream will result in an {@link IndexOutOfBoundsException}.
+ *
+ * <p>This method moves the data starting at the given position to the beginning of the {@link #buffer},
+ * thus making more room for new data before the data at the given position is discarded.</p>
+ *
+ * @param position The length of the stream prefix that may be flushed.
+ * @throws IOException If an I/O error occurred.
+ */
+ public final void flushBefore(final long position) throws IOException {
+ 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);
+
+ // Discard obolete marks.
+ Mark parent = null;
+ for (Mark m = mark; m != null; m = m.next) {
+ if (m.position < position) {
+ if (parent != null) {
+ parent.next = null;
+ } else {
+ mark = null;
+ }
+ break;
+ }
+ parent = m;
+ }
+ }
+
+ /**
+ * Moves to the given position in the stream, relative to the stream position at construction time.
+ *
+ * @param position The position where to move.
+ * @throws IOException If the stream can not be moved to the given position.
+ */
+ public abstract void seek(final long position) throws IOException;
+
+ /**
+ * Pushes the current stream position onto a stack of marked positions.
+ */
+ public final void mark() {
+ mark = new Mark(getStreamPosition(), (byte) getBitOffset(), mark);
+ }
+
+ /**
+ * Resets the current stream byte and bit positions from the stack of marked positions.
+ * An {@code IOException} will be thrown if the previous marked position lies in the
+ * discarded portion of the stream.
+ *
+ * @throws IOException If an I/O error occurs.
+ */
+ public final void reset() throws IOException {
+ if (mark == null) {
+ throw new IOException("No marked position.");
+ }
+ seek(mark.position);
+ setBitOffset(mark.bitOffset);
+ mark = mark.next;
+ }
+
+ /**
+ * Invoked when an operation between the channel and the buffer transfered no byte. Note that this is unrelated
+ * to end-of-file, in which case {@link java.nio.channels.ReadableByteChannel#read(ByteBuffer)} returns -1.
+ * A return value of 0 happen for example if the channel is a socket in non-blocking mode and the socket buffer
+ * has not yet transfered new data.
+ *
+ * <p>The current implementation sleeps an arbitrary amount of time before to allow a new try.
+ * We do that in order to avoid high CPU consumption when data are expected to take more than
+ * a few nanoseconds to arrive.</p>
+ *
+ * @throws IOException If the implementation chooses to stop the process.
+ */
+ protected void onEmptyTransfer() throws IOException {
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ /*
+ * Someone doesn't want to let us sleep. Stop the reading or writing process. We don't try to go back to
+ * work, because the waiting time was short and this method is invoked in loops. Consequently if the user
+ * interrupted us, it is probably because he waited for a long time and we still have not transfered any
+ * new data.
+ */
+ throw new IOException(e);
+ }
}
/**
Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java?rev=1589674&r1=1589673&r2=1589674&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java [UTF-8] Thu Apr 24 11:39:05 2014
@@ -29,6 +29,8 @@ import java.nio.DoubleBuffer;
import java.nio.channels.ReadableByteChannel;
import org.apache.sis.util.resources.Errors;
+import static org.apache.sis.util.ArgumentChecks.ensureBetween;
+
// Related to JDK7
import java.nio.channels.SeekableByteChannel;
@@ -39,11 +41,11 @@ import java.nio.channels.SeekableByteCha
* before {@code ChannelDataInput} creation.
*
* {@section Encapsulation}
- * This class exposes publicly the {@linkplain #channel} and the {@linkplain #buffer} because it is not expected
- * to perform all possible data manipulations that we can do with the buffers. This class is only a helper tool,
- * which often needs to be completed by specialized operations performed directly on the buffer. However, users
- * are encouraged to transfer data from the channel to the buffer using only the methods provided in this class
- * if they want to keep the {@link #seek(long)} and {@link #getStreamPosition()} values accurate.
+ * This class exposes publicly the {@linkplain #channel} and the {@linkplain #buffer buffer} because this class
+ * is not expected to perform all possible data manipulations that we can do with the buffers. This class is only
+ * a helper tool, which often needs to be completed by specialized operations performed directly on the buffer.
+ * However, users are encouraged to transfer data from the channel to the buffer using only the methods provided
+ * in this class if they want to keep the {@link #seek(long)} and {@link #getStreamPosition()} values accurate.
*
* <p>Since this class is only a helper tool, it does not "own" the channel and consequently does not provide
* {@code close()} method. It is users responsibility to close the channel after usage.</p>
@@ -58,7 +60,7 @@ import java.nio.channels.SeekableByteCha
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.07)
- * @version 0.4
+ * @version 0.5
* @module
*/
public class ChannelDataInput extends ChannelData {
@@ -113,7 +115,7 @@ public class ChannelDataInput extends Ch
buffer.limit(capacity).position(limit);
int c = channel.read(buffer);
while (c == 0) {
- onEmptyChannelBuffer();
+ onEmptyTransfer();
c = channel.read(buffer);
}
buffer.limit(buffer.position()).position(position);
@@ -122,9 +124,9 @@ public class ChannelDataInput extends Ch
/**
* Returns {@code true} if the buffer or the channel has at least one byte remaining.
- * If the {@linkplain #buffer} has no remaining bytes, then this method will attempts
- * to read at least one byte from the {@linkplain #channel}. If no bytes can be read
- * because the channel has reached the end of stream, then this method returns {@code false}.
+ * If the {@linkplain #buffer buffer} has no remaining bytes, then this method will attempts
+ * to read at least one byte from the {@linkplain #channel}. If no bytes can be read because
+ * the channel has reached the end of stream, then this method returns {@code false}.
*
* @return {@code true} if the buffer contains at least one remaining byte.
* @throws IOException If it was necessary to read from the channel and this operation failed.
@@ -137,7 +139,7 @@ public class ChannelDataInput extends Ch
buffer.clear();
int c = channel.read(buffer);
while (c == 0) {
- onEmptyChannelBuffer();
+ onEmptyTransfer();
c = channel.read(buffer);
}
buffer.flip();
@@ -149,7 +151,7 @@ public class ChannelDataInput extends Ch
* It is caller's responsibility to ensure that the given number of bytes is
* not greater than the {@linkplain ByteBuffer#capacity() buffer capacity}.
*
- * @param n The minimal number of bytes needed in the {@linkplain #buffer}.
+ * @param n The minimal number of bytes needed in the {@linkplain #buffer buffer}.
* @throws EOFException If the channel has reached the end of stream.
* @throws IOException If an other kind of error occurred while reading.
*/
@@ -165,7 +167,7 @@ public class ChannelDataInput extends Ch
if (c != 0) {
throw new EOFException(eof());
}
- onEmptyChannelBuffer();
+ onEmptyTransfer();
}
n -= c;
} while (n > 0);
@@ -193,6 +195,51 @@ public class ChannelDataInput extends Ch
}
/**
+ * Reads a single bit from the stream. The bit to be read depends on the
+ * {@linkplain #getBitOffset() current bit offset}.
+ *
+ * @return The value of the next bit from the stream.
+ * @throws IOException If an error occurred while reading (including EOF).
+ */
+ public final int readBit() throws IOException {
+ 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}.
+ *
+ * @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).
+ */
+ public final long readBits(int numBits) throws IOException {
+ ensureBetween("numBits", 0, Long.SIZE, numBits);
+ if (numBits == 0) {
+ return 0;
+ }
+ /*
+ * Reads the bits available in the next bytes (all of them if bitOffset == 0)
+ * 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) {
+ value = (value << Byte.SIZE) | readUnsignedByte();
+ numBits -= Byte.SIZE;
+ }
+ if (numBits != 0) {
+ value >>>= (-numBits); // Discard the unwanted bits.
+ numBits += Byte.SIZE;
+ pushBack();
+ }
+ setBitOffset(numBits);
+ return value;
+ }
+
+ /**
* Reads the next byte value (8 bits) from the stream. This method ensures that there is at
* least 1 byte remaining in the buffer, reading new bytes from the channel if necessary,
* then delegates to {@link ByteBuffer#get()}.
@@ -644,6 +691,7 @@ public class ChannelDataInput extends Ch
* @param position The position where to move.
* @throws IOException If the stream can not be moved to the given position.
*/
+ @Override
public final void seek(final long position) throws IOException {
long p = position - bufferOffset;
if (p >= 0 && p <= buffer.limit()) {
@@ -677,7 +725,7 @@ public class ChannelDataInput extends Ch
if (c != 0) {
throw new EOFException(eof());
}
- onEmptyChannelBuffer();
+ onEmptyTransfer();
}
buffer.flip();
} while (p > buffer.limit());
Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java?rev=1589674&r1=1589673&r2=1589674&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java [UTF-8] Thu Apr 24 11:39:05 2014
@@ -39,11 +39,11 @@ import java.nio.channels.SeekableByteCha
* before {@code ChannelDataOutput} creation.
*
* {@section Encapsulation}
- * This class exposes publicly the {@linkplain #channel} and the {@linkplain #buffer} because it is not expected
- * to perform all possible data manipulations that we can do with the buffers. This class is only a helper tool,
- * which often needs to be completed by specialized operations performed directly on the buffer. However, users
- * are encouraged to transfer data from the buffer to the channel using only the methods provided in this class
- * if they want to keep the {@link #seek(long)} and {@link #getStreamPosition()} values accurate.
+ * This class exposes publicly the {@linkplain #channel} and the {@linkplain #buffer buffer} because this class
+ * is not expected to perform all possible data manipulations that we can do with the buffers. This class is only
+ * a helper tool, which often needs to be completed by specialized operations performed directly on the buffer.
+ * However, users are encouraged to transfer data from the buffer to the channel using only the methods provided
+ * in this class if they want to keep the {@link #seek(long)} and {@link #getStreamPosition()} values accurate.
*
* <p>Since this class is only a helper tool, it does not "own" the channel and consequently does not provide
* {@code close()} method. It is users responsibility to close the channel after usage.</p>
@@ -93,10 +93,10 @@ public class ChannelDataOutput extends C
* <p>After this method call, the buffer {@linkplain ByteBuffer#limit() limit}
* will be equal or greater than {@code position + n}.</p>
*
- * @param n The minimal number of additional bytes that the {@linkplain #buffer} shall accept.
+ * @param n The minimal number of additional bytes that the {@linkplain #buffer buffer} shall accept.
* @throws IOException If an error occurred while writing to the channel.
*/
- final void ensureBufferAccept(final int n) throws IOException {
+ private void ensureBufferAccepts(final int n) throws IOException {
final int capacity = buffer.capacity();
assert n >= 0 && n <= capacity : n;
int after = buffer.position() + n;
@@ -111,7 +111,7 @@ public class ChannelDataOutput extends C
do {
final int c = channel.write(buffer);
if (c == 0) {
- onEmptyChannelBuffer();
+ onEmptyTransfer();
}
after -= c;
} while (after > capacity);
@@ -148,7 +148,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeByte(final int v) throws IOException {
- ensureBufferAccept(Byte.BYTES);
+ ensureBufferAccepts(Byte.BYTES);
buffer.put((byte) v);
}
@@ -162,7 +162,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeShort(final int v) throws IOException {
- ensureBufferAccept(Short.BYTES);
+ ensureBufferAccepts(Short.BYTES);
buffer.putShort((short) v);
}
@@ -175,7 +175,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeChar(final int v) throws IOException {
- ensureBufferAccept(Character.BYTES);
+ ensureBufferAccepts(Character.BYTES);
buffer.putChar((char) v);
}
@@ -188,7 +188,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeInt(final int v) throws IOException {
- ensureBufferAccept(Integer.BYTES);
+ ensureBufferAccepts(Integer.BYTES);
buffer.putInt(v);
}
@@ -201,7 +201,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeLong(final long v) throws IOException {
- ensureBufferAccept(Long.BYTES);
+ ensureBufferAccepts(Long.BYTES);
buffer.putLong(v);
}
@@ -214,7 +214,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeFloat(final float v) throws IOException {
- ensureBufferAccept(Float.BYTES);
+ ensureBufferAccepts(Float.BYTES);
buffer.putFloat(v);
}
@@ -227,7 +227,7 @@ public class ChannelDataOutput extends C
* @throws IOException if some I/O exception occurs during writing.
*/
public final void writeDouble(final double v) throws IOException {
- ensureBufferAccept(Double.BYTES);
+ ensureBufferAccepts(Double.BYTES);
buffer.putDouble(v);
}
@@ -347,7 +347,7 @@ public class ChannelDataOutput extends C
public void write(final byte[] src, int offset, int length) throws IOException {
while (length != 0) {
final int n = Math.min(buffer.capacity(), length);
- ensureBufferAccept(n);
+ ensureBufferAccepts(n);
buffer.put(src, offset, n);
offset += n;
length -= n;
@@ -390,14 +390,14 @@ public class ChannelDataOutput extends C
* @throws IOException If an error occurred while writing the stream.
*/
final void writeFully(final int dataSize, int offset, int length) throws IOException {
- ensureBufferAccept(Math.min(length * dataSize, buffer.capacity()));
+ ensureBufferAccepts(Math.min(length * dataSize, buffer.capacity()));
final Buffer view = createView(); // Must be after ensureBufferAccept
int n = Math.min(view.remaining(), length);
transfer(offset, n);
skipInBuffer(n * dataSize);
while ((length -= n) != 0) {
offset += n;
- ensureBufferAccept(Math.min(length, view.capacity()) * dataSize);
+ ensureBufferAccepts(Math.min(length, view.capacity()) * dataSize);
view.position(0).limit(buffer.remaining() / dataSize);
transfer(offset, n = view.remaining());
skipInBuffer(n * dataSize);
@@ -507,6 +507,7 @@ public class ChannelDataOutput extends C
* @param position The position where to move.
* @throws IOException If the stream can not be moved to the given position.
*/
+ @Override
public void seek(final long position) throws IOException {
long p = position - bufferOffset;
if (p >= 0 && p <= buffer.limit()) {
@@ -523,13 +524,13 @@ public class ChannelDataOutput extends C
((SeekableByteChannel) channel).position(channelOffset + position);
bufferOffset = position;
} else {
- //-- we can not move buffer more beyond channel.size --//
+ // We can not move position beyond the buffered part.
throw new IOException(Errors.format(Errors.Keys.StreamIsForwardOnly_1, filename));
}
}
/**
- * Flushes the {@link #buffer} content to the channel.
+ * Flushes the {@link #buffer buffer} content to the channel.
*
* @throws IOException If an error occurred while writing the stream.
*/
@@ -540,7 +541,7 @@ public class ChannelDataOutput extends C
while (n != 0) {
final int c = channel.write(buffer);
if (c == 0) {
- onEmptyChannelBuffer();
+ onEmptyTransfer();
}
n -= c;
}
Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java?rev=1589674&r1=1589673&r2=1589674&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageInputStream.java [UTF-8] Thu Apr 24 11:39:05 2014
@@ -23,9 +23,6 @@ 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;
// Related to JDK7
import java.nio.channels.SeekableByteChannel;
@@ -49,7 +46,7 @@ import java.nio.channels.SeekableByteCha
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.07)
- * @version 0.3
+ * @version 0.5
* @module
*
* @see javax.imageio.stream.FileImageInputStream
@@ -58,48 +55,7 @@ import java.nio.channels.SeekableByteCha
*/
public class ChannelImageInputStream extends ChannelDataInput implements ImageInputStream {
/**
- * 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 long bitPosition;
-
- /**
- * The most recent mark, or {@code null} if none.
- * This is the tail of a chained list of marks.
- */
- private Mark mark;
-
- /**
- * A mark pushed by the {@link ChannelImageInputStream#mark()} method
- * and pooled by the {@link ChannelImageInputStream#reset()} method.
- */
- private static final class Mark {
- final long position;
- final byte bitOffset;
- Mark next;
-
- Mark(long position, byte bitOffset, Mark next) {
- this.position = position;
- this.bitOffset = bitOffset;
- this.next = next;
- }
- }
-
- /**
- * Creates a new input source for the given channel and using the given buffer.
+ * Creates a new input stream for the given channel and using the given buffer.
*
* @param filename A file identifier used only for formatting error message.
* @param channel The channel from where data are read.
@@ -115,7 +71,7 @@ public class ChannelImageInputStream ext
}
/**
- * Creates a new input source from the given {@code ChannelDataInput}.
+ * Creates a new input stream from the given {@code ChannelDataInput}.
* This constructor is invoked when we need to change the implementation class
* from {@code ChannelDataInput} to {@code ChannelImageInputStream}.
*
@@ -128,7 +84,9 @@ public class ChannelImageInputStream ext
/**
* Sets the desired byte order for future reads of data values from this stream.
- * The default value is {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
+ * The default value is {@link ByteOrder#BIG_ENDIAN}.
+ *
+ * @param byteOrder The new {@linkplain #buffer buffer} byte order.
*/
@Override
public final void setByteOrder(final ByteOrder byteOrder) {
@@ -137,6 +95,9 @@ public class ChannelImageInputStream ext
/**
* Returns the byte order with which data values will be read from this stream.
+ * This is the {@linkplain #buffer buffer} byte order.
+ *
+ * @return The {@linkplain #buffer buffer} byte order.
*/
@Override
public final ByteOrder getByteOrder() {
@@ -146,6 +107,7 @@ public class ChannelImageInputStream ext
/**
* Returns the length of the stream (in bytes), or -1 if unknown.
*
+ * @return The length of the stream (in bytes), or -1 if unknown.
* @throws IOException If an error occurred while fetching the stream length.
*/
@Override
@@ -157,101 +119,6 @@ public class ChannelImageInputStream ext
}
/**
- * Returns the earliest position in the stream to which {@linkplain #seek(long) seeking}
- * may be performed.
- *
- * @return the earliest legal position for seeking.
- */
- @Override
- public final long getFlushedPosition() {
- return getStreamPosition() - buffer.position();
- }
-
- /**
- * Push back the last processed byte. This is used when a call to {@link #readBit()}
- * did not used every bits in a byte, or when {@link #readLine()} checked for the
- * Windows-style of EOL.
- */
- private void pushBack() {
- buffer.position(buffer.position() - 1);
- }
-
- /**
- * Returns the current bit offset, as an integer between 0 and 7 inclusive.
- *
- * <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.
- */
- @Override
- 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.
- *
- * @param bitOffset The new bit offset of the stream.
- */
- @Override
- 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}.
- *
- * @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 {
- 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}.
- *
- * @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).
- */
- @Override
- public final long readBits(int numBits) throws IOException {
- ensureBetween("numBits", 0, Long.SIZE, numBits);
- if (numBits == 0) {
- return 0;
- }
- /*
- * Reads the bits available in the next bytes (all of them if bitOffset == 0)
- * 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) {
- value = (value << Byte.SIZE) | readUnsignedByte();
- numBits -= Byte.SIZE;
- }
- if (numBits != 0) {
- value >>>= (-numBits); // Discard the unwanted bits.
- numBits += Byte.SIZE;
- pushBack();
- }
- setBitOffset(numBits);
- return value;
- }
-
- /**
* Reads a byte from the stream and returns a {@code true} if it is nonzero, {@code false} otherwise.
* The implementation is as below:
*
@@ -426,31 +293,6 @@ loop: while ((c = read()) >= 0) {
}
/**
- * Pushes the current stream position onto a stack of marked positions.
- */
- @Override
- public final void mark() {
- mark = new Mark(getStreamPosition(), (byte) getBitOffset(), mark);
- }
-
- /**
- * Resets the current stream byte and bit positions from the stack of marked positions.
- * An {@code IOException} will be thrown if the previous marked position lies in the
- * discarded portion of the stream.
- *
- * @throws IOException If an I/O error occurs.
- */
- @Override
- public final void reset() throws IOException {
- if (mark == null) {
- throw new IOException("No marked position.");
- }
- seek(mark.position);
- setBitOffset(mark.bitOffset);
- mark = mark.next;
- }
-
- /**
* Discards the initial position of the stream prior to the current stream position.
* The implementation is as below:
*
@@ -466,47 +308,6 @@ loop: while ((c = read()) >= 0) {
}
/**
- * Discards the initial portion of the stream prior to the indicated position.
- * Attempting to {@linkplain #seek(long) seek} to an offset within the flushed
- * portion of the stream will result in an {@link IndexOutOfBoundsException}.
- *
- * <p>This method moves the data starting at the given position to the beginning of the {@link #buffer},
- * thus making more room for new data before the data at the given position is discarded.</p>
- *
- * @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(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);
-
- // Discard obolete marks.
- Mark parent = null;
- for (Mark m = mark; m != null; m = m.next) {
- if (m.position < position) {
- if (parent != null) {
- parent.next = null;
- } else {
- mark = null;
- }
- break;
- }
- parent = m;
- }
- }
-
- /**
* Synonymous of {@link #isCachedMemory()} since the caching behavior of this class is uniquely determined
* by the policy that we choose for {@code isCachedMemory()}. This class never creates temporary files.
*