Ask for review: java.io.Ouputs
Rémi Forax
forax at univ-mlv.fr
Tue Nov 25 16:11:30 PST 2008
Alan Bateman a écrit :
> Xueming Shen wrote:
>> Alan Bateman wrote:
>>>
>>>>
>>>> (2)Is it really necessary to check "Charset.isSupproted()"? The csn
>>>> will be checked when the
>>>> Charset is created later anyway, why do lookup twice. Yes,
>>>> understood it can catch the problem
>>>> 1-2 step(s) earlier, I double you want to pay that price.
>>> All parameters need to be checked before the file is created (or
>>> truncated).
>>
>> Did not consider that:-) an alternative is to get the encoder first
>> then the file creation.
> Yes, that is the right thing to do and this also ensures that the
> encoder has the default action to report errors.
Ok, here is the new diff since the last diff
- writeLines that takes a charset no longer says that
UnsupportedCharsetException
can be thrown.
- all writeLines now use an encoder which is get before the file is created
to dectet null charset or unsupported ones.
>
> -Alan.
Rémi
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
@@ -27,13 +27,12 @@ package java.io;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
-import java.nio.charset.UnsupportedCharsetException;
+import java.nio.charset.CharsetEncoder;
import java.nio.file.FileRef;
import static java.nio.file.StandardOpenOption.*;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.Arrays;
-import java.util.List;
/**
* {@note experimental}
@@ -50,14 +49,26 @@ public final class Outputs {
public final class Outputs {
private Outputs() { }
- // checks that charset is supported
- private static void ensureCharsetIsSupported(String csn) {
- if (csn == null)
- throw new NullPointerException("'csn' is null");
- if (!Charset.isSupported(csn))
- throw new UnsupportedCharsetException(csn);
- }
-
+ // checks that charset is not null
+ // returns an encoder with action REPORT for unmappable-character
errors
+ private static CharsetEncoder getEncoder(Charset charset) {
+ ensureCharsetIsNotNull(charset);
+ return charset.newEncoder();
+ }
+
+ // checks that charset is not null and get encoder
+ // returns an encoder with action REPORT for unmappable-character
errors
+ private static CharsetEncoder getEncoder(String csn) {
+ ensureCharsetIsNotNull(csn);
+ return Charset.forName(csn).newEncoder();
+ }
+
+ // checks that charset is not null
+ private static void ensureCharsetIsNotNull(Object 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.
* If the file already exists, it is first truncated.
@@ -105,6 +116,37 @@ 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.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, Iterable<? extends
CharSequence> lines, Charset charset)
+ throws IOException
+ {
+ CharsetEncoder encoder = getEncoder(charset);
+ WritableByteChannel wbc = file.newByteChannel(WRITE, CREATE,
TRUNCATE_EXISTING);
+ BufferedWriter writer = new
BufferedWriter(Channels.newWriter(wbc, encoder, -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,12 +159,12 @@ public final class Outputs {
* @throws IOException
* If an I/O error occurs
*/
- public static void writeLines(FileRef file, List<String> lines,
String csn)
- throws IOException
- {
- ensureCharsetIsSupported(csn);
+ public static void writeLines(FileRef file, Iterable<? extends
CharSequence> lines, String csn)
+ throws IOException
+ {
+ CharsetEncoder encoder = getEncoder(csn);
WritableByteChannel wbc = file.newByteChannel(WRITE, CREATE,
TRUNCATE_EXISTING);
- BufferedWriter writer = new
BufferedWriter(Channels.newWriter(wbc, csn));
+ BufferedWriter writer = new
BufferedWriter(Channels.newWriter(wbc, encoder, -1));
try {
implWriteLines(writer, lines);
} finally {
@@ -147,10 +189,10 @@ public final class Outputs {
* @throws IOException
* If an I/O error occurs
*/
- public static void writeLines(FileRef file, List<String> lines)
- throws IOException
- {
- writeLines(file, lines, Charset.defaultCharset().name());
+ public static void writeLines(FileRef file, Iterable<? extends
CharSequence> lines)
+ throws IOException
+ {
+ writeLines(file, lines, Charset.defaultCharset());
}
/**
@@ -170,10 +212,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 +264,37 @@ 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.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, Iterable<? extends
CharSequence> lines, Charset charset)
+ throws IOException
+ {
+ CharsetEncoder encoder = getEncoder(charset);
+ FileOutputStream out = new FileOutputStream(file);
+ BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(out, encoder));
+ 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,12 +307,12 @@ public final class Outputs {
* @throws IOException
* If an I/O error occurs
*/
- public static void writeLines(File file, List<String> lines, String
csn)
- throws IOException
- {
- ensureCharsetIsSupported(csn);
+ public static void writeLines(File file, Iterable<? extends
CharSequence> lines, String csn)
+ throws IOException
+ {
+ CharsetEncoder encoder = getEncoder(csn);
FileOutputStream out = new FileOutputStream(file);
- BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(out, csn));
+ BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(out, encoder));
try {
implWriteLines(writer, lines);
} finally {
@@ -264,10 +337,10 @@ public final class Outputs {
* @throws IOException
* If an I/O error occurs
*/
- public static void writeLines(File file, List<String> lines)
- throws IOException
- {
- writeLines(file, lines, Charset.defaultCharset().name());
+ public static void writeLines(File file, Iterable<? extends
CharSequence> lines)
+ throws IOException
+ {
+ writeLines(file, lines, Charset.defaultCharset());
}
/**
@@ -287,10 +360,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,19 +374,49 @@ 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, Iterable<? extends
CharSequence> lines, Charset charset)
+ throws IOException
+ {
+ CharsetEncoder encoder = getEncoder(charset);
+ BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(out, encoder));
+ 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<String> lines,
String csn)
- throws IOException
- {
- BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(out, csn));
+ * @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, Iterable<? extends
CharSequence> lines, String csn)
+ throws IOException
+ {
+ CharsetEncoder encoder = getEncoder(csn);
+ BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(out, encoder));
implWriteLines(writer, lines);
writer.flush();
}
@@ -334,17 +437,17 @@ public final class Outputs {
* @throws IOException
* If an I/O error occurs
*/
- public static void writeLines(OutputStream out, List<String> lines)
- throws IOException
- {
- writeLines(out, lines, Charset.defaultCharset().name());
- }
-
- private static void implWriteLines(BufferedWriter writer,
List<String> lines)
- throws IOException
- {
- for (String line: lines) {
- writer.write(line);
+ public static void writeLines(OutputStream out, Iterable<? extends
CharSequence> lines)
+ throws IOException
+ {
+ writeLines(out, lines, Charset.defaultCharset());
+ }
+
+ private static void implWriteLines(BufferedWriter writer,
Iterable<? extends CharSequence> lines)
+ throws IOException
+ {
+ for (CharSequence line: lines) {
+ writer.write(line.toString());
writer.newLine();
}
}
More information about the nio-dev
mailing list