1 11 package org.eclipse.core.runtime.adaptor; 12 13 import java.io.*; 14 import org.eclipse.osgi.framework.internal.reliablefile.*; 15 16 23 public class StreamManager { 24 32 public static final int OPEN_BEST_AVAILABLE = ReliableFile.OPEN_BEST_AVAILABLE; 33 40 public static final int OPEN_FAIL_ON_PRIMARY = ReliableFile.OPEN_FAIL_ON_PRIMARY; 41 42 private static boolean useReliableFilesDefault = Boolean.valueOf(System.getProperty("osgi.useReliableFiles")).booleanValue(); private FileManager manager; 44 private boolean useReliableFiles; 45 46 private static final int ST_OPEN = 0; 47 private static final int ST_CLOSED = 1; 48 49 54 public StreamManager(FileManager manager) { 55 this.manager = manager; 56 useReliableFiles = useReliableFilesDefault; 57 } 58 59 63 public void setUseReliableFiles(boolean state) { 64 useReliableFiles = state; 65 } 66 67 78 public InputStream getInputStream(String target) throws IOException { 79 return getInputStream(target, OPEN_BEST_AVAILABLE); 80 } 81 82 97 public InputStream getInputStream(String target, int openMask) throws IOException { 98 99 if (useReliableFiles) { 100 int id = manager.getId(target); 101 return new ReliableFileInputStream(new File(manager.getBase(), target), id, openMask); 102 } 103 File lookup = manager.lookup(target, false); 104 if (lookup == null) 105 return null; 106 return new FileInputStream(lookup); 107 } 108 109 119 public StreamManagerOutputStream getOutputStream(String target) throws IOException { 120 if (useReliableFiles) { 121 ReliableFileOutputStream out = new ReliableFileOutputStream(new File(manager.getBase(), target)); 122 return new StreamManagerOutputStream(out, this, target, null, ST_OPEN); 123 } 124 File tmpFile = manager.createTempFile(target); 125 return new StreamManagerOutputStream(new FileOutputStream(tmpFile), this, target, tmpFile, ST_OPEN); 126 } 127 128 141 public StreamManagerOutputStream[] getOutputStreamSet(String [] targets) throws IOException { 142 int count = targets.length; 143 StreamManagerOutputStream[] streams = new StreamManagerOutputStream[count]; 144 int idx = 0; 145 try { 146 for (; idx < count; idx++) { 147 StreamManagerOutputStream newStream = getOutputStream(targets[idx]); 148 newStream.setStreamSet(streams); 149 streams[idx] = newStream; 150 } 151 } catch (IOException e) { 152 for (int jdx = 0; jdx < idx; jdx++) 154 streams[jdx].abort(); 155 throw e; 156 } 157 return streams; 158 } 159 160 171 void abortOutputStream(StreamManagerOutputStream out) { 172 StreamManagerOutputStream[] set = out.getStreamSet(); 173 if (set == null) { 174 set = new StreamManagerOutputStream[] {out}; 175 } 176 synchronized (set) { 177 for (int idx = 0; idx < set.length; idx++) { 178 out = set[idx]; 179 if (out.getOutputFile() == null) { 180 ReliableFileOutputStream rfos = (ReliableFileOutputStream) out.getOutputStream(); 182 rfos.abort(); 183 } else { 184 if (out.getState() == ST_OPEN) { 186 try { 187 out.getOutputStream().close(); 188 } catch (IOException e) { 189 } 190 } 191 out.getOutputFile().delete(); 192 } 193 out.setState(ST_CLOSED); 194 } 195 } 196 } 197 198 208 void closeOutputStream(StreamManagerOutputStream smos) throws IOException { 209 if (smos.getState() != ST_OPEN) 210 return; 211 StreamManagerOutputStream[] streamSet = smos.getStreamSet(); 212 if (smos.getOutputFile() == null) { 213 ReliableFileOutputStream rfos = (ReliableFileOutputStream) smos.getOutputStream(); 215 File file = rfos.closeIntermediateFile(); 217 smos.setState(ST_CLOSED); 218 String target = smos.getTarget(); 219 if (streamSet == null) { 220 manager.add(target, FileManager.FILETYPE_RELIABLEFILE); 221 manager.update(new String [] {smos.getTarget()}, new String [] {file.getName()}); 222 ReliableFile.fileUpdated(new File(manager.getBase(), smos.getTarget())); 223 } 224 } else { 225 OutputStream out = smos.getOutputStream(); 227 out.flush(); 228 try { 229 ((FileOutputStream) out).getFD().sync(); 230 } catch (SyncFailedException e) { 231 } 232 out.close(); 233 smos.setState(ST_CLOSED); 234 String target = smos.getTarget(); 235 if (streamSet == null) { 236 manager.add(target, FileManager.FILETYPE_STANDARD); 237 manager.update(new String [] {target}, new String [] {smos.getOutputFile().getName()}); 238 } 239 } 240 241 if (streamSet != null) { 242 synchronized (streamSet) { 243 for (int idx = 0; idx < streamSet.length; idx++) { 245 if (streamSet[idx].getState() == ST_OPEN) 246 return; } 248 String [] targets = new String [streamSet.length]; 250 String [] sources = new String [streamSet.length]; 251 for (int idx = 0; idx < streamSet.length; idx++) { 252 smos = streamSet[idx]; 253 targets[idx] = smos.getTarget(); 254 File outputFile = smos.getOutputFile(); 255 if (outputFile == null) { 256 manager.add(smos.getTarget(), FileManager.FILETYPE_RELIABLEFILE); 258 ReliableFileOutputStream rfos = (ReliableFileOutputStream) smos.getOutputStream(); 259 File file = rfos.closeIntermediateFile(); sources[idx] = file.getName(); 261 ReliableFile.fileUpdated(new File(manager.getBase(), smos.getTarget())); 262 } else { 263 manager.add(smos.getTarget(), FileManager.FILETYPE_STANDARD); 264 sources[idx] = outputFile.getName(); 265 } 266 } 267 manager.update(targets, sources); 268 } 269 } 270 } 271 272 277 public FileManager getFileManager() { 278 return manager; 279 } 280 } 281 | Popular Tags |