001 package biweekly.util;
002
003 import java.io.ByteArrayOutputStream;
004 import java.io.Closeable;
005 import java.io.File;
006 import java.io.FileInputStream;
007 import java.io.FileNotFoundException;
008 import java.io.FileOutputStream;
009 import java.io.IOException;
010 import java.io.InputStream;
011 import java.io.InputStreamReader;
012 import java.io.OutputStream;
013 import java.io.OutputStreamWriter;
014 import java.io.Reader;
015 import java.io.Writer;
016 import java.nio.charset.Charset;
017
018 /*
019 Copyright (c) 2013, Michael Angstadt
020 All rights reserved.
021
022 Redistribution and use in source and binary forms, with or without
023 modification, are permitted provided that the following conditions are met:
024
025 1. Redistributions of source code must retain the above copyright notice, this
026 list of conditions and the following disclaimer.
027 2. Redistributions in binary form must reproduce the above copyright notice,
028 this list of conditions and the following disclaimer in the documentation
029 and/or other materials provided with the distribution.
030
031 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
032 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
033 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
034 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
035 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
036 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
037 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
038 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
039 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
040 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
041 */
042
043 /**
044 * I/O helper classes.
045 * @author Michael Angstadt
046 */
047 public class IOUtils {
048 private static final Charset UTF8 = Charset.forName("UTF-8");
049
050 /**
051 * Reads all the bytes from an input stream.
052 * @param in the input stream
053 * @return the bytes
054 * @throws IOException if there's a problem reading from the input stream
055 */
056 public static byte[] toByteArray(InputStream in) throws IOException {
057 return toByteArray(in, false);
058 }
059
060 /**
061 * Reads all the bytes from an input stream.
062 * @param in the input stream
063 * @param close true to close the input stream when done, false not to
064 * @return the bytes
065 * @throws IOException if there's a problem reading from the input stream
066 */
067 public static byte[] toByteArray(InputStream in, boolean close) throws IOException {
068 try {
069 ByteArrayOutputStream out = new ByteArrayOutputStream();
070 byte[] buffer = new byte[4096];
071 int read;
072 while ((read = in.read(buffer)) != -1) {
073 out.write(buffer, 0, read);
074 }
075 return out.toByteArray();
076 } finally {
077 if (close) {
078 closeQuietly(in);
079 }
080 }
081 }
082
083 /**
084 * Reads the contents of a text file.
085 * @param file the file to read
086 * @return the file contents
087 * @throws IOException if there's a problem reading the file
088 */
089 public static String getFileContents(File file) throws IOException {
090 return getFileContents(file, Charset.defaultCharset().name());
091 }
092
093 /**
094 * Reads the contents of a text file.
095 * @param file the file to read
096 * @param charset the character encoding of the file
097 * @return the file contents
098 * @throws IOException if there's a problem reading the file
099 */
100 public static String getFileContents(File file, String charset) throws IOException {
101 byte[] bytes = toByteArray(new FileInputStream(file), true);
102 return new String(bytes, charset);
103 }
104
105 /**
106 * Closes a closeable resource, catching its {@link IOException}.
107 * @param closeable the resource to close (can be null)
108 */
109 public static void closeQuietly(Closeable closeable) {
110 try {
111 if (closeable != null) {
112 closeable.close();
113 }
114 } catch (IOException e) {
115 //ignore
116 }
117 }
118
119 /**
120 * Creates a writer whose character encoding is set to "UTF-8".
121 * @param out the output stream to write to
122 * @return the writer
123 */
124 public static Writer utf8Writer(OutputStream out) {
125 return new OutputStreamWriter(out, UTF8);
126 }
127
128 /**
129 * Creates a writer whose character encoding is set to "UTF-8".
130 * @param file the file to write to
131 * @return the writer
132 * @throws FileNotFoundException if the file cannot be written to
133 */
134 public static Writer utf8Writer(File file) throws FileNotFoundException {
135 return utf8Writer(file, false);
136 }
137
138 /**
139 * Creates a writer whose character encoding is set to "UTF-8".
140 * @param file the file to write to
141 * @param append true to append to the end of the file, false to overwrite
142 * it
143 * @return the writer
144 * @throws FileNotFoundException if the file cannot be written to
145 */
146 public static Writer utf8Writer(File file, boolean append) throws FileNotFoundException {
147 return utf8Writer(new FileOutputStream(file, append));
148 }
149
150 /**
151 * Creates a reader whose character encoding is set to "UTF-8".
152 * @param in the input stream to read from
153 * @return the reader
154 */
155 public static Reader utf8Reader(InputStream in) {
156 return new InputStreamReader(in, UTF8);
157 }
158
159 /**
160 * Creates a reader whose character encoding is set to "UTF-8".
161 * @param file the file to read from
162 * @return the reader
163 * @throws FileNotFoundException if the file can't be read
164 */
165 public static Reader utf8Reader(File file) throws FileNotFoundException {
166 return utf8Reader(new FileInputStream(file));
167 }
168
169 private IOUtils() {
170 //hide
171 }
172 }