Skip to content

Commit 55bae88

Browse files
committed
[IO-318] Add Charset sister APIs to method that take a String charset name. FileUtils.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1307412 13f79535-47bb-0310-9956-ffa450edef68
1 parent 34f049c commit 55bae88

5 files changed

Lines changed: 252 additions & 84 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io;
18+
19+
import java.nio.charset.Charset;
20+
import java.nio.charset.UnsupportedCharsetException;
21+
22+
/**
23+
* Helps use {@link Charset}
24+
*
25+
* @version $Id$
26+
* @since 2.3
27+
*/
28+
public class Charsets {
29+
30+
/**
31+
* Returns the given Charset or the default Charset if the given Charset is null.
32+
*
33+
* @param charset
34+
* A charset or null.
35+
* @return the given Charset or the default Charset if the given Charset is null
36+
* @since 2.3
37+
*/
38+
public static Charset toCharset(Charset charset) {
39+
return charset == null ? Charset.defaultCharset() : charset;
40+
}
41+
42+
/**
43+
* Returns a Charset for the named charset. If the name is null, return the default Charset.
44+
*
45+
* @param charset
46+
* The name of the requested charset, may be null.
47+
* @return a Charset for the named charset
48+
* @throws UnsupportedCharsetException
49+
* If the named charset is unavailable
50+
* @since 2.3
51+
*/
52+
public static Charset toCharset(String charset) {
53+
return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
54+
}
55+
56+
}

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 105 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import java.io.InputStreamReader;
2727
import java.io.OutputStream;
2828
import java.io.Reader;
29+
import java.io.UnsupportedEncodingException;
2930
import java.math.BigInteger;
3031
import java.net.URL;
3132
import java.net.URLConnection;
3233
import java.nio.ByteBuffer;
3334
import java.nio.channels.FileChannel;
3435
import java.nio.charset.Charset;
36+
import java.nio.charset.UnsupportedCharsetException;
3537
import java.util.ArrayList;
3638
import java.util.Collection;
3739
import java.util.Date;
@@ -1633,18 +1635,37 @@ public static boolean waitFor(File file, int seconds) {
16331635
* @param encoding the encoding to use, <code>null</code> means platform default
16341636
* @return the file contents, never <code>null</code>
16351637
* @throws IOException in case of an I/O error
1636-
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1638+
* @since 2.3
16371639
*/
1638-
public static String readFileToString(File file, String encoding) throws IOException {
1640+
public static String readFileToString(File file, Charset encoding) throws IOException {
16391641
InputStream in = null;
16401642
try {
16411643
in = openInputStream(file);
1642-
return IOUtils.toString(in, encoding);
1644+
return IOUtils.toString(in, Charsets.toCharset(encoding));
16431645
} finally {
16441646
IOUtils.closeQuietly(in);
16451647
}
16461648
}
16471649

1650+
/**
1651+
* Reads the contents of a file into a String. The file is always closed.
1652+
*
1653+
* @param file
1654+
* the file to read, must not be <code>null</code>
1655+
* @param encoding
1656+
* the encoding to use, <code>null</code> means platform default
1657+
* @return the file contents, never <code>null</code>
1658+
* @throws IOException
1659+
* in case of an I/O error
1660+
* @throws UnsupportedCharsetException
1661+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
1662+
* supported.
1663+
* @since 2.3
1664+
*/
1665+
public static String readFileToString(File file, String encoding) throws IOException {
1666+
return readFileToString(file, Charsets.toCharset(encoding));
1667+
}
1668+
16481669

16491670
/**
16501671
* Reads the contents of a file into a String using the default encoding for the VM.
@@ -1656,7 +1677,7 @@ public static String readFileToString(File file, String encoding) throws IOExcep
16561677
* @since 1.3.1
16571678
*/
16581679
public static String readFileToString(File file) throws IOException {
1659-
return readFileToString(file, null);
1680+
return readFileToString(file, Charset.defaultCharset());
16601681
}
16611682

16621683
/**
@@ -1686,19 +1707,37 @@ public static byte[] readFileToByteArray(File file) throws IOException {
16861707
* @param encoding the encoding to use, <code>null</code> means platform default
16871708
* @return the list of Strings representing each line in the file, never <code>null</code>
16881709
* @throws IOException in case of an I/O error
1689-
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1690-
* @since 1.1
1710+
* @since 2.3
16911711
*/
1692-
public static List<String> readLines(File file, String encoding) throws IOException {
1712+
public static List<String> readLines(File file, Charset encoding) throws IOException {
16931713
InputStream in = null;
16941714
try {
16951715
in = openInputStream(file);
1696-
return IOUtils.readLines(in, encoding);
1716+
return IOUtils.readLines(in, Charsets.toCharset(encoding));
16971717
} finally {
16981718
IOUtils.closeQuietly(in);
16991719
}
17001720
}
17011721

1722+
/**
1723+
* Reads the contents of a file line by line to a List of Strings. The file is always closed.
1724+
*
1725+
* @param file
1726+
* the file to read, must not be <code>null</code>
1727+
* @param encoding
1728+
* the encoding to use, <code>null</code> means platform default
1729+
* @return the list of Strings representing each line in the file, never <code>null</code>
1730+
* @throws IOException
1731+
* in case of an I/O error
1732+
* @throws UnsupportedCharsetException
1733+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
1734+
* supported.
1735+
* @since 1.1
1736+
*/
1737+
public static List<String> readLines(File file, String encoding) throws IOException {
1738+
return readLines(file, Charsets.toCharset(encoding));
1739+
}
1740+
17021741
/**
17031742
* Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
17041743
* The file is always closed.
@@ -1709,7 +1748,7 @@ public static List<String> readLines(File file, String encoding) throws IOExcept
17091748
* @since 1.3
17101749
*/
17111750
public static List<String> readLines(File file) throws IOException {
1712-
return readLines(file, null);
1751+
return readLines(file, Charset.defaultCharset());
17131752
}
17141753

17151754
/**
@@ -1796,10 +1835,9 @@ public static void writeStringToFile(File file, String data, String encoding) th
17961835
* @param append if <code>true</code>, then the String will be added to the
17971836
* end of the file rather than overwriting
17981837
* @throws IOException in case of an I/O error
1799-
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1800-
* @since 2.1
1838+
* @since 2.3
18011839
*/
1802-
public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {
1840+
public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException {
18031841
OutputStream out = null;
18041842
try {
18051843
out = openOutputStream(file, append);
@@ -1810,6 +1848,24 @@ public static void writeStringToFile(File file, String data, String encoding, bo
18101848
}
18111849
}
18121850

1851+
/**
1852+
* Writes a String to a file creating the file if it does not exist.
1853+
*
1854+
* @param file the file to write
1855+
* @param data the content to write to the file
1856+
* @param encoding the encoding to use, <code>null</code> means platform default
1857+
* @param append if <code>true</code>, then the String will be added to the
1858+
* end of the file rather than overwriting
1859+
* @throws IOException in case of an I/O error
1860+
* @throws UnsupportedCharsetException
1861+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
1862+
* supported by the VM
1863+
* @since 2.1
1864+
*/
1865+
public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {
1866+
writeStringToFile(file, data, Charsets.toCharset(encoding), append);
1867+
}
1868+
18131869
/**
18141870
* Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
18151871
*
@@ -1818,7 +1874,7 @@ public static void writeStringToFile(File file, String data, String encoding, bo
18181874
* @throws IOException in case of an I/O error
18191875
*/
18201876
public static void writeStringToFile(File file, String data) throws IOException {
1821-
writeStringToFile(file, data, null, false);
1877+
writeStringToFile(file, data, Charset.defaultCharset(), false);
18221878
}
18231879

18241880
/**
@@ -1832,7 +1888,7 @@ public static void writeStringToFile(File file, String data) throws IOException
18321888
* @since 2.1
18331889
*/
18341890
public static void writeStringToFile(File file, String data, boolean append) throws IOException {
1835-
writeStringToFile(file, data, null, append);
1891+
writeStringToFile(file, data, Charset.defaultCharset(), append);
18361892
}
18371893

18381894
/**
@@ -1844,7 +1900,7 @@ public static void writeStringToFile(File file, String data, boolean append) thr
18441900
* @since 2.0
18451901
*/
18461902
public static void write(File file, CharSequence data) throws IOException {
1847-
write(file, data, null, false);
1903+
write(file, data, Charset.defaultCharset(), false);
18481904
}
18491905

18501906
/**
@@ -1858,7 +1914,20 @@ public static void write(File file, CharSequence data) throws IOException {
18581914
* @since 2.1
18591915
*/
18601916
public static void write(File file, CharSequence data, boolean append) throws IOException {
1861-
write(file, data, null, append);
1917+
write(file, data, Charset.defaultCharset(), append);
1918+
}
1919+
1920+
/**
1921+
* Writes a CharSequence to a file creating the file if it does not exist.
1922+
*
1923+
* @param file the file to write
1924+
* @param data the content to write to the file
1925+
* @param encoding the encoding to use, <code>null</code> means platform default
1926+
* @throws IOException in case of an I/O error
1927+
* @since 2.3
1928+
*/
1929+
public static void write(File file, CharSequence data, Charset encoding) throws IOException {
1930+
write(file, data, encoding, false);
18621931
}
18631932

18641933
/**
@@ -1884,14 +1953,31 @@ public static void write(File file, CharSequence data, String encoding) throws I
18841953
* @param append if <code>true</code>, then the data will be added to the
18851954
* end of the file rather than overwriting
18861955
* @throws IOException in case of an I/O error
1887-
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1888-
* @since IO 2.1
1956+
* @since IO 2.3
18891957
*/
1890-
public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {
1958+
public static void write(File file, CharSequence data, Charset encoding, boolean append) throws IOException {
18911959
String str = data == null ? null : data.toString();
18921960
writeStringToFile(file, str, encoding, append);
18931961
}
18941962

1963+
/**
1964+
* Writes a CharSequence to a file creating the file if it does not exist.
1965+
*
1966+
* @param file the file to write
1967+
* @param data the content to write to the file
1968+
* @param encoding the encoding to use, <code>null</code> means platform default
1969+
* @param append if <code>true</code>, then the data will be added to the
1970+
* end of the file rather than overwriting
1971+
* @throws IOException in case of an I/O error
1972+
* @throws UnsupportedCharsetException
1973+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
1974+
* supported by the VM
1975+
* @since IO 2.1
1976+
*/
1977+
public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {
1978+
write(file, data, Charsets.toCharset(encoding), append);
1979+
}
1980+
18951981
/**
18961982
* Writes a byte array to a file creating the file if it does not exist.
18971983
* <p>

0 commit comments

Comments
 (0)