diff --git a/src/java.base/share/classes/java/io/ByteArrayOutputStream.java b/src/java.base/share/classes/java/io/ByteArrayOutputStream.java
index bd867c08d76..7448eac2560 100644
--- a/src/java.base/share/classes/java/io/ByteArrayOutputStream.java
+++ b/src/java.base/share/classes/java/io/ByteArrayOutputStream.java
@@ -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}.
+ *
+ *
This method is equivalent to {@code #toString(charset)} that takes a
+ * {@link java.nio.charset.Charset charset}.
+ *
+ *
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.
+ *
+ *
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 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.
diff --git a/src/java.base/share/classes/java/io/PrintStream.java b/src/java.base/share/classes/java/io/PrintStream.java
index 09028de7358..ae041697826 100644
--- a/src/java.base/share/classes/java/io/PrintStream.java
+++ b/src/java.base/share/classes/java/io/PrintStream.java
@@ -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.
*
*
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.
*
+ *
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)
diff --git a/src/java.base/share/classes/java/io/PrintWriter.java b/src/java.base/share/classes/java/io/PrintWriter.java
index 0513312781b..e5ecc79c555 100644
--- a/src/java.base/share/classes/java/io/PrintWriter.java
+++ b/src/java.base/share/classes/java/io/PrintWriter.java
@@ -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()}.
*
+ *
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)
diff --git a/src/java.base/share/classes/java/net/URLDecoder.java b/src/java.base/share/classes/java/net/URLDecoder.java
index 0832d9161d6..69330299673 100644
--- a/src/java.base/share/classes/java/net/URLDecoder.java
+++ b/src/java.base/share/classes/java/net/URLDecoder.java
@@ -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
+ *
+ *
+ * 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
+ * character
+ * encoding.
+ * @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 "{@code %xy}".
*
@@ -118,29 +158,25 @@ public class URLDecoder {
* UTF-8 should be used. Not doing so may introduce
* incompatibilities.
*
+ * @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
- * character
- * encoding.
+ * @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 - "
diff --git a/src/java.base/share/classes/java/net/URLEncoder.java b/src/java.base/share/classes/java/net/URLEncoder.java
index 81eaa957cca..0a14c6b5f42 100644
--- a/src/java.base/share/classes/java/net/URLEncoder.java
+++ b/src/java.base/share/classes/java/net/URLEncoder.java
@@ -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.
*
- * Note: The
- * World Wide Web Consortium Recommendation states that
- * UTF-8 should be used. Not doing so may introduce
- * incompatibilities.
+ * 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
* character
* encoding.
* @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.
+ *
+ * Note: The
+ * World Wide Web Consortium Recommendation states that
+ * UTF-8 should be used. Not doing so may introduce incompatibilities.
+ *
+ * @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);
diff --git a/src/java.base/share/classes/java/nio/channels/Channels.java b/src/java.base/share/classes/java/nio/channels/Channels.java
index 2a4254711b3..dba3534053c 100644
--- a/src/java.base/share/classes/java/nio/channels/Channels.java
+++ b/src/java.base/share/classes/java/nio/channels/Channels.java
@@ -527,7 +527,7 @@ public final class Channels {
* behaves in exactly the same way as the expression
*
*
*
* @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.
+ *
+ *
An invocation of this method of the form
+ *
+ *
{@code
+ * Channels.newReader(ch, charset)
+ * }
+ *
+ * behaves in exactly the same way as the expression
+ *
+ *
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
*
*
*
* @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.
+ *
+ *
An invocation of this method of the form
+ *
+ *
{@code
+ * Channels.newWriter(ch, charset)
+ * }
+ *
+ * behaves in exactly the same way as the expression
+ *
+ *
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);
+}
}
diff --git a/src/java.base/share/classes/java/util/Formatter.java b/src/java.base/share/classes/java/util/Formatter.java
index 3c2abe2bcfb..9bbf068489b 100644
--- a/src/java.base/share/classes/java/util/Formatter.java
+++ b/src/java.base/share/classes/java/util/Formatter.java
@@ -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);
diff --git a/src/java.base/share/classes/java/util/Properties.java b/src/java.base/share/classes/java/util/Properties.java
index 1ae9a49a028..9d024694135 100644
--- a/src/java.base/share/classes/java/util/Properties.java
+++ b/src/java.base/share/classes/java/util/Properties.java
@@ -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