1 16 package org.apache.cocoon.util; 17 18 import java.io.BufferedInputStream ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.FileReader ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.Writer ; 31 import java.text.Collator ; 32 import java.util.Arrays ; 33 import java.util.Locale ; 34 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log.Hierarchy; 37 38 46 public class IOUtils { 47 48 52 60 public static void serializeString(File file, String string) 61 throws IOException { 62 serializeString(file, string, null); 63 } 64 65 74 public static void serializeString(File file, String string, String encoding) 75 throws IOException { 76 final Writer fw = 77 (encoding == null)? 78 new FileWriter (file): 79 new OutputStreamWriter (new FileOutputStream (file), encoding); 80 try { 81 fw.write(string); 82 fw.flush(); 83 } finally { 84 if (fw != null) fw.close(); 85 } 86 } 87 88 96 public static String deserializeString(File file) 97 throws IOException { 98 int len; 99 char[] chr = new char[4096]; 100 final StringBuffer buffer = new StringBuffer (); 101 final FileReader reader = new FileReader (file); 102 try { 103 while ((len = reader.read(chr)) > 0) { 104 buffer.append(chr, 0, len); 105 } 106 } finally { 107 if (reader != null) reader.close(); 108 } 109 return buffer.toString(); 110 } 111 112 120 public static void serializeObject(File file, Object object) 121 throws IOException { 122 FileOutputStream fos = new FileOutputStream (file); 123 try { 124 ObjectOutputStream oos = new ObjectOutputStream (new BufferedOutputStream(fos)); 125 oos.writeObject(object); 126 oos.flush(); 127 } finally { 128 if (fos != null) fos.close(); 129 } 130 } 131 132 140 public static Object deserializeObject(File file) 141 throws IOException , ClassNotFoundException { 142 FileInputStream fis = new FileInputStream (file); 143 Object object = null; 144 try { 145 ObjectInputStream ois = new ObjectInputStream (new BufferedInputStream (fis)); 146 object = ois.readObject(); 147 } finally { 148 if (fis != null) fis.close(); 149 } 150 return object; 151 } 152 153 157 static final String keywords[] = 158 { 159 "abstract", "boolean", "break", "byte", "case", 160 "catch", "char", "class", "const", "continue", 161 "default", "do", "double", "else", "extends", 162 "final", "finally", "float", "for", "goto", 163 "if", "implements", "import", "instanceof", "int", 164 "interface", "long", "native", "new", "package", 165 "private", "protected", "public", "return", "short", 166 "static", "strictfp", "super", "switch", "synchronized", 167 "this", "throw", "throws", "transient", "try", 168 "void", "volatile", "while" 169 }; 170 171 172 static final Collator englishCollator = Collator.getInstance(Locale.ENGLISH); 173 174 175 static final char keywordSuffix = '_'; 176 177 181 private static boolean isJavaKeyword(String keyword) { 182 return (Arrays.binarySearch(keywords, keyword, englishCollator) >= 0); 183 } 184 185 189 201 public static String normalizedFilename(String filename) { 202 if ("".equals(filename)) { 203 return ""; 204 } 205 filename = (File.separatorChar == '\\') ? filename.replace('/','\\') : filename.replace('\\','/'); 206 String [] path = StringUtils.split(filename, File.separator); 207 int start = (path[0].length() == 0) ? 1 : 0; 208 209 StringBuffer buffer = new StringBuffer (); 210 for (int i = start; i < path.length; i++) { 211 212 if (i > start) { 213 buffer.append(File.separator); 214 } 215 216 if(path[i].equals("..")) { 217 int lio; 218 for (lio = buffer.length() - 2; lio >= 0; lio--) { 219 if (buffer.substring(lio).startsWith(File.separator)) { 220 break; 221 } 222 } 223 if (lio >= 0) { 224 buffer.setLength(lio); 225 } 226 } else { 227 char[] chars = path[i].toCharArray(); 228 229 if (chars.length < 1 || !Character.isLetter(chars[0])) { 230 buffer.append('_'); 231 } 232 233 for (int j = 0; j < chars.length; j++) { 234 if (org.apache.cocoon.util.StringUtils.isAlphaNumeric(chars[j])) { 235 buffer.append(chars[j]); 236 } else { 237 buffer.append('_'); 238 } 239 } 240 241 if(isJavaKeyword(path[i])) 243 buffer.append(keywordSuffix); 244 } 245 246 } 247 return buffer.toString(); 248 } 249 250 258 public static String pathComponent(String filename) { 259 int i = filename.lastIndexOf(File.separator); 260 return (i > -1) ? filename.substring(0, i) : filename; 261 } 262 263 271 public static String fileComponent(String filename) { 272 int i = filename.lastIndexOf(File.separator); 273 return (i > -1) ? filename.substring(i + 1) : filename; 274 } 275 276 284 public static String baseName(String filename) { 285 int i = filename.lastIndexOf('.'); 286 return (i > -1) ? filename.substring(0, i) : filename; 287 } 288 289 299 public static String getFullFilename(File file) { 300 try { 301 return file.getCanonicalPath(); 302 } catch (Exception e) { 303 Hierarchy.getDefaultHierarchy().getLoggerFor("cocoon").debug("IOUtils.getFullFilename", e); 304 return file.getAbsolutePath(); 305 } 306 } 307 308 311 public static String getContextFilePath(String directoryPath, String filePath) { 312 try 313 { 314 File directory = new File (directoryPath); 315 File file = new File (filePath); 316 317 directoryPath = directory.getCanonicalPath(); 318 filePath = file.getCanonicalPath(); 319 320 if(!directoryPath.endsWith(File.separator)){ 323 directoryPath += File.separator; 324 } 325 326 if ((directoryPath.indexOf('/') !=-1) && (directoryPath.indexOf('\\') !=-1)) { 329 directoryPath = directoryPath.replace('\\', File.separator.charAt(0)); 330 directoryPath = directoryPath.replace('/', File.separator.charAt(0)); 331 } 332 333 if ((filePath.indexOf('/') !=-1) && (filePath.indexOf('\\') !=-1)) { 336 filePath = filePath.replace('\\', File.separator.charAt(0)); 337 filePath = filePath.replace('/', File.separator.charAt(0)); 338 } 339 340 if (filePath.startsWith(directoryPath)) { 341 filePath = filePath.substring(directoryPath.length()); 342 } 343 } catch (Exception e){ 344 Hierarchy.getDefaultHierarchy().getLoggerFor("cocoon").debug("IOUtils.getContextFilePath", e); 345 } 346 347 return filePath; 348 } 349 350 357 public static File createFile(File destDir, String filename) { 358 File file = new File (destDir, filename); 359 File parent = file.getParentFile(); 360 if (parent != null) parent.mkdirs(); 361 return file; 362 } 363 364 371 public static byte[] objectToBytes(Object object) throws IOException { 372 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 373 ObjectOutputStream os = new ObjectOutputStream (baos); 374 os.writeObject(object); 375 return baos.toByteArray(); 376 } 377 378 385 public static Object bytesToObject(byte[] bytes) throws IOException , ClassNotFoundException { 386 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 387 ObjectInputStream is = new ObjectInputStream (bais); 388 return is.readObject(); 389 } 390 } 391 | Popular Tags |