8183743: Umbrella: add overloads that take a Charset parameter

Reviewed-by: alanb, rriggs
This commit is contained in:
Joe Wang 2017-12-12 11:10:12 -08:00
parent f065141ddc
commit 4f080a83af
28 changed files with 1959 additions and 131 deletions

View file

@ -2136,6 +2136,39 @@ public final class Formatter implements Closeable, Flushable {
this(toCharset(csn), l, new File(fileName));
}
/**
* Constructs a new formatter with the specified file name, charset, and
* locale.
*
* @param fileName
* The name of the file to use as the destination of this
* formatter. If the file exists then it will be truncated to
* zero size; otherwise, a new file will be created. The output
* will be written to the file and is buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
*
* @param l
* The {@linkplain java.util.Locale locale} to apply during
* formatting. If {@code l} is {@code null} then no localization
* is applied.
*
* @throws IOException
* if an I/O error occurs while opening or creating the file
*
* @throws SecurityException
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(fileName)} denies write
* access to the file
*
* @throws NullPointerException
* if {@code fileName} or {@code charset} is {@code null}.
*/
public Formatter(String fileName, Charset charset, Locale l) throws IOException {
this(Objects.requireNonNull(charset, "charset"), l, new File(fileName));
}
/**
* Constructs a new formatter with the specified file.
*
@ -2247,6 +2280,40 @@ public final class Formatter implements Closeable, Flushable {
this(toCharset(csn), l, file);
}
/**
* Constructs a new formatter with the specified file, charset, and
* locale.
*
* @param file
* The file to use as the destination of this formatter. If the
* file exists then it will be truncated to zero size; otherwise,
* a new file will be created. The output will be written to the
* file and is buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
*
* @param l
* The {@linkplain java.util.Locale locale} to apply during
* formatting. If {@code l} is {@code null} then no localization
* is applied.
*
* @throws IOException
* if an I/O error occurs while opening or creating the file
*
* @throws SecurityException
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(file.getPath())} denies
* write access to the file
*
* @throws NullPointerException
* if {@code file} or {@code charset} is {@code null}.
*/
public Formatter(File file, Charset charset, Locale l) throws IOException {
this(Objects.requireNonNull(charset, "charset"), l, file);
}
/**
* Constructs a new formatter with the specified print stream.
*
@ -2340,6 +2407,29 @@ public final class Formatter implements Closeable, Flushable {
this(l, new BufferedWriter(new OutputStreamWriter(os, csn)));
}
/**
* Constructs a new formatter with the specified output stream, charset,
* and locale.
*
* @param os
* The output stream to use as the destination of this formatter.
* The output will be buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
*
* @param l
* The {@linkplain java.util.Locale locale} to apply during
* formatting. If {@code l} is {@code null} then no localization
* is applied.
*
* @throws NullPointerException
* if {@code os} or {@code charset} is {@code null}.
*/
public Formatter(OutputStream os, Charset charset, Locale l) {
this(l, new BufferedWriter(new OutputStreamWriter(os, charset)));
}
private static char getZero(Locale l) {
if ((l != null) && !l.equals(Locale.US)) {
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);

View file

@ -37,6 +37,10 @@ import java.io.BufferedWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@ -997,6 +1001,11 @@ class Properties extends Hashtable<Object,Object> {
*
* <p>The specified stream remains open after this method returns.
*
* <p>This method behaves the same as
* {@linkplain #storeToXML(OutputStream os, String comment, Charset charset)}
* except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
* using the given encoding name.
*
* @param os the output stream on which to emit the XML document.
* @param comment a description of the property list, or {@code null}
* if no comment is desired.
@ -1011,20 +1020,67 @@ class Properties extends Hashtable<Object,Object> {
* @throws NullPointerException if {@code os} is {@code null},
* or if {@code encoding} is {@code null}.
* @throws ClassCastException if this {@code Properties} object
* contains any keys or values that are not
* {@code Strings}.
* contains any keys or values that are not {@code Strings}.
* @see #loadFromXML(InputStream)
* @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character
* Encoding in Entities</a>
* @since 1.5
*/
public void storeToXML(OutputStream os, String comment, String encoding)
throws IOException
{
throws IOException {
Objects.requireNonNull(os);
Objects.requireNonNull(encoding);
try {
Charset charset = Charset.forName(encoding);
storeToXML(os, comment, charset);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(encoding);
}
}
/**
* Emits an XML document representing all of the properties contained
* in this table, using the specified encoding.
*
* <p>The XML document will have the following DOCTYPE declaration:
* <pre>
* &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
* </pre>
*
* <p>If the specified comment is {@code null} then no comment
* will be stored in the document.
*
* <p> An implementation is required to support writing of XML documents
* that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An
* implementation may support additional encodings.
*
* <p> Unmappable characters for the specified charset will be encoded as
* numeric character references.
*
* <p>The specified stream remains open after this method returns.
*
* @param os the output stream on which to emit the XML document.
* @param comment a description of the property list, or {@code null}
* if no comment is desired.
* @param charset the charset
*
* @throws IOException if writing to the specified output stream
* results in an {@code IOException}.
* @throws NullPointerException if {@code os} or {@code charset} is {@code null}.
* @throws ClassCastException if this {@code Properties} object
* contains any keys or values that are not {@code Strings}.
* @see #loadFromXML(InputStream)
* @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character
* Encoding in Entities</a>
* @since 10
*/
public void storeToXML(OutputStream os, String comment, Charset charset)
throws IOException {
Objects.requireNonNull(os, "OutputStream");
Objects.requireNonNull(charset, "Charset");
PropertiesDefaultHandler handler = new PropertiesDefaultHandler();
handler.store(this, os, comment, encoding);
handler.store(this, os, comment, charset);
}
/**

View file

@ -575,7 +575,21 @@ public final class Scanner implements Iterator<String>, Closeable {
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
this(source, toCharset(charsetName));
}
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the specified charset.
*
* @param source an input stream to be scanned
* @param charset the charset used to convert bytes from the file
* into characters to be scanned
* @since 10
*/
public Scanner(InputStream source, Charset charset) {
this(makeReadable(Objects.requireNonNull(source, "source"), charset),
WHITESPACE_PATTERN);
}
@ -594,7 +608,18 @@ public final class Scanner implements Iterator<String>, Closeable {
}
}
/*
* This method is added so that null-check on charset can be performed before
* creating InputStream as an existing test required it.
*/
private static Readable makeReadable(Path source, Charset charset)
throws IOException {
Objects.requireNonNull(charset, "charset");
return makeReadable(Files.newInputStream(source), charset);
}
private static Readable makeReadable(InputStream source, Charset charset) {
Objects.requireNonNull(charset, "charset");
return new InputStreamReader(source, charset);
}
@ -629,6 +654,22 @@ public final class Scanner implements Iterator<String>, Closeable {
this(Objects.requireNonNull(source), toDecoder(charsetName));
}
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the specified charset.
*
* @param source A file to be scanned
* @param charset The charset used to convert bytes from the file
* into characters to be scanned
* @throws IOException
* if an I/O error occurs opening the source
* @since 10
*/
public Scanner(File source, Charset charset) throws IOException {
this(Objects.requireNonNull(source), charset.newDecoder());
}
private Scanner(File source, CharsetDecoder dec)
throws FileNotFoundException
{
@ -649,6 +690,12 @@ public final class Scanner implements Iterator<String>, Closeable {
return Channels.newReader(source, dec, -1);
}
private static Readable makeReadable(ReadableByteChannel source,
Charset charset) {
Objects.requireNonNull(charset, "charset");
return Channels.newReader(source, charset);
}
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified file. Bytes from the file are converted into
@ -688,8 +735,22 @@ public final class Scanner implements Iterator<String>, Closeable {
this(Objects.requireNonNull(source), toCharset(charsetName));
}
private Scanner(Path source, Charset charset) throws IOException {
this(makeReadable(Files.newInputStream(source), charset));
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the specified charset.
*
* @param source
* the path to the file to be scanned
* @param charset
* the charset used to convert bytes from the file
* into characters to be scanned
* @throws IOException
* if an I/O error occurs opening the source
* @since 10
*/
public Scanner(Path source, Charset charset) throws IOException {
this(makeReadable(source, charset));
}
/**
@ -735,6 +796,21 @@ public final class Scanner implements Iterator<String>, Closeable {
WHITESPACE_PATTERN);
}
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified channel. Bytes from the source are converted into
* characters using the specified charset.
*
* @param source a channel to scan
* @param charset the encoding type used to convert bytes from the
* channel into characters to be scanned
* @since 10
*/
public Scanner(ReadableByteChannel source, Charset charset) {
this(makeReadable(Objects.requireNonNull(source, "source"), charset),
WHITESPACE_PATTERN);
}
// Private primitives used to support scanning
private void saveState() {