|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jruby.util.io.ChannelDescriptor
public class ChannelDescriptor
ChannelDescriptor provides an abstraction similar to the concept of a "file descriptor" on any POSIX system. In our case, it's a numbered object (fileno) enclosing a Channel (@see java.nio.channels.Channel), FileDescriptor (@see java.io.FileDescriptor), and flags under which the original open occured (@see org.jruby.util.io.ModeFlags). Several operations you would normally expect to use with a POSIX file descriptor are implemented here and used by higher-level classes to implement higher-level IO behavior. Note that the channel specified when constructing a ChannelDescriptor will be reference-counted; that is, until all known references to it through this class have gone away, it will be left open. This is to support operations like "dup" which must produce two independent ChannelDescriptor instances that can be closed separately without affecting the other. At present there's no way to simulate the behavior on some platforms where POSIX dup also allows independent positioning information.
Field Summary | |
---|---|
protected static java.util.concurrent.atomic.AtomicInteger |
internalFilenoIndex
|
Constructor Summary | |
---|---|
ChannelDescriptor(java.nio.channels.Channel channel)
Construct a new ChannelDescriptor with the given channel, file number, and file descriptor object. |
|
ChannelDescriptor(java.nio.channels.Channel channel,
java.io.FileDescriptor fileDescriptor)
Construct a new ChannelDescriptor with the given channel, file number, and file descriptor object. |
|
ChannelDescriptor(java.nio.channels.Channel channel,
int fileno,
java.io.FileDescriptor fileDescriptor)
Deprecated. |
|
ChannelDescriptor(java.nio.channels.Channel channel,
ModeFlags originalModes)
Construct a new ChannelDescriptor with the given channel, file number, mode flags, and file descriptor object. |
|
ChannelDescriptor(java.nio.channels.Channel channel,
ModeFlags originalModes,
java.io.FileDescriptor fileDescriptor)
Construct a new ChannelDescriptor with the given channel, file number, mode flags, and file descriptor object. |
|
ChannelDescriptor(java.io.InputStream baseInputStream,
ModeFlags originalModes)
Special constructor to create the ChannelDescriptor out of the stream, file number, mode flags, and file descriptor object. |
|
ChannelDescriptor(java.io.InputStream baseInputStream,
ModeFlags originalModes,
java.io.FileDescriptor fileDescriptor)
Special constructor to create the ChannelDescriptor out of the stream, file number, mode flags, and file descriptor object. |
Method Summary | |
---|---|
void |
checkNewModes(ModeFlags newModes)
Check whether a specified set of mode flags is a superset of this descriptor's original set of mode flags. |
void |
checkOpen()
Check whether the isOpen returns true, raising a BadDescriptorException if it returns false. |
void |
close()
Close this descriptor. |
ChannelDescriptor |
dup()
Mimics the POSIX dup(2) function, returning a new descriptor that references the same open channel. |
ChannelDescriptor |
dup2(int fileno)
Mimics the POSIX dup2(2) function, returning a new descriptor that references the same open channel but with a specified fileno. |
void |
dup2Into(ChannelDescriptor other)
Mimics the POSIX dup2(2) function, returning a new descriptor that references the same open channel but with a specified fileno. |
java.nio.channels.Channel |
getChannel()
The channel associated with this descriptor. |
static ChannelDescriptor |
getDescriptorByFileno(int aFileno)
|
java.io.FileDescriptor |
getFileDescriptor()
Get the FileDescriptor object associated with this descriptor. |
int |
getFileno()
Get this descriptor's file number. |
static int |
getNewFileno()
|
ModeFlags |
getOriginalModes()
Get the original mode flags for the descriptor. |
int |
internalWrite(java.nio.ByteBuffer buffer)
Write the bytes in the specified byte list to the associated channel. |
boolean |
isNull()
Whether the channel associated with this descriptor is a NullChannel, for which many operations are simply noops. |
boolean |
isOpen()
Whether the channel associated with this descriptor is open. |
boolean |
isSeekable()
Whether the channel associated with this descriptor is seekable (i.e. |
boolean |
isWritable()
Whether the channel associated with this descriptor is writable (i.e. |
long |
lseek(long offset,
int whence)
Perform a low-level seek operation on the associated channel if it is instanceof FileChannel, or raise PipeException if it is not a FileChannel. |
static ChannelDescriptor |
open(java.lang.String cwd,
java.lang.String path,
ModeFlags flags)
Open a new descriptor using the given working directory, file path, mode flags, and file permission. |
static ChannelDescriptor |
open(java.lang.String cwd,
java.lang.String path,
ModeFlags flags,
java.lang.ClassLoader classLoader)
Open a new descriptor using the given working directory, file path, mode flags, and file permission. |
static ChannelDescriptor |
open(java.lang.String cwd,
java.lang.String path,
ModeFlags flags,
int perm,
org.jruby.ext.posix.POSIX posix)
Open a new descriptor using the given working directory, file path, mode flags, and file permission. |
static ChannelDescriptor |
open(java.lang.String cwd,
java.lang.String path,
ModeFlags flags,
int perm,
org.jruby.ext.posix.POSIX posix,
java.lang.ClassLoader classLoader)
Open a new descriptor using the given working directory, file path, mode flags, and file permission. |
int |
read(java.nio.ByteBuffer buffer)
Perform a low-level read of the remaining number of bytes into the specified byte buffer. |
int |
read(int number,
org.jruby.util.ByteList byteList)
Perform a low-level read of the specified number of bytes into the specified byte list. |
ChannelDescriptor |
reopen(java.nio.channels.Channel channel,
ModeFlags modes)
|
ChannelDescriptor |
reopen(java.io.RandomAccessFile file,
ModeFlags modes)
|
void |
setCanBeSeekable(boolean canBeSeekable)
Set the channel to be explicitly seekable or not, for streams that appear to be seekable with the instanceof FileChannel check. |
int |
write(java.nio.ByteBuffer buffer)
Write the bytes in the specified byte list to the associated channel. |
int |
write(org.jruby.util.ByteList buf)
Write the bytes in the specified byte list to the associated channel. |
int |
write(org.jruby.util.ByteList buf,
int offset,
int len)
Write the bytes in the specified byte list to the associated channel. |
int |
write(int c)
Write the byte represented by the specified int to the associated channel. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final java.util.concurrent.atomic.AtomicInteger internalFilenoIndex
Constructor Detail |
---|
public ChannelDescriptor(java.nio.channels.Channel channel, ModeFlags originalModes, java.io.FileDescriptor fileDescriptor)
channel
- The channel for the new descriptorfileno
- The file number for the new descriptororiginalModes
- The mode flags for the new descriptorfileDescriptor
- The java.io.FileDescriptor object for the new descriptorpublic ChannelDescriptor(java.nio.channels.Channel channel, ModeFlags originalModes)
channel
- The channel for the new descriptororiginalModes
- The mode flags for the new descriptorfileDescriptor
- The java.io.FileDescriptor object for the new descriptorpublic ChannelDescriptor(java.io.InputStream baseInputStream, ModeFlags originalModes, java.io.FileDescriptor fileDescriptor)
baseInputStream
- The stream to create the channel for the new descriptorfileno
- The file number for the new descriptororiginalModes
- The mode flags for the new descriptorfileDescriptor
- The java.io.FileDescriptor object for the new descriptorpublic ChannelDescriptor(java.io.InputStream baseInputStream, ModeFlags originalModes)
baseInputStream
- The stream to create the channel for the new descriptororiginalModes
- The mode flags for the new descriptorfileDescriptor
- The java.io.FileDescriptor object for the new descriptorpublic ChannelDescriptor(java.nio.channels.Channel channel, java.io.FileDescriptor fileDescriptor) throws InvalidValueException
channel
- The channel for the new descriptorfileno
- The file number for the new descriptorfileDescriptor
- The java.io.FileDescriptor object for the new descriptor
InvalidValueException
@Deprecated public ChannelDescriptor(java.nio.channels.Channel channel, int fileno, java.io.FileDescriptor fileDescriptor) throws InvalidValueException
InvalidValueException
public ChannelDescriptor(java.nio.channels.Channel channel) throws InvalidValueException
channel
- The channel for the new descriptorfileDescriptor
- The java.io.FileDescriptor object for the new descriptor
InvalidValueException
Method Detail |
---|
public int getFileno()
public java.io.FileDescriptor getFileDescriptor()
public java.nio.channels.Channel getChannel()
public boolean isSeekable()
public void setCanBeSeekable(boolean canBeSeekable)
seekable
- Whether the channel is seekable or not.public boolean isNull()
public boolean isWritable()
public boolean isOpen()
public void checkOpen() throws BadDescriptorException
BadDescriptorException
- if isOpen returns falsepublic ModeFlags getOriginalModes()
public void checkNewModes(ModeFlags newModes) throws InvalidValueException
newModes
- The modes to confirm as superset
InvalidValueException
- if the modes are not a supersetpublic ChannelDescriptor dup()
public ChannelDescriptor dup2(int fileno)
fileno
- The fileno to use for the new descriptor
public void dup2Into(ChannelDescriptor other) throws BadDescriptorException, java.io.IOException
fileno
- The fileno to use for the new descriptor
BadDescriptorException
java.io.IOException
public ChannelDescriptor reopen(java.nio.channels.Channel channel, ModeFlags modes)
public ChannelDescriptor reopen(java.io.RandomAccessFile file, ModeFlags modes) throws java.io.IOException
java.io.IOException
public long lseek(long offset, int whence) throws java.io.IOException, InvalidValueException, PipeException, BadDescriptorException
offset
- the offset value to usewhence
- whence to seek
java.io.IOException
- If there is an exception while seeking
InvalidValueException
- If the value specified for
offset or whence is invalid
PipeException
- If the target channel is not seekable
BadDescriptorException
- If the target channel is
already closed.public int read(int number, org.jruby.util.ByteList byteList) throws java.io.IOException, BadDescriptorException
number
- the number of bytes to readbyteList
- the byte list on which to append the incoming bytes
java.io.IOException
- if there is an exception during IO
BadDescriptorException
- if the associated
channel is already closed.java.util.ByteList
public int read(java.nio.ByteBuffer buffer) throws java.io.IOException, BadDescriptorException
buffer
- the java.nio.ByteBuffer in which to put the incoming bytes
java.io.IOException
- if there is an exception during IO
BadDescriptorException
- if the associated
channel is already closedByteBuffer
public int internalWrite(java.nio.ByteBuffer buffer) throws java.io.IOException, BadDescriptorException
buf
- the byte list containing the bytes to be written
java.io.IOException
- if there is an exception during IO
BadDescriptorException
- if the associated
channel is already closedpublic int write(java.nio.ByteBuffer buffer) throws java.io.IOException, BadDescriptorException
buf
- the byte list containing the bytes to be written
java.io.IOException
- if there is an exception during IO
BadDescriptorException
- if the associated
channel is already closedpublic int write(org.jruby.util.ByteList buf) throws java.io.IOException, BadDescriptorException
buf
- the byte list containing the bytes to be written
java.io.IOException
- if there is an exception during IO
BadDescriptorException
- if the associated
channel is already closedpublic int write(org.jruby.util.ByteList buf, int offset, int len) throws java.io.IOException, BadDescriptorException
buf
- the byte list containing the bytes to be writtenoffset
- the offset to start at. this is relative to the begin variable in the butlen
- the amount of bytes to write. this should not be longer than the buffer
java.io.IOException
- if there is an exception during IO
BadDescriptorException
- if the associated
channel is already closedpublic int write(int c) throws java.io.IOException, BadDescriptorException
c
- The byte to write
java.io.IOException
- If there was an exception during IO
BadDescriptorException
- if the associated
channel is already closedpublic static ChannelDescriptor open(java.lang.String cwd, java.lang.String path, ModeFlags flags) throws java.io.FileNotFoundException, DirectoryAsFileException, FileExistsException, java.io.IOException
cwd
- the "current working directory" to use when opening the filepath
- the file path to openflags
- the mode flags to use for opening the file
java.io.FileNotFoundException
- if the target file could not be found
and the create flag was not specified
DirectoryAsFileException
- if the target file is
a directory being opened as a file
FileExistsException
- if the target file should
be created anew, but already exists
java.io.IOException
- if there is an exception during IOpublic static ChannelDescriptor open(java.lang.String cwd, java.lang.String path, ModeFlags flags, java.lang.ClassLoader classLoader) throws java.io.FileNotFoundException, DirectoryAsFileException, FileExistsException, java.io.IOException
cwd
- the "current working directory" to use when opening the filepath
- the file path to openflags
- the mode flags to use for opening the fileclassLoader
- a ClassLoader to use for classpath: resources
java.io.FileNotFoundException
- if the target file could not be found
and the create flag was not specified
DirectoryAsFileException
- if the target file is
a directory being opened as a file
FileExistsException
- if the target file should
be created anew, but already exists
java.io.IOException
- if there is an exception during IOpublic static ChannelDescriptor open(java.lang.String cwd, java.lang.String path, ModeFlags flags, int perm, org.jruby.ext.posix.POSIX posix) throws java.io.FileNotFoundException, DirectoryAsFileException, FileExistsException, java.io.IOException
cwd
- the "current working directory" to use when opening the filepath
- the file path to openflags
- the mode flags to use for opening the fileperm
- the file permissions to use when creating a new file (currently
unobserved)posix
- a POSIX api implementation, used for setting permissions; if null, permissions are ignored
java.io.FileNotFoundException
- if the target file could not be found
and the create flag was not specified
DirectoryAsFileException
- if the target file is
a directory being opened as a file
FileExistsException
- if the target file should
be created anew, but already exists
java.io.IOException
- if there is an exception during IOpublic static ChannelDescriptor open(java.lang.String cwd, java.lang.String path, ModeFlags flags, int perm, org.jruby.ext.posix.POSIX posix, java.lang.ClassLoader classLoader) throws java.io.FileNotFoundException, DirectoryAsFileException, FileExistsException, java.io.IOException
cwd
- the "current working directory" to use when opening the filepath
- the file path to openflags
- the mode flags to use for opening the fileperm
- the file permissions to use when creating a new file (currently
unobserved)posix
- a POSIX api implementation, used for setting permissions; if null, permissions are ignoredclassLoader
- a ClassLoader to use for classpath: resources
java.io.FileNotFoundException
- if the target file could not be found
and the create flag was not specified
DirectoryAsFileException
- if the target file is
a directory being opened as a file
FileExistsException
- if the target file should
be created anew, but already exists
java.io.IOException
- if there is an exception during IOpublic void close() throws BadDescriptorException, java.io.IOException
BadDescriptorException
- if the associated
channel is already closed
java.io.IOException
- if there is an exception during IOpublic static int getNewFileno()
public static ChannelDescriptor getDescriptorByFileno(int aFileno)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |