mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8183743: Umbrella: add overloads that take a Charset parameter
Reviewed-by: alanb, rriggs
This commit is contained in:
parent
f065141ddc
commit
4f080a83af
28 changed files with 1959 additions and 131 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue