KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > reliablefile > ReliableFileOutputStream


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.reliablefile;
13
14 import java.io.*;
15 import java.util.zip.Checksum JavaDoc;
16
17 /**
18  * A ReliableFile FileOutputStream replacement class.
19  * This class can be used just like FileOutputStream. The class
20  * is in partnership with ReliableFileInputStream to avoid losing
21  * file data by using multiple files.
22  *
23  * @see ReliableFileInputStream
24  */

25 public class ReliableFileOutputStream extends FilterOutputStream {
26     /**
27      * ReliableFile object for the file.
28      */

29     private ReliableFile reliable;
30
31     /**
32      * Checksum calculator
33      */

34     private Checksum JavaDoc crc;
35
36     private boolean outputOpen = false;
37
38     /**
39      * Constructs a new ReliableFileOutputStream on the File <code>file</code>. If the
40      * file exists, it is written over. See the constructor which can append to
41      * the file if so desired.
42      *
43      * @param file the File on which to stream reads.
44      * @exception java.io.IOException If an error occurs opening the file.
45      */

46     public ReliableFileOutputStream(File file) throws IOException {
47         this(ReliableFile.getReliableFile(file), false);
48     }
49
50     /**
51      * Constructs a new ReliableFileOutputStream on the File <code>file</code>.
52      *
53      * @param file the File on which to stream reads.
54      * @param append a boolean indicating whether or not to append to an existing file.
55      * @exception java.io.IOException If an error occurs opening the file.
56      */

57     public ReliableFileOutputStream(File file, boolean append) throws IOException {
58         this(ReliableFile.getReliableFile(file), append);
59     }
60
61     /**
62      * Constructs a new ReliableFileOutputStream on the file named <code>name</code>. If
63      * the file exists, it is written over. See the constructor which can append to
64      * the file if so desired.
65      * The <code>name</code> may be absolute or relative
66      * to the System property <code>"user.dir"</code>.
67      *
68      * @param name the file on which to stream writes.
69      * @exception java.io.IOException If an error occurs opening the file.
70      */

71     public ReliableFileOutputStream(String JavaDoc name) throws IOException {
72         this(ReliableFile.getReliableFile(name), false);
73     }
74
75     /**
76      * Constructs a new ReliableFileOutputStream on the file named <code>name</code>.
77      * The <code>name</code> may be absolute or relative
78      * to the System property <code>"user.dir"</code>.
79      *
80      * @param name the file on which to stream writes.
81      * @param append a boolean indicating whether or not to append to an existing file.
82      * @exception java.io.IOException If an error occurs opening the file.
83      */

84     public ReliableFileOutputStream(String JavaDoc name, boolean append) throws IOException {
85         this(ReliableFile.getReliableFile(name), append);
86     }
87
88     /**
89      * Private constructor used by other constructors.
90      *
91      * @param reliable the ReliableFile on which to read.
92      * @param append a boolean indicating whether or not to append to an existing file.
93      * @exception java.io.IOException If an error occurs opening the file.
94      */

95     private ReliableFileOutputStream(ReliableFile reliable, boolean append) throws IOException {
96         super(reliable.getOutputStream(append, ReliableFile.GENERATION_LATEST));
97
98         this.reliable = reliable;
99         outputOpen = true;
100         if (append)
101             crc = reliable.getFileChecksum();
102         else
103             crc = reliable.getChecksumCalculator();
104     }
105
106     /**
107      * Closes this output stream and releases any system resources
108      * associated with this stream. The general contract of <code>close</code>
109      * is that it closes the output stream. A closed stream cannot perform
110      * output operations and cannot be reopened.
111      *
112      * @exception java.io.IOException If an error occurs closing the file.
113      */

114     public synchronized void close() throws IOException {
115         closeIntermediateFile();
116         reliable.closeOutputFile(crc);
117         // if the previouse closeOutpuFile() throws exception,
118
// we don't null out reliable to give another opportunity
119
// to rename the file.
120
reliable = null;
121     }
122
123     public File closeIntermediateFile() throws IOException {
124         if (reliable == null)
125             throw new IOException("ReliableFile stream not open"); //$NON-NLS-1$
126
if (outputOpen) {
127             // tag on our signature and checksum
128
reliable.writeChecksumSignature(out, crc);
129             out.flush();
130             try {
131                 ((FileOutputStream) out).getFD().sync();
132             } catch (IOException e) {
133                 // just ignore this Exception
134
//Debug
135
e.printStackTrace();
136             }
137             out.close();
138             outputOpen = false;
139         }
140         return reliable.getOutputFile();
141     }
142
143     /**
144      * Override default FilterOutputStream method.
145      * @see FilterOutputStream#write(byte[])
146      */

147     public void write(byte[] b) throws IOException {
148         this.write(b, 0, b.length);
149     }
150
151     /**
152      * Override default FilterOutputStream method.
153      * @see FilterOutputStream#write(byte[], int, int)
154      */

155     public void write(byte[] b, int off, int len) throws IOException {
156         out.write(b, off, len);
157         crc.update(b, off, len);
158     }
159
160     /**
161      * Override default FilterOutputStream method.
162      * @see FilterOutputStream#write(int)
163      */

164     public void write(int b) throws IOException {
165         out.write(b);
166         crc.update((byte) b);
167     }
168
169     public void abort() {
170         if (reliable == null)
171             return;
172         if (outputOpen) {
173             try {
174                 out.close();
175             } catch (IOException e) {/*ignore*/
176             }
177             outputOpen = false;
178         }
179         reliable.abortOutputFile();
180         reliable = null;
181     }
182 }
183
Popular Tags