1 11 package org.eclipse.core.internal.localstore; 12 13 import java.io.*; 14 import org.eclipse.core.internal.utils.FileUtil; 15 16 26 public class SafeFileOutputStream extends OutputStream { 27 protected File temp; 28 protected File target; 29 protected OutputStream output; 30 protected boolean failed; 31 protected static final String EXTENSION = ".bak"; 33 public SafeFileOutputStream(File file) throws IOException { 34 this(file.getAbsolutePath(), null); 35 } 36 37 40 public SafeFileOutputStream(String targetPath, String tempPath) throws IOException { 41 failed = false; 42 target = new File(targetPath); 43 createTempFile(tempPath); 44 if (!target.exists()) { 45 if (!temp.exists()) { 46 output = new BufferedOutputStream(new FileOutputStream(target)); 47 return; 48 } 49 copy(temp, target); 53 } 54 output = new BufferedOutputStream(new FileOutputStream(temp)); 55 } 56 57 public void close() throws IOException { 58 try { 59 output.close(); 60 } catch (IOException e) { 61 failed = true; 62 throw e; } 64 if (failed) 65 temp.delete(); 66 else 67 commit(); 68 } 69 70 protected void commit() throws IOException { 71 if (!temp.exists()) 72 return; 73 target.delete(); 74 copy(temp, target); 75 temp.delete(); 76 } 77 78 protected void copy(File sourceFile, File destinationFile) throws IOException { 79 if (!sourceFile.exists()) 80 return; 81 if (sourceFile.renameTo(destinationFile)) 82 return; 83 InputStream source = null; 84 OutputStream destination = null; 85 try { 86 source = new BufferedInputStream(new FileInputStream(sourceFile)); 87 destination = new BufferedOutputStream(new FileOutputStream(destinationFile)); 88 transferStreams(source, destination); 89 } finally { 90 FileUtil.safeClose(source); 91 FileUtil.safeClose(destination); 92 } 93 } 94 95 protected void createTempFile(String tempPath) { 96 if (tempPath == null) 97 tempPath = target.getAbsolutePath() + EXTENSION; 98 temp = new File(tempPath); 99 } 100 101 public void flush() throws IOException { 102 try { 103 output.flush(); 104 } catch (IOException e) { 105 failed = true; 106 throw e; } 108 } 109 110 public String getTempFilePath() { 111 return temp.getAbsolutePath(); 112 } 113 114 protected void transferStreams(InputStream source, OutputStream destination) throws IOException { 115 byte[] buffer = new byte[8192]; 116 while (true) { 117 int bytesRead = source.read(buffer); 118 if (bytesRead == -1) 119 break; 120 destination.write(buffer, 0, bytesRead); 121 } 122 } 123 124 public void write(int b) throws IOException { 125 try { 126 output.write(b); 127 } catch (IOException e) { 128 failed = true; 129 throw e; } 131 } 132 } 133 | Popular Tags |