1 11 12 package org.eclipse.osgi.framework.internal.reliablefile; 13 14 import java.io.*; 15 import java.util.zip.Checksum ; 16 17 25 public class ReliableFileOutputStream extends FilterOutputStream { 26 29 private ReliableFile reliable; 30 31 34 private Checksum crc; 35 36 private boolean outputOpen = false; 37 38 46 public ReliableFileOutputStream(File file) throws IOException { 47 this(ReliableFile.getReliableFile(file), false); 48 } 49 50 57 public ReliableFileOutputStream(File file, boolean append) throws IOException { 58 this(ReliableFile.getReliableFile(file), append); 59 } 60 61 71 public ReliableFileOutputStream(String name) throws IOException { 72 this(ReliableFile.getReliableFile(name), false); 73 } 74 75 84 public ReliableFileOutputStream(String name, boolean append) throws IOException { 85 this(ReliableFile.getReliableFile(name), append); 86 } 87 88 95 private ReliableFileOutputStream(ReliableFile reliable, boolean append) throws IOException { 96 super(reliable.getOutputStream(append, ReliableFile.GENERATION_LATEST)); 97 98 this.reliable = reliable; 99 outputOpen = true; 100 if (append) 101 crc = reliable.getFileChecksum(); 102 else 103 crc = reliable.getChecksumCalculator(); 104 } 105 106 114 public synchronized void close() throws IOException { 115 closeIntermediateFile(); 116 reliable.closeOutputFile(crc); 117 reliable = null; 121 } 122 123 public File closeIntermediateFile() throws IOException { 124 if (reliable == null) 125 throw new IOException("ReliableFile stream not open"); if (outputOpen) { 127 reliable.writeChecksumSignature(out, crc); 129 out.flush(); 130 try { 131 ((FileOutputStream) out).getFD().sync(); 132 } catch (IOException e) { 133 e.printStackTrace(); 136 } 137 out.close(); 138 outputOpen = false; 139 } 140 return reliable.getOutputFile(); 141 } 142 143 147 public void write(byte[] b) throws IOException { 148 this.write(b, 0, b.length); 149 } 150 151 155 public void write(byte[] b, int off, int len) throws IOException { 156 out.write(b, off, len); 157 crc.update(b, off, len); 158 } 159 160 164 public void write(int b) throws IOException { 165 out.write(b); 166 crc.update((byte) b); 167 } 168 169 public void abort() { 170 if (reliable == null) 171 return; 172 if (outputOpen) { 173 try { 174 out.close(); 175 } catch (IOException e) { 176 } 177 outputOpen = false; 178 } 179 reliable.abortOutputFile(); 180 reliable = null; 181 } 182 } 183 | Popular Tags |