1 17 package com.sslexplorer.vfs; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.commons.vfs.FileObject; 26 import org.apache.commons.vfs.FileSystemException; 27 28 import com.sslexplorer.vfs.webdav.DAVException; 29 import com.sslexplorer.vfs.webdav.DAVListener; 30 31 32 46 public class VFSOutputStream extends OutputStream { 47 48 final static Log log = LogFactory.getLog(VFSOutputStream.class); 49 50 51 private static int tmpno = (int)( Math.random() * 100000 ); 52 53 private FileObject temporary = null; 54 55 private OutputStream output = null; 56 57 private VFSResource resource = null; 58 59 62 protected VFSOutputStream(VFSResource resource) { 63 if (resource == null) throw new NullPointerException (); 64 this.resource = resource; 65 66 try { 67 if(resource instanceof FileObjectVFSResource) { 68 this.temporary = resource.getFile().getParent(); 72 this.temporary = this.temporary.resolveFile( 73 VFSResource.PREFIX + ( tmpno++) + VFSResource.SUFFIX); 74 this.output = this.temporary.getContent().getOutputStream(); 75 } 76 else { 77 throw new IOException ("DAV resource is not a true file."); 78 } 79 } catch (IOException e) { 80 String message = "Unable to create temporary file. " + e.getMessage(); 81 throw new DAVException(507, message, e, resource); 82 } 83 } 84 85 88 protected void rename(FileObject temporary, FileObject original) 89 throws IOException { 90 if ((original.exists()) && (!original.delete())) { 91 throw new IOException ("Unable to delete original file"); 92 } 93 temporary.moveTo(original); 94 } 95 96 99 public void abort() { 100 try { 101 if (this.temporary.exists()) 102 this.temporary.delete(); 103 } catch (FileSystemException e) { 104 log.error(e); 105 } 106 if (this.output != null) try { 107 this.output.close(); 108 } catch (IOException exception) { 109 } finally { 111 this.output = null; 112 } 113 } 114 115 119 public void close() { 120 if (this.output == null) return; 121 try { 122 123 int event = ((FileObjectVFSResource)this.resource).getFile().exists() ? 124 DAVListener.RESOURCE_MODIFIED: 125 DAVListener.RESOURCE_CREATED; 126 127 128 this.output.close(); 129 this.output = null; 130 this.rename(this.temporary, ((FileObjectVFSResource)this.resource).getFile()); 131 132 133 this.resource.getMount().getStore().getRepository().notify(this.resource, event); 134 135 } catch (IOException e) { 136 String message = "Error processing temporary file"; 137 throw new DAVException(507, message, e, this.resource); 138 } finally { 139 this.abort(); 140 } 141 } 142 143 146 public void flush() { 147 if (this.output == null) throw new IllegalStateException ("Closed"); 148 try { 149 this.output.flush(); 150 } catch (IOException e) { 151 this.abort(); 152 String message = "Unable to flush buffers"; 153 throw new DAVException(507, message, e, this.resource); 154 } 155 } 156 157 160 public void write(int b) { 161 if (this.output == null) throw new IllegalStateException ("Closed"); 162 try { 163 this.output.write(b); 164 } catch (IOException e) { 165 this.abort(); 166 String message = "Unable to write data"; 167 throw new DAVException(507, message, e, this.resource); 168 } 169 } 170 171 174 public void write(byte b[]) { 175 if (this.output == null) throw new IllegalStateException ("Closed"); 176 try { 177 this.output.write(b); 178 } catch (IOException e) { 179 this.abort(); 180 String message = "Unable to write data"; 181 throw new DAVException(507, message, e, this.resource); 182 } 183 } 184 185 188 public void write(byte b[], int o, int l) { 189 if (this.output == null) throw new IllegalStateException ("Closed"); 190 try { 191 this.output.write(b, o, l); 192 } catch (IOException e) { 193 this.abort(); 194 String message = "Unable to write data"; 195 throw new DAVException(507, message, e, this.resource); 196 } 197 } 198 199 202 public void finalize() { 203 this.abort(); 204 } 205 } 206 | Popular Tags |