public class FileStream extends Stream
Modifier and Type | Class and Description |
---|---|
static class |
FileStream.BOM
A structure that identifies the know types of BOMs and their useful properties.
|
Stream.PrintMode
Modifier and Type | Field and Description |
---|---|
protected java.nio.ByteBuffer |
binaryBuffer
Buffer for mapped file usage; this is in memory only if
mapped flag is true . |
private byte[] |
buffer
Buffer used for non-memory-mapped writes.
|
private byte[] |
byteMapping
The current byte mapping, if any, otherwise,
null . |
private int |
currLineNumber
The current line nunber under reading for the source file.
|
protected java.nio.charset.CharsetDecoder |
decoder
The cached character decoder used in more complex operations.
|
protected java.nio.channels.FileChannel |
file
The backing construct for the "stream".
|
protected java.lang.String |
fileName
The name of the file.
|
private boolean |
mapped
true if using memory mapped file support. |
protected static int |
MAPPED_BUF_SZ
The maximum size of mapped memory "chunks".
|
private boolean |
memoryMapped
Flag indicating if this is a memory-mapped stream.
|
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 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.
|
_convert, binary, convert, convertCached, CR, DELIM, doneIn, doneOut, EOF, increment, internalCp, LF, mapEntryProtermcap, MODE_EXPORT, MODE_PUT_CONTROL, MODE_PUT_NORMAL, MODE_PUT_UNFORMATTED, NEW_LINES, NEWLINE, NEWLINE_LEN, noMap, PAGED_DEFAULT, PIPE_BROKEN_MESSAGE, SET_PAGED_DEFAULT, sourceCharset, sourceCp, streamCp, targetCharset, targetCp, UNKNOWN_VALUE
Modifier | Constructor and Description |
---|---|
|
FileStream()
Default constructor.
|
|
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. |
protected |
FileStream(java.lang.String filename,
boolean write,
boolean append,
boolean memoryMapped)
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.
|
protected java.nio.CharBuffer |
decode(byte[] bytes)
Decode the specified bytes using the
Stream.sourceCharset or the cached decoder , if set. |
protected java.nio.CharBuffer |
decode(byte[] bytes,
boolean throwIO)
Decode the specified bytes using the
Stream.sourceCharset or the cached decoder , if set. |
void |
flush()
Flushes any data remaining in buffers to file or device.
|
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.
|
protected void |
initBinaryBuffer(long start,
long size)
Initialize the binary buffer storage with content of the file or steam to be read depending on class
implementation..
|
protected boolean |
isFileResource()
Check if this is a file resource.
|
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.
|
private boolean |
mappingInvalidBytesEnabled()
Test whether a bye mapping array is in use.
|
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.
|
protected FileStream.BOM |
readBOM()
Attempt to detect the file encoding based on the BOM.
|
int |
readByte()
Read a single byte from the underlying stream.
|
byte[] |
readBytes(int len)
Read a chunk of bytes from the current read position in the stream to the
offset calculated by given length or to the
EOF , whichever
comes first. |
private int |
readByteWorker(boolean peek)
Read a byte from the file at the current file pointer.
|
int |
readCh()
Read the raw integer value of a character from the current read
position in the stream.
|
private int |
readCharacterWorker(boolean peek)
Read a character from the file at the current file pointer.
|
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 . |
void |
setConvertSource(java.lang.String cp)
Sets the stream's source codepage and activates character conversion mode.
|
void |
setInvalidBytesMapping(byte[] newByteMapping)
Sets a new mapping byte array.
|
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.
|
protected 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, flushControlled, forcePageBreak, forcePageBreak, generateStringFormat, getAvailableLines, getControl, getConvert, getDelimiter, getDisplayMode, getEcho, getEffectivePageSize, getLength, getName, getNextLineNum, getPageNum, getPageSize, getPendingRowCount, getPosition, getPulseOnClose, getUnformatted, hasPageHeadersFooters, indexOfNewLine, isContiguous, isFreshPage, isKeepMessages, isNewLine, 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, readBlockRemote, readBlockWorker, readChar, readChar, readChunk, readField, readField, readField, readField, readFieldWorker, readLine, readLine, readLine, readLine, readLineCleanup, registerAdvanceListener, registerCloseListener, registerFlushListener, registerFooter, registerHeader, registerPageBreakListener, resetCurrentLine, retry, setAppend, setBinary, setContiguous, setControl, setConvert, setConvertSource, setConvertTarget, setConvertTarget, setDelimiter, setDelimiter, setEcho, setFont, setFont, setKeepMessages, setLandscape, setMap, setName, setNoMap, 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
entry, initFailure, weight
protected static final int MAPPED_BUF_SZ
private static final int WRITE_BUF_SZ
private final boolean memoryMapped
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
protected java.nio.ByteBuffer binaryBuffer
mapped
flag is true
.protected java.nio.charset.CharsetDecoder decoder
private long offset
private long memsz
private byte[] buffer
private int pending
private byte[] byteMapping
null
.protected final java.lang.String fileName
private int currLineNumber
public FileStream()
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.protected FileStream(java.lang.String filename, boolean write, boolean append, boolean memoryMapped) 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.memoryMapped
- Flag indicating if the mapping is done in memory. Only for read-only files.ErrorConditionException
- - If the file does not exist.
- If the current user does not have the proper rights to open the file or device.public void flush()
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 int readByte()
public byte[] readBytes(int len) throws java.io.IOException
EOF
, whichever
comes first. The EOF
character is not returned.readBytes
in class Stream
len
- The number of bytes to read.null
if there
is nothing to read (the EOF
has been reached). The
length of the returned array will be the smaller of the
len
parameter or the actual bytes left before the
EOF
.java.io.IOException
- If an I/O error occurs.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
- When attempting to read past the end of the file.java.io.IOException
- In the event of an other error while reading the line.java.lang.InterruptedException
- If the thread was interrupted while it was blocked reading the stream.public void closeIn()
public void closeOut()
public void close()
public void assign(Stream stream) throws java.lang.UnsupportedOperationException
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.protected boolean isFileResource()
isFileResource
in class Stream
true
protected void flushData() throws java.io.IOException
private void write(byte b) throws java.io.IOException
b
- The data to write.java.io.IOException
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 readCharacterWorker(boolean peek) throws java.io.IOException
peek
- true
if the character is only peeked, ie the file pointer is not incremented.java.io.IOException
private int readByteWorker(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 FileStream.BOM readBOM()
BOM
structure containing data about character encoding if one is detected,
otherwise null
.public void setConvertSource(java.lang.String cp)
setConvertSource
in class Stream
cp
- The source codepage for character conversion mode.public void setInvalidBytesMapping(byte[] newByteMapping)
FileStream
s at no memory/CPU expense to copy and store individual
instances of the array.newByteMapping
- The new byte mapping. An array of 256 bytes values with mapping of bytes. All bytes must be
mapped. If no special mapping is necessary use newByteMapping[i] = i;
. Use null
to disable byte mapping.protected void map(long start) throws java.io.IOException
start
- The starting offset (0-based) in bytes.java.io.IOException
protected void initBinaryBuffer(long start, long size) throws java.io.IOException
start
- The starting position inside source buffer.size
- The size of the binary buffer to re-allocate.java.io.IOException
- In the event of an error when decoding the bytes.protected java.nio.CharBuffer decode(byte[] bytes) throws java.io.IOException
Stream.sourceCharset
or the cached decoder
, if set.
For multi-byte characters, this API expects for all of them to be available in the given array.
bytes
- The bytes to be decoded.java.io.IOException
- In the event of an error when decoding the bytes.protected java.nio.CharBuffer decode(byte[] bytes, boolean throwIO) throws java.io.IOException
Stream.sourceCharset
or the cached decoder
, if set.
For multi-byte characters, this API expects for all of them to be available in the given array.
bytes
- The bytes to be decoded.throwIO
- Flag indication if IOException
will be throw in case of decoding issues.java.io.IOException
- In the event of an error when decoding the bytes.private boolean mappingInvalidBytesEnabled()
protected void unmap()