8183743: Umbrella: add overloads that take a Charset parameter
Reviewed-by: alanb, rriggs
This commit is contained in:
parent
f065141ddc
commit
4f080a83af
@ -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 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.
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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 - "
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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>
|
||||
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
|
||||
* </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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 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,7 @@
|
||||
package jdk.internal.util.xml;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.InvalidPropertiesFormatException;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
@ -94,11 +95,11 @@ public class PropertiesDefaultHandler extends DefaultHandler {
|
||||
*/
|
||||
}
|
||||
|
||||
public void store(Properties props, OutputStream os, String comment, String encoding)
|
||||
public void store(Properties props, OutputStream os, String comment, Charset charset)
|
||||
throws IOException
|
||||
{
|
||||
try {
|
||||
XMLStreamWriter writer = new XMLStreamWriterImpl(os, encoding);
|
||||
XMLStreamWriter writer = new XMLStreamWriterImpl(os, charset);
|
||||
writer.writeStartDocument();
|
||||
writer.writeDTD(PROPS_DTD_DECL);
|
||||
writer.writeStartElement(ELEMENT_ROOT);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 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,9 @@
|
||||
|
||||
package jdk.internal.util.xml;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Basic XMLStreamWriter for writing simple XML files such as those
|
||||
* defined in java.util.Properties
|
||||
@ -38,6 +41,7 @@ public interface XMLStreamWriter {
|
||||
//Defaults the XML version to 1.0, and the encoding to utf-8
|
||||
public static final String DEFAULT_XML_VERSION = "1.0";
|
||||
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
/**
|
||||
* Writes a start tag to the output. All writeStartElement methods
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 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
|
||||
@ -66,7 +66,7 @@ public class XMLStreamWriterImpl implements XMLStreamWriter {
|
||||
private int _state = 0;
|
||||
private Element _currentEle;
|
||||
private XMLWriter _writer;
|
||||
private String _encoding;
|
||||
private Charset _charset;
|
||||
/**
|
||||
* This flag can be used to turn escaping off for content. It does
|
||||
* not apply to attribute content.
|
||||
@ -79,26 +79,23 @@ public class XMLStreamWriterImpl implements XMLStreamWriter {
|
||||
System.getProperty("line.separator").toCharArray();
|
||||
|
||||
public XMLStreamWriterImpl(OutputStream os) throws XMLStreamException {
|
||||
this(os, XMLStreamWriter.DEFAULT_ENCODING);
|
||||
this(os, XMLStreamWriter.DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
public XMLStreamWriterImpl(OutputStream os, String encoding)
|
||||
public XMLStreamWriterImpl(OutputStream os, Charset cs)
|
||||
throws XMLStreamException
|
||||
{
|
||||
Charset cs = null;
|
||||
if (encoding == null) {
|
||||
_encoding = XMLStreamWriter.DEFAULT_ENCODING;
|
||||
if (cs == null) {
|
||||
_charset = XMLStreamWriter.DEFAULT_CHARSET;
|
||||
} else {
|
||||
try {
|
||||
cs = getCharset(encoding);
|
||||
_charset = checkCharset(cs);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new XMLStreamException(e);
|
||||
}
|
||||
|
||||
this._encoding = encoding;
|
||||
}
|
||||
|
||||
_writer = new XMLWriter(os, encoding, cs);
|
||||
_writer = new XMLWriter(os, null, _charset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +105,7 @@ public class XMLStreamWriterImpl implements XMLStreamWriter {
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public void writeStartDocument() throws XMLStreamException {
|
||||
writeStartDocument(_encoding, XMLStreamWriter.DEFAULT_XML_VERSION);
|
||||
writeStartDocument(_charset.name(), XMLStreamWriter.DEFAULT_XML_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,7 +115,7 @@ public class XMLStreamWriterImpl implements XMLStreamWriter {
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public void writeStartDocument(String version) throws XMLStreamException {
|
||||
writeStartDocument(_encoding, version, null);
|
||||
writeStartDocument(_charset.name(), version, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -155,7 +152,7 @@ public class XMLStreamWriterImpl implements XMLStreamWriter {
|
||||
_state = STATE_XML_DECL;
|
||||
String enc = encoding;
|
||||
if (enc == null) {
|
||||
enc = _encoding;
|
||||
enc = _charset.name();
|
||||
} else {
|
||||
//check if the encoding is supported
|
||||
try {
|
||||
@ -564,6 +561,20 @@ public class XMLStreamWriterImpl implements XMLStreamWriter {
|
||||
return cs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for charset support.
|
||||
* @param charset the specified charset
|
||||
* @return the charset
|
||||
* @throws UnsupportedEncodingException if the charset is not supported
|
||||
*/
|
||||
private Charset checkCharset(Charset charset) throws UnsupportedEncodingException {
|
||||
if (charset.name().equalsIgnoreCase("UTF-32")) {
|
||||
throw new UnsupportedEncodingException("The basic XMLWriter does "
|
||||
+ "not support " + charset.name());
|
||||
}
|
||||
return charset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Start of Internal classes.
|
||||
*
|
||||
|
84
test/jdk/java/io/ByteArrayOutputStream/EncodingTest.java
Normal file
84
test/jdk/java/io/ByteArrayOutputStream/EncodingTest.java
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
/*
|
||||
* DataProvider for the toString method test. Provides the following fields:
|
||||
* byte array, charset name string, charset object
|
||||
*/
|
||||
@DataProvider(name = "parameters")
|
||||
public Object[][] getParameters() throws IOException {
|
||||
byte[] data = getData();
|
||||
return new Object[][]{
|
||||
{data, StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8},
|
||||
{data, StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the new overload method that takes a Charset is equivalent to
|
||||
* the existing one that takes a charset name.
|
||||
* @param data a byte array
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws Exception if the test fails
|
||||
*/
|
||||
@Test(dataProvider = "parameters")
|
||||
public void test(byte[] data, String csn, Charset charset) throws Exception {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
baos.write(data);
|
||||
String str1 = baos.toString(csn);
|
||||
String str2 = baos.toString(charset);
|
||||
Assert.assertEquals(str1, str2);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns an array containing a character that's invalid for UTF-8
|
||||
* but valid for ISO-8859-1
|
||||
*/
|
||||
byte[] getData() throws IOException {
|
||||
String str1 = "A string that contains ";
|
||||
String str2 = " , an invalid character for UTF-8.";
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
baos.write(str1.getBytes());
|
||||
baos.write(0xFA);
|
||||
baos.write(str2.getBytes());
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
134
test/jdk/java/io/PrintStream/EncodingTest.java
Normal file
134
test/jdk/java/io/PrintStream/EncodingTest.java
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
static String USER_DIR = System.getProperty("user.dir", ".");
|
||||
static boolean AUTOFLUSH = true;
|
||||
public static enum ConstructorType {
|
||||
STRING,
|
||||
FILE,
|
||||
OUTPUTSTREAM
|
||||
}
|
||||
|
||||
/*
|
||||
* DataProvider fields:
|
||||
* Type of the constructor, a file to be written with a charset name,
|
||||
* a file to be written with a charset, charset name, charset.
|
||||
*/
|
||||
@DataProvider(name = "parameters")
|
||||
public Object[][] getParameters() throws IOException {
|
||||
String csn = StandardCharsets.UTF_8.name();
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
File file1 = new File(USER_DIR, "PSCharsetTest1.txt");
|
||||
File file2 = new File(USER_DIR, "PSCharsetTest2.txt");
|
||||
|
||||
return new Object[][]{
|
||||
{ConstructorType.STRING, file1, file2, csn, charset},
|
||||
{ConstructorType.FILE, file1, file2, csn, charset},
|
||||
{ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the overloading constructor behaves the same as the existing
|
||||
* one.
|
||||
* @param type the type of the constructor
|
||||
* @param file1 file1 written with the name of a charset
|
||||
* @param file2 file2 written with a charset
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test(dataProvider = "parameters")
|
||||
public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
|
||||
throws Exception {
|
||||
createFile(getPrintStream(type, file1.getPath(), csn, null));
|
||||
createFile(getPrintStream(type, file2.getPath(), null, charset));
|
||||
|
||||
Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
|
||||
Files.readAllLines(Paths.get(file2.getPath()), charset));
|
||||
}
|
||||
|
||||
public void createFile(PrintStream out) throws IOException {
|
||||
out.println("high surrogate");
|
||||
out.println(Character.MIN_HIGH_SURROGATE);
|
||||
out.println("low surrogate");
|
||||
out.println(Character.MIN_LOW_SURROGATE);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
PrintStream getPrintStream(ConstructorType type, String path, String csn, Charset charset)
|
||||
throws IOException {
|
||||
PrintStream out = null;
|
||||
if (csn != null) {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
out = new PrintStream(path, csn);
|
||||
break;
|
||||
case FILE:
|
||||
out = new PrintStream(new File(path), csn);
|
||||
break;
|
||||
case OUTPUTSTREAM:
|
||||
FileOutputStream fout = new FileOutputStream(path);
|
||||
out = new PrintStream(fout, AUTOFLUSH, csn);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
out = new PrintStream(path, charset);
|
||||
break;
|
||||
case FILE:
|
||||
out = new PrintStream(new File(path), charset);
|
||||
break;
|
||||
case OUTPUTSTREAM:
|
||||
FileOutputStream fout = new FileOutputStream(path);
|
||||
out = new PrintStream(fout, AUTOFLUSH, charset);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011, 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
|
||||
@ -23,7 +23,7 @@
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 7000511
|
||||
* @bug 7000511 8190577
|
||||
* @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
|
||||
* exception thrown
|
||||
*/
|
||||
@ -68,7 +68,7 @@ public class FailingConstructors {
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new PrintStream(file, null);
|
||||
new PrintStream(file, (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
@ -87,7 +87,7 @@ public class FailingConstructors {
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new PrintStream(file.getName(), null);
|
||||
new PrintStream(file.getName(), (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
|
135
test/jdk/java/io/PrintWriter/EncodingTest.java
Normal file
135
test/jdk/java/io/PrintWriter/EncodingTest.java
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
static String USER_DIR = System.getProperty("user.dir", ".");
|
||||
static boolean AUTOFLUSH = true;
|
||||
public static enum ConstructorType {
|
||||
STRING,
|
||||
FILE,
|
||||
OUTPUTSTREAM
|
||||
}
|
||||
|
||||
/*
|
||||
* DataProvider fields:
|
||||
* Type of the constructor, a file to be written with a charset name,
|
||||
* a file to be written with a charset, charset name, charset.
|
||||
*/
|
||||
@DataProvider(name = "parameters")
|
||||
public Object[][] getParameters() throws IOException {
|
||||
String csn = StandardCharsets.UTF_8.name();
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
File file1 = new File(USER_DIR, "PWCharsetTest1.txt");
|
||||
File file2 = new File(USER_DIR, "PWCharsetTest2.txt");
|
||||
|
||||
return new Object[][]{
|
||||
{ConstructorType.STRING, file1, file2, csn, charset},
|
||||
{ConstructorType.FILE, file1, file2, csn, charset},
|
||||
{ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the overloading constructor behaves the same as the existing
|
||||
* one.
|
||||
*
|
||||
* @param type the type of the constructor
|
||||
* @param file1 file1 written with the name of a charset
|
||||
* @param file2 file2 written with a charset
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test(dataProvider = "parameters")
|
||||
public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
|
||||
throws Exception {
|
||||
createFile(getWriter(type, file1.getPath(), csn, null));
|
||||
createFile(getWriter(type, file2.getPath(), null, charset));
|
||||
|
||||
Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
|
||||
Files.readAllLines(Paths.get(file2.getPath()), charset));
|
||||
}
|
||||
|
||||
void createFile(PrintWriter out) throws IOException {
|
||||
out.println("high surrogate");
|
||||
out.println(Character.MIN_HIGH_SURROGATE);
|
||||
out.println("low surrogate");
|
||||
out.println(Character.MIN_LOW_SURROGATE);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
PrintWriter getWriter(ConstructorType type, String path, String csn, Charset charset)
|
||||
throws IOException {
|
||||
PrintWriter out = null;
|
||||
if (csn != null) {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
out = new PrintWriter(path, csn);
|
||||
break;
|
||||
case FILE:
|
||||
out = new PrintWriter(new File(path), csn);
|
||||
break;
|
||||
case OUTPUTSTREAM:
|
||||
// No corresponding method with charset name
|
||||
// compare with PrintWriter(path, csn) instead
|
||||
out = new PrintWriter(path, csn);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
out = new PrintWriter(path, charset);
|
||||
break;
|
||||
case FILE:
|
||||
out = new PrintWriter(new File(path), charset);
|
||||
break;
|
||||
case OUTPUTSTREAM:
|
||||
FileOutputStream fout = new FileOutputStream(path);
|
||||
out = new PrintWriter(fout, AUTOFLUSH, charset);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
@ -67,7 +67,7 @@ public class FailingConstructors {
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new PrintWriter(file, null);
|
||||
new PrintWriter(file, (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
@ -86,7 +86,7 @@ public class FailingConstructors {
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new PrintWriter(file.getName(), null);
|
||||
new PrintWriter(file.getName(), (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
|
122
test/jdk/java/net/URLDecoder/EncodingTest.java
Normal file
122
test/jdk/java/net/URLDecoder/EncodingTest.java
Normal file
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the
|
||||
* same as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
public static enum ParameterType {
|
||||
STRING,
|
||||
CHARSET
|
||||
}
|
||||
|
||||
@DataProvider(name = "illegalArgument")
|
||||
public Object[][] getParameters() {
|
||||
return new Object[][]{
|
||||
{ParameterType.STRING},
|
||||
{ParameterType.CHARSET}
|
||||
};
|
||||
}
|
||||
|
||||
@DataProvider(name = "decode")
|
||||
public Object[][] getDecodeParameters() {
|
||||
return new Object[][]{
|
||||
{"The string \u00FC@foo-bar"},
|
||||
// the string from javadoc example
|
||||
|
||||
{""}, // an empty string
|
||||
|
||||
{"x"}, // a string of length 1
|
||||
|
||||
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"},
|
||||
// the string of characters should remain the same
|
||||
|
||||
{charactersRange('\u0000', '\u007F')},
|
||||
// a string of characters from 0 to 127
|
||||
|
||||
{charactersRange('\u0080', '\u00FF')},
|
||||
// a string of characters from 128 to 255
|
||||
|
||||
{"\u0100 \u0101 \u0555 \u07FD \u07FF"},
|
||||
// a string of Unicode values can be expressed as 2 bytes
|
||||
|
||||
{"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that IAE is thrown when decoding an invalid string using the
|
||||
* existing method or the new overload method.
|
||||
*
|
||||
* @param type the type of the argument, e.g a String charset name or
|
||||
* charset
|
||||
*/
|
||||
@Test(dataProvider = "illegalArgument", expectedExceptions = IllegalArgumentException.class)
|
||||
public void testIllegalArgument(ParameterType type) throws Exception {
|
||||
String encoded = URLEncoder.encode("http://www.xyz.com/find?key=\u0100\u0101",
|
||||
StandardCharsets.UTF_8.name());
|
||||
String illegal = "%" + encoded;
|
||||
String returned;
|
||||
if (type == ParameterType.STRING) {
|
||||
returned = URLDecoder.decode(illegal, StandardCharsets.UTF_8.name());
|
||||
} else {
|
||||
returned = URLDecoder.decode(illegal, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the returned values of decoding with the existing
|
||||
* and the overload methods match.
|
||||
*
|
||||
* @param s the string to be encoded and then decoded with both existing
|
||||
* and the overload methods.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test(dataProvider = "decode")
|
||||
public void decode(String s) throws Exception {
|
||||
String encoded = URLEncoder.encode(s, StandardCharsets.UTF_8.name());
|
||||
String returned1 = URLDecoder.decode(encoded, StandardCharsets.UTF_8.name());
|
||||
String returned2 = URLDecoder.decode(encoded, StandardCharsets.UTF_8);
|
||||
Assert.assertEquals(returned1, returned2);
|
||||
}
|
||||
|
||||
String charactersRange(char c1, char c2) {
|
||||
StringBuilder sb = new StringBuilder(c2 - c1);
|
||||
for (char c = c1; c < c2; c++) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2001, 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
|
||||
@ -23,7 +23,7 @@
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 4444194
|
||||
* @bug 4444194 8190577
|
||||
* @summary java.net.URLDecoder.decode(s, enc) treats an empty encoding name as "UTF-8"
|
||||
*/
|
||||
import java.net.URLDecoder;
|
||||
@ -32,7 +32,7 @@ import java.io.UnsupportedEncodingException;
|
||||
public class URLDecoderArgs {
|
||||
public static void main (String[] args) {
|
||||
try {
|
||||
String s1 = URLDecoder.decode ("Hello World", null);
|
||||
String s1 = URLDecoder.decode ("Hello World", (String)null);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException ("NPE should have been thrown");
|
||||
} catch (NullPointerException e) {
|
||||
|
97
test/jdk/java/net/URLEncoder/EncodingTest.java
Normal file
97
test/jdk/java/net/URLEncoder/EncodingTest.java
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
public static enum ParameterType {
|
||||
STRING,
|
||||
CHARSET
|
||||
}
|
||||
|
||||
@DataProvider(name = "encode")
|
||||
public Object[][] getDecodeParameters() {
|
||||
return new Object[][]{
|
||||
{"The string \u00FC@foo-bar"},
|
||||
// the string from javadoc example
|
||||
|
||||
{""}, // an empty string
|
||||
|
||||
{"x"}, // a string of length 1
|
||||
|
||||
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"},
|
||||
// the string of characters should remain the same
|
||||
|
||||
{charactersRange('\u0000', '\u007F')},
|
||||
// a string of characters from 0 to 127
|
||||
|
||||
{charactersRange('\u0080', '\u00FF')},
|
||||
// a string of characters from 128 to 255
|
||||
|
||||
{"\u0100 \u0101 \u0555 \u07FD \u07FF"},
|
||||
// a string of Unicode values can be expressed as 2 bytes
|
||||
|
||||
{"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the new overload method returns the same result as the
|
||||
* existing method.
|
||||
*
|
||||
* @param s the string to be encoded
|
||||
* @throws Exception if the test fails
|
||||
*/
|
||||
@Test(dataProvider = "encode")
|
||||
public void encode(String s) throws Exception {
|
||||
String encoded1 = URLEncoder.encode(s, StandardCharsets.UTF_8.name());
|
||||
String encoded2 = URLEncoder.encode(s, StandardCharsets.UTF_8);
|
||||
Assert.assertEquals(encoded1, encoded2);
|
||||
|
||||
// cross check
|
||||
String returned1 = URLDecoder.decode(encoded1, StandardCharsets.UTF_8.name());
|
||||
String returned2 = URLDecoder.decode(encoded2, StandardCharsets.UTF_8);
|
||||
Assert.assertEquals(returned1, returned2);
|
||||
}
|
||||
|
||||
String charactersRange(char c1, char c2) {
|
||||
StringBuilder sb = new StringBuilder(c2 - c1);
|
||||
for (char c = c1; c < c2; c++) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ import java.io.UnsupportedEncodingException;
|
||||
public class URLEncoderEncodeArgs {
|
||||
public static void main (String[] args) {
|
||||
try {
|
||||
String s1 = URLEncoder.encode ("Hello World", null);
|
||||
String s1 = URLEncoder.encode ("Hello World", (String)null);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException ("NPE should have been thrown");
|
||||
} catch (NullPointerException e) {
|
||||
|
@ -108,13 +108,24 @@ public class Basic {
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
Channels.newReader(rbc, null);
|
||||
Channels.newReader(rbc, (String)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
|
||||
try {
|
||||
Channels.newReader(null, null);
|
||||
Channels.newReader(null, (String)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
Channels.newReader(rbc, (Charset)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
|
||||
try {
|
||||
Channels.newReader(null, (Charset)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
@ -142,15 +153,24 @@ public class Basic {
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
Channels.newWriter(wbc, null);
|
||||
Channels.newWriter(wbc, (String)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
Channels.newWriter(null, null);
|
||||
Channels.newWriter(null, (String)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
Channels.newWriter(wbc, (Charset)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
Channels.newWriter(null, (Charset)null);
|
||||
failNpeExpected();
|
||||
} catch (NullPointerException npe) {}
|
||||
|
||||
try {
|
||||
blah = File.createTempFile("blah", null);
|
||||
|
266
test/jdk/java/nio/channels/Channels/EncodingTest.java
Normal file
266
test/jdk/java/nio/channels/Channels/EncodingTest.java
Normal file
@ -0,0 +1,266 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.MalformedInputException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
static final int ITERATIONS = 2;
|
||||
public static final String CS_UTF8 = StandardCharsets.UTF_8.name();
|
||||
public static final String CS_ISO8859 = StandardCharsets.ISO_8859_1.name();
|
||||
static String USER_DIR = System.getProperty("user.dir", ".");
|
||||
|
||||
// malformed input: a high surrogate without the low surrogate
|
||||
static char[] illChars = {
|
||||
'\u00fa', '\ud800'
|
||||
};
|
||||
|
||||
static byte[] data = getData();
|
||||
|
||||
static byte[] getData() {
|
||||
try {
|
||||
String str1 = "A string that contains ";
|
||||
String str2 = " , an invalid character for UTF-8.";
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
baos.write(str1.getBytes());
|
||||
baos.write(0xFA);
|
||||
baos.write(str2.getBytes());
|
||||
return baos.toByteArray();
|
||||
} catch (IOException ex) {
|
||||
return null; //shouldn't happen
|
||||
}
|
||||
}
|
||||
|
||||
String testFile = Paths.get(USER_DIR, "channelsEncodingTest.txt").toString();
|
||||
String testIllegalInput = Paths.get(USER_DIR, "channelsIllegalInputTest.txt").toString();
|
||||
String testIllegalOutput = Paths.get(USER_DIR, "channelsIllegalOutputTest.txt").toString();
|
||||
|
||||
|
||||
/*
|
||||
* DataProvider for read and write test.
|
||||
* Writes and reads with the same encoding
|
||||
*/
|
||||
@DataProvider(name = "writeAndRead")
|
||||
public Object[][] getWRParameters() {
|
||||
return new Object[][]{
|
||||
{testFile, StandardCharsets.ISO_8859_1.name(), null,
|
||||
StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1},
|
||||
{testFile, null, StandardCharsets.ISO_8859_1,
|
||||
StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1},
|
||||
{testFile, StandardCharsets.UTF_8.name(), null,
|
||||
StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8},
|
||||
{testFile, null, StandardCharsets.UTF_8,
|
||||
StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8}
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* DataProvider for illegal input test
|
||||
* Writes the data in ISO8859 and reads with UTF8, expects MalformedInputException
|
||||
*/
|
||||
@DataProvider(name = "illegalInput")
|
||||
public Object[][] getParameters() {
|
||||
return new Object[][]{
|
||||
{testIllegalInput, StandardCharsets.ISO_8859_1.name(), null, StandardCharsets.UTF_8.name(), null},
|
||||
{testIllegalInput, StandardCharsets.ISO_8859_1.name(), null, null, StandardCharsets.UTF_8},
|
||||
{testIllegalInput, null, StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8.name(), null},
|
||||
{testIllegalInput, null, StandardCharsets.ISO_8859_1, null, StandardCharsets.UTF_8},
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* DataProvider for illegal output test
|
||||
* Attemps to write some malformed chars, expects MalformedInputException
|
||||
*/
|
||||
@DataProvider(name = "illegalOutput")
|
||||
public Object[][] getWriteParameters() {
|
||||
return new Object[][]{
|
||||
{testIllegalOutput, StandardCharsets.UTF_8.name(), null},
|
||||
{testIllegalOutput, null, StandardCharsets.UTF_8}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the Readers created with the following methods are
|
||||
* equivalent:
|
||||
* newReader(ReadableByteChannel ch, String csName)
|
||||
* newReader(ReadableByteChannel ch, Charset charset)
|
||||
*
|
||||
* The verification follows the following steps:
|
||||
* Writes a file with a writer created with the specified charset
|
||||
* Reads it with a reader created with newReader using the same charset;
|
||||
* Compares that the results are the same.
|
||||
*
|
||||
* @param file the file name
|
||||
* @param csnWriter the charset name for creating the writer
|
||||
* @param charsetWriter the charset for creating the writer
|
||||
* @param csnReader the charset name for creating the reader
|
||||
* @param charsetReader the charset for creating the reader
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test(dataProvider = "writeAndRead")
|
||||
public void testWriteAndRead(String file, String csnWriter, Charset charsetWriter,
|
||||
String csnReader, Charset charsetReader) throws Exception {
|
||||
writeToFile(data, file, csnWriter, charsetWriter);
|
||||
// read using charset name
|
||||
String result1 = readFileToString(file, csnReader, null);
|
||||
String result2 = readFileToString(file, null, charsetReader);
|
||||
|
||||
Assert.assertEquals(result1, result2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that MalformedInputException is thrown when an input byte sequence
|
||||
* is illegal for given charset that is configured for the reader.
|
||||
*
|
||||
* @param file the file to be read
|
||||
* @param csnWriter the charset name for creating the writer
|
||||
* @param charsetWriter the charset for creating the writer
|
||||
* @param csnReader the charset name for creating the reader
|
||||
* @param charsetReader the charset for creating the reader
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class)
|
||||
void testMalformedInput(String file, String csnWriter, Charset charsetWriter,
|
||||
String csnReader, Charset charsetReader) throws Exception {
|
||||
writeToFile(data, file, csnWriter, charsetWriter);
|
||||
readFileToString(file, csnReader, charsetReader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to write illegal characters using a writer created by calling
|
||||
* the newWriter method and expects a MalformedInputException.
|
||||
*
|
||||
* @param fileName the file name
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test(dataProvider = "illegalOutput", expectedExceptions = MalformedInputException.class)
|
||||
public void testMalformedOutput(String fileName, String csn, Charset charset)
|
||||
throws Exception {
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName);
|
||||
WritableByteChannel wbc = (WritableByteChannel) fos.getChannel();) {
|
||||
Writer writer;
|
||||
if (csn != null) {
|
||||
writer = Channels.newWriter(wbc, csn);
|
||||
} else {
|
||||
writer = Channels.newWriter(wbc, charset);
|
||||
}
|
||||
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
writer.write(illChars);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the data to a file using a writer created by calling the newWriter
|
||||
* method.
|
||||
*
|
||||
* @param data the data to be written
|
||||
* @param fileName the file name
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws Exception
|
||||
*/
|
||||
private void writeToFile(byte[] data, String fileName, String csn, Charset charset) throws Exception {
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName);
|
||||
WritableByteChannel wbc = (WritableByteChannel) fos.getChannel()) {
|
||||
Writer writer;
|
||||
String temp;
|
||||
if (csn != null) {
|
||||
writer = Channels.newWriter(wbc, csn);
|
||||
temp = new String(data, csn);
|
||||
} else {
|
||||
writer = Channels.newWriter(wbc, charset);
|
||||
temp = new String(data, charset);
|
||||
}
|
||||
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
writer.write(temp);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file into a String.
|
||||
*
|
||||
* @param file the file to be read
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws Exception
|
||||
*/
|
||||
String readFileToString(String file, String csn, Charset charset) throws Exception {
|
||||
String result;
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
ReadableByteChannel rbc = (ReadableByteChannel) fis.getChannel()) {
|
||||
Reader reader;
|
||||
if (csn != null) {
|
||||
reader = Channels.newReader(rbc, csn);
|
||||
} else {
|
||||
reader = Channels.newReader(rbc, charset);
|
||||
}
|
||||
|
||||
int messageSize = data.length * ITERATIONS;
|
||||
char data1[] = new char[messageSize];
|
||||
int totalRead = 0;
|
||||
int charsRead = 0;
|
||||
while (totalRead < messageSize) {
|
||||
totalRead += charsRead;
|
||||
charsRead = reader.read(data1, totalRead, messageSize - totalRead);
|
||||
}
|
||||
|
||||
result = new String(data1, 0, totalRead);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -401,12 +401,23 @@ public class Constructors {
|
||||
}
|
||||
|
||||
try (PrintStream ps = new PrintStream("foo")) {
|
||||
new Formatter(ps, null, Locale.UK);
|
||||
fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)");
|
||||
new Formatter(ps, (String)null, Locale.UK);
|
||||
fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)",
|
||||
fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)",
|
||||
x);
|
||||
}
|
||||
|
||||
// Formatter(OutputStream os, Charset charset, Locale l)
|
||||
try (PrintStream ps = new PrintStream("foo")) {
|
||||
new Formatter(ps, (Charset)null, Locale.UK);
|
||||
fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)",
|
||||
x);
|
||||
}
|
||||
|
||||
@ -438,12 +449,22 @@ public class Constructors {
|
||||
|
||||
// PrintStream(String fileName, String csn)
|
||||
try {
|
||||
new PrintStream("foo", null);
|
||||
fail("new PrintStream(\"foo\", null)");
|
||||
new PrintStream("foo", (String)null);
|
||||
fail("new PrintStream(\"foo\", (String)null)");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintStream(\"foo\", null)", x);
|
||||
fail("new PrintStream(\"foo\", (String)null)", x);
|
||||
}
|
||||
|
||||
// PrintStream(String fileName, Charset charset)
|
||||
try {
|
||||
new PrintStream("foo", (Charset)null);
|
||||
fail("new PrintStream(\"foo\", (Charset)null)");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintStream(\"foo\", (Charset)null)", x);
|
||||
}
|
||||
|
||||
// PrintStream(File file)
|
||||
@ -455,12 +476,22 @@ public class Constructors {
|
||||
|
||||
// PrintStream(File file, String csn)
|
||||
try {
|
||||
new PrintStream(new File("foo"), null);
|
||||
fail("new PrintStream(new File(\"foo\"), null)");
|
||||
new PrintStream(new File("foo"), (String)null);
|
||||
fail("new PrintStream(new File(\"foo\"), (String)null)");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintStream(new File(\"foo\"), null)", x);
|
||||
fail("new PrintStream(new File(\"foo\"), (String)null)", x);
|
||||
}
|
||||
|
||||
// PrintStream(File file, Charset charset)
|
||||
try {
|
||||
new PrintStream(new File("foo"), (Charset)null);
|
||||
fail("new PrintStream(new File(\"foo\"), (Charset)null)");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintStream(new File(\"foo\"), (Charset)null)", x);
|
||||
}
|
||||
|
||||
// PrintWriter(String fileName)
|
||||
@ -472,12 +503,22 @@ public class Constructors {
|
||||
|
||||
// PrintWriter(String fileName, String csn)
|
||||
try {
|
||||
new PrintWriter("foo", null);
|
||||
fail("new PrintWriter(\"foo\"), null");
|
||||
new PrintWriter("foo", (String)null);
|
||||
fail("new PrintWriter(\"foo\"), (String)null");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintWriter(\"foo\"), null", x);
|
||||
fail("new PrintWriter(\"foo\"), (String)null", x);
|
||||
}
|
||||
|
||||
// PrintWriter(String fileName, Charset charset)
|
||||
try {
|
||||
new PrintWriter("foo", (Charset)null);
|
||||
fail("new PrintWriter(\"foo\"), (Charset)null");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintWriter(\"foo\"), (Charset)null", x);
|
||||
}
|
||||
|
||||
// PrintWriter(File file)
|
||||
@ -489,12 +530,22 @@ public class Constructors {
|
||||
|
||||
// PrintWriter(File file, String csn)
|
||||
try {
|
||||
new PrintWriter(new File("foo"), null);
|
||||
fail("new PrintWriter(new File(\"foo\")), null");
|
||||
new PrintWriter(new File("foo"), (String)null);
|
||||
fail("new PrintWriter(new File(\"foo\")), (String)null");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintWriter(new File(\"foo\")), null", x);
|
||||
fail("new PrintWriter(new File(\"foo\")), (String)null", x);
|
||||
}
|
||||
|
||||
// PrintWriter(File file, Charset charset)
|
||||
try {
|
||||
new PrintWriter(new File("foo"), (Charset)null);
|
||||
fail("new PrintWriter(new File(\"foo\")), (Charset)null");
|
||||
} catch (NullPointerException x) {
|
||||
pass();
|
||||
} catch (Exception x) {
|
||||
fail("new PrintWriter(new File(\"foo\")), (Charset)null", x);
|
||||
}
|
||||
|
||||
if (fail != 0)
|
||||
|
133
test/jdk/java/util/Formatter/EncodingTest.java
Normal file
133
test/jdk/java/util/Formatter/EncodingTest.java
Normal file
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Formatter;
|
||||
import java.util.Locale;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
static String USER_DIR = System.getProperty("user.dir", ".");
|
||||
|
||||
// Charset added only for the 3-parameter constructors
|
||||
public static enum ConstructorType {
|
||||
STRING3,
|
||||
FILE3,
|
||||
OUTPUTSTREAM3
|
||||
}
|
||||
|
||||
@DataProvider(name = "parameters")
|
||||
public Object[][] getParameters() throws IOException {
|
||||
Locale l = Locale.getDefault();
|
||||
String csn1 = StandardCharsets.ISO_8859_1.name();
|
||||
Charset charset1 = StandardCharsets.ISO_8859_1;
|
||||
String csn2 = StandardCharsets.UTF_8.name();
|
||||
Charset charset2 = StandardCharsets.UTF_8;
|
||||
|
||||
File file1 = new File(USER_DIR, "FormatterCharsetTest1.txt");
|
||||
File file2 = new File(USER_DIR, "FormatterCharsetTest2.txt");
|
||||
|
||||
return new Object[][]{
|
||||
{ConstructorType.STRING3, file1, file2, csn1, charset1},
|
||||
{ConstructorType.FILE3, file1, file2, csn1, charset1},
|
||||
{ConstructorType.OUTPUTSTREAM3, file1, file2, csn1, charset1},
|
||||
{ConstructorType.STRING3, file1, file2, csn2, charset2},
|
||||
{ConstructorType.FILE3, file1, file2, csn2, charset2},
|
||||
{ConstructorType.OUTPUTSTREAM3, file1, file2, csn2, charset2},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the overloading constructor behaves the same as the existing
|
||||
* one.
|
||||
* @param type the type of the constructor
|
||||
* @param file1 file1 written with the name of a charset
|
||||
* @param file2 file2 written with a charset
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test(dataProvider = "parameters")
|
||||
public void testConstructor(ConstructorType type, File file1, File file2,
|
||||
String csn, Charset charset) throws Exception {
|
||||
format(getFormatter(type, file1.getPath(), csn, null));
|
||||
format(getFormatter(type, file2.getPath(), null, charset));
|
||||
Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
|
||||
Files.readAllLines(Paths.get(file2.getPath()), charset));
|
||||
}
|
||||
|
||||
void format(Formatter formatter)
|
||||
throws IOException {
|
||||
formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
|
||||
formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
|
||||
formatter.flush();
|
||||
formatter.close();
|
||||
}
|
||||
|
||||
|
||||
Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
|
||||
throws IOException {
|
||||
Formatter formatter = null;
|
||||
if (csn != null) {
|
||||
switch (type) {
|
||||
case STRING3:
|
||||
formatter = new Formatter(path, csn, Locale.getDefault());
|
||||
break;
|
||||
case FILE3:
|
||||
formatter = new Formatter(new File(path), csn, Locale.getDefault());
|
||||
break;
|
||||
case OUTPUTSTREAM3:
|
||||
formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (type) {
|
||||
case STRING3:
|
||||
formatter = new Formatter(path, charset, Locale.getDefault());
|
||||
break;
|
||||
case FILE3:
|
||||
formatter = new Formatter(new File(path), charset, Locale.getDefault());
|
||||
break;
|
||||
case OUTPUTSTREAM3:
|
||||
formatter = new Formatter(new FileOutputStream(path), charset, Locale.getDefault());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
}
|
79
test/jdk/java/util/Properties/EncodingTest.java
Normal file
79
test/jdk/java/util/Properties/EncodingTest.java
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Properties;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the
|
||||
* same as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
@DataProvider(name = "parameters")
|
||||
public Object[][] getParameters() throws IOException {
|
||||
return new Object[][]{
|
||||
{StandardCharsets.UTF_8.name(), null},
|
||||
{null, StandardCharsets.UTF_8},};
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that properties saved with Properties#storeToXML with either an
|
||||
* encoding name or a charset can be read with Properties#loadFromXML that
|
||||
* returns the same Properties object.
|
||||
*/
|
||||
@Test(dataProvider = "parameters")
|
||||
void testLoadAndStore(String encoding, Charset charset) throws IOException {
|
||||
Properties props = new Properties();
|
||||
props.put("k0", "\u6C34");
|
||||
props.put("k1", "foo");
|
||||
props.put("k2", "bar");
|
||||
props.put("k3", "\u0020\u0391\u0392\u0393\u0394\u0395\u0396\u0397");
|
||||
props.put("k4", "\u7532\u9aa8\u6587");
|
||||
props.put("k5", "<java.home>/conf/jaxp.properties");
|
||||
props.put("k6", "\ud800\u00fa");
|
||||
|
||||
Properties p;
|
||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
if (encoding != null) {
|
||||
props.storeToXML(out, null, encoding);
|
||||
} else {
|
||||
props.storeToXML(out, null, charset);
|
||||
} p = new Properties();
|
||||
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
|
||||
p.loadFromXML(in);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertEquals(props, p);
|
||||
}
|
||||
}
|
135
test/jdk/java/util/Scanner/EncodingTest.java
Normal file
135
test/jdk/java/util/Scanner/EncodingTest.java
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Scanner;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the
|
||||
* same as the existing method that takes a charset name.
|
||||
* @run testng EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
static String USER_DIR = System.getProperty("user.dir", ".");
|
||||
|
||||
public static enum ConstructorType {
|
||||
FILE,
|
||||
PATH,
|
||||
INPUTSTREAM,
|
||||
READABLEBYTECHANNEL
|
||||
}
|
||||
|
||||
static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";
|
||||
|
||||
@DataProvider(name = "parameters")
|
||||
public Object[][] getParameters() throws IOException {
|
||||
String csn = StandardCharsets.UTF_8.name();
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
File file1 = new File(USER_DIR, "ScannerCharsetTest1.txt");
|
||||
File file2 = new File(USER_DIR, "ScannerCharsetTest2.txt");
|
||||
|
||||
return new Object[][]{
|
||||
{ConstructorType.FILE, file1, file2, csn, charset},
|
||||
{ConstructorType.PATH, file1, file2, csn, charset},
|
||||
{ConstructorType.INPUTSTREAM, file1, file2, csn, charset},
|
||||
{ConstructorType.READABLEBYTECHANNEL, file1, file2, csn, charset},};
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the overloading constructor behaves the same as the
|
||||
* existing one.
|
||||
*
|
||||
* @param type the type of the constructor
|
||||
* @param file1 file1 written with the name of a charset
|
||||
* @param file2 file2 written with a charset
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test(dataProvider = "parameters")
|
||||
void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
|
||||
throws Exception {
|
||||
prepareFile(file1, TEST_STRING);
|
||||
prepareFile(file2, TEST_STRING);
|
||||
|
||||
try (Scanner s1 = getScanner(type, file1.getPath(), csn, null);
|
||||
Scanner s2 = getScanner(type, file2.getPath(), null, charset);) {
|
||||
String result1 = s1.findInLine(TEST_STRING);
|
||||
String result2 = s2.findInLine(TEST_STRING);
|
||||
Assert.assertEquals(result1, result2);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates a Scanner over the given input file.
|
||||
*/
|
||||
Scanner getScanner(ConstructorType type, String file, String csn, Charset charset)
|
||||
throws Exception {
|
||||
if (csn != null) {
|
||||
switch (type) {
|
||||
case FILE:
|
||||
return new Scanner(new File(file), csn);
|
||||
case PATH:
|
||||
return new Scanner(Paths.get(file), csn);
|
||||
case INPUTSTREAM:
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
return new Scanner(fis, csn);
|
||||
case READABLEBYTECHANNEL:
|
||||
FileInputStream fis1 = new FileInputStream(file);
|
||||
return new Scanner(fis1.getChannel(), csn);
|
||||
}
|
||||
} else {
|
||||
switch (type) {
|
||||
case FILE:
|
||||
return new Scanner(new File(file), charset);
|
||||
case PATH:
|
||||
return new Scanner(Paths.get(file), charset);
|
||||
case INPUTSTREAM:
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
return new Scanner(fis, charset);
|
||||
case READABLEBYTECHANNEL:
|
||||
FileInputStream fis1 = new FileInputStream(file);
|
||||
return new Scanner(fis1.getChannel(), charset);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void prepareFile(File file, String content) throws IOException {
|
||||
try (FileWriter writer = new FileWriter(file);) {
|
||||
writer.write(content);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011, 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
|
||||
@ -23,7 +23,7 @@
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 7000511
|
||||
* @bug 7000511 8190577
|
||||
* @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
|
||||
* exception thrown
|
||||
*/
|
||||
@ -33,6 +33,7 @@ import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Scanner;
|
||||
|
||||
@ -65,7 +66,14 @@ public class FailingConstructors {
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new Scanner(file, null);
|
||||
new Scanner(file, (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
}
|
||||
|
||||
try {
|
||||
new Scanner(file, (Charset)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
@ -84,7 +92,14 @@ public class FailingConstructors {
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new Scanner(file.toPath(), null);
|
||||
new Scanner(file.toPath(), (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
}
|
||||
|
||||
try {
|
||||
new Scanner(file.toPath(), (Charset)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
|
Loading…
x
Reference in New Issue
Block a user