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.lang.String |
baseDirectoryName
The name of the directory of the file.
|
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 java.lang.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 |
hasDuplicatedLastLine
Flag to indicate if the last line in the file is duplicate (true for LOB files).
|
protected java.lang.String |
lobDirectoryName
The name of the directory to store LOB.
|
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 |
optionalBOMSize
Optional BOM signature size to be skipped for text files.
|
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, CRLF, DELIM, doneIn, doneOut, EOF, increment, internalCp, isWindows, 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,
FileSystemResource fsr)
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,
FileSystemResource fsr,
boolean hasDuplicatedLastLine)
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.
|
private java.lang.String |
getLobDirectory()
Gets the stream's lob directory defined to put/get lob data fields.
|
long |
getPos()
The 0-based offset into the stream at which the next read or write will occur.
|
int64 |
getPosition()
Obtains the current read and/or write position in the stream, if the stream type supports
such a query.
|
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(java.lang.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 |
setLobDirectory(java.lang.String lobDir)
Sets the stream's lob directory located as a subdirectory for opened current file stream directory.
|
void |
setPos(long pos)
Moves the current read/write position to the specified absolute 0-based offset.
|
void |
setPosition(long newpos)
Sets the current read and/or write position in the stream, if the
stream type supports the operation.
|
private void |
skipBOMHeader()
Skips file internal pointer for the text files to the value of the BOM size from the file beginning.
|
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 |
writeBlob(blob val)
Write the entire block of data (as represented by the given
blob) to the stream with no
delimiters, end of line insertions or other modifications. |
void |
writeByte(byte b)
Write the given byte to the output stream.
|
void |
writeCh(char ch)
Write the given character to the output stream.
|
void |
writeClob(clob val)
Write the entire block of data (as represented by the given
clob) to the stream with no
delimiters, end of line insertions or other modifications. |
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, 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, setFlushBeforePut, setFont, setFont, setKeepMessages, setLandscape, setLobDirectory, setMap, setName, setNoMap, setPageSize, setPageSize, setPageSize, setPendingRowCount, setPortrait, setPosition, setPulseOnClose, setUnbuffered, setUnformatted, setUnnamed, skipField, skipLoneHyphenInput, startOutput, writeBlock, writeControlled, writeControlledSpecial, writeField, writeField, writeField, writeField, writeFieldclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitentry, initFailure, weightprotected static final int MAPPED_BUF_SZ
private static final int WRITE_BUF_SZ
private final boolean memoryMapped
private final boolean hasDuplicatedLastLine
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 java.lang.Byte[] byteMapping
null.protected final java.lang.String fileName
protected final java.lang.String baseDirectoryName
protected java.lang.String lobDirectoryName
private int currLineNumber
private int optionalBOMSize
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,
FileSystemResource fsr)
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.fsr - The file system resource to use for this stream. May be null if no resource
checking is required.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,
FileSystemResource fsr,
boolean hasDuplicatedLastLine)
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.fsr - The file system resource to use for this stream. May be null if no resource
checking is required.hasDuplicatedLastLine - Flag to indicate if the last line in the file is duplicate (true for LOB 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 Streamlen - 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 Streamjava.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 Streamdata - 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.public void writeBlob(blob val) throws ErrorConditionException, StopConditionException
blob) to the stream with no
delimiters, end of line insertions or other modifications. The data will be exported into separate
file within subdirectory defined in LOB-DIR option or if not defined in current base directory.
Use of this method does NOT require bracketing between Stream.startOutput(int) and Stream.endOutput()
methods and these must not be called otherwise the result is undefined.
This method is called only from client-side, via the ExportField.output method. WARNING: any other usage from server-side needs to be reviewed, as it may be expensive.
writeBlob in class Streamval - The value to be output.ErrorConditionException - On any error during I/O.StopConditionException - If any interruption occurs during I/O processing.public void writeClob(clob val) throws ErrorConditionException, StopConditionException
clob) to the stream with no
delimiters, end of line insertions or other modifications. The data will be exported into separate
file within subdirectory defined in LOB-DIR option or if not defined in current base directory.
Use of this method does NOT require bracketing between Stream.startOutput(int) and Stream.endOutput()
methods and these must not be called otherwise the result is undefined.
This method is called only from client-side, via the ExportField.output method. WARNING: any other usage from server-side needs to be reviewed, as it may be expensive.
writeClob in class Streamval - The value to be output.ErrorConditionException - On any error during I/O.StopConditionException - If any interruption occurs during I/O processing.protected boolean isFileResource()
isFileResource in class Streamtrueprotected void flushData()
throws java.io.IOException
private java.lang.String getLobDirectory()
private void write(byte b)
throws java.io.IOException
b - The data to write.java.io.IOExceptionprivate void writeWorker(byte b)
throws java.io.IOException
b - The byte to write.java.io.IOExceptionprivate 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.IOExceptionprivate 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.IOExceptionprivate 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.IOExceptionprivate void allocate()
private void flushBuffered()
throws java.io.IOException
java.io.IOExceptionprivate 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.IOExceptionprivate 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.IOExceptionprotected 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 Streamcp - The source codepage for character conversion mode.public void setLobDirectory(java.lang.String lobDir)
setLobDirectory in class StreamlobDir - The name for subdirectory to store LOB files.public void setInvalidBytesMapping(java.lang.Byte[] newByteMapping)
FileStreams 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.public void setPosition(long newpos)
Starting with OpenEdge Release 10.1B, ABL includes a 64-bit integer data type. They
are mapped to the underlying Java platform that can handle streams with offsets that are
based on a 64-bit long.
A negative offset is ignored (no action is taken).
If the specified offset is larger than the length of the stream, in Progress the seek position will report as the number requested, but if no subsequent writes occur to the file, the file size is left the same as it was before setting the position. If writes do occur, all writes occur at the new position. In this case, the resulting file will be sized based on the requested offset + the number of bytes written.
setPosition in class Streamnewpos - The new offset. If this is the unknown value,
the set operation will silently return without any change.public int64 getPosition()
Starting with OpenEdge Release 10.1B, ABL includes a 64-bit integer data type. They
are mapped to the underlying Java platform that can handle streams with offsets that are
based on a 64-bit long.
getPosition in class Streamunknown value if the
stream type doesn't support a stream position.protected void map(long start)
throws java.io.IOException
start - The starting offset (0-based) in bytes.java.io.IOExceptionprotected 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()
private void skipBOMHeader()
throws java.io.IOException
java.io.IOExceptionprotected void unmap()