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

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,7 @@
package java.io;
import java.nio.charset.Charset;
import java.util.Arrays;
/**
@ -223,14 +224,27 @@ public class ByteArrayOutputStream extends OutputStream {
/**
* Converts the buffer's contents into a string by decoding the bytes using
* the named {@link java.nio.charset.Charset charset}. The length of the new
* {@code String} is a function of the charset, and hence may not be equal
* to the length of the byte array.
* the named {@link java.nio.charset.Charset charset}.
*
* <p> This method is equivalent to {@code #toString(charset)} that takes a
* {@link java.nio.charset.Charset charset}.
*
* <p> An invocation of this method of the form
*
* <pre> {@code
* ByteArrayOutputStream b = ...
* b.toString("UTF-8")
* }
* </pre>
*
* behaves in exactly the same way as the expression
*
* <pre> {@code
* ByteArrayOutputStream b = ...
* b.toString(StandardCharsets.UTF_8)
* }
* </pre>
*
* <p> This method always replaces malformed-input and unmappable-character
* sequences with this charset's default replacement string. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param charsetName the name of a supported
* {@link java.nio.charset.Charset charset}
@ -245,6 +259,26 @@ public class ByteArrayOutputStream extends OutputStream {
return new String(buf, 0, count, charsetName);
}
/**
* Converts the buffer's contents into a string by decoding the bytes using
* the specified {@link java.nio.charset.Charset charset}. The length of the new
* {@code String} is a function of the charset, and hence may not be equal
* to the length of the byte array.
*
* <p> This method always replaces malformed-input and unmappable-character
* sequences with the charset's default replacement string. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param charset the {@linkplain java.nio.charset.Charset charset}
* to be used to decode the {@code bytes}
* @return String decoded from the buffer's contents.
* @since 10
*/
public synchronized String toString(Charset charset) {
return new String(buf, 0, count, charset);
}
/**
* Creates a newly allocated string. Its size is the current size of
* the output stream and the valid contents of the buffer have been
@ -257,9 +291,10 @@ public class ByteArrayOutputStream extends OutputStream {
*
* @deprecated This method does not properly convert bytes into characters.
* As of JDK&nbsp;1.1, the preferred way to do this is via the
* {@code toString(String enc)} method, which takes an encoding-name
* argument, or the {@code toString()} method, which uses the
* platform's default character encoding.
* {@link #toString(String charsetName)} or {@link #toString(Charset charset)}
* method, which takes an encoding-name or charset argument,
* or the {@code toString()} method, which uses the platform's default
* character encoding.
*
* @param hibyte the high byte of each resulting Unicode character.
* @return the current contents of the output stream, as a string.

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,10 +45,16 @@ import java.nio.charset.UnsupportedCharsetException;
* ({@code '\n'}) is written.
*
* <p> All characters printed by a {@code PrintStream} are converted into
* bytes using the platform's default character encoding.
* bytes using the given encoding or charset, or platform's default character
* encoding if not specified.
* The {@link PrintWriter} class should be used in situations that require
* writing characters rather than bytes.
*
* <p> This class always replaces malformed and unmappable character sequences with
* the charset's default replacement string.
* The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
* control over the encoding process is required.
*
* @author Frank Yellin
* @author Mark Reinhold
* @since 1.0
@ -105,22 +111,13 @@ public class PrintStream extends FilterOutputStream
this.textOut = new BufferedWriter(charOut);
}
private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
super(out);
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this, charset);
this.textOut = new BufferedWriter(charOut);
}
/* Variant of the private constructor so that the given charset name
* can be verified before evaluating the OutputStream argument. Used
* by constructors creating a FileOutputStream that also take a
* charset name.
*/
private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
throws UnsupportedEncodingException
{
this(autoFlush, out, charset);
private PrintStream(boolean autoFlush, Charset charset, OutputStream out) {
this(out, autoFlush, charset);
}
/**
@ -172,9 +169,30 @@ public class PrintStream extends FilterOutputStream
public PrintStream(OutputStream out, boolean autoFlush, String encoding)
throws UnsupportedEncodingException
{
this(autoFlush,
requireNonNull(out, "Null output stream"),
toCharset(encoding));
this(requireNonNull(out, "Null output stream"), autoFlush, toCharset(encoding));
}
/**
* Creates a new print stream, with the specified OutputStream, automatic line
* flushing and charset. This convenience constructor creates the necessary
* intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
* which will encode characters using the provided charset.
*
* @param out The output stream to which values and objects will be
* printed
* @param autoFlush A boolean; if true, the output buffer will be flushed
* whenever a byte array is written, one of the
* {@code println} methods is invoked, or a newline
* character or byte ({@code '\n'}) is written
* @param charset A {@linkplain java.nio.charset.Charset charset}
*
* @since 10
*/
public PrintStream(OutputStream out, boolean autoFlush, Charset charset) {
super(out);
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this, charset);
this.textOut = new BufferedWriter(charOut);
}
/**
@ -248,6 +266,36 @@ public class PrintStream extends FilterOutputStream
this(false, toCharset(csn), new FileOutputStream(fileName));
}
/**
* Creates a new print stream, without automatic line flushing, with the
* specified file name and charset. This convenience constructor creates
* the necessary intermediate {@link java.io.OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
* @param fileName
* The name of the file to use as the destination of this print
* stream. 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}
*
* @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
*
* @since 10
*/
public PrintStream(String fileName, Charset charset) throws IOException {
this(false, requireNonNull(charset, "charset"), new FileOutputStream(fileName));
}
/**
* Creates a new print stream, without automatic line flushing, with the
* specified file. This convenience constructor creates the necessary
@ -319,6 +367,37 @@ public class PrintStream extends FilterOutputStream
this(false, toCharset(csn), new FileOutputStream(file));
}
/**
* Creates a new print stream, without automatic line flushing, with the
* specified file and charset. This convenience constructor creates
* the necessary intermediate {@link java.io.OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
* @param file
* The file to use as the destination of this print stream. 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}
*
* @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
*
* @since 10
*/
public PrintStream(File file, Charset charset) throws IOException {
this(false, requireNonNull(charset, "charset"), new FileOutputStream(file));
}
/** Check to make sure that the stream has not been closed */
private void ensureOpen() throws IOException {
if (out == null)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -48,6 +48,11 @@ import java.nio.charset.UnsupportedCharsetException;
* constructors may. The client may inquire as to whether any errors have
* occurred by invoking {@link #checkError checkError()}.
*
* <p> This class always replaces malformed and unmappable character sequences with
* the charset's default replacement string.
* The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
* control over the encoding process is required.
*
* @author Frank Yellin
* @author Mark Reinhold
* @since 1.1
@ -137,7 +142,26 @@ public class PrintWriter extends Writer {
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out, boolean autoFlush) {
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
this(out, autoFlush, Charset.defaultCharset());
}
/**
* Creates a new PrintWriter from an existing OutputStream. This
* convenience constructor creates the necessary intermediate
* OutputStreamWriter, which will convert characters into bytes using the
* specified charset.
*
* @param out An output stream
* @param autoFlush A boolean; if true, the {@code println},
* {@code printf}, or {@code format} methods will
* flush the output buffer
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
*
* @since 10
*/
public PrintWriter(OutputStream out, boolean autoFlush, Charset charset) {
this(new BufferedWriter(new OutputStreamWriter(out, charset)), autoFlush);
// save print stream for error propagation
if (out instanceof java.io.PrintStream) {
@ -224,6 +248,36 @@ public class PrintWriter extends Writer {
this(toCharset(csn), new File(fileName));
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file name and charset. This convenience constructor creates
* the necessary intermediate {@link java.io.OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
* @param fileName
* The name of the file to use as the destination of this writer.
* 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}
*
* @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
*
* @since 10
*/
public PrintWriter(String fileName, Charset charset) throws IOException {
this(Objects.requireNonNull(charset, "charset"), new File(fileName));
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file. This convenience constructor creates the necessary
@ -295,6 +349,36 @@ public class PrintWriter extends Writer {
this(toCharset(csn), file);
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file and charset. This convenience constructor creates the
* necessary intermediate {@link java.io.OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
* @param file
* The file to use as the destination of this writer. 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}
*
* @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
*
* @since 10
*/
public PrintWriter(File file, Charset charset) throws IOException {
this(Objects.requireNonNull(charset, "charset"), file);
}
/** Checks to make sure that the stream has not been closed */
private void ensureOpen() throws IOException {
if (out == null)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,10 @@
package java.net;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Objects;
/**
* Utility class for HTML form decoding. This class contains static methods
@ -108,7 +112,43 @@ public class URLDecoder {
/**
* Decodes an {@code application/x-www-form-urlencoded} string using
* a specific encoding scheme.
* The supplied encoding is used to determine
*
* <p>
* This method behaves the same as {@linkplain decode(String s, Charset charset)}
* except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
* using the given encoding name.
*
* @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
* when illegal strings are encountered.
*
* @param s the {@code String} to decode
* @param enc The name of a supported
* <a href="../lang/package-summary.html#charenc">character
* encoding</a>.
* @return the newly decoded {@code String}
* @throws UnsupportedEncodingException
* If character encoding needs to be consulted, but
* named character encoding is not supported
* @see URLEncoder#encode(java.lang.String, java.lang.String)
* @since 1.4
*/
public static String decode(String s, String enc) throws UnsupportedEncodingException {
if (enc.length() == 0) {
throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
}
try {
Charset charset = Charset.forName(enc);
return decode(s, charset);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(enc);
}
}
/**
* Decodes an {@code application/x-www-form-urlencoded} string using
* a specific {@linkplain java.nio.charset.Charset Charset}.
* The supplied charset is used to determine
* what characters are represented by any consecutive sequences of the
* form "<i>{@code %xy}</i>".
* <p>
@ -118,29 +158,25 @@ public class URLDecoder {
* UTF-8 should be used. Not doing so may introduce
* incompatibilities.</em>
*
* @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
* when illegal strings are encountered.
*
* @param s the {@code String} to decode
* @param enc The name of a supported
* <a href="../lang/package-summary.html#charenc">character
* encoding</a>.
* @param charset the given charset
* @return the newly decoded {@code String}
* @exception UnsupportedEncodingException
* If character encoding needs to be consulted, but
* named character encoding is not supported
* @see URLEncoder#encode(java.lang.String, java.lang.String)
* @since 1.4
* @throws NullPointerException if {@code s} or {@code charset} is {@code null}
* @throws IllegalArgumentException if the implementation encounters illegal
* characters
* @see URLEncoder#encode(java.lang.String, java.nio.charset.Charset)
* @since 10
*/
public static String decode(String s, String enc)
throws UnsupportedEncodingException{
public static String decode(String s, Charset charset) {
Objects.requireNonNull(charset, "Charset");
boolean needToChange = false;
int numChars = s.length();
StringBuilder sb = new StringBuilder(numChars > 500 ? numChars / 2 : numChars);
int i = 0;
if (enc.length() == 0) {
throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
}
char c;
byte[] bytes = null;
while (i < numChars) {
@ -173,7 +209,9 @@ public class URLDecoder {
(c=='%')) {
int v = Integer.parseInt(s, i + 1, i + 3, 16);
if (v < 0)
throw new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern - negative value");
throw new IllegalArgumentException(
"URLDecoder: Illegal hex characters in escape "
+ "(%) pattern - negative value");
bytes[pos++] = (byte) v;
i+= 3;
if (i < numChars)
@ -187,7 +225,7 @@ public class URLDecoder {
throw new IllegalArgumentException(
"URLDecoder: Incomplete trailing escape (%) pattern");
sb.append(new String(bytes, 0, pos, enc));
sb.append(new String(bytes, 0, pos, charset));
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"URLDecoder: Illegal hex characters in escape (%) pattern - "

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,6 +31,7 @@ import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException ;
import java.util.BitSet;
import java.util.Objects;
import sun.security.action.GetPropertyAction;
/**
@ -168,45 +169,61 @@ public class URLEncoder {
/**
* Translates a string into {@code application/x-www-form-urlencoded}
* format using a specific encoding scheme. This method uses the
* supplied encoding scheme to obtain the bytes for unsafe
* characters.
* format using a specific encoding scheme.
* <p>
* <em><strong>Note:</strong> The <a href=
* "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
* World Wide Web Consortium Recommendation</a> states that
* UTF-8 should be used. Not doing so may introduce
* incompatibilities.</em>
* This method behaves the same as {@linkplain encode(String s, Charset charset)}
* except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
* using the given encoding name.
*
* @param s {@code String} to be translated.
* @param enc The name of a supported
* <a href="../lang/package-summary.html#charenc">character
* encoding</a>.
* @return the translated {@code String}.
* @exception UnsupportedEncodingException
* @throws UnsupportedEncodingException
* If the named encoding is not supported
* @see URLDecoder#decode(java.lang.String, java.lang.String)
* @since 1.4
*/
public static String encode(String s, String enc)
throws UnsupportedEncodingException {
if (enc == null) {
throw new NullPointerException("charsetName");
}
try {
Charset charset = Charset.forName(enc);
return encode(s, charset);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(enc);
}
}
/**
* Translates a string into {@code application/x-www-form-urlencoded}
* format using a specific {@linkplain java.nio.charset.Charset Charset}.
* This method uses the supplied charset to obtain the bytes for unsafe
* characters.
* <p>
* <em><strong>Note:</strong> The <a href=
* "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
* World Wide Web Consortium Recommendation</a> states that
* UTF-8 should be used. Not doing so may introduce incompatibilities.</em>
*
* @param s {@code String} to be translated.
* @param charset the given charset
* @return the translated {@code String}.
* @throws NullPointerException if {@code s} or {@code charset} is {@code null}.
* @see URLDecoder#decode(java.lang.String, java.nio.charset.Charset)
* @since 10
*/
public static String encode(String s, Charset charset) {
Objects.requireNonNull(charset, "charset");
boolean needToChange = false;
StringBuilder out = new StringBuilder(s.length());
Charset charset;
CharArrayWriter charArrayWriter = new CharArrayWriter();
if (enc == null)
throw new NullPointerException("charsetName");
try {
charset = Charset.forName(enc);
} catch (IllegalCharsetNameException e) {
throw new UnsupportedEncodingException(enc);
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(enc);
}
for (int i = 0; i < s.length();) {
int c = (int) s.charAt(i);
//System.out.println("Examining character: " + c);

View file

@ -527,7 +527,7 @@ public final class Channels {
* behaves in exactly the same way as the expression
*
* <pre> {@code
* Channels.newReader(ch, Charset.forName(csName).newDecoder(), -1)
* Channels.newReader(ch, Charset.forName(csName))
* } </pre>
*
* @param ch
@ -549,6 +549,38 @@ public final class Channels {
return newReader(ch, Charset.forName(csName).newDecoder(), -1);
}
/**
* Constructs a reader that decodes bytes from the given channel according
* to the given charset.
*
* <p> An invocation of this method of the form
*
* <pre> {@code
* Channels.newReader(ch, charset)
* } </pre>
*
* behaves in exactly the same way as the expression
*
* <pre> {@code
* Channels.newReader(ch, Charset.forName(csName).newDecoder(), -1)
* } </pre>
*
* <p> The reader's default action for malformed-input and unmappable-character
* errors is to {@linkplain java.nio.charset.CodingErrorAction#REPORT report}
* them. When more control over the error handling is required, the constructor
* that takes a {@linkplain java.nio.charset.CharsetDecoder} should be used.
*
* @param ch The channel from which bytes will be read
*
* @param charset The charset to be used
*
* @return A new reader
*/
public static Reader newReader(ReadableByteChannel ch, Charset charset) {
Objects.requireNonNull(charset, "charset");
return newReader(ch, charset.newDecoder(), -1);
}
/**
* Constructs a writer that encodes characters using the given encoder and
* writes the resulting bytes to the given channel.
@ -595,7 +627,7 @@ public final class Channels {
* behaves in exactly the same way as the expression
*
* <pre> {@code
* Channels.newWriter(ch, Charset.forName(csName).newEncoder(), -1)
* Channels.newWriter(ch, Charset.forName(csName))
* } </pre>
*
* @param ch
@ -616,4 +648,38 @@ public final class Channels {
Objects.requireNonNull(csName, "csName");
return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
}
/**
* Constructs a writer that encodes characters according to the given
* charset and writes the resulting bytes to the given channel.
*
* <p> An invocation of this method of the form
*
* <pre> {@code
* Channels.newWriter(ch, charset)
* } </pre>
*
* behaves in exactly the same way as the expression
*
* <pre> {@code
* Channels.newWriter(ch, Charset.forName(csName).newEncoder(), -1)
* } </pre>
*
* <p> The writer's default action for malformed-input and unmappable-character
* errors is to {@linkplain java.nio.charset.CodingErrorAction#REPORT report}
* them. When more control over the error handling is required, the constructor
* that takes a {@linkplain java.nio.charset.CharsetEncoder} should be used.
*
* @param ch
* The channel to which bytes will be written
*
* @param charset
* The charset to be used
*
* @return A new writer
*/
public static Writer newWriter(WritableByteChannel ch, Charset charset) {
Objects.requireNonNull(charset, "charset");
return newWriter(ch, charset.newEncoder(), -1);
}
}

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() {