1 19 package org.netbeans.modules.localhistory.utils; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import org.netbeans.modules.localhistory.store.StoreEntry; 25 import org.openide.ErrorManager; 26 import org.openide.filesystems.FileAlreadyLockedException; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 import org.openide.nodes.Node; 30 31 36 public class Utils { 37 38 public static void revert(final Node[] nodes) { 39 for(Node node : nodes) { 40 StoreEntry se = node.getLookup().lookup(StoreEntry.class); 41 if(se != null) { 42 Utils.revert(se); 43 } 44 } 45 } 46 47 public static void revert(StoreEntry se) { 48 InputStream is = null; 49 OutputStream os = null; 50 try { 51 FileObject fo = FileUtil.toFileObject(se.getFile()); 52 if(se.getStoreFile() != null) { if(fo == null) { 54 fo = FileUtil.createData(se.getFile()); 55 } 56 os = getOutputStream(fo); 57 is = se.getStoreFileInputStream(); 58 FileUtil.copy(is, os); 59 } else { 60 fo.delete(); 61 } 62 } catch (Exception e) { 63 ErrorManager.getDefault().notify(ErrorManager.ERROR, e); 64 } finally { 65 try { 66 if(os != null) { os.close(); } 67 if(is != null) { is.close(); } 68 } catch (IOException e) {} 69 } 70 } 71 72 private static OutputStream getOutputStream(FileObject fo) throws FileAlreadyLockedException, IOException , InterruptedException { 73 int retry = 0; 74 while (true) { 75 try { 76 return fo.getOutputStream(); 77 } catch (IOException ioe) { 78 retry++; 79 if (retry > 7) { 80 throw ioe; 81 } 82 Thread.sleep(retry * 30); 83 } 84 } 85 } 86 87 } 88 | Popular Tags |