1 17 18 19 20 package org.apache.lenya.util; 21 22 import java.io.File ; 23 import java.io.FileDescriptor ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 27 import org.apache.avalon.excalibur.io.FileUtil; 28 import org.apache.log4j.Category; 29 30 31 34 public class XPSFileOutputStream extends FileOutputStream { 35 static Category log = Category.getInstance(XPSFileOutputStream.class); 36 private static final String suffixBase = ".xpstemp"; 37 protected String realFilename = null; 38 protected String suffix = null; 39 40 47 public XPSFileOutputStream(String name) throws IOException { 48 super(getTempFilename(name)); 49 setRealFilename(name); 50 } 51 52 59 public XPSFileOutputStream(File file) throws IOException { 60 super(getTempFilename(file.getAbsolutePath())); 61 setRealFilename(file.getAbsolutePath()); 62 } 63 64 72 public XPSFileOutputStream(String filename, boolean append) 73 throws IOException { 74 super(getTempFilename(filename), append); 75 setRealFilename(filename); 76 } 77 78 86 public XPSFileOutputStream(FileDescriptor fdObj) throws IOException { 87 super(fdObj); 88 throw new IOException ( 89 "Constructing an XPSFileOutputStream using a FileDescriptor is not suported because we depend on a filename"); 90 } 91 92 96 protected static String getTempFilename(String realname) { 101 return realname + XPSFileOutputStream.suffixBase + "." + Runtime.getRuntime().hashCode(); 102 } 103 104 107 protected String getRealFilename() { 108 return this.realFilename; 109 } 110 111 114 protected void setRealFilename(String filename) { 115 this.realFilename = filename; 116 } 117 118 123 public void close() throws IOException { 124 super.close(); 125 File temp = new File (getTempFilename(getRealFilename())); 126 File file = new File (getRealFilename()); 127 FileUtil.copyFile(temp, file); 128 boolean deleted = temp.delete(); 129 if (deleted) { 130 log.debug("The temporary file "+temp.getAbsolutePath() +"is deleted"); 131 } else { 132 log.debug("The temporary file "+temp.getAbsolutePath() +" couldn't be deleted"); 133 } 134 log.debug(".close(): mv " + getTempFilename(getRealFilename()) + " " + getRealFilename()); 135 } 136 137 140 public void flush() { 141 log.debug("flush() called"); 142 } 143 } 144 | Popular Tags |