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:40:32 UTC
svn commit: r1307412 - 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:40:31 2012
New Revision: 1307412
URL: http://svn.apache.org/viewvc?rev=1307412&view=rev
Log:
[IO-318] Add Charset sister APIs to method that take a String charset name. FileUtils.
Added:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
Added: commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java?rev=1307412&view=auto
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java (added)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java Fri Mar 30 13:40:31 2012
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io;
+
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+
+/**
+ * Helps use {@link Charset}
+ *
+ * @version $Id$
+ * @since 2.3
+ */
+public class Charsets {
+
+ /**
+ * 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);
+ }
+
+}
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1307412&r1=1307411&r2=1307412&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Fri Mar 30 13:40:31 2012
@@ -26,12 +26,14 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
+import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@@ -1633,18 +1635,37 @@ public class FileUtils {
* @param encoding the encoding to use, <code>null</code> means platform default
* @return the file contents, never <code>null</code>
* @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 2.3
*/
- public static String readFileToString(File file, String encoding) throws IOException {
+ public static String readFileToString(File file, Charset encoding) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
- return IOUtils.toString(in, encoding);
+ return IOUtils.toString(in, Charsets.toCharset(encoding));
} finally {
IOUtils.closeQuietly(in);
}
}
+ /**
+ * Reads the contents of a file into a String. The file is always closed.
+ *
+ * @param file
+ * the file to read, must not be <code>null</code>
+ * @param encoding
+ * the encoding to use, <code>null</code> means platform default
+ * @return the file contents, never <code>null</code>
+ * @throws IOException
+ * in case of an I/O error
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
+ * @since 2.3
+ */
+ public static String readFileToString(File file, String encoding) throws IOException {
+ return readFileToString(file, Charsets.toCharset(encoding));
+ }
+
/**
* Reads the contents of a file into a String using the default encoding for the VM.
@@ -1656,7 +1677,7 @@ public class FileUtils {
* @since 1.3.1
*/
public static String readFileToString(File file) throws IOException {
- return readFileToString(file, null);
+ return readFileToString(file, Charset.defaultCharset());
}
/**
@@ -1686,20 +1707,38 @@ public class FileUtils {
* @param encoding the encoding to use, <code>null</code> means platform default
* @return the list of Strings representing each line in the file, never <code>null</code>
* @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 1.1
+ * @since 2.3
*/
- public static List<String> readLines(File file, String encoding) throws IOException {
+ public static List<String> readLines(File file, Charset encoding) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
- return IOUtils.readLines(in, encoding);
+ return IOUtils.readLines(in, Charsets.toCharset(encoding));
} finally {
IOUtils.closeQuietly(in);
}
}
/**
+ * Reads the contents of a file line by line to a List of Strings. The file is always closed.
+ *
+ * @param file
+ * the file to read, must not be <code>null</code>
+ * @param encoding
+ * the encoding to use, <code>null</code> means platform default
+ * @return the list of Strings representing each line in the file, never <code>null</code>
+ * @throws IOException
+ * in case of an I/O error
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
+ * @since 1.1
+ */
+ public static List<String> readLines(File file, String encoding) throws IOException {
+ return readLines(file, Charsets.toCharset(encoding));
+ }
+
+ /**
* Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
* The file is always closed.
*
@@ -1709,7 +1748,7 @@ public class FileUtils {
* @since 1.3
*/
public static List<String> readLines(File file) throws IOException {
- return readLines(file, null);
+ return readLines(file, Charset.defaultCharset());
}
/**
@@ -1796,10 +1835,9 @@ public class FileUtils {
* @param append if <code>true</code>, then the String will be added to the
* end of the file rather than overwriting
* @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 2.1
+ * @since 2.3
*/
- public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {
+ public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
@@ -1811,6 +1849,24 @@ public class FileUtils {
}
/**
+ * Writes a String to a file creating the file if it does not exist.
+ *
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, <code>null</code> means platform default
+ * @param append if <code>true</code>, then the String will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported by the VM
+ * @since 2.1
+ */
+ public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {
+ writeStringToFile(file, data, Charsets.toCharset(encoding), append);
+ }
+
+ /**
* Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
*
* @param file the file to write
@@ -1818,7 +1874,7 @@ public class FileUtils {
* @throws IOException in case of an I/O error
*/
public static void writeStringToFile(File file, String data) throws IOException {
- writeStringToFile(file, data, null, false);
+ writeStringToFile(file, data, Charset.defaultCharset(), false);
}
/**
@@ -1832,7 +1888,7 @@ public class FileUtils {
* @since 2.1
*/
public static void writeStringToFile(File file, String data, boolean append) throws IOException {
- writeStringToFile(file, data, null, append);
+ writeStringToFile(file, data, Charset.defaultCharset(), append);
}
/**
@@ -1844,7 +1900,7 @@ public class FileUtils {
* @since 2.0
*/
public static void write(File file, CharSequence data) throws IOException {
- write(file, data, null, false);
+ write(file, data, Charset.defaultCharset(), false);
}
/**
@@ -1858,7 +1914,20 @@ public class FileUtils {
* @since 2.1
*/
public static void write(File file, CharSequence data, boolean append) throws IOException {
- write(file, data, null, append);
+ write(file, data, Charset.defaultCharset(), append);
+ }
+
+ /**
+ * Writes a CharSequence to a file creating the file if it does not exist.
+ *
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, <code>null</code> means platform default
+ * @throws IOException in case of an I/O error
+ * @since 2.3
+ */
+ public static void write(File file, CharSequence data, Charset encoding) throws IOException {
+ write(file, data, encoding, false);
}
/**
@@ -1884,15 +1953,32 @@ public class FileUtils {
* @param append if <code>true</code>, then the data will be added to the
* end of the file rather than overwriting
* @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since IO 2.1
+ * @since IO 2.3
*/
- public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {
+ public static void write(File file, CharSequence data, Charset encoding, boolean append) throws IOException {
String str = data == null ? null : data.toString();
writeStringToFile(file, str, encoding, append);
}
/**
+ * Writes a CharSequence to a file creating the file if it does not exist.
+ *
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, <code>null</code> means platform default
+ * @param append if <code>true</code>, then the data will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported by the VM
+ * @since IO 2.1
+ */
+ public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {
+ write(file, data, Charsets.toCharset(encoding), append);
+ }
+
+ /**
* Writes a byte array to a file creating the file if it does not exist.
* <p>
* NOTE: As from v1.3, the parent directories of the file will be created
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=1307412&r1=1307411&r2=1307412&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:40:31 2012
@@ -30,6 +30,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
+import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
@@ -559,10 +560,13 @@ public class IOUtils {
* @return the requested byte array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static byte[] toByteArray(Reader input, String encoding) throws IOException {
- return toByteArray(input, toCharset(encoding));
+ return toByteArray(input, Charsets.toCharset(encoding));
}
/**
@@ -637,10 +641,13 @@ public class IOUtils {
* @return the requested character array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static char[] toCharArray(InputStream is, String encoding) throws IOException {
- return toCharArray(is, toCharset(encoding));
+ return toCharArray(is, Charsets.toCharset(encoding));
}
/**
@@ -714,10 +721,13 @@ public class IOUtils {
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
*/
public static String toString(InputStream input, String encoding)
throws IOException {
- return toString(input, toCharset(encoding));
+ return toString(input, Charsets.toCharset(encoding));
}
/**
@@ -762,7 +772,7 @@ public class IOUtils {
* @since 2.3.
*/
public static String toString(URI uri, Charset encoding) throws IOException {
- return toString(uri.toURL(), toCharset(encoding));
+ return toString(uri.toURL(), Charsets.toCharset(encoding));
}
/**
@@ -774,10 +784,13 @@ 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.
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 2.1.
*/
public static String toString(URI uri, String encoding) throws IOException {
- return toString(uri, toCharset(encoding));
+ return toString(uri, Charsets.toCharset(encoding));
}
/**
@@ -822,10 +835,13 @@ 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.
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 2.1.
*/
public static String toString(URL url, String encoding) throws IOException {
- return toString(url, toCharset(encoding));
+ return toString(url, Charsets.toCharset(encoding));
}
/**
@@ -857,7 +873,7 @@ public class IOUtils {
* @throws IOException if an I/O error occurs (never occurs)
*/
public static String toString(byte[] input, String encoding) throws IOException {
- return new String(input, toCharset(encoding));
+ return new String(input, Charsets.toCharset(encoding));
}
// readLines
@@ -894,7 +910,7 @@ public class IOUtils {
* @since 2.3
*/
public static List<String> readLines(InputStream input, Charset encoding) throws IOException {
- InputStreamReader reader = new InputStreamReader(input, toCharset(encoding));
+ InputStreamReader reader = new InputStreamReader(input, Charsets.toCharset(encoding));
return readLines(reader);
}
@@ -913,10 +929,13 @@ public class IOUtils {
* @return the list of Strings, never null
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static List<String> readLines(InputStream input, String encoding) throws IOException {
- return readLines(input, toCharset(encoding));
+ return readLines(input, Charsets.toCharset(encoding));
}
/**
@@ -1007,7 +1026,7 @@ public class IOUtils {
* @since 2.3
*/
public static LineIterator lineIterator(InputStream input, Charset encoding) throws IOException {
- return new LineIterator(new InputStreamReader(input, toCharset(encoding)));
+ return new LineIterator(new InputStreamReader(input, Charsets.toCharset(encoding)));
}
/**
@@ -1038,10 +1057,13 @@ public class IOUtils {
* @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
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.2
*/
public static LineIterator lineIterator(InputStream input, String encoding) throws IOException {
- return lineIterator(input, toCharset(encoding));
+ return lineIterator(input, Charsets.toCharset(encoding));
}
//-----------------------------------------------------------------------
@@ -1079,12 +1101,15 @@ public class IOUtils {
*
* @param input the CharSequence to convert
* @param encoding the encoding to use, null means platform default
- * @throws IOException if the encoding is invalid
* @return an input stream
+ * @throws IOException if the encoding is invalid
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 2.0
*/
public static InputStream toInputStream(CharSequence input, String encoding) throws IOException {
- return toInputStream(input, toCharset(encoding));
+ return toInputStream(input, Charsets.toCharset(encoding));
}
//-----------------------------------------------------------------------
@@ -1110,7 +1135,7 @@ public class IOUtils {
* @since 2.3
*/
public static InputStream toInputStream(String input, Charset encoding) {
- return new ByteArrayInputStream(input.getBytes(toCharset(encoding)));
+ return new ByteArrayInputStream(input.getBytes(Charsets.toCharset(encoding)));
}
/**
@@ -1122,12 +1147,15 @@ public class IOUtils {
*
* @param input the string to convert
* @param encoding the encoding to use, null means platform default
- * @throws IOException if the encoding is invalid
* @return an input stream
+ * @throws IOException if the encoding is invalid
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static InputStream toInputStream(String input, String encoding) throws IOException {
- byte[] bytes = input.getBytes(toCharset(encoding));
+ byte[] bytes = input.getBytes(Charsets.toCharset(encoding));
return new ByteArrayInputStream(bytes);
}
@@ -1183,7 +1211,7 @@ public class IOUtils {
*/
public static void write(byte[] data, Writer output, Charset encoding) throws IOException {
if (data != null) {
- output.write(new String(data, toCharset(encoding)));
+ output.write(new String(data, Charsets.toCharset(encoding)));
}
}
@@ -1202,10 +1230,13 @@ public class IOUtils {
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static void write(byte[] data, Writer output, String encoding) throws IOException {
- write(data, output, toCharset(encoding));
+ write(data, output, Charsets.toCharset(encoding));
}
// write char[]
@@ -1263,7 +1294,7 @@ public class IOUtils {
*/
public static void write(char[] data, OutputStream output, Charset encoding) throws IOException {
if (data != null) {
- output.write(new String(data).getBytes(toCharset(encoding)));
+ output.write(new String(data).getBytes(Charsets.toCharset(encoding)));
}
}
@@ -1283,11 +1314,14 @@ public class IOUtils {
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static void write(char[] data, OutputStream output, String encoding)
throws IOException {
- write(data, output, toCharset(encoding));
+ write(data, output, Charsets.toCharset(encoding));
}
// write CharSequence
@@ -1358,10 +1392,13 @@ public class IOUtils {
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 2.0
*/
public static void write(CharSequence data, OutputStream output, String encoding) throws IOException {
- write(data, output, toCharset(encoding));
+ write(data, output, Charsets.toCharset(encoding));
}
// write String
@@ -1414,7 +1451,7 @@ public class IOUtils {
*/
public static void write(String data, OutputStream output, Charset encoding) throws IOException {
if (data != null) {
- output.write(data.getBytes(toCharset(encoding)));
+ output.write(data.getBytes(Charsets.toCharset(encoding)));
}
}
@@ -1432,11 +1469,14 @@ public class IOUtils {
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static void write(String data, OutputStream output, String encoding)
throws IOException {
- write(data, output, toCharset(encoding));
+ write(data, output, Charsets.toCharset(encoding));
}
// write StringBuffer
@@ -1493,13 +1533,16 @@ public class IOUtils {
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
* @deprecated replaced by write(CharSequence, OutputStream, String)
*/
@Deprecated
public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException {
if (data != null) {
- output.write(data.toString().getBytes(toCharset(encoding)));
+ output.write(data.toString().getBytes(Charsets.toCharset(encoding)));
}
}
@@ -1543,7 +1586,7 @@ public class IOUtils {
if (lineEnding == null) {
lineEnding = LINE_SEPARATOR;
}
- Charset cs = toCharset(encoding);
+ Charset cs = Charsets.toCharset(encoding);
for (Object line : lines) {
if (line != null) {
output.write(line.toString().getBytes(cs));
@@ -1566,11 +1609,14 @@ public class IOUtils {
* @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
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static void writeLines(Collection<?> lines, String lineEnding,
OutputStream output, String encoding) throws IOException {
- writeLines(lines, lineEnding, output, toCharset(encoding));
+ writeLines(lines, lineEnding, output, Charsets.toCharset(encoding));
}
/**
@@ -1784,7 +1830,7 @@ public class IOUtils {
* @since 2.3
*/
public static void copy(InputStream input, Writer output, Charset encoding) throws IOException {
- InputStreamReader in = new InputStreamReader(input, toCharset(encoding));
+ InputStreamReader in = new InputStreamReader(input, Charsets.toCharset(encoding));
copy(in, output);
}
@@ -1805,10 +1851,13 @@ public class IOUtils {
* @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
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
public static void copy(InputStream input, Writer output, String encoding) throws IOException {
- copy(input, output, toCharset(encoding));
+ copy(input, output, Charsets.toCharset(encoding));
}
// copy from Reader
@@ -1999,7 +2048,7 @@ public class IOUtils {
* @since 2.3
*/
public static void copy(Reader input, OutputStream output, Charset encoding) throws IOException {
- OutputStreamWriter out = new OutputStreamWriter(output, toCharset(encoding));
+ OutputStreamWriter out = new OutputStreamWriter(output, Charsets.toCharset(encoding));
copy(input, out);
// XXX Unless anyone is planning on rewriting OutputStreamWriter,
// we have to flush here.
@@ -2027,36 +2076,13 @@ public class IOUtils {
* @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
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
* @since 1.1
*/
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);
+ copy(input, output, Charsets.toCharset(encoding));
}
// content equals
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1307412&r1=1307411&r2=1307412&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Fri Mar 30 13:40:31 2012
@@ -1544,7 +1544,7 @@ public class FileUtilsTestCase extends F
public void testWriteCharSequence2() throws Exception {
File file = new File(getTestDirectory(), "write.txt");
- FileUtils.write(file, "Hello /u1234", null);
+ FileUtils.write(file, "Hello /u1234", (String) null);
byte[] text = "Hello /u1234".getBytes();
assertEqualContent(text, file);
}
@@ -1735,7 +1735,7 @@ public class FileUtilsTestCase extends F
File file = newFile("lines.txt");
FileUtils.writeStringToFile(file, "This line was there before you...");
- FileUtils.writeStringToFile(file, "this is brand new data", null, true);
+ FileUtils.writeStringToFile(file, "this is brand new data", (String) null, true);
String expected = "This line was there before you..."
+ "this is brand new data";
@@ -1747,7 +1747,7 @@ public class FileUtilsTestCase extends F
File file = newFile("lines.txt");
FileUtils.writeStringToFile(file, "This line was there before you...");
- FileUtils.writeStringToFile(file, "this is brand new data", null, false);
+ FileUtils.writeStringToFile(file, "this is brand new data", (String) null, false);
String expected = "this is brand new data";
String actual = FileUtils.readFileToString(file);
@@ -1781,7 +1781,7 @@ public class FileUtilsTestCase extends F
File file = newFile("lines.txt");
FileUtils.writeStringToFile(file, "This line was there before you...");
- FileUtils.write(file, "this is brand new data", null, true);
+ FileUtils.write(file, "this is brand new data", (String) null, true);
String expected = "This line was there before you..."
+ "this is brand new data";
@@ -1793,7 +1793,7 @@ public class FileUtilsTestCase extends F
File file = newFile("lines.txt");
FileUtils.writeStringToFile(file, "This line was there before you...");
- FileUtils.write(file, "this is brand new data", null, false);
+ FileUtils.write(file, "this is brand new data", (String) null, false);
String expected = "this is brand new data";
String actual = FileUtils.readFileToString(file);
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=1307412&r1=1307411&r2=1307412&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:40:31 2012
@@ -292,10 +292,10 @@ public class IOUtilsTestCase extends Fil
}
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")));
+ Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
+ Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
+ Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
+ Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
}
public void testInputStreamToByteArray() throws Exception {
FileInputStream fin = new FileInputStream(m_testFile);