You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/03/30 15:12:17 UTC
svn commit: r1307397 - in /commons/proper/io/trunk/src:
main/java/org/apache/commons/io/ test/java/org/apache/commons/io/
Author: ggregory
Date: Fri Mar 30 13:12:16 2012
New Revision: 1307397
URL: http://svn.apache.org/viewvc?rev=1307397&view=rev
Log:
[IO-318] Add Charset sister APIs to method that take a String charset name.
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/LineIteratorTestCase.java
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1307397&r1=1307396&r2=1307397&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java Fri Mar 30 13:12:16 2012
@@ -36,6 +36,8 @@ import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Selector;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -519,8 +521,26 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
*/
public static byte[] toByteArray(Reader input) throws IOException {
+ return toByteArray(input, Charset.defaultCharset());
+ }
+
+ /**
+ * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
+ * using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedReader</code>.
+ *
+ * @param input the <code>Reader</code> to read from
+ * @param encoding the encoding to use, null means platform default
+ * @return the requested byte array
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static byte[] toByteArray(Reader input, Charset encoding) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- copy(input, output);
+ copy(input, output, encoding);
return output.toByteArray();
}
@@ -541,11 +561,8 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static byte[] toByteArray(Reader input, String encoding)
- throws IOException {
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- copy(input, output, encoding);
- return output.toByteArray();
+ public static byte[] toByteArray(Reader input, String encoding) throws IOException {
+ return toByteArray(input, toCharset(encoding));
}
/**
@@ -581,8 +598,27 @@ public class IOUtils {
* @since 1.1
*/
public static char[] toCharArray(InputStream is) throws IOException {
+ return toCharArray(is, Charset.defaultCharset());
+ }
+
+ /**
+ * Get the contents of an <code>InputStream</code> as a character array
+ * using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param is the <code>InputStream</code> to read from
+ * @param encoding the encoding to use, null means platform default
+ * @return the requested character array
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static char[] toCharArray(InputStream is, Charset encoding)
+ throws IOException {
CharArrayWriter output = new CharArrayWriter();
- copy(is, output);
+ copy(is, output, encoding);
return output.toCharArray();
}
@@ -603,11 +639,8 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static char[] toCharArray(InputStream is, String encoding)
- throws IOException {
- CharArrayWriter output = new CharArrayWriter();
- copy(is, output, encoding);
- return output.toCharArray();
+ public static char[] toCharArray(InputStream is, String encoding) throws IOException {
+ return toCharArray(is, toCharset(encoding));
}
/**
@@ -643,7 +676,27 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
*/
public static String toString(InputStream input) throws IOException {
- return toString(input, null);
+ return toString(input, Charset.defaultCharset());
+ }
+
+ /**
+ * Get the contents of an <code>InputStream</code> as a String
+ * using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ * </p>
+ * @param input the <code>InputStream</code> to read from
+ * @param encoding the encoding to use, null means platform default
+ * @return the requested String
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static String toString(InputStream input, Charset encoding) throws IOException {
+ StringBuilderWriter sw = new StringBuilderWriter();
+ copy(input, sw, encoding);
+ return sw.toString();
}
/**
@@ -664,9 +717,7 @@ public class IOUtils {
*/
public static String toString(InputStream input, String encoding)
throws IOException {
- StringBuilderWriter sw = new StringBuilderWriter();
- copy(input, sw, encoding);
- return sw.toString();
+ return toString(input, toCharset(encoding));
}
/**
@@ -696,7 +747,22 @@ public class IOUtils {
* @since 2.1.
*/
public static String toString(URI uri) throws IOException {
- return toString(uri, null);
+ return toString(uri, Charset.defaultCharset());
+ }
+
+ /**
+ * Gets the contents at the given URI.
+ *
+ * @param uri
+ * The URI source.
+ * @param encoding
+ * The encoding name for the URL contents.
+ * @return The contents of the URL as a String.
+ * @throws IOException if an I/O exception occurs.
+ * @since 2.3.
+ */
+ public static String toString(URI uri, Charset encoding) throws IOException {
+ return toString(uri.toURL(), toCharset(encoding));
}
/**
@@ -711,7 +777,7 @@ public class IOUtils {
* @since 2.1.
*/
public static String toString(URI uri, String encoding) throws IOException {
- return toString(uri.toURL(), encoding);
+ return toString(uri, toCharset(encoding));
}
/**
@@ -724,7 +790,7 @@ public class IOUtils {
* @since 2.1.
*/
public static String toString(URL url) throws IOException {
- return toString(url, null);
+ return toString(url, Charset.defaultCharset());
}
/**
@@ -736,9 +802,9 @@ public class IOUtils {
* The encoding name for the URL contents.
* @return The contents of the URL as a String.
* @throws IOException if an I/O exception occurs.
- * @since 2.1.
+ * @since 2.3
*/
- public static String toString(URL url, String encoding) throws IOException {
+ public static String toString(URL url, Charset encoding) throws IOException {
InputStream inputStream = url.openStream();
try {
return toString(inputStream, encoding);
@@ -748,6 +814,21 @@ public class IOUtils {
}
/**
+ * Gets the contents at the given URL.
+ *
+ * @param url
+ * The URL source.
+ * @param encoding
+ * The encoding name for the URL contents.
+ * @return The contents of the URL as a String.
+ * @throws IOException if an I/O exception occurs.
+ * @since 2.1.
+ */
+ public static String toString(URL url, String encoding) throws IOException {
+ return toString(url, toCharset(encoding));
+ }
+
+ /**
* Get the contents of a <code>byte[]</code> as a String
* using the default character encoding of the platform.
*
@@ -774,16 +855,9 @@ public class IOUtils {
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs (never occurs)
- * @deprecated Use {@link String#String(byte[],String)}
*/
- @Deprecated
- public static String toString(byte[] input, String encoding)
- throws IOException {
- if (encoding == null) {
- return new String(input);
- } else {
- return new String(input, encoding);
- }
+ public static String toString(byte[] input, String encoding) throws IOException {
+ return new String(input, toCharset(encoding));
}
// readLines
@@ -802,7 +876,25 @@ public class IOUtils {
* @since 1.1
*/
public static List<String> readLines(InputStream input) throws IOException {
- InputStreamReader reader = new InputStreamReader(input);
+ return readLines(input, Charset.defaultCharset());
+ }
+
+ /**
+ * Get the contents of an <code>InputStream</code> as a list of Strings,
+ * one entry per line, using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param input the <code>InputStream</code> to read from, not null
+ * @param encoding the encoding to use, null means platform default
+ * @return the list of Strings, never null
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static List<String> readLines(InputStream input, Charset encoding) throws IOException {
+ InputStreamReader reader = new InputStreamReader(input, toCharset(encoding));
return readLines(reader);
}
@@ -824,12 +916,7 @@ public class IOUtils {
* @since 1.1
*/
public static List<String> readLines(InputStream input, String encoding) throws IOException {
- if (encoding == null) {
- return readLines(input);
- } else {
- InputStreamReader reader = new InputStreamReader(input, encoding);
- return readLines(reader);
- }
+ return readLines(input, toCharset(encoding));
}
/**
@@ -902,6 +989,40 @@ public class IOUtils {
* The recommended usage pattern is:
* <pre>
* try {
+ * LineIterator it = IOUtils.lineIterator(stream, charset);
+ * while (it.hasNext()) {
+ * String line = it.nextLine();
+ * /// do something with line
+ * }
+ * } finally {
+ * IOUtils.closeQuietly(stream);
+ * }
+ * </pre>
+ *
+ * @param input the <code>InputStream</code> to read from, not null
+ * @param encoding the encoding to use, null means platform default
+ * @return an Iterator of the lines in the reader, never null
+ * @throws IllegalArgumentException if the input is null
+ * @throws IOException if an I/O error occurs, such as if the encoding is invalid
+ * @since 2.3
+ */
+ public static LineIterator lineIterator(InputStream input, Charset encoding) throws IOException {
+ return new LineIterator(new InputStreamReader(input, toCharset(encoding)));
+ }
+
+ /**
+ * Return an Iterator for the lines in an <code>InputStream</code>, using
+ * the character encoding specified (or default encoding if null).
+ * <p>
+ * <code>LineIterator</code> holds a reference to the open
+ * <code>InputStream</code> specified here. When you have finished with
+ * the iterator you should close the stream to free internal resources.
+ * This can be done by closing the stream directly, or by calling
+ * {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}.
+ * <p>
+ * The recommended usage pattern is:
+ * <pre>
+ * try {
* LineIterator it = IOUtils.lineIterator(stream, "UTF-8");
* while (it.hasNext()) {
* String line = it.nextLine();
@@ -919,15 +1040,8 @@ public class IOUtils {
* @throws IOException if an I/O error occurs, such as if the encoding is invalid
* @since 1.2
*/
- public static LineIterator lineIterator(InputStream input, String encoding)
- throws IOException {
- Reader reader = null;
- if (encoding == null) {
- reader = new InputStreamReader(input);
- } else {
- reader = new InputStreamReader(input, encoding);
- }
- return new LineIterator(reader);
+ public static LineIterator lineIterator(InputStream input, String encoding) throws IOException {
+ return lineIterator(input, toCharset(encoding));
}
//-----------------------------------------------------------------------
@@ -940,7 +1054,20 @@ public class IOUtils {
* @since 2.0
*/
public static InputStream toInputStream(CharSequence input) {
- return toInputStream(input.toString());
+ return toInputStream(input, Charset.defaultCharset());
+ }
+
+ /**
+ * Convert the specified CharSequence to an input stream, encoded as bytes
+ * using the specified character encoding.
+ *
+ * @param input the CharSequence to convert
+ * @param encoding the encoding to use, null means platform default
+ * @return an input stream
+ * @since 2.3
+ */
+ public static InputStream toInputStream(CharSequence input, Charset encoding) {
+ return toInputStream(input.toString(), encoding);
}
/**
@@ -957,7 +1084,7 @@ public class IOUtils {
* @since 2.0
*/
public static InputStream toInputStream(CharSequence input, String encoding) throws IOException {
- return toInputStream(input.toString(), encoding);
+ return toInputStream(input, toCharset(encoding));
}
//-----------------------------------------------------------------------
@@ -970,8 +1097,20 @@ public class IOUtils {
* @since 1.1
*/
public static InputStream toInputStream(String input) {
- byte[] bytes = input.getBytes();
- return new ByteArrayInputStream(bytes);
+ return toInputStream(input, Charset.defaultCharset());
+ }
+
+ /**
+ * Convert the specified string to an input stream, encoded as bytes
+ * using the specified character encoding.
+ *
+ * @param input the string to convert
+ * @param encoding the encoding to use, null means platform default
+ * @return an input stream
+ * @since 2.3
+ */
+ public static InputStream toInputStream(String input, Charset encoding) {
+ return new ByteArrayInputStream(input.getBytes(toCharset(encoding)));
}
/**
@@ -988,7 +1127,7 @@ public class IOUtils {
* @since 1.1
*/
public static InputStream toInputStream(String input, String encoding) throws IOException {
- byte[] bytes = encoding != null ? input.getBytes(encoding) : input.getBytes();
+ byte[] bytes = input.getBytes(toCharset(encoding));
return new ByteArrayInputStream(bytes);
}
@@ -1025,8 +1164,26 @@ public class IOUtils {
* @since 1.1
*/
public static void write(byte[] data, Writer output) throws IOException {
+ write(data, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
+ * using the specified character encoding.
+ * <p>
+ * This method uses {@link String#String(byte[], String)}.
+ *
+ * @param data the byte array to write, do not modify during output,
+ * null ignored
+ * @param output the <code>Writer</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void write(byte[] data, Writer output, Charset encoding) throws IOException {
if (data != null) {
- output.write(new String(data));
+ output.write(new String(data, toCharset(encoding)));
}
}
@@ -1047,15 +1204,8 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void write(byte[] data, Writer output, String encoding)
- throws IOException {
- if (data != null) {
- if (encoding == null) {
- write(data, output);
- } else {
- output.write(new String(data, encoding));
- }
- }
+ public static void write(byte[] data, Writer output, String encoding) throws IOException {
+ write(data, output, toCharset(encoding));
}
// write char[]
@@ -1093,8 +1243,27 @@ public class IOUtils {
*/
public static void write(char[] data, OutputStream output)
throws IOException {
+ write(data, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Writes chars from a <code>char[]</code> to bytes on an
+ * <code>OutputStream</code> using the specified character encoding.
+ * <p>
+ * This method uses {@link String#String(char[])} and
+ * {@link String#getBytes(String)}.
+ *
+ * @param data the char array to write, do not modify during output,
+ * null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void write(char[] data, OutputStream output, Charset encoding) throws IOException {
if (data != null) {
- output.write(new String(data).getBytes());
+ output.write(new String(data).getBytes(toCharset(encoding)));
}
}
@@ -1118,13 +1287,7 @@ public class IOUtils {
*/
public static void write(char[] data, OutputStream output, String encoding)
throws IOException {
- if (data != null) {
- if (encoding == null) {
- write(data, output);
- } else {
- output.write(new String(data).getBytes(encoding));
- }
- }
+ write(data, output, toCharset(encoding));
}
// write CharSequence
@@ -1159,8 +1322,25 @@ public class IOUtils {
*/
public static void write(CharSequence data, OutputStream output)
throws IOException {
+ write(data, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Writes chars from a <code>CharSequence</code> to bytes on an
+ * <code>OutputStream</code> using the specified character encoding.
+ * <p>
+ * This method uses {@link String#getBytes(String)}.
+ *
+ * @param data the <code>CharSequence</code> to write, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void write(CharSequence data, OutputStream output, Charset encoding) throws IOException {
if (data != null) {
- write(data.toString(), output);
+ write(data.toString(), output, encoding);
}
}
@@ -1180,11 +1360,8 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
* @since 2.0
*/
- public static void write(CharSequence data, OutputStream output, String encoding)
- throws IOException {
- if (data != null) {
- write(data.toString(), output, encoding);
- }
+ public static void write(CharSequence data, OutputStream output, String encoding) throws IOException {
+ write(data, output, toCharset(encoding));
}
// write String
@@ -1219,8 +1396,25 @@ public class IOUtils {
*/
public static void write(String data, OutputStream output)
throws IOException {
+ write(data, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Writes chars from a <code>String</code> to bytes on an
+ * <code>OutputStream</code> using the specified character encoding.
+ * <p>
+ * This method uses {@link String#getBytes(String)}.
+ *
+ * @param data the <code>String</code> to write, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void write(String data, OutputStream output, Charset encoding) throws IOException {
if (data != null) {
- output.write(data.getBytes());
+ output.write(data.getBytes(toCharset(encoding)));
}
}
@@ -1242,13 +1436,7 @@ public class IOUtils {
*/
public static void write(String data, OutputStream output, String encoding)
throws IOException {
- if (data != null) {
- if (encoding == null) {
- write(data, output);
- } else {
- output.write(data.getBytes(encoding));
- }
- }
+ write(data, output, toCharset(encoding));
}
// write StringBuffer
@@ -1288,9 +1476,7 @@ public class IOUtils {
@Deprecated
public static void write(StringBuffer data, OutputStream output)
throws IOException {
- if (data != null) {
- output.write(data.toString().getBytes());
- }
+ write(data, output, (String) null);
}
/**
@@ -1311,14 +1497,9 @@ public class IOUtils {
* @deprecated replaced by write(CharSequence, OutputStream, String)
*/
@Deprecated
- public static void write(StringBuffer data, OutputStream output,
- String encoding) throws IOException {
+ public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException {
if (data != null) {
- if (encoding == null) {
- write(data, output);
- } else {
- output.write(data.toString().getBytes(encoding));
- }
+ output.write(data.toString().getBytes(toCharset(encoding)));
}
}
@@ -1338,17 +1519,36 @@ public class IOUtils {
*/
public static void writeLines(Collection<?> lines, String lineEnding,
OutputStream output) throws IOException {
+ writeLines(lines, lineEnding, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * an <code>OutputStream</code> line by line, using the specified character
+ * encoding and the specified line ending.
+ *
+ * @param lines the lines to write, null entries produce blank lines
+ * @param lineEnding the line separator to use, null is system default
+ * @param output the <code>OutputStream</code> to write to, not null, not closed
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if the output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)
+ throws IOException {
if (lines == null) {
return;
}
if (lineEnding == null) {
lineEnding = LINE_SEPARATOR;
}
+ Charset cs = toCharset(encoding);
for (Object line : lines) {
if (line != null) {
- output.write(line.toString().getBytes());
+ output.write(line.toString().getBytes(cs));
}
- output.write(lineEnding.getBytes());
+ output.write(lineEnding.getBytes(cs));
}
}
@@ -1370,22 +1570,7 @@ public class IOUtils {
*/
public static void writeLines(Collection<?> lines, String lineEnding,
OutputStream output, String encoding) throws IOException {
- if (encoding == null) {
- writeLines(lines, lineEnding, output);
- } else {
- if (lines == null) {
- return;
- }
- if (lineEnding == null) {
- lineEnding = LINE_SEPARATOR;
- }
- for (Object line : lines) {
- if (line != null) {
- output.write(line.toString().getBytes(encoding));
- }
- output.write(lineEnding.getBytes(encoding));
- }
- }
+ writeLines(lines, lineEnding, output, toCharset(encoding));
}
/**
@@ -1579,7 +1764,27 @@ public class IOUtils {
*/
public static void copy(InputStream input, Writer output)
throws IOException {
- InputStreamReader in = new InputStreamReader(input);
+ copy(input, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Copy bytes from an <code>InputStream</code> to chars on a
+ * <code>Writer</code> using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ * <p>
+ * This method uses {@link InputStreamReader}.
+ *
+ * @param input the <code>InputStream</code> to read from
+ * @param output the <code>Writer</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void copy(InputStream input, Writer output, Charset encoding) throws IOException {
+ InputStreamReader in = new InputStreamReader(input, toCharset(encoding));
copy(in, output);
}
@@ -1602,14 +1807,8 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void copy(InputStream input, Writer output, String encoding)
- throws IOException {
- if (encoding == null) {
- copy(input, output);
- } else {
- InputStreamReader in = new InputStreamReader(input, encoding);
- copy(in, output);
- }
+ public static void copy(InputStream input, Writer output, String encoding) throws IOException {
+ copy(input, output, toCharset(encoding));
}
// copy from Reader
@@ -1773,10 +1972,37 @@ public class IOUtils {
*/
public static void copy(Reader input, OutputStream output)
throws IOException {
- OutputStreamWriter out = new OutputStreamWriter(output);
+ copy(input, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Copy chars from a <code>Reader</code> to bytes on an
+ * <code>OutputStream</code> using the specified character encoding, and
+ * calling flush.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedReader</code>.
+ * </p>
+ * <p>
+ * Due to the implementation of OutputStreamWriter, this method performs a
+ * flush.
+ * </p>
+ * <p>
+ * This method uses {@link OutputStreamWriter}.
+ * </p>
+ *
+ * @param input the <code>Reader</code> to read from
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void copy(Reader input, OutputStream output, Charset encoding) throws IOException {
+ OutputStreamWriter out = new OutputStreamWriter(output, toCharset(encoding));
copy(input, out);
- // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
- // have to flush here.
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter,
+ // we have to flush here.
out.flush();
}
@@ -1803,17 +2029,34 @@ public class IOUtils {
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void copy(Reader input, OutputStream output, String encoding)
- throws IOException {
- if (encoding == null) {
- copy(input, output);
- } else {
- OutputStreamWriter out = new OutputStreamWriter(output, encoding);
- copy(input, out);
- // XXX Unless anyone is planning on rewriting OutputStreamWriter,
- // we have to flush here.
- out.flush();
- }
+ public static void copy(Reader input, OutputStream output, String encoding) throws IOException {
+ copy(input, output, toCharset(encoding));
+ }
+
+ /**
+ * Returns the given Charset or the default Charset if the given Charset is null.
+ *
+ * @param charset
+ * A charset or null.
+ * @return the given Charset or the default Charset if the given Charset is null
+ * @since 2.3
+ */
+ public static Charset toCharset(Charset charset) {
+ return charset == null ? Charset.defaultCharset() : charset;
+ }
+
+ /**
+ * Returns a Charset for the named charset. If the name is null, return the default Charset.
+ *
+ * @param charset
+ * The name of the requested charset, may be null.
+ * @return a Charset for the named charset
+ * @throws UnsupportedCharsetException
+ * If the named charset is unavailable
+ * @since 2.3
+ */
+ public static Charset toCharset(String charset) {
+ return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
}
// content equals
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java?rev=1307397&r1=1307396&r2=1307397&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java Fri Mar 30 13:12:16 2012
@@ -201,7 +201,7 @@ public class IOUtilsCopyTestCase extends
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
- IOUtils.copy(in, writer, null);
+ IOUtils.copy(in, writer, (String) null);
out.off();
writer.flush();
@@ -295,7 +295,7 @@ public class IOUtilsCopyTestCase extends
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
- IOUtils.copy(reader, out, null);
+ IOUtils.copy(reader, out, (String) null);
// note: this method *does* flush.
// note: we don't flush here; this IOUtils method does it for us
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java?rev=1307397&r1=1307396&r2=1307397&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java Fri Mar 30 13:12:16 2012
@@ -33,15 +33,18 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Selector;
+import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.testtools.FileBasedTestCase;
+import org.junit.Assert;
// Note: jdk1.2 dependency
@@ -77,7 +80,7 @@ public class IOUtilsTestCase extends Fil
createFile(m_testFile, FILE_SIZE);
} catch (IOException ioe) {
- throw new RuntimeException("Can't run this test because " + "environment could not be built: "
+ throw new RuntimeException("Can't run this test because the environment could not be built: "
+ ioe.getMessage());
}
// Create and init a byte array as input data
@@ -99,8 +102,8 @@ public class IOUtilsTestCase extends Fil
iarr = null;
try {
FileUtils.deleteDirectory(getTestDirectory());
- } catch (IOException ioe) {
- throw new RuntimeException("Could not clear up " + getTestDirectory());
+ } catch (IOException e) {
+ throw new RuntimeException("Could not clear up " + getTestDirectory() + ": " + e);
}
}
@@ -279,6 +282,21 @@ public class IOUtilsTestCase extends Fil
deleteFile(destination);
}
+ public void testToByteArray_Reader() throws IOException {
+ final String charsetName = "UTF-8";
+ final byte[] expecteds = charsetName.getBytes(charsetName);
+ byte[] actuals = IOUtils.toByteArray(new InputStreamReader(new ByteArrayInputStream(expecteds)));
+ Assert.assertArrayEquals(expecteds, actuals);
+ actuals = IOUtils.toByteArray(new InputStreamReader(new ByteArrayInputStream(expecteds)), charsetName);
+ Assert.assertArrayEquals(expecteds, actuals);
+ }
+
+ public void testToCharset() {
+ Assert.assertEquals(Charset.defaultCharset(), IOUtils.toCharset((String) null));
+ Assert.assertEquals(Charset.defaultCharset(), IOUtils.toCharset((Charset) null));
+ Assert.assertEquals(Charset.defaultCharset(), IOUtils.toCharset(Charset.defaultCharset()));
+ Assert.assertEquals(Charset.forName("UTF-8"), IOUtils.toCharset(Charset.forName("UTF-8")));
+ }
public void testInputStreamToByteArray() throws Exception {
FileInputStream fin = new FileInputStream(m_testFile);
try {
@@ -443,7 +461,7 @@ public class IOUtilsTestCase extends Fil
InputStream inStream = IOUtils.toInputStream(csq);
byte[] bytes = IOUtils.toByteArray(inStream);
assertEqualContent(csq.toString().getBytes(), bytes);
- inStream = IOUtils.toInputStream(csq, null);
+ inStream = IOUtils.toInputStream(csq, (String) null);
bytes = IOUtils.toByteArray(inStream);
assertEqualContent(csq.toString().getBytes(), bytes);
inStream = IOUtils.toInputStream(csq, "UTF-8");
@@ -464,7 +482,7 @@ public class IOUtilsTestCase extends Fil
InputStream inStream = IOUtils.toInputStream(str);
byte[] bytes = IOUtils.toByteArray(inStream);
assertEqualContent(str.getBytes(), bytes);
- inStream = IOUtils.toInputStream(str, null);
+ inStream = IOUtils.toInputStream(str, (String) null);
bytes = IOUtils.toByteArray(inStream);
assertEqualContent(str.getBytes(), bytes);
inStream = IOUtils.toInputStream(str, "UTF-8");
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java?rev=1307397&r1=1307396&r2=1307397&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java Fri Mar 30 13:12:16 2012
@@ -162,7 +162,7 @@ public class IOUtilsWriteTestCase extend
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
- IOUtils.write(inData, writer, null);
+ IOUtils.write(inData, writer, (String) null);
out.off();
writer.flush();
@@ -244,7 +244,7 @@ public class IOUtilsWriteTestCase extend
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
- IOUtils.write(csq, out, null);
+ IOUtils.write(csq, out, (String) null);
out.off();
out.flush();
@@ -362,7 +362,7 @@ public class IOUtilsWriteTestCase extend
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
- IOUtils.write(str, out, null);
+ IOUtils.write(str, out, (String) null);
out.off();
out.flush();
@@ -481,7 +481,7 @@ public class IOUtilsWriteTestCase extend
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
- IOUtils.write(str.toCharArray(), out, null);
+ IOUtils.write(str.toCharArray(), out, (String) null);
out.off();
out.flush();
@@ -643,7 +643,7 @@ public class IOUtilsWriteTestCase extend
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
- IOUtils.writeLines(list, "*", out, null);
+ IOUtils.writeLines(list, "*", out, (String) null);
out.off();
out.flush();
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/LineIteratorTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/LineIteratorTestCase.java?rev=1307397&r1=1307396&r2=1307397&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/LineIteratorTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/LineIteratorTestCase.java Fri Mar 30 13:12:16 2012
@@ -24,11 +24,15 @@ import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import org.apache.commons.io.testtools.FileBasedTestCase;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
/**
* This is used to test LineIterator for correctness.
@@ -87,6 +91,7 @@ public class LineIteratorTestCase extend
/** @see junit.framework.TestCase#setUp() */
@Override
+ @Before
protected void setUp() throws Exception {
File dir = getTestDirectory();
if (dir.exists()) {
@@ -98,6 +103,7 @@ public class LineIteratorTestCase extend
/** @see junit.framework.TestCase#tearDown() */
@Override
+ @After
protected void tearDown() throws Exception {
FileUtils.deleteDirectory(getTestDirectory());
}
@@ -106,6 +112,7 @@ public class LineIteratorTestCase extend
/**
* Test constructor.
*/
+ @Test
public void testConstructor() throws Exception {
try {
new LineIterator((Reader) null);
@@ -118,6 +125,7 @@ public class LineIteratorTestCase extend
/**
* Test a file with no lines.
*/
+ @Test
public void testZeroLines() throws Exception {
doTestFileWithSpecifiedLines(0);
}
@@ -125,6 +133,7 @@ public class LineIteratorTestCase extend
/**
* Test a file with 1 line.
*/
+ @Test
public void testOneLines() throws Exception {
doTestFileWithSpecifiedLines(1);
}
@@ -132,6 +141,7 @@ public class LineIteratorTestCase extend
/**
* Test a file with 2 lines.
*/
+ @Test
public void testTwoLines() throws Exception {
doTestFileWithSpecifiedLines(2);
}
@@ -139,6 +149,7 @@ public class LineIteratorTestCase extend
/**
* Test a file with 3 lines.
*/
+ @Test
public void testThreeLines() throws Exception {
doTestFileWithSpecifiedLines(3);
}
@@ -146,6 +157,7 @@ public class LineIteratorTestCase extend
/**
* Test a missing File.
*/
+ @Test
public void testMissingFile() throws Exception {
File testFile = new File(getTestDirectory(), "dummy-missing-file.txt");
@@ -163,6 +175,7 @@ public class LineIteratorTestCase extend
/**
* Test a file with a Valid encoding.
*/
+ @Test
public void testValidEncoding() throws Exception {
String encoding = "UTF-8";
@@ -185,6 +198,7 @@ public class LineIteratorTestCase extend
/**
* Test a file with an Invalid encoding.
*/
+ @Test
public void testInvalidEncoding() throws Exception {
String encoding = "XXXXXXXX";
@@ -194,8 +208,8 @@ public class LineIteratorTestCase extend
LineIterator iterator = null;
try {
iterator = FileUtils.lineIterator(testFile, encoding);
- fail("Expected UnsupportedEncodingException");
- } catch (UnsupportedEncodingException expected) {
+ fail("Expected UnsupportedCharsetException");
+ } catch (UnsupportedCharsetException expected) {
// ignore, expected result
} finally {
LineIterator.closeQuietly(iterator);
@@ -205,6 +219,7 @@ public class LineIteratorTestCase extend
/**
* Test the iterator using only the nextLine() method.
*/
+ @Test
public void testNextLineOnlyDefaultEncoding() throws Exception {
File testFile = new File(getTestDirectory(), "LineIterator-nextOnly.txt");
List<String> lines = createLinesFile(testFile, 3);
@@ -216,6 +231,7 @@ public class LineIteratorTestCase extend
/**
* Test the iterator using only the nextLine() method.
*/
+ @Test
public void testNextLineOnlyNullEncoding() throws Exception {
String encoding = null;
@@ -229,6 +245,7 @@ public class LineIteratorTestCase extend
/**
* Test the iterator using only the nextLine() method.
*/
+ @Test
public void testNextLineOnlyUtf8Encoding() throws Exception {
String encoding = "UTF-8";
@@ -242,6 +259,7 @@ public class LineIteratorTestCase extend
/**
* Test the iterator using only the next() method.
*/
+ @Test
public void testNextOnly() throws Exception {
String encoding = null;
@@ -263,6 +281,7 @@ public class LineIteratorTestCase extend
/**
* Tests hasNext when it throws an exception.
*/
+ @Test
public void testNextWithException() throws Exception {
Reader reader = new BufferedReader(new StringReader("")) {
@Override
@@ -281,6 +300,7 @@ public class LineIteratorTestCase extend
/**
* Test closing the iterator before all the file has been processed.
*/
+ @Test
public void testCloseEarly() throws Exception {
String encoding = "UTF-8";
@@ -375,6 +395,7 @@ public class LineIteratorTestCase extend
}
// -----------------------------------------------------------------------
+ @Test
public void testFilteringFileReader() throws Exception {
String encoding = "UTF-8";
@@ -386,6 +407,7 @@ public class LineIteratorTestCase extend
this.testFiltering(lines, reader);
}
+ @Test
public void testFilteringBufferedReader() throws Exception {
String encoding = "UTF-8";