KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > localstore > SafeChunkyOutputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.core.internal.localstore;
12
13 import java.io.*;
14
15 /**
16  * Appends data, in chunks, to a file. Each chunk is defined by the moment
17  * the stream is opened (created) and a call to #succeed is made. It is
18  * necessary to use the <code>SafeChunkyInputStream</code> to read its
19  * contents back. The user of this class does not need to know explicitly about
20  * its chunk implementation.
21  * It is only an implementation detail. What really matters to the outside
22  * world is that it tries to keep the file data consistent.
23  * If some data becomes corrupted while writing or later, upon reading
24  * the file, the chunk that contains the corrupted data is skipped.
25  * <p>
26  * Because of this class purpose (keep data consistent), it is important that the
27  * user only calls <code>#succeed</code> when the chunk of data is successfully
28  * written. After this call, the user can continue writing data to the file and it
29  * will not be considered related to the previous chunk. So, if this data is
30  * corrupted, the previous one is still safe.
31  *
32  * @see SafeChunkyInputStream
33  */

34 public class SafeChunkyOutputStream extends FilterOutputStream {
35     protected String JavaDoc filePath;
36     protected boolean isOpen;
37
38     public SafeChunkyOutputStream(File target) throws IOException {
39         this(target.getAbsolutePath());
40     }
41
42     public SafeChunkyOutputStream(String JavaDoc filePath) throws IOException {
43         super(new BufferedOutputStream(new FileOutputStream(filePath, true)));
44         this.filePath = filePath;
45         isOpen = true;
46         beginChunk();
47     }
48
49     protected void beginChunk() throws IOException {
50         write(ILocalStoreConstants.BEGIN_CHUNK);
51     }
52
53     protected void endChunk() throws IOException {
54         write(ILocalStoreConstants.END_CHUNK);
55     }
56
57     protected void open() throws IOException {
58         out = new BufferedOutputStream(new FileOutputStream(filePath, true));
59         isOpen = true;
60         beginChunk();
61     }
62
63     public void succeed() throws IOException {
64         try {
65             endChunk();
66         } finally {
67             isOpen = false;
68             close();
69         }
70     }
71
72     public void write(int b) throws IOException {
73         if (!isOpen)
74             open();
75         super.write(b);
76     }
77 }
78
Popular Tags