public class CodepageReader extends Object
This reader is implementing buffering, so there is no need to add it.
An option for special case of having the codepage specified in the first line is also provided. First line normally looks like ";#CODEPAGE=" for e.g. text and translation tabled, but can be configured.
Support for Byte Order Mark (BOM) is included, see http://www.unicode.org/unicode/faq/utf_bom.html for more information.
The different BOM's are:
Unfortunately, this code is written for JDK 1.6, so the Closable interface cannot be implemented, for use with try-catch-resource.
Modifier and Type | Field and Description |
---|---|
static String |
TEXTFILE_CODEPAGE_LINE_BEGINNING
The default codepage line used for text and translation tables for codepage specification.
|
Constructor and Description |
---|
CodepageReader(InputStream in,
String codepageLineBeginning,
boolean enableBOM,
String defaultCodepage)
Creates the reader from an input stream with default system codepage (e.g.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the stream.
|
String |
getEncoding()
Gets the encoding.
|
boolean |
hasStreamCodepage()
Gets if the stream contained a BOM or a Codepage line.
|
int |
read()
Reads a single character.
|
int |
read(char[] cbuf,
int off,
int len)
Reads characters into a portion of an array.
|
String |
readLine()
Reads a line of text.
|
long |
skip(long n)
Skips characters.
|
public static final String TEXTFILE_CODEPAGE_LINE_BEGINNING
public CodepageReader(InputStream in, String codepageLineBeginning, boolean enableBOM, String defaultCodepage) throws IOException, UnsupportedEncodingException
The beginning of the stream is read for 32 bytes blocking.
NOTE: If an IOException or UnsupportedEncodingException is thrown, the input stream is closed.
in
- The input stream.codepageLineBeginning
- Code page line beginning specification, null for none. Note that this will not be used if a BOM is found.enableBOM
- Flag indicating BOM support should be used.defaultCodepage
- The default codepage, null for system default. This codepage can be Ansi, OEM or EBCDIC.IOException
- For I/O errors.UnsupportedEncodingException
- If wrong encoding is specified.public String getEncoding()
public boolean hasStreamCodepage()
public int read() throws IOException
IOException
- If an I/O error occurspublic int read(char[] cbuf, int off, int len) throws IOException
This method implements the general contract of the corresponding
method of the
Reader.read(char[], int, int)
read
class. As an additional convenience, it
attempts to read as many characters as possible by repeatedly invoking
the Reader
read
method of the underlying stream. This iterated
read
continues until one of the following conditions becomes
true:
read
method of the underlying stream returns
-1
, indicating end-of-file, or
ready
method of the underlying stream
returns false
, indicating that further input requests
would block.
read
on the underlying stream returns
-1
to indicate end-of-file then this method returns
-1
. Otherwise this method returns the number of characters
actually read.
Subclasses of this class are encouraged, but not required, to attempt to read as many characters as possible in the same fashion.
Ordinarily this method takes characters from this stream's character
buffer, filling it from the underlying stream as necessary. If,
however, the buffer is empty, the mark is not valid, and the requested
length is at least as large as the buffer, then this method will read
characters directly from the underlying stream into the given array.
Thus redundant BufferedReader
s will not copy data
unnecessarily.
cbuf
- Destination bufferoff
- Offset at which to start storing characterslen
- Maximum number of characters to readIOException
- If an I/O error occurspublic String readLine() throws IOException
IOException
- If an I/O error occursFiles.readAllLines(java.nio.file.Path, java.nio.charset.Charset)
public long skip(long n) throws IOException
n
- The number of characters to skipIllegalArgumentException
- If n
is negative.IOException
- If an I/O error occurspublic void close()
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© © Mindus SARL, 2024. All rights reserved.