1 package commonsio;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.Reader;
8 import java.nio.charset.Charset;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 /**
13 * see: org.apache.commons.io.IOUtils and org.apache.commons.io.Charsets
14 */
15 public class IOUtils {
16
17 // readLines
18 //-----------------------------------------------------------------------
19
20 /**
21 * Gets the contents of an <code>InputStream</code> as a list of Strings,
22 * one entry per line, using the default character encoding of the platform.
23 * <p>
24 * This method buffers the input internally, so there is no need to use a
25 * <code>BufferedInputStream</code>.
26 *
27 * @param input the <code>InputStream</code> to read from, not null
28 * @return the list of Strings, never null
29 * @throws NullPointerException if the input is null
30 * @throws IOException if an I/O error occurs
31 * @since 1.1
32 * @deprecated 2.5 use {@link #readLines(InputStream, Charset)} instead
33 */
34 @Deprecated
35 public static List<String> readLines(final InputStream input) throws IOException {
36 return readLines(input, Charset.defaultCharset());
37 }
38
39 /**
40 * Gets the contents of an <code>InputStream</code> as a list of Strings,
41 * one entry per line, using the specified character encoding.
42 * <p>
43 * This method buffers the input internally, so there is no need to use a
44 * <code>BufferedInputStream</code>.
45 *
46 * @param input the <code>InputStream</code> to read from, not null
47 * @param encoding the encoding to use, null means platform default
48 * @return the list of Strings, never null
49 * @throws NullPointerException if the input is null
50 * @throws IOException if an I/O error occurs
51 * @since 2.3
52 */
53 public static List<String> readLines(final InputStream input, final Charset encoding) throws IOException {
54 final InputStreamReader reader = new InputStreamReader(input, toCharset(encoding));
55 return readLines(reader);
56 }
57
58 /**
59 * Gets the contents of an <code>InputStream</code> as a list of Strings,
60 * one entry per line, using the specified character encoding.
61 * <p>
62 * Character encoding names can be found at
63 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
64 * <p>
65 * This method buffers the input internally, so there is no need to use a
66 * <code>BufferedInputStream</code>.
67 *
68 * @param input the <code>InputStream</code> to read from, not null
69 * @param encoding the encoding to use, null means platform default
70 * @return the list of Strings, never null
71 * @throws NullPointerException if the input is null
72 * @throws IOException if an I/O error occurs
73 * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
74 * .UnsupportedEncodingException} in version 2.2 if the
75 * encoding is not supported.
76 * @since 1.1
77 */
78 public static List<String> readLines(final InputStream input, final String encoding) throws IOException {
79 return readLines(input, toCharset(encoding));
80 }
81
82 /**
83 * Gets the contents of a <code>Reader</code> as a list of Strings,
84 * one entry per line.
85 * <p>
86 * This method buffers the input internally, so there is no need to use a
87 * <code>BufferedReader</code>.
88 *
89 * @param input the <code>Reader</code> to read from, not null
90 * @return the list of Strings, never null
91 * @throws NullPointerException if the input is null
92 * @throws IOException if an I/O error occurs
93 * @since 1.1
94 */
95 public static List<String> readLines(final Reader input) throws IOException {
96 final BufferedReader reader = toBufferedReader(input);
97 final List<String> list = new ArrayList<>();
98 String line = reader.readLine();
99 while (line != null) {
100 list.add(line);
101 line = reader.readLine();
102 }
103 return list;
104 }
105
106 /**
107 * Returns the given reader if it is a {@link BufferedReader}, otherwise creates a BufferedReader from the given
108 * reader.
109 *
110 * @param reader the reader to wrap or return (not null)
111 * @return the given reader or a new {@link BufferedReader} for the given reader
112 * @throws NullPointerException if the input parameter is null
113 * @since 2.2
114 */
115 public static BufferedReader toBufferedReader(final Reader reader) {
116 return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
117 }
118
119 /**
120 * Returns the given Charset or the default Charset if the given Charset is null.
121 *
122 * @param charset A charset or null.
123 * @return the given Charset or the default Charset if the given Charset is null
124 */
125 public static Charset toCharset(final Charset charset) {
126 return charset == null ? Charset.defaultCharset() : charset;
127 }
128
129 /**
130 * Returns a Charset for the named charset. If the name is null, return the default Charset.
131 *
132 * @param charset The name of the requested charset, may be null.
133 * @return a Charset for the named charset
134 * @throws java.nio.charset.UnsupportedCharsetException If the named charset is unavailable
135 */
136 public static Charset toCharset(final String charset) {
137 return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
138 }
139 }