1 16 package scriptella.util; 17 18 import java.io.BufferedReader ; 19 import java.io.BufferedWriter ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.Closeable ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Reader ; 30 import java.io.UnsupportedEncodingException ; 31 import java.io.Writer ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 35 41 public final class IOUtils { 42 private IOUtils() { 44 } 45 46 49 static final long MAX_LENGTH = 1024 * 10000; 51 56 public static void closeSilently(Closeable closeable) { 57 if (closeable != null) { 58 try { 59 closeable.close(); 60 } catch (Exception e) { 61 ExceptionUtils.ignoreThrowable(e); 62 } 63 } 64 } 65 66 71 public static void closeSilently(Iterable <? extends Closeable > closeables) { 72 if (closeables != null) { 73 for (Closeable closeable : closeables) { 74 closeSilently(closeable); 75 } 76 } 77 } 78 79 80 86 public static String toString(Reader reader) throws IOException { 87 return toString(reader, MAX_LENGTH); 88 89 } 90 91 98 public static String toString(final Reader reader, final long maxLength) throws IOException { 99 char cb[] = new char[4096]; 100 StringBuilder sb = new StringBuilder (cb.length); 101 long len = 0; 102 103 try { 104 for (int n; (n = reader.read(cb)) >= 0;) { 105 len += n; 106 if (len > maxLength) { 107 throw new IOException ("Content too long to fit in memory"); 108 } 109 sb.append(cb, 0, n); 110 } 111 } finally { 112 closeSilently(reader); 113 } 114 115 return sb.toString(); 116 } 117 118 125 public static byte[] toByteArray(InputStream is) throws IOException { 126 return toByteArray(is, MAX_LENGTH); 127 } 128 129 137 public static byte[] toByteArray(InputStream is, long maxLength) throws IOException { 138 byte b[] = new byte[4096]; 139 ByteArrayOutputStream os = new ByteArrayOutputStream (b.length); 140 long len = 0; 141 142 try { 143 for (int n; (n = is.read(b)) >= 0;) { 144 len += n; 145 if (len > maxLength) { 146 throw new IOException ("Content too long to fit in memory"); 147 } 148 os.write(b, 0, n); 149 } 150 } finally { 151 closeSilently(is); 152 } 153 154 return os.toByteArray(); 155 } 156 157 168 public static OutputStream getOutputStream(final URL url) throws IOException { 169 if ("file".equals(url.getProtocol())) { 170 return new FileOutputStream (url.getFile()); 171 } else { 172 return url.openConnection().getOutputStream(); 173 } 174 175 } 176 177 182 public static Reader getReader(final InputStream is, final String enc) throws UnsupportedEncodingException { 183 return getReader(is, enc, true); 184 } 185 186 194 public static Reader getReader(final InputStream is, final String enc, final boolean buffered) throws UnsupportedEncodingException { 195 Reader r = enc == null ? new InputStreamReader (is) : new InputStreamReader (is, enc); 196 return buffered ? new BufferedReader (r) : r; 197 } 198 199 205 public static BufferedReader asBuffered(Reader reader) { 206 if (reader==null) { 207 throw new IllegalArgumentException ("Reader cannot be null"); 208 } 209 return (reader instanceof BufferedReader ?(BufferedReader )reader:new BufferedReader (reader)); 210 } 211 212 218 public static BufferedWriter asBuffered(Writer writer) { 219 if (writer==null) { 220 throw new IllegalArgumentException ("Writer cannot be null"); 221 } 222 return (writer instanceof BufferedWriter ?(BufferedWriter )writer:new BufferedWriter (writer)); 223 } 224 225 226 231 public static Writer getWriter(final OutputStream os, final String enc) throws IOException { 232 return getWriter(os, enc, true); 233 } 234 235 243 public static Writer getWriter(final OutputStream os, final String enc, final boolean buffered) throws IOException { 244 Writer w = enc == null ? new OutputStreamWriter (os) : new OutputStreamWriter (os, enc); 245 return buffered ? new BufferedWriter (w) : w; 246 } 247 248 255 public static URL toUrl(File file) throws MalformedURLException { 256 return file.toURI().toURL(); 257 } 258 259 } 260 | Popular Tags |