Top Description Fields Constructors Methods
org.jruby.util.io

pack-priv final Class SeekableByteChannelImpl

extends AbstractInterruptibleChannel
implements ReadableByteChannel, SeekableByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.SeekableByteChannel, java.nio.channels.ByteChannel, java.nio.channels.WritableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ReadableByteChannel
Imports
org.jruby.javasupport.Java, java.io.ByteArrayInputStream, .IOException, java.lang.reflect.Field, java.nio.ByteBuffer, java.nio.channels.ReadableByteChannel, .SeekableByteChannel, java.nio.channels.spi.AbstractInterruptibleChannel

Seekable byte channel impl over a byte array stream.
Author
kares

Field Summary

Modifier and TypeField and Description
private final int
private static final Field
private final ByteArrayInputStream
private final int
private static final Field
private static final Field
private int
pack-priv static final boolean

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static Field
accessibleField(final String name)

private static int
protected void
private static int
private static int
public long
position()

Implements java.nio.channels.SeekableByteChannel.position.

Returns this channel's position.

public synchronized SeekableByteChannel
position(long
The new position, a non-negative integer counting the number of bytes from the beginning of the entity
newPosition
)

Implements java.nio.channels.SeekableByteChannel.position.

Sets this channel's position.

public synchronized int
read(ByteBuffer
The buffer into which bytes are to be transferred
target
)

Implements java.nio.channels.ReadableByteChannel.read, java.nio.channels.SeekableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

private static int
public long
size()

Implements java.nio.channels.SeekableByteChannel.size.

Returns the current size of entity to which this channel is connected.

public SeekableByteChannel
truncate(long
The new size, a non-negative byte count
size
)

Implements java.nio.channels.SeekableByteChannel.truncate.

Truncates the entity, to which this channel is connected, to the given size.

public int
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.SeekableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

Inherited from java.nio.channels.spi.AbstractInterruptibleChannel:
begincloseendisOpen

Field Detail

countback to summary
private final int count
countFieldback to summary
private static final Field countField
inback to summary
private final ByteArrayInputStream in
markback to summary
private final int mark
markFieldback to summary
private static final Field markField
posFieldback to summary
private static final Field posField
truncatedByback to summary
private int truncatedBy
USABLEback to summary
pack-priv static final boolean USABLE

Constructor Detail

SeekableByteChannelImplback to summary
pack-priv SeekableByteChannelImpl(ByteArrayInputStream in)

Method Detail

accessibleFieldback to summary
private static Field accessibleField(final String name)
countback to summary
private static int count(ByteArrayInputStream in)
implCloseChannelback to summary
protected void implCloseChannel() throws IOException

Implements abstract java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel.

Doc from java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel.

Closes this channel.

This method is invoked by the close method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.

An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs while closing the channel
markback to summary
private static int mark(ByteArrayInputStream in)
posback to summary
private static int pos(ByteArrayInputStream in)
positionback to summary
public long position()

Implements java.nio.channels.SeekableByteChannel.position.

Doc from java.nio.channels.SeekableByteChannel.position.

Returns this channel's position.

Returns:long

This channel's position, a non-negative integer counting the number of bytes from the beginning of the entity to the current position

positionback to summary
public synchronized SeekableByteChannel position(long newPosition) throws IOException

Implements java.nio.channels.SeekableByteChannel.position.

Doc from java.nio.channels.SeekableByteChannel.position.

Sets this channel's position.

Setting the position to a value that is greater than the current size is legal but does not change the size of the entity. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the entity to grow to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Setting the channel's position is not recommended when connected to an entity, typically a file, that is opened with the APPEND option. When opened for append, the position is first advanced to the end before writing.

Parameters
newPosition:long

The new position, a non-negative integer counting the number of bytes from the beginning of the entity

Returns:SeekableByteChannel

This channel

Exceptions
IOException:
If some other I/O error occurs
readback to summary
public synchronized int read(ByteBuffer target) throws IOException

Implements java.nio.channels.ReadableByteChannel.read, java.nio.channels.SeekableByteChannel.read.

Doc from java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

An attempt is made to read up to r bytes from the channel, where r is the number of bytes remaining in the buffer, that is, dst.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is read, where 0 <= n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index p + n - 1, where p is the buffer's position at the moment this method is invoked. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

A read operation might not fill the buffer, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the channel. A socket channel in non-blocking mode, for example, cannot read any more bytes than are immediately available from the socket's input buffer; similarly, a file channel cannot read any more bytes than remain in the file. It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read.

This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
target:ByteBuffer

The buffer into which bytes are to be transferred

Returns:int

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
readIntFieldback to summary
private static int readIntField(ByteArrayInputStream self, Field field)
sizeback to summary
public long size()

Implements java.nio.channels.SeekableByteChannel.size.

Doc from java.nio.channels.SeekableByteChannel.size.

Returns the current size of entity to which this channel is connected.

Returns:long

The current size, measured in bytes

truncateback to summary
public SeekableByteChannel truncate(long size) throws IOException

Implements java.nio.channels.SeekableByteChannel.truncate.

Doc from java.nio.channels.SeekableByteChannel.truncate.

Truncates the entity, to which this channel is connected, to the given size.

If the given size is less than the current size then the entity is truncated, discarding any bytes beyond the new end. If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then it is set to that size.

An implementation of this interface may prohibit truncation when connected to an entity, typically a file, opened with the APPEND option.

Parameters
size:long

The new size, a non-negative byte count

Returns:SeekableByteChannel

This channel

Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public int write(ByteBuffer src) throws IOException

Implements java.nio.channels.SeekableByteChannel.write.

Doc from java.nio.channels.SeekableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel's current position, unless the channel is connected to an entity such as a file that is opened with the APPEND option, in which case the position is first advanced to the end. The entity to which the channel is connected is grown, if necessary, to accommodate the written bytes, and then the position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel interface.

Parameters
src:ByteBuffer

Doc from java.nio.channels.WritableByteChannel.write.

The buffer from which bytes are to be retrieved

Returns:int

Doc from java.nio.channels.WritableByteChannel.write.

The number of bytes written, possibly zero

Exceptions
IOException:

Doc from java.nio.channels.WritableByteChannel.write.

If some other I/O error occurs