public class FileStream extends Stream
Stream.PrintMode
Modifier and Type | Field and Description |
---|---|
private byte[] |
buffer
Buffer used for non-memory-mapped writes.
|
private static CharsetConverter |
cc
Optimized character set conversion helper.
|
private boolean |
chunk
true if the file is larger than the maximum chunk size. |
protected java.nio.channels.FileChannel |
file
The backing construct for the "stream".
|
private boolean |
mapped
true if using memory mapped file support. |
private static int |
MAPPED_BUF_SZ
The maximum size of mapped memory "chunks".
|
private java.nio.MappedByteBuffer |
mem
Buffer for memory mapped file usage.
|
private long |
memsz
The size (in bytes) of the memory mapped buffer.
|
private long |
offset
The starting offset (in bytes) of the memory mapped buffer.
|
private static java.lang.String |
override
Charset name to use for encoding or
null for default. |
private int |
pending
Number of bytes in the write buffer (non-memory-mapped mode).
|
private boolean |
write
true if the file must be writable. |
private static int |
WRITE_BUF_SZ
The size of the non-memory mapped write buffer.
|
binary, convert, CR, DELIM, doneIn, doneOut, EOF, increment, LF, MODE_EXPORT, MODE_PUT_CONTROL, MODE_PUT_NORMAL, MODE_PUT_UNFORMATTED, NEWLINE, NEWLINE_LEN, PAGED_DEFAULT, PIPE_BROKEN_MESSAGE, SET_PAGED_DEFAULT, sourceCp, targetCp
Constructor and Description |
---|
FileStream(java.lang.String filename,
boolean write,
boolean append)
Constructs an instance using a filename, this file or device will be
opened for reading or writing based on the given
write
flag. |
Modifier and Type | Method and Description |
---|---|
private void |
allocate()
Allocate the write buffer if it is not already allocated.
|
void |
assign(Stream stream)
Assigns the internal stream reference to the given reference.
|
long |
available()
The number of bytes available to be immediately read without blocking.
|
void |
close()
Closes the stream and releases OS resources associated with it.
|
void |
closeIn()
Closes the input stream and releases OS resources associated with it.
|
void |
closeOut()
Closes the output stream and releases OS resources associated with it.
|
private void |
flushBuffered()
Output any buffered writes to the file.
|
protected void |
flushData()
Flushes this output stream and forces any buffered output bytes to be written out.
|
long |
getLen()
The length of the stream in bytes.
|
long |
getPos()
The 0-based offset into the stream at which the next read or write will
occur.
|
boolean |
isIn()
State of the input side of the stream.
|
boolean |
isOut()
State of the output side of the stream.
|
protected void |
map(long start)
Setup memory mapping starting at the given offset and extending to
the end of the file or to the maximum chunk size, whichever is
smaller.
|
int |
peekCh()
Peeks at the character from the current read position in the stream (reads a character from
the current read position in the stream without incrementing stream read position.
|
private int |
read()
Worker to read a character and hide any charset conversion that may or may not be needed.
|
int |
readCh()
Read the raw integer value of a character from the current read
position in the stream.
|
java.lang.String |
readLn()
Read all characters from the current read position in the stream to the
next line separator (as determined by the
File.separator
or to the EOF . |
private int |
readWorker(boolean peek)
Read a byte from the file at the current file pointer.
|
void |
setLen(long len)
Truncates or extends the stream to the specified length if this stream
supports such an operation.
|
void |
setPos(long pos)
Moves the current read/write position to the specified absolute 0-based
offset.
|
private void |
unmap()
Clear/release the memory mapped buffer.
|
private void |
write(byte b)
Worker to output a single byte to our file at the current output
position.
|
void |
write(byte[] data)
Write the given byte array to the output stream.
|
void |
write(byte[] data,
int off,
int len)
Write the specified range of bytes from the given byte array to the output stream.
|
void |
write(java.lang.String data)
Write the given string to the output stream.
|
void |
writeByte(byte b)
Write the given byte to the output stream.
|
void |
writeCh(char ch)
Write the given character to the output stream.
|
private void |
writeFully(java.nio.ByteBuffer[] buf,
int[] sz,
int len)
Write
len bytes from the given buffer to the file before
returning. |
private void |
writeFully(java.nio.ByteBuffer buf,
int len)
Write
len bytes from the given buffer to the file before
returning. |
private void |
writeWorker(byte b)
Write a byte to the buffer, flushing if the buffer is full.
|
private void |
writeWorker(byte[] data,
int offset,
int len)
Worker that provides a mechanism to write a buffer directly to the
file channel at the current write position.
|
advancePage, afterWrite, assignDatum, cleanup, deleted, deregisterFooter, deregisterHeader, endOutput, endOutput, export, finished, flush, flushControlled, forcePageBreak, forcePageBreak, generateStringFormat, getAvailableLines, getControl, getConvert, getDelimiter, getDisplayMode, getEcho, getEffectivePageSize, getLength, getName, getNextLineNum, getPageNum, getPageSize, getPendingRowCount, getPosition, getPulseOnClose, getUnformatted, hasPageHeadersFooters, indexOfNewLine, isContiguous, isFreshPage, isSilent, isSuppressFormFeed, isTerm, isUnbuffered, isUnnamed, isValid, iterate, needsHeadersOutput, needsPageBreak, needsPageBreak, processFooters, processHeaders, processHeadersInt, processHeadersUncond, protectReads, protectWrites, pulseOutput, put, putControl, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putField, putLineEnd, putLineEnd, putNull, putNull, putSpace, putSpace, putUnformatted, putWorker, rawGetNextLineNum, rawGetPageNum, rawGetPageSize, rawIncrementNextLineNum, rawIncrementPageNum, rawNextLineNum, rawPageSize, rawResetNextLineNum, rawSetPageSize, readBlock, readChar, readChar, readChunk, readField, readField, readField, readField, readFieldWorker, readLine, readLineCleanup, registerAdvanceListener, registerCloseListener, registerFlushListener, registerFooter, registerHeader, registerPageBreakListener, resetCurrentLine, retry, setAppend, setBinary, setContiguous, setControl, setConvert, setConvertSource, setConvertSource, setConvertTarget, setConvertTarget, setDelimiter, setDelimiter, setEcho, setFont, setFont, setLandscape, setName, setPageSize, setPageSize, setPageSize, setPendingRowCount, setPortrait, setPosition, setPosition, setPulseOnClose, setUnbuffered, setUnformatted, setUnnamed, skipField, skipLoneHyphenInput, startOutput, writeBlock, writeControlled, writeControlledSpecial, writeField, writeField, writeField, writeField, writeField
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
initFailure
private static final int MAPPED_BUF_SZ
private static final int WRITE_BUF_SZ
private static final java.lang.String override
null
for default.private static final CharsetConverter cc
private boolean chunk
true
if the file is larger than the maximum chunk size.private boolean mapped
true
if using memory mapped file support.private boolean write
true
if the file must be writable.protected java.nio.channels.FileChannel file
private java.nio.MappedByteBuffer mem
private long offset
private long memsz
private byte[] buffer
private int pending
public FileStream(java.lang.String filename, boolean write, boolean append) throws ErrorConditionException
write
flag. The current read/write position will be at offset 0 unless in
write
mode and the append
flag is on.filename
- The name of the file or device to open.write
- If true
, open in write mode, otherwise only
open the file for reading.append
- In write
mode, if true
, the current
file pointer is set to the end of the file. Otherwise if in
write
mode, the file is truncated to 0 length.
This value is ignored in read
mode.ErrorConditionException
- - If the file does not exist.
- If the current user does not have the proper rights to open
the file or device.public long available() throws java.io.IOException
public long getPos() throws java.io.IOException
public void setPos(long pos) throws java.io.IOException
public boolean isIn()
public boolean isOut()
public long getLen() throws java.io.IOException
public void setLen(long len) throws java.io.IOException
public void writeCh(char ch) throws java.io.IOException
LF
) are converted to proper CRLF
sequence.public void writeByte(byte b) throws java.io.IOException
LF
) are converted to proper CRLF
sequence.public void write(java.lang.String data) throws java.io.IOException
public int peekCh()
peekCh()
and readCh()
will return the same value).
The underlying stream subclass determines the content of the result. Byte oriented streams such as pipes or files will return a byte while streams that generate keystrokes or characters may return a DBCS or Unicode character.
public int readCh()
At this time, only a single byte will be read no matter what encoding is used by the underlying stream. This means that DBCS or Unicode encodings will not be properly handled.
public java.lang.String readLn() throws java.io.EOFException, java.io.IOException, java.lang.InterruptedException
File.separator
or to the EOF
. Any line separator character(s) and the
EOF
character are not returned.readLn
in class Stream
java.io.EOFException
- If this input stream reaches the end before reading all the bytes.java.io.IOException
- If an I/O error occurs.java.lang.InterruptedException
- If any thread interrupted the current thread before or while
the current thread was waiting for a notification.public void closeIn()
public void closeOut()
public void close()
public void assign(Stream stream) throws java.lang.UnsupportedOperationException
private void write(byte b) throws java.io.IOException
b
- The data to write.java.io.IOException
public void write(byte[] data) throws java.io.IOException
public void write(byte[] data, int off, int len) throws java.io.IOException
write
in class Stream
data
- The data to be written.off
- Starting offset in data from which to read bytes to be written. Must be
non-negative and < data.length
.len
- Length of data to be written. Must be non-negative and <= (data.length
- offset)
.java.io.IOException
- If an I/O error occurs.private void writeWorker(byte b) throws java.io.IOException
b
- The byte to write.java.io.IOException
private void writeWorker(byte[] data, int offset, int len) throws java.io.IOException
data
- The buffer to write.offset
- The starting index to output.len
- The number of bytes to output.java.io.IOException
private int read() throws java.io.IOException
java.io.IOException
private int readWorker(boolean peek) throws java.io.IOException
peek
- true
if the byte is only peeked, ie the file pointer is not incremented.java.io.IOException
private void allocate()
private void flushBuffered() throws java.io.IOException
java.io.IOException
private void writeFully(java.nio.ByteBuffer buf, int len) throws java.io.IOException
len
bytes from the given buffer to the file before
returning.buf
- The buffer to output.len
- The size of the buffer in bytes. This MUST NOT be different
than the amount of data in the buffer. It is provided here
only for efficiency since the callers of this method already
have this value.java.io.IOException
private void writeFully(java.nio.ByteBuffer[] buf, int[] sz, int len) throws java.io.IOException
len
bytes from the given buffer to the file before
returning. This takes advantage of the NIO scatter/gather functionality
to improve performance.buf
- The list of buffers to output.sz
- The size in bytes of each corresponding buffer. The length of
this array must be the same as that of buf
.len
- The cumulative size of the data in all buffers in bytes. This
MUST NOT be different than the amount of data in the buffer.
It is provided here only for efficiency since the callers of
this method already have this value.java.io.IOException
protected void flushData() throws java.io.IOException
protected void map(long start) throws java.io.IOException
start
- The starting offset (0-based) in bytes.java.io.IOException
private void unmap()