Skip to main content
summaryrefslogtreecommitdiffstats
blob: f023093617991a3c6e21d854103f759732bd921b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.framework.internal.reliablefile;

import java.io.*;
import java.util.zip.Checksum;

/**
 * A ReliableFile FileOutputStream replacement class.
 * This class can be used just like FileOutputStream. The class
 * is in partnership with ReliableFileInputStream to avoid losing
 * file data by using multiple files.
 *
 * @see			ReliableFileInputStream
 */
public class ReliableFileOutputStream extends FilterOutputStream {
	/**
	 * ReliableFile object for the file.
	 */
	private ReliableFile reliable;

	/**
	 * Checksum calculator
	 */
	private Checksum crc;

	/**
	 * Constructs a new ReliableFileOutputStream on the File <code>file</code>.  If the
	 * file exists, it is written over.  See the constructor which can append to
	 * the file if so desired.
	 *
	 * @param		file		the File on which to stream reads.
	 * @exception 	java.io.IOException If an error occurs opening the file.
	 */
	public ReliableFileOutputStream(File file) throws IOException {
		this(ReliableFile.getReliableFile(file), false);
	}

	/**
	 * Constructs a new ReliableFileOutputStream on the File <code>file</code>.
	 *
	 * @param		file		the File on which to stream reads.
	 * @param		append		a boolean indicating whether or not to append to an existing file.
	 * @exception 	java.io.IOException If an error occurs opening the file.
	 */
	public ReliableFileOutputStream(File file, boolean append) throws IOException {
		this(ReliableFile.getReliableFile(file), append);
	}

	/**
	 * Constructs a new ReliableFileOutputStream on the file named <code>name</code>. If
	 * the file exists, it is written over.  See the constructor which can append to
	 * the file if so desired.
	 * The <code>name</code> may be absolute or relative
	 * to the System property <code>"user.dir"</code>.
	 *
	 * @param		name	the file on which to stream writes.
	 * @exception 	java.io.IOException If an error occurs opening the file.
	 */
	public ReliableFileOutputStream(String name) throws IOException {
		this(ReliableFile.getReliableFile(name), false);
	}

	/**
	 * Constructs a new ReliableFileOutputStream on the file named <code>name</code>.
	 * The <code>name</code> may be absolute or relative
	 * to the System property <code>"user.dir"</code>.
	 *
	 * @param		name	the file on which to stream writes.
	 * @param		append		a boolean indicating whether or not to append to an existing file.
	 * @exception 	java.io.IOException If an error occurs opening the file.
	 */
	public ReliableFileOutputStream(String name, boolean append) throws IOException {
		this(ReliableFile.getReliableFile(name), append);
	}

	/**
	 * Private constructor used by other constructors.
	 *
	 * @param		reliable		the ReliableFile on which to read.
	 * @param		append		a boolean indicating whether or not to append to an existing file.
	 * @exception 	java.io.IOException If an error occurs opening the file.
	 */
	private ReliableFileOutputStream(ReliableFile reliable, boolean append) throws IOException {
		super(reliable.getOutputStream(append));

		this.reliable = reliable;
		if (append)
			crc = reliable.getFileChecksum();
		else
			crc = ReliableFile.getChecksumCalculator();
	}

	/**
	 * Closes this output stream and releases any system resources
	 * associated with this stream. The general contract of <code>close</code>
	 * is that it closes the output stream. A closed stream cannot perform
	 * output operations and cannot be reopened.
	 *
	 * @exception 	java.io.IOException If an error occurs closing the file.
	 */
	public synchronized void close() throws IOException {
		if (reliable != null) {
			try {
				// tag on our signature and checksum
				out.write(ReliableFile.identifier1);
				byte crcStr[] = ReliableFile.intToHex((int) crc.getValue()).getBytes();
				out.write(crcStr);
				out.write(ReliableFile.identifier2);

				try {
					out.flush();
					((FileOutputStream) out).getFD().sync();
				} catch (IOException e) {
					// just ignore this Exception
					//Debug
					e.printStackTrace();
				}
				super.close();
			} finally {
				reliable.closeOutputFile();
				reliable = null;
			}
		}
	}

	/**
	 * Override default FilterOutputStream method.
	 */
	public void write(byte[] b) throws IOException {
		this.write(b, 0, b.length);
	}

	/**
	 * Override default FilterOutputStream method.
	 */
	public void write(byte[] b, int off, int len) throws IOException {
		out.write(b, off, len);
		crc.update(b, off, len);
	}

	/**
	 * Override default FilterOutputStream method.
	 */
	public void write(int b) throws IOException {
		out.write(b);
		crc.update((byte) b);
	}

}

Back to the top