001 package ezvcard.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 The views and conclusions contained in the software and documentation are those
043 of the authors and should not be interpreted as representing official policies,
044 either expressed or implied, of the FreeBSD Project.
045 */
046
047 /**
048 * I/O helper classes.
049 * @author Michael Angstadt
050 */
051 public class IOUtils {
052 private static final Charset UTF8 = Charset.forName("UTF-8");
053
054 /**
055 * Gets the extension off a file's name.
056 * @param file the file
057 * @return its extension (e.g. "jpg") or null if it doesn't have one
058 */
059 public static String getFileExtension(File file) {
060 String fileName = file.getName();
061 int dot = fileName.lastIndexOf('.');
062 if (dot >= 0 && dot < fileName.length() - 1) {
063 return fileName.substring(dot + 1);
064 }
065 return null;
066 }
067
068 /**
069 * Reads all the bytes from an input stream.
070 * @param in the input stream
071 * @return the bytes
072 * @throws IOException if there's a problem reading from the input stream
073 */
074 public static byte[] toByteArray(InputStream in) throws IOException {
075 return toByteArray(in, false);
076 }
077
078 /**
079 * Reads all the bytes from an input stream.
080 * @param in the input stream
081 * @param close true to close the input stream when done, false not to
082 * @return the bytes
083 * @throws IOException if there's a problem reading from the input stream
084 */
085 public static byte[] toByteArray(InputStream in, boolean close) throws IOException {
086 try {
087 ByteArrayOutputStream out = new ByteArrayOutputStream();
088 byte[] buffer = new byte[4096];
089 int read;
090 while ((read = in.read(buffer)) != -1) {
091 out.write(buffer, 0, read);
092 }
093 return out.toByteArray();
094 } finally {
095 if (close) {
096 closeQuietly(in);
097 }
098 }
099 }
100
101 /**
102 * Reads the contents of a text file.
103 * @param file the file to read
104 * @return the file contents
105 * @throws IOException if there's a problem reading the file
106 */
107 public static String getFileContents(File file) throws IOException {
108 return getFileContents(file, Charset.defaultCharset().name());
109 }
110
111 /**
112 * Reads the contents of a text file.
113 * @param file the file to read
114 * @param charset the character encoding of the file
115 * @return the file contents
116 * @throws IOException if there's a problem reading the file
117 */
118 public static String getFileContents(File file, String charset) throws IOException {
119 byte[] bytes = toByteArray(new FileInputStream(file), true);
120 return new String(bytes, charset);
121 }
122
123 /**
124 * Closes a closeable resource, catching its {@link IOException}.
125 * @param closeable the resource to close (can be null)
126 */
127 public static void closeQuietly(Closeable closeable) {
128 try {
129 if (closeable != null) {
130 closeable.close();
131 }
132 } catch (IOException e) {
133 //ignore
134 }
135 }
136
137 /**
138 * Creates a writer whose character encoding is set to "UTF-8".
139 * @param out the output stream to write to
140 * @return the writer
141 */
142 public static Writer utf8Writer(OutputStream out) {
143 return new OutputStreamWriter(out, UTF8);
144 }
145
146 /**
147 * Creates a writer whose character encoding is set to "UTF-8".
148 * @param file the file to write to
149 * @return the writer
150 * @throws FileNotFoundException if the file cannot be written to
151 */
152 public static Writer utf8Writer(File file) throws FileNotFoundException {
153 return utf8Writer(file, false);
154 }
155
156 /**
157 * Creates a writer whose character encoding is set to "UTF-8".
158 * @param file the file to write to
159 * @param append true to append to the end of the file, false to overwrite
160 * it
161 * @return the writer
162 * @throws FileNotFoundException if the file cannot be written to
163 */
164 public static Writer utf8Writer(File file, boolean append) throws FileNotFoundException {
165 return utf8Writer(new FileOutputStream(file, append));
166 }
167
168 /**
169 * Creates a reader whose character encoding is set to "UTF-8".
170 * @param in the input stream to read from
171 * @return the reader
172 */
173 public static Reader utf8Reader(InputStream in) {
174 return new InputStreamReader(in, UTF8);
175 }
176
177 /**
178 * Creates a reader whose character encoding is set to "UTF-8".
179 * @param file the file to read from
180 * @return the reader
181 * @throws FileNotFoundException if the file can't be read
182 */
183 public static Reader utf8Reader(File file) throws FileNotFoundException {
184 return utf8Reader(new FileInputStream(file));
185 }
186
187 private IOUtils() {
188 //hide
189 }
190 }