1 19 package org.jahia.utils; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.BufferedReader ; 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.io.OutputStreamWriter ; 33 import java.io.Reader ; 34 35 import org.jahia.exceptions.JahiaException; 36 37 public class FileUtils { 38 39 private static org.apache.log4j.Logger logger = 40 org.apache.log4j.Logger.getLogger(FileUtils.class); 41 42 47 private FileUtils () { 48 logger.debug("Starting fileUtils"); 49 } 50 51 56 public static FileUtils getInstance () { 57 if (_fileUtils == null) { 58 _fileUtils = new FileUtils(); 59 } 60 return _fileUtils; 61 } 62 63 70 public String composeBigTextFullPathName (String jahiaDiskPath, int jahiaID, 71 int pageID, int fieldID, int versionID, 72 int versionStatus, String languageCode) { 73 return composeBigTextFullPathName(jahiaDiskPath, 74 composeBigTextFileNamePart(jahiaID, pageID, fieldID, versionID, versionStatus, languageCode)); 75 } 76 77 public String composeBigTextFullPathName(String jahiaDiskPath, String bigTextFileNamePart) { 78 StringBuffer fileName = new StringBuffer (); 79 fileName.append(jahiaDiskPath); 80 fileName.append(File.separator); 81 fileName.append(bigTextFileNamePart); 82 return fileName.toString(); 83 } 84 85 public String composeBigTextFileNamePart (int jahiaID, int pageID, 86 int fieldID, int versionID, 87 int versionStatus, 88 String languageCode) { 89 StringBuffer fileName = new StringBuffer (); 90 fileName.append(Integer.toString(jahiaID)); 91 fileName.append("-"); 92 fileName.append(Integer.toString(pageID)); 93 fileName.append("-"); 94 fileName.append(Integer.toString(fieldID)); 95 fileName.append("-"); 96 fileName.append(languageCode); 97 if (versionStatus > 1) { 98 fileName.append("-s"); 99 } else if ( (versionID != 0) && (versionStatus <= 0)) { 100 fileName.append("-" + versionID); 101 } 102 fileName.append(".jahia"); 103 return fileName.toString(); 104 } 105 106 112 public boolean fileExists (String fileName) 113 throws JahiaException { 114 try { 115 File theFile = new File (fileName); 116 return theFile.exists(); 117 } catch (SecurityException se) { 118 String errorMsg = "Security error in readFile : " + se.getMessage(); 119 logger.error(errorMsg, se); 120 throw new JahiaException("Cannot access to jahia files", 121 errorMsg, JahiaException.FILE_ERROR, 122 JahiaException.CRITICAL_SEVERITY); 123 } 124 } 125 126 137 public String readFile (String fileName) 138 throws JahiaException { 139 if (!tryToReadFile(fileName, UTF8)) { 140 logger.debug("Cannot read files in UTF-8 format trying ASCII"); 141 if (!tryToReadFile(fileName, ASCII)) { 142 logger.error("Cannot read files -> BAILING OUT"); 143 throw new JahiaException("Cannot access to jahia files", 144 "Error in readFile : " + fileName, 145 JahiaException.FILE_ERROR, 146 JahiaException.ERROR_SEVERITY); 147 } 148 } 149 return _content; 150 } 151 152 161 public String readFile (String fileName, String langCode) 162 throws JahiaException { 163 readFile(fileName); 164 return _content; 165 } 166 167 176 public void writeFile (String fileName, String fileContent) 177 throws JahiaException { 178 try { 179 BufferedWriter out = new BufferedWriter ( 180 new OutputStreamWriter ( 181 new FileOutputStream (fileName), "UTF-8")); 182 out.write(fileContent); 183 out.close(); 184 } catch (IOException ie) { 185 String errorMsg = "Error in writeFile : " + fileName + 186 "\nIOException : " + ie.getMessage(); 187 logger.error( errorMsg, ie); 188 throw new JahiaException("Cannot access to jahia files", 189 errorMsg, JahiaException.FILE_ERROR, 190 JahiaException.CRITICAL_SEVERITY); 191 } 192 } 193 194 203 public void writeFile (String fileName, String fileContent, String langCode) 204 throws JahiaException { 205 writeFile(fileName, fileContent); 206 } 207 208 213 public boolean deleteFile (String fileName) { 214 File theFile = new File (fileName); 215 return theFile.delete(); 216 } 217 218 224 public boolean renameFile (String oldFileName, String newFileName) { 225 File oldFile = new File (oldFileName); 226 File newFile = new File (newFileName); 227 return oldFile.renameTo(newFile); 228 } 229 230 237 public boolean copyFile (String oldFileName, String newFileName) { 238 File oldFile = new File (oldFileName); 239 File newFile = new File (newFileName); 240 if (!oldFile.exists()) { 241 logger.error("Cannot copy file: source file doesn't exist"); 242 return false; 243 } 244 if (oldFile.getAbsolutePath().equals(newFile.getAbsolutePath())) { 245 return true; 246 } 247 try { 248 copyStream(new FileInputStream (oldFile), 249 new FileOutputStream (newFile)); 250 } catch (IOException ioe) { 251 return false; 252 } 253 return true; 254 } 255 256 262 public void copyStream (InputStream ins, OutputStream outs) 263 throws IOException { 264 int writeBufferSize = 4096; 265 byte[] writeBuffer = new byte[writeBufferSize]; 266 267 BufferedInputStream bis = new BufferedInputStream (ins, writeBufferSize); 268 BufferedOutputStream bos = new BufferedOutputStream (outs, 269 writeBufferSize); 270 int bufread; 271 while ( (bufread = bis.read(writeBuffer)) != -1) { 272 bos.write(writeBuffer, 0, bufread); 273 } 274 bos.flush(); 275 bos.close(); 276 bis.close(); 277 } 278 279 284 public static String readerToString(Reader reader) throws IOException { 285 if ( reader == null ){ 286 return null; 287 } 288 StringBuffer buff = new StringBuffer (); 289 char[] stringToRead = new char[4096]; 290 int count = -1; 291 while ( (count = reader.read(stringToRead, 0, 4096)) != -1) { 292 buff.append(stringToRead, 0, count); 293 } 294 return buff.toString(); 295 } 296 297 304 public static String getFileCharset(InputStream inputStream){ 305 306 BufferedReader in; 307 try { 308 in = new BufferedReader ( 309 new InputStreamReader ( 310 inputStream, "UTF-8")); 311 return "UTF-8"; 313 } catch (IOException ie) { 314 } 315 try { 316 in = new BufferedReader ( 317 new InputStreamReader ( 318 inputStream, "GBK")); 319 return "GBK"; 321 } catch (IOException ie) { 322 } 323 try { 324 in = new BufferedReader ( 325 new InputStreamReader ( 326 inputStream, "ASCII")); 327 return "ASCII"; 329 } catch (IOException ie) { 330 } 331 return null; 332 } 333 334 342 private boolean tryToReadFile (String fileName, byte format) { 343 try { 344 BufferedReader in; 345 if (format == GBK) { 346 in = new BufferedReader ( 347 new InputStreamReader ( 348 new FileInputStream (fileName), "GBK")); 349 } else { 350 if (format == UTF8) { 351 in = new BufferedReader ( 352 new InputStreamReader ( 353 new FileInputStream (fileName), "UTF-8")); 354 } else { 355 in = new BufferedReader ( 356 new InputStreamReader ( 357 new FileInputStream (fileName))); 358 } 359 } 360 _content = ""; 361 362 StringBuffer buff = new StringBuffer (); 363 char[] stringToRead = new char[4096]; 364 int count = -1; 365 while ( (count = in.read(stringToRead, 0, 4096)) != -1) { 366 buff.append(stringToRead, 0, count); 367 } 368 _content = buff.toString(); 369 buff = null; 370 377 in.close(); 378 return true; 379 } catch (IOException ie) { 380 return false; 381 } 382 } 383 384 private final byte ASCII = 0; 385 private final byte UTF8 = 1; 386 private final byte GBK = 2; 387 private String _content; 388 389 private static FileUtils _fileUtils = null; 390 } 391 | Popular Tags |