Updated to support Java 11, but only Java 8 features

This commit is contained in:
SuspiciousActivity committed 2018-09-26 17:19:03 +02:00
1 parent 58c8e90c81
commit 89f9753d8b
19 files changed
+1877 -2145

No files matched your search

+68 -93
View File
@@ -25,8 +25,8 @@ import java.io.Reader;
* <li>UTF-32LE
* <li>UTF-32BE
* </ul>
* If the stream is not found to be any of the above, then a default encoding
* is used for reading. The user can specify this default encoding, or a system
* If the stream is not found to be any of the above, then a default encoding is
* used for reading. The user can specify this default encoding, or a system
* default will be used.
* <p>
*
@@ -40,121 +40,101 @@ import java.io.Reader;
* @author Robert Futrell
* @version 0.9
*/
@SuppressWarnings({ "checkstyle:magicnumber" })
public class UnicodeReader extends Reader {
/**
* The input stream from which we're really reading.
*/
private InputStreamReader internalIn = null;
/**
* The encoding being used. We keep our own instead of using the string
* returned by <code>java.io.InputStreamReader</code> since that class
* does not return user-friendly names.
* The encoding being used. We keep our own instead of using the string returned
* by <code>java.io.InputStreamReader</code> since that class does not return
* user-friendly names.
*/
private String encoding;
/**
* The size of a BOM.
*/
private static final int BOM_SIZE = 4;
/**
* This utility constructor is here because you will usually use a
* <code>UnicodeReader</code> on files.
* <p>
* Creates a reader using the encoding specified by the BOM in the file;
* if there is no recognized BOM, then a system default encoding is used.
* Creates a reader using the encoding specified by the BOM in the file; if
* there is no recognized BOM, then a system default encoding is used.
*
* @param file
* The file from which you want to read.
* @throws IOException
* If an error occurs when checking for/reading the
* BOM.
* @throws SecurityException
* If a security manager exists and its
* checkRead method denies read access to the file.
* @param file The file from which you want to read.
* @throws IOException If an error occurs when checking for/reading the
* BOM.
* @throws SecurityException If a security manager exists and its checkRead
* method denies read access to the file.
*/
public UnicodeReader(String file) throws IOException {
this(new File(file));
}
/**
* This utility constructor is here because you will usually use a
* <code>UnicodeReader</code> on files.
* <p>
* Creates a reader using the encoding specified by the BOM in the file;
* if there is no recognized BOM, then a system default encoding is used.
* Creates a reader using the encoding specified by the BOM in the file; if
* there is no recognized BOM, then a system default encoding is used.
*
* @param file
* The file from which you want to read.
* @throws IOException
* If an error occurs when checking for/reading the
* BOM.
* @throws SecurityException
* If a security manager exists and its
* checkRead method denies read access to the file.
* @param file The file from which you want to read.
* @throws IOException If an error occurs when checking for/reading the
* BOM.
* @throws SecurityException If a security manager exists and its checkRead
* method denies read access to the file.
*/
public UnicodeReader(File file) throws IOException {
this(new FileInputStream(file));
}
/**
* This utility constructor is here because you will usually use a
* <code>UnicodeReader</code> on files.
* <p>
* Creates a reader using the encoding specified by the BOM in the file;
* if there is no recognized BOM, then a specified default encoding is
* used.
* Creates a reader using the encoding specified by the BOM in the file; if
* there is no recognized BOM, then a specified default encoding is used.
*
* @param file
* The file from which you want to read.
* @param defaultEncoding
* The encoding to use if no BOM is found. If
* this value is <code>null</code>, a system default is used.
* @throws IOException
* If an error occurs when checking for/reading the
* BOM.
* @throws SecurityException
* If a security manager exists and its
* checkRead method denies read access to the file.
* @param file The file from which you want to read.
* @param defaultEncoding The encoding to use if no BOM is found. If this value
* is <code>null</code>, a system default is used.
* @throws IOException If an error occurs when checking for/reading the
* BOM.
* @throws SecurityException If a security manager exists and its checkRead
* method denies read access to the file.
*/
public UnicodeReader(File file, String defaultEncoding) throws IOException {
this(new FileInputStream(file), defaultEncoding);
}
/**
* Creates a reader using the encoding specified by the BOM in the file;
* if there is no recognized BOM, then a system default encoding is used.
* Creates a reader using the encoding specified by the BOM in the file; if
* there is no recognized BOM, then a system default encoding is used.
*
* @param in
* The input stream from which to read.
* @throws IOException
* If an error occurs when checking for/reading the
* BOM.
* @param in The input stream from which to read.
* @throws IOException If an error occurs when checking for/reading the BOM.
*/
public UnicodeReader(InputStream in) throws IOException {
this(in, null);
}
/**
* Creates a reader using the encoding specified by the BOM in the file;
* if there is no recognized BOM, then <code>defaultEncoding</code> is
* used.
* Creates a reader using the encoding specified by the BOM in the file; if
* there is no recognized BOM, then <code>defaultEncoding</code> is used.
*
* @param in
* The input stream from which to read.
* @param defaultEncoding
* The encoding to use if no recognized BOM is
* found. If this value is <code>null</code>, a system default
* is used.
* @throws IOException
* If an error occurs when checking for/reading the
* BOM.
* @param in The input stream from which to read.
* @param defaultEncoding The encoding to use if no recognized BOM is found. If
* this value is <code>null</code>, a system default is
* used.
* @throws IOException If an error occurs when checking for/reading the BOM.
*/
public UnicodeReader(InputStream in, String defaultEncoding) throws IOException {
init(in, defaultEncoding);
}
/**
* Closes this reader.
*/
@@ -162,29 +142,27 @@ public class UnicodeReader extends Reader {
public void close() throws IOException {
internalIn.close();
}
/**
* Returns the encoding being used to read this input stream (i.e., the
* encoding of the file). If a BOM was recognized, then the specific
* Unicode type is returned; otherwise, either the default encoding passed
* into the constructor or the system default is returned.
* Returns the encoding being used to read this input stream (i.e., the encoding
* of the file). If a BOM was recognized, then the specific Unicode type is
* returned; otherwise, either the default encoding passed into the constructor
* or the system default is returned.
*
* @return The encoding of the stream.
*/
public String getEncoding() {
return encoding;
}
/**
* Read-ahead four bytes and check for BOM marks. Extra bytes are
* unread back to the stream, only BOM bytes are skipped.
* Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to
* the stream, only BOM bytes are skipped.
*
* @param defaultEncoding
* The encoding to use if no BOM was recognized. If
* this value is <code>null</code>, then a system default is
* used.
* @throws IOException
* If an error occurs when trying to read a BOM.
* @param defaultEncoding The encoding to use if no BOM was recognized. If this
* value is <code>null</code>, then a system default is
* used.
* @throws IOException If an error occurs when trying to read a BOM.
*/
protected void init(InputStream in, String defaultEncoding) throws IOException {
PushbackInputStream tempIn = new PushbackInputStream(in, BOM_SIZE);
@@ -196,7 +174,8 @@ public class UnicodeReader extends Reader {
unread = n - 4;
} else if (n == BOM_SIZE && // Last 2 bytes are 0; could be an empty
// UTF-16
(bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
(bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00)
&& (bom[3] == (byte) 0x00)) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
@@ -226,21 +205,17 @@ public class UnicodeReader extends Reader {
internalIn = new InputStreamReader(tempIn, encoding);
}
}
/**
* Read characters into a portion of an array. This method will block until
* some input is available, an I/O error occurs, or the end of the stream
* is reached.
* Read characters into a portion of an array. This method will block until some
* input is available, an I/O error occurs, or the end of the stream is reached.
*
* @param cbuf
* The buffer into which to read.
* @param off
* The offset at which to start storing characters.
* @param len
* The maximum number of characters to read.
* @param cbuf The buffer into which to read.
* @param off The offset at which to start storing characters.
* @param len The maximum number of characters to read.
*
* @return The number of characters read, or <code>-1</code> if the end
* of the stream has been reached.
* @return The number of characters read, or <code>-1</code> if the end of the
* stream has been reached.
*/
@Override
public int read(char[] cbuf, int off, int len) throws IOException {