1 23 package org.archive.util; 24 25 import it.unimi.dsi.fastutil.io.FastBufferedOutputStream; 26 27 import java.io.BufferedInputStream ; 28 import java.io.EOFException ; 29 import java.io.File ; 30 import java.io.FileOutputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 import java.nio.charset.Charset ; 35 import java.nio.charset.IllegalCharsetNameException ; 36 import java.nio.charset.UnsupportedCharsetException ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 47 public class IoUtils { 48 protected static Logger logger = 49 Logger.getLogger(IoUtils.class.getName()); 50 51 57 public static String getClasspathPath(File file) { 58 String path = file.getPath(); 59 if (File.separatorChar != '/') { 60 path = path.replace(File.separatorChar, '/'); 63 int index = path.indexOf(':'); 64 if (index > 0 && index < 3) { 65 path = path.substring(index + 1); 66 } 67 } 68 return path; 69 } 70 71 83 public static File ensureWriteableDirectory(String dir) 84 throws IOException { 85 return ensureWriteableDirectory(new File (dir)); 86 } 87 88 100 public static List ensureWriteableDirectory(List <File > dirs) 101 throws IOException { 102 for (Iterator <File > i = dirs.iterator(); i.hasNext();) { 103 ensureWriteableDirectory(i.next()); 104 } 105 return dirs; 106 } 107 108 120 public static File ensureWriteableDirectory(File dir) 121 throws IOException { 122 if (!dir.exists()) { 123 dir.mkdirs(); 124 } else { 125 if (!dir.canWrite()) { 126 throw new IOException ("Dir " + dir.getAbsolutePath() + 127 " not writeable."); 128 } else if (!dir.isDirectory()) { 129 throw new IOException ("Dir " + dir.getAbsolutePath() + 130 " is not a directory."); 131 } 132 } 133 134 return dir; 135 } 136 137 144 public static String readFullyAsString(InputStream inputStream) 145 throws IOException { 146 StringBuffer sb = new StringBuffer (); 147 int c; 148 while((c = inputStream.read()) > -1) { 149 sb.append((char)c); 150 } 151 return sb.toString(); 152 } 153 154 161 public static void readFullyToFile(InputStream is, 162 File toFile) throws IOException { 163 readFullyToFile(is, toFile, new byte[4096]); 164 } 165 166 175 public static long readFullyToFile(final InputStream is, final File toFile, 176 final byte [] buffer) 177 throws IOException { 178 long totalcount = -1; 179 OutputStream os = 180 new FastBufferedOutputStream(new FileOutputStream (toFile)); 181 InputStream localIs = (is instanceof BufferedInputStream )? 182 is: new BufferedInputStream (is); 183 try { 184 for (int count = -1; 185 (count = localIs.read(buffer, 0, buffer.length)) != -1; 186 totalcount += count) { 187 os.write(buffer, 0, count); 188 } 189 } finally { 190 os.close(); 191 if (localIs != null) { 192 localIs.close(); 193 } 194 } 195 return totalcount; 196 } 197 198 203 public static IOException wrapAsIOException(Throwable e) { 204 IOException ioe = new IOException (e.toString()); 205 ioe.initCause(e); 206 return ioe; 207 } 208 209 210 public static void readFully(InputStream input, byte[] buf) 211 throws IOException { 212 int max = buf.length; 213 int ofs = 0; 214 while (ofs < max) { 215 int l = input.read(buf, ofs, max - ofs); 216 if (l == 0) { 217 throw new EOFException (); 218 } 219 ofs += l; 220 } 221 } 222 223 232 public static float encodingMaxBytesPerChar(String encoding) { 233 boolean isMultibyte = false; 234 final Charset cs; 235 try { 236 if (encoding != null && encoding.length() > 0) { 237 cs = Charset.forName(encoding); 238 if(cs.canEncode()) { 239 return cs.newEncoder().maxBytesPerChar(); 240 } else { 241 logger.info("Encoding not fully supported: " + encoding 242 + ". Defaulting to single byte."); 243 } 244 } 245 } catch (IllegalArgumentException e) { 246 logger.log(Level.INFO,"Illegal encoding name: " + encoding,e); 248 } 249 250 logger.fine("Encoding " + encoding + " is multibyte: " 251 + ((isMultibyte) ? Boolean.TRUE : Boolean.FALSE)); 252 return 0; 254 } 255 } | Popular Tags |