1 21 24 package org.lobobrowser.util.io; 25 26 import java.io.*; 27 28 31 public class IORoutines { 32 public static final byte[] LINE_BREAK_BYTES = { (byte) 13, (byte) 10 }; 33 34 public static String loadAsText(InputStream in, String encoding) throws IOException { 35 return loadAsText(in, encoding, 4096); 36 } 37 38 public static String loadAsText(InputStream in, String encoding, int bufferSize) throws IOException { 39 InputStreamReader reader = new InputStreamReader(in, encoding); 40 char[] buffer = new char[bufferSize]; 41 int offset = 0; 42 for(;;) { 43 int remain = buffer.length - offset; 44 if(remain <= 0) { 45 char[] newBuffer = new char[buffer.length * 2]; 46 System.arraycopy(buffer, 0, newBuffer, 0, offset); 47 buffer = newBuffer; 48 remain = buffer.length - offset; 49 } 50 int numRead = reader.read(buffer, offset, remain); 51 if(numRead == -1) { 52 break; 53 } 54 offset += numRead; 55 } 56 return new String (buffer, 0, offset); 57 } 58 59 public static byte[] load(File file) throws IOException { 60 long fileLength = file.length(); 61 if(fileLength > Integer.MAX_VALUE) { 62 throw new IOException("File '" + file.getName() + "' too big"); 63 } 64 InputStream in = new FileInputStream(file); 65 try { 66 return loadExact(in, (int) fileLength); 67 } finally { 68 in.close(); 69 } 70 } 71 72 public static byte[] load(InputStream in) throws IOException { 73 return load(in, 4096); 74 } 75 public static byte[] load(InputStream in, int initialBufferSize) throws IOException { 76 if(initialBufferSize == 0) { 77 initialBufferSize = 1; 78 } 79 byte[] buffer = new byte[initialBufferSize]; 80 int offset = 0; 81 for(;;) { 82 int remain = buffer.length - offset; 83 if(remain <= 0) { 84 int newSize = buffer.length * 2; 85 byte[] newBuffer = new byte[newSize]; 86 System.arraycopy(buffer, 0, newBuffer, 0, offset); 87 buffer = newBuffer; 88 remain = buffer.length - offset; 89 } 90 int numRead = in.read(buffer, offset, remain); 91 if(numRead == -1) { 92 break; 93 } 94 offset += numRead; 95 } 96 if(offset < buffer.length) { 97 byte[] newBuffer = new byte[offset]; 98 System.arraycopy(buffer, 0, newBuffer, 0, offset); 99 buffer = newBuffer; 100 } 101 return buffer; 102 } 103 104 public static byte[] loadExact(InputStream in, int length) throws IOException { 105 byte[] buffer = new byte[length]; 106 int offset = 0; 107 for(;;) { 108 int remain = length - offset; 109 if(remain <= 0) { 110 break; 111 } 112 int numRead = in.read(buffer, offset, remain); 113 if(numRead == -1) { 114 throw new IOException("Reached EOF, read " + offset + " expecting " + length); 115 } 116 offset += numRead; 117 } 118 return buffer; 119 } 120 121 public static boolean equalContent(File file, byte[] content) throws IOException { 122 long length = file.length(); 123 if(length > Integer.MAX_VALUE) { 124 throw new IOException("File '" + file + "' too big"); 125 } 126 InputStream in = new FileInputStream(file); 127 try { 128 byte[] fileContent = loadExact(in, (int) length); 129 return java.util.Arrays.equals(content, fileContent); 130 } finally { 131 in.close(); 132 } 133 } 134 135 public static void save(File file, byte[] content) throws IOException { 136 FileOutputStream out = new FileOutputStream(file); 137 try { 138 out.write(content); 139 } finally { 140 out.close(); 141 } 142 } 143 144 147 public static String readLine(InputStream in) throws IOException { 148 int b; 149 StringBuffer sb = null; 150 OUTER: 151 while((b = in.read()) != -1) { 152 if(sb == null) { 153 sb = new StringBuffer (); 154 } 155 switch(b) { 156 case (byte) '\n': 157 break OUTER; 158 case (byte) '\r': 159 break; 160 default: 161 sb.append((char) b); 162 break; 163 } 164 } 165 return sb == null ? null : sb.toString(); 166 } 167 168 public static void touch(File file) { 169 file.setLastModified(System.currentTimeMillis()); 170 } 171 172 public static void saveStrings(File file, java.util.Collection list) throws IOException { 173 BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file)); 174 try { 175 PrintWriter writer = new PrintWriter(bout); 176 java.util.Iterator i = list.iterator(); 177 while(i.hasNext()) { 178 String text = (String ) i.next(); 179 writer.println(text); 180 } 181 writer.flush(); 182 } finally { 183 bout.close(); 184 } 185 } 186 187 public static java.util.List loadStrings(File file) throws IOException { 188 java.util.List list = new java.util.LinkedList (); 189 InputStream in = new FileInputStream(file); 190 try { 191 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 192 String line; 193 while((line = reader.readLine()) != null) { 194 list.add(line); 195 } 196 return list; 197 } finally { 198 in.close(); 199 } 200 } 201 202 } 203 | Popular Tags |