review patch java.io.Ouputs (original) (raw)

Rémi Forax forax at univ-mlv.fr
Mon Nov 17 15:41:24 PST 2008


Ok, i've re-applied my changes to Ouputs.java, there is no more change to Inputs.java until java.util.Scanner is not changed.

All writeLines now use a List<? extends CharSequence> instead of a List.

I've added methods

I've rewritten all methods that use Charset.defaultCharset().name() to use Charset.defaultCharset().

I've also noted that writeLines(OutputStream out, List lines, String csn) (now writeLines(OutputStream out, List<? extends CharSequence> lines, String csn)) currently doesn't call ensureCharsetIsSupported so I've fix that too.

Rémi

Here is the patch:

diff --git a/src/share/classes/java/io/Outputs.java b/src/share/classes/java/io/Outputs.java --- a/src/share/classes/java/io/Outputs.java +++ b/src/share/classes/java/io/Outputs.java @@ -57,6 +57,12 @@ public final class Outputs { if (!Charset.isSupported(csn)) throw new UnsupportedCharsetException(csn); } +
+ // checks that charset is not null + private static void ensureCharsetIsNotNull(Charset charset) { + if (charset == null) + throw new NullPointerException("'charset' is null"); + } /** * Writes a byte array to a file. The file is created if it does not exist. @@ -105,6 +111,40 @@ public final class Outputs { * The file * @param lines * The list of lines to write (in order) + * @param charset + * The charset to be used + * + * @throws java.nio.charset.UnsupportedCharsetException + * If no support for the named charset is available + * in this instance of the Java virtual machine + * @throws java.nio.charset.UnmappableCharacterException + * Where a line contains a character that cannot be mapped to an + * output byte sequence + * @throws IOException + * If an I/O error occurs + / + public static void writeLines(FileRef file, List<? extends CharSequence> lines, Charset charset) + throws IOException + { + ensureCharsetIsNotNull(charset); + WritableByteChannel wbc = file.newByteChannel(WRITE, CREATE, TRUNCATE_EXISTING); + BufferedWriter writer = new BufferedWriter(Channels.newWriter(wbc, charset.newEncoder(), -1)); + try { + implWriteLines(writer, lines); + } finally { + writer.close(); + } + } +
+ /
* + * Writes the given lines of text to the specified file. The characters in + * each line are encoded into bytes using the specified charset. When all + * lines have been written, or an I/O error occurs, then the file is closed. + * + * @param file + * The file + * @param lines + * The list of lines to write (in order) * @param csn * The name of the charset to be used * @@ -117,7 +157,7 @@ public final class Outputs { * @throws IOException * If an I/O error occurs / - public static void writeLines(FileRef file, List lines, String csn) + public static void writeLines(FileRef file, List<? extends CharSequence> lines, String csn) throws IOException { ensureCharsetIsSupported(csn); @@ -147,10 +187,10 @@ public final class Outputs { * @throws IOException * If an I/O error occurs / - public static void writeLines(FileRef file, List lines) - throws IOException - { - writeLines(file, lines, Charset.defaultCharset().name()); + public static void writeLines(FileRef file, List<? extends CharSequence> lines) + throws IOException + { + writeLines(file, lines, Charset.defaultCharset()); } / @@ -170,10 +210,10 @@ public final class Outputs { * @throws IOException * If an I/O error occurs / - public static void writeLines(FileRef file, String... lines) - throws IOException - { - writeLines(file, Arrays.asList(lines), Charset.defaultCharset().name()); + public static void writeLines(FileRef file, CharSequence... lines) + throws IOException + { + writeLines(file, Arrays.asList(lines), Charset.defaultCharset()); } /* @@ -222,6 +262,40 @@ public final class Outputs { * The file * @param lines * The list of lines to write (in order) + * @param charset + * The charset to be used + * + * @throws java.nio.charset.UnsupportedCharsetException + * If no support for the named charset is available + * in this instance of the Java virtual machine + * @throws java.nio.charset.UnmappableCharacterException + * Where a line contains a character that cannot be mapped to an + * output byte sequence + * @throws IOException + * If an I/O error occurs + / + public static void writeLines(File file, List<? extends CharSequence> lines, Charset charset) + throws IOException + { + ensureCharsetIsNotNull(charset); + FileOutputStream out = new FileOutputStream(file); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset)); + try { + implWriteLines(writer, lines); + } finally { + writer.close(); + } + } +
+ /
* + * Writes the given lines of text to the specified file. The characters in + * each line are encoded into bytes using the specified charset. When all + * lines have been written, or an I/O error occurs, then the file is closed. + * + * @param file + * The file + * @param lines + * The list of lines to write (in order) * @param csn * The name of the charset to be used * @@ -234,7 +308,7 @@ public final class Outputs { * @throws IOException * If an I/O error occurs / - public static void writeLines(File file, List lines, String csn) + public static void writeLines(File file, List<? extends CharSequence> lines, String csn) throws IOException { ensureCharsetIsSupported(csn); @@ -264,10 +338,10 @@ public final class Outputs { * @throws IOException * If an I/O error occurs / - public static void writeLines(File file, List lines) - throws IOException - { - writeLines(file, lines, Charset.defaultCharset().name()); + public static void writeLines(File file, List<? extends CharSequence> lines) + throws IOException + { + writeLines(file, lines, Charset.defaultCharset()); } / @@ -287,10 +361,10 @@ public final class Outputs { * @throws IOException * If an I/O error occurs / - public static void writeLines(File file, String... lines) - throws IOException - { - writeLines(file, Arrays.asList(lines), Charset.defaultCharset().name()); + public static void writeLines(File file, CharSequence... lines) + throws IOException + { + writeLines(file, Arrays.asList(lines), Charset.defaultCharset()); } /* @@ -301,18 +375,48 @@ public final class Outputs { * The output stream * @param lines * The list of lines to write (in order) + * @param charset + * The charset to be used + * + * @throws java.nio.charset.UnmappableCharacterException + * Where a line contains a character that cannot be mapped to an + * output byte sequence + * @throws IOException + * If an I/O error occurs + / + public static void writeLines(OutputStream out, List<? extends CharSequence> lines, Charset charset) + throws IOException + { + ensureCharsetIsNotNull(charset); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset)); + implWriteLines(writer, lines); + writer.flush(); + } +
+ /
* + * Writes the given lines of text to the specified output stream. The + * characters in each line are encoded into bytes using the specified charset. + * + * @param out + * The output stream + * @param lines + * The list of lines to write (in order) * @param csn * The name of the charset to be used * - * @throws java.nio.charset.UnmappableCharacterException - * Where a line contains a character that cannot be mapped to an - * output byte sequence - * @throws IOException - * If an I/O error occurs - */ - public static void writeLines(OutputStream out, List lines, String csn) - throws IOException - { + * @throws java.nio.charset.UnsupportedCharsetException + * If no support for the named charset is available + * in this instance of the Java virtual machine + * @throws java.nio.charset.UnmappableCharacterException + * Where a line contains a character that cannot be mapped to an + * output byte sequence + * @throws IOException + * If an I/O error occurs + */ + public static void writeLines(OutputStream out, List<? extends CharSequence> lines, String csn) + throws IOException + { + ensureCharsetIsSupported(csn); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, csn)); implWriteLines(writer, lines); writer.flush(); @@ -334,17 +438,17 @@ public final class Outputs { * @throws IOException * If an I/O error occurs */ - public static void writeLines(OutputStream out, List lines) - throws IOException - { - writeLines(out, lines, Charset.defaultCharset().name()); - }

List lines)

CharSequence> lines)

extends CharSequence> lines)



More information about the nio-dev mailing list