/ Published in: Java
I wrote this for an enterprise level event logging cluster that has more traffic than Oprah. Has worked great for years of uptime... wish java had a decent bz2 implementation though.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.io.*; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.Random; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; boolean result; try{ result=true; result=false; e.printStackTrace(); } return result; } StringBuilder result = new StringBuilder(); try{ if(!result.toString().endsWith("/")){result.append("/");} return result.toString(); } StringBuilder sb = new StringBuilder(); FileChannel channel = null; try{ if(file.exists()){ FileLock lock = null; int attempt = 0; while (lock==null && attempt < 5){ try{lock=channel.tryLock();}catch(OverlappingFileLockException ofe){} attempt++; } if(lock !=null){ String line; while ((line = is.readLine( )) != null)sb.append(line).append("\n"); lock.release(); } } e.printStackTrace(); e.printStackTrace(); }finally{ } return sb; } boolean result=false; FileChannel channel=null; makeDirectories(filename); FileLock lock = null; int attempt = 0; while (lock==null && attempt < 5){ try{lock=channel.tryLock();}catch(OverlappingFileLockException ofe){} attempt++; } if(lock !=null){ out.write(content.toString().getBytes()); lock.release(); result=true; } e.printStackTrace(); }finally{ } return result; } boolean result=false; FileChannel readChannel = null; FileChannel tempChannel=null; FileLock readLock=null; FileLock tempLock=null; // USE TEMP FILE makeDirectories(filename); String tempname = new StringBuilder(getParent(filename)).append("temp_").append(r.nextInt(9999999)).toString(); try{ int attempt=0; while(tempLock==null&&attempt<5){if(attempt>0){Thread.sleep(1000);}try{tempLock=tempChannel.tryLock();}catch(OverlappingFileLockException ofe){attempt++;}} // COPY PREVIOUS RECORDS TO TEMP FILE if(readFile.exists()){ int attemptA=0; while(readLock==null&&attemptA<5){ try{readLock=readChannel.tryLock();}catch(OverlappingFileLockException ofe){} attemptA++; } if(readLock!=null){ String line; while((line=readBR.readLine())!=null){if(tempLock!=null)tempGZ.write(new StringBuilder(line.trim()).append("\n").toString().getBytes());} } } // WRITE NEW DATA TO TEMPFILE if(tempLock!=null){ if(content.charAt(content.length()-1)!='\n'){content.append("\n");} tempGZ.write(content.toString().getBytes()); tempGZ.finish(); tempGZ.close(); } // RENAME THE TEMP FILE TO THE OLDFILE boolean success = tempFile.renameTo(readFile); if(success){tempFile.delete();result=true;}else{System.out.println("HUGE PROBLEM... could not rename file:" + readFile.getAbsolutePath());result=false;} // RELEASE THE FILE LOCKS if(tempLock!=null){tempLock.release();} if(readLock!=null){readLock.release();} }catch(EOFException e){System.out.println(new StringBuilder("EOF: (probably minor) ").append(filename).toString());e.printStackTrace(); }finally{ } return result; } } boolean result=false; StringBuilder previous = null; FileChannel channel=null; makeDirectories(filename); FileLock lock = null; int attempt = 0; while (lock==null && attempt < 5){ try{lock=channel.tryLock();}catch(OverlappingFileLockException ofe){} attempt++; } if(lock !=null){ out.write(content.toString().getBytes()); if(previous!=null){out.write(previous.toString().getBytes());} lock.release(); result=true; } e.printStackTrace(); } finally{ } return result; } }