public class DFSInputStream
extends java.io.InputStream
Read calls on a DFSInputStream
are directed to a storage server
hosting the given file. Each read call corresponds to one network request.
If this behavior is not desirable, the DFSInputStream
should be
wrapped in a BufferedInputStream
or other class providing
buffered input.
DFSInputStream
does not support marks.
DFSInputStream
does not provide locking. This must be done
manually by the user. The reason for this is that if
DFSInputStream
attempted to lock a file for shared access, it
would not be possible to combine use of the input stream with other
operations, such as retrieving file size, or operations that require the
parent directory of the file to be locked for exclusive access.
Modifier and Type | Field and Description |
---|---|
private boolean |
closed
Indicates that the stream has been closed.
|
private long |
length
Total file length.
|
private Service |
naming_server
Naming server used to find the storage server hosting the file.
|
private long |
offset
Current read offset in the file.
|
private Path |
path
Path to the file.
|
private Storage |
storage_server
Storage server hosting the file.
|
Constructor and Description |
---|
DFSInputStream(Service naming_server,
Path file)
Creates a
DFSInputStream for a file listed by the given
naming server. |
DFSInputStream(Service naming_server,
java.lang.String filename)
Creates a
DFSInputStream for a file listed by the given
naming server. |
DFSInputStream(java.lang.String hostname,
Path file)
Creates a
DFSInputStream for a file listed by the given
naming server. |
DFSInputStream(java.lang.String hostname,
java.lang.String filename)
Creates a
DFSInputStream for a file listed by the given
naming server. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes remaining between the current stream offset
and the end of file.
|
void |
close()
Closes the input stream.
|
int |
read()
Reads a single byte from the input stream.
|
int |
read(byte[] buffer,
int buffer_offset,
int read_length)
Reads bytes from the input stream into a byte buffer.
|
long |
skip(long count)
Advances the stream offset.
|
private final Path path
private final Storage storage_server
private final Service naming_server
private long offset
private final long length
private boolean closed
public DFSInputStream(Service naming_server, Path file) throws java.io.FileNotFoundException, java.io.IOException
DFSInputStream
for a file listed by the given
naming server.
The file should be locked on the naming server for shared access.
naming_server
- Stub for the naming server hosting metadata for the
file.file
- Path to the file.java.io.FileNotFoundException
- If the file is not listed by the given
naming server, or if the path refers to a
directory.java.io.IOException
- If either the naming server or the storage server
hosting the file cannot be contacted to retrieve
file metadata.public DFSInputStream(java.lang.String hostname, Path file) throws java.io.FileNotFoundException, java.io.IOException
DFSInputStream
for a file listed by the given
naming server.
The file should be locked on the naming server for shared access. The naming server is contacted on the default client interface port.
hostname
- Address of the naming server hosting metadata for the
file.file
- Path to the file.java.io.FileNotFoundException
- If the file is not listed by the given
naming server, or if the path refers to a
directory.java.io.IOException
- If either the naming server or the storage server
hosting the file cannot be contacted to retrieve
file metadata.public DFSInputStream(Service naming_server, java.lang.String filename) throws java.io.FileNotFoundException, java.io.IOException
DFSInputStream
for a file listed by the given
naming server.
The file should be locked on the naming server for shared access.
naming_server
- Stub for the naming server hosting metadata for the
file.filename
- Path to the file, as a string.java.io.FileNotFoundException
- If the file is not listed by the given
naming server, or if the path refers to a
directory.java.io.IOException
- If either the naming server or the storage server
hosting the file cannot be contacted to retrieve
file metadata.public DFSInputStream(java.lang.String hostname, java.lang.String filename) throws java.io.FileNotFoundException, java.io.IOException
DFSInputStream
for a file listed by the given
naming server.
The file should be locked on the naming server for shared access. The naming server is contacted on the default client interface port.
hostname
- Address of the naming server hosting metadata for the
file.filename
- Path to the file, as a string.java.io.FileNotFoundException
- If the file is not listed by the given
naming server, or if the path refers to a
directory.java.io.IOException
- If either the naming server or the storage server
hosting the file cannot be contacted to retrieve
file metadata.public void close()
The stream is marked as closed. Further attempts to use the input stream
will result in IOException
.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
public int read(byte[] buffer, int buffer_offset, int read_length) throws java.io.IOException
The read is performed in a single request to the storage server. If the
operation succeeds, the number of bytes read will be either
read_length
or the number of bytes remaining in the file,
whichever is less.
read
in class java.io.InputStream
buffer
- Buffer to receive bytes read from the stream.buffer_offset
- Offset into the buffer at which the bytes are to be
written.read_length
- The maximum number of bytes to read.-1
if the
end of file is reached before any bytes are read. The number of
bytes successfully read may be zero if read_length
is zero.java.io.IOException
- If the stream is closed, if the storage server
cannot be contacted, or if a read error occurs on
the storage server.java.lang.NullPointerException
- If buffer
is
null
.java.lang.IndexOutOfBoundsException
- If buffer_offset
or
read_length
is negative,
or if buffer_offset +
read_length
exceeds the length
of the given buffer.public int read() throws java.io.IOException
read
in class java.io.InputStream
0
and 255
, or -1
if the end of file has
been reached.java.io.IOException
- If the stream is closed, if the storage server
cannot be contacted, or if a read error occurs on
the storage server.public long skip(long count) throws java.io.IOException
The stream offset is advanced by either the given number of bytes, or by the number of bytes remaining in the file, whichever is less.
skip
in class java.io.InputStream
count
- Number of bytes by which the stream offset should be
advanced.java.io.IOException
- If the stream has been closed.public int available() throws java.io.IOException
The number returned is exact and not an estimate. However, if the number
exceeds Integer.MAX_VALUE
, it is clamped to
Integer.MAX_VALUE
.
available
in class java.io.InputStream
java.io.IOException
- If the stream has been closed.