1 5 package org.h2.util; 6 7 import java.io.ByteArrayInputStream ; 8 import java.io.ByteArrayOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.InputStreamReader ; 12 import java.io.OutputStream ; 13 import java.io.Reader ; 14 import java.io.StringReader ; 15 import java.io.StringWriter ; 16 import java.io.UnsupportedEncodingException ; 17 import java.io.Writer ; 18 import java.sql.SQLException ; 19 20 import org.h2.engine.Constants; 21 import org.h2.message.Message; 22 23 public class IOUtils { 24 25 private static final int BUFFER_BLOCK_SIZE = 4 * 1024; 26 27 public static void closeSilently(OutputStream out) { 28 if(out != null) { 29 try { 30 out.close(); 31 } catch(IOException e) { 32 } 34 } 35 } 36 37 public static void skipFully(InputStream in, long skip) throws IOException { 38 while(skip > 0) { 39 skip -= in.skip(skip); 40 } 41 } 42 43 public static void skipFully(Reader reader, long skip) throws IOException { 44 while(skip > 0) { 45 skip -= reader.skip(skip); 46 } 47 } 48 49 public static long copyAndClose(InputStream in, OutputStream out) throws IOException { 50 try { 51 return copyAndCloseInput(in, out); 52 } finally { 53 out.close(); 54 } 55 } 56 57 public static long copyAndCloseInput(InputStream in, OutputStream out) throws IOException { 58 int written = 0; 59 try { 60 byte[] buffer = new byte[4 * 1024]; 61 while(true) { 62 int len = in.read(buffer); 63 if(len < 0) { 64 break; 65 } 66 out.write(buffer, 0, len); 67 written += len; 68 } 69 } finally { 70 in.close(); 71 } 72 return written; 73 } 74 75 public static long copyAndCloseInput(Reader in, Writer out) throws IOException { 76 long written = 0; 77 try { 78 char[] buffer = new char[4 * 1024]; 79 while(true) { 80 int len = in.read(buffer); 81 if(len < 0) { 82 break; 83 } 84 out.write(buffer, 0, len); 85 written += len; 86 } 87 } finally { 88 in.close(); 89 } 90 return written; 91 } 92 93 public static void closeSilently(InputStream in) { 94 if(in != null) { 95 try { 96 in.close(); 97 } catch(IOException e) { 98 } 100 } 101 } 102 103 public static void closeSilently(Reader reader) { 104 if(reader != null) { 105 try { 106 reader.close(); 107 } catch(IOException e) { 108 } 110 } 111 } 112 113 public static void closeSilently(Writer writer) { 114 if(writer != null) { 115 try { 116 writer.flush(); 117 writer.close(); 118 } catch(IOException e) { 119 } 121 } 122 } 123 124 public static byte[] readBytesAndClose(InputStream in, int length) throws IOException { 125 try { 126 if(length <= 0) { 127 length = Integer.MAX_VALUE; 128 } 129 int block = Math.min(BUFFER_BLOCK_SIZE, length); 130 ByteArrayOutputStream out=new ByteArrayOutputStream (block); 131 byte[] buff=new byte[block]; 132 while(length > 0) { 133 int len = Math.min(block, length); 134 len = in.read(buff, 0, len); 135 if(len < 0) { 136 break; 137 } 138 out.write(buff, 0, len); 139 length -= len; 140 } 141 return out.toByteArray(); 142 } finally { 143 in.close(); 144 } 145 } 146 147 public static String readStringAndClose(Reader in, int length) throws IOException { 148 if(length <= 0) { 149 length = Integer.MAX_VALUE; 150 } 151 int block = Math.min(BUFFER_BLOCK_SIZE, length); 152 StringWriter out=new StringWriter (length == Integer.MAX_VALUE ? block : length); 153 char[] buff=new char[block]; 154 while(length > 0) { 155 int len = Math.min(block, length); 156 len = in.read(buff, 0, len); 157 if(len < 0) { 158 break; 159 } 160 out.write(buff, 0, len); 161 length -= len; 162 } 163 in.close(); 164 return out.toString(); 165 } 166 167 public static int readFully(InputStream in, byte[] buffer, int max) throws IOException { 168 int off = 0, len = Math.min(max, buffer.length); 169 if(len == 0) { 170 return 0; 171 } 172 while (true) { 173 int l = len - off; 174 if (l <= 0) { 175 break; 176 } 177 l = in.read(buffer, off, l); 178 if (l < 0) { 179 break; 180 } 181 off += l; 182 } 183 return off < 0 ? -1 : off; 184 } 185 186 public static int readFully(Reader in, char[] buffer, int max) throws IOException { 187 int off = 0, len = Math.min(max, buffer.length); 188 if(len == 0) { 189 return 0; 190 } 191 while (true) { 192 int l = len - off; 193 if (l <= 0) { 194 break; 195 } 196 l = in.read(buffer, off, l); 197 if (l < 0) { 198 break; 199 } 200 off += l; 201 } 202 return off < 0 ? -1 : off; 203 } 204 205 public static Reader getReader(InputStream in) throws SQLException { 206 try { 207 return in == null ? null : new InputStreamReader (in, Constants.UTF8); 209 } catch (UnsupportedEncodingException e) { 210 throw Message.convert(e); 211 } 212 } 213 214 public static InputStream getInputStream(String s) throws SQLException { 215 if(s == null) { 216 return null; 217 } 218 return new ByteArrayInputStream (StringUtils.utf8Encode(s)); 219 } 220 221 public static InputStream getInputStream(Reader x) throws SQLException { 222 return x == null ? null : new ReaderInputStream(x); 223 } 224 225 public static Reader getReader(String s) { 226 return s == null ? null : new StringReader (s); 227 } 228 229 public static Reader getAsciiReader(InputStream x) throws SQLException { 230 try { 231 return x == null ? null : new InputStreamReader (x, "US-ASCII"); 232 } catch (UnsupportedEncodingException e) { 233 throw Message.convert(e); 234 } 235 } 236 237 } 238 | Popular Tags |