KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > XPSFileOutputStream


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: XPSFileOutputStream.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.util;
21
22 import java.io.File JavaDoc;
23 import java.io.FileDescriptor JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import org.apache.avalon.excalibur.io.FileUtil;
28 import org.apache.log4j.Category;
29
30
31 /**
32  * DOCUMENT ME!
33  */

34 public class XPSFileOutputStream extends FileOutputStream JavaDoc {
35     static Category log = Category.getInstance(XPSFileOutputStream.class);
36     private static final String JavaDoc suffixBase = ".xpstemp";
37     protected String JavaDoc realFilename = null;
38     protected String JavaDoc suffix = null;
39
40     /**
41      * Creates a new XPSFileOutputStream object.
42      *
43      * @param name DOCUMENT ME!
44      *
45      * @throws IOException DOCUMENT ME!
46      */

47     public XPSFileOutputStream(String JavaDoc name) throws IOException JavaDoc {
48         super(getTempFilename(name));
49         setRealFilename(name);
50     }
51
52     /**
53      * Creates a new XPSFileOutputStream object.
54      *
55      * @param file DOCUMENT ME!
56      *
57      * @throws IOException DOCUMENT ME!
58      */

59     public XPSFileOutputStream(File JavaDoc file) throws IOException JavaDoc {
60         super(getTempFilename(file.getAbsolutePath()));
61         setRealFilename(file.getAbsolutePath());
62     }
63
64     /**
65      * Creates a new XPSFileOutputStream object.
66      *
67      * @param filename DOCUMENT ME!
68      * @param append DOCUMENT ME!
69      *
70      * @throws IOException DOCUMENT ME!
71      */

72     public XPSFileOutputStream(String JavaDoc filename, boolean append)
73         throws IOException JavaDoc {
74         super(getTempFilename(filename), append);
75         setRealFilename(filename);
76     }
77
78     /**
79      * We cannot support this version of the constructer because we need to play tricks with the
80      * filename. There is no filename available when starting with a FileDescriptor.
81      *
82      * @param fdObj DOCUMENT ME!
83      *
84      * @throws IOException DOCUMENT ME!
85      */

86     public XPSFileOutputStream(FileDescriptor JavaDoc fdObj) throws IOException JavaDoc {
87         super(fdObj);
88         throw new IOException JavaDoc(
89             "Constructing an XPSFileOutputStream using a FileDescriptor is not suported because we depend on a filename");
90     }
91
92     /**
93      * @param realname DOCUMENT ME!
94      * @return DOCUMENT ME!
95      */

96     // FIXME: the hashCode() is probably not good enough
97
// We need to find a better source of a random
98
// string that is available to a static method.
99
//
100
protected static String JavaDoc getTempFilename(String JavaDoc realname) {
101         return realname + XPSFileOutputStream.suffixBase + "." + Runtime.getRuntime().hashCode();
102     }
103
104     /**
105      * @return DOCUMENT ME!
106      */

107     protected String JavaDoc getRealFilename() {
108         return this.realFilename;
109     }
110
111     /**
112      * @param filename DOCUMENT ME!
113      */

114     protected void setRealFilename(String JavaDoc filename) {
115         this.realFilename = filename;
116     }
117
118     /**
119      * DOCUMENT ME!
120      *
121      * @throws IOException DOCUMENT ME!
122      */

123     public void close() throws IOException JavaDoc {
124         super.close();
125         File JavaDoc temp = new File JavaDoc(getTempFilename(getRealFilename()));
126         File JavaDoc file = new File JavaDoc(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     /**
138      * DOCUMENT ME!
139      */

140     public void flush() {
141         log.debug("flush() called");
142     }
143 }
144
Popular Tags