KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 /**
17  * A ReliableFile FileInputStream replacement class.
18  * This class can be used just like FileInputStream. The class
19  * is in partnership with ReliableFileOutputStream to avoid losing
20  * file data by using multiple files.
21  *
22  * @see ReliableFileOutputStream
23  */

24 public class ReliableFileInputStream extends FilterInputStream {
25     /**
26      * ReliableFile object for this file.
27      */

28     private ReliableFile reliable;
29
30     /**
31      * size of crc and signature
32      */

33     private int sigSize;
34
35     /**
36      * current position reading from file
37      */

38     private int readPos;
39
40     /**
41      * total file length available for reading
42      */

43     private int length;
44
45     /**
46      * Constructs a new ReliableFileInputStream on the file named <code>name</code>. If the
47      * file does not exist, the <code>FileNotFoundException</code> is thrown.
48      * The <code>name</code> may be absolute or relative
49      * to the System property <code>"user.dir"</code>.
50      *
51      * @param name the file on which to stream reads.
52      * @exception java.io.IOException If an error occurs opening the file.
53      */

54     public ReliableFileInputStream(String JavaDoc name) throws IOException {
55         this(ReliableFile.getReliableFile(name), ReliableFile.GENERATION_LATEST, ReliableFile.OPEN_BEST_AVAILABLE);
56     }
57
58     /**
59      * Constructs a new ReliableFileInputStream on the File <code>file</code>. If the
60      * file does not exist, the <code>FileNotFoundException</code> is thrown.
61      *
62      * @param file the File on which to stream reads.
63      * @exception java.io.IOException If an error occurs opening the file.
64      */

65     public ReliableFileInputStream(File file) throws IOException {
66         this(ReliableFile.getReliableFile(file), ReliableFile.GENERATION_LATEST, ReliableFile.OPEN_BEST_AVAILABLE);
67     }
68
69     /**
70      * Constructs a new ReliableFileInputStream on the File <code>file</code>. If the
71      * file does not exist, the <code>FileNotFoundException</code> is thrown.
72      *
73      * @param file the File on which to stream reads.
74      * @param generation a specific generation requested.
75      * @param openMask mask used to open data.
76      * are invalid (corrupt, missing, etc).
77      * @exception java.io.IOException If an error occurs opening the file.
78      */

79     public ReliableFileInputStream(File file, int generation, int openMask) throws IOException {
80         this(ReliableFile.getReliableFile(file), generation, openMask);
81     }
82
83     /**
84      *
85      * @param reliable The ReliableFile on which to read.
86      * @param generation a specific generation requested.
87      * @param openMask mask used to open data.
88      * are invalid (corrupt, missing, etc).
89      * @throws IOException If an error occurs opening the file.
90      */

91     private ReliableFileInputStream(ReliableFile reliable, int generation, int openMask) throws IOException {
92         super(reliable.getInputStream(generation, openMask));
93
94         this.reliable = reliable;
95         sigSize = reliable.getSignatureSize();
96         readPos = 0;
97         length = super.available();
98         if (sigSize > length)
99             length = 0; // shouldn't ever happen
100
else
101             length -= sigSize;
102     }
103
104     /**
105      * Closes this input stream and releases any system resources associated
106      * with the stream.
107      *
108      * @exception java.io.IOException If an error occurs closing the file.
109      */

110     public synchronized void close() throws IOException {
111         if (reliable != null) {
112             try {
113                 super.close();
114             } finally {
115                 reliable.closeInputFile();
116                 reliable = null;
117             }
118         }
119     }
120
121     /**
122      * Override default FilterInputStream method.
123      * @see FilterInputStream#read(byte[], int, int)
124      */

125     public synchronized int read(byte b[], int off, int len) throws IOException {
126         if (readPos >= length) {
127             return -1;
128         }
129         int num = super.read(b, off, len);
130
131         if (num != -1) {
132             if (num + readPos > length) {
133                 num = length - readPos;
134             }
135             readPos += num;
136         }
137         return num;
138     }
139
140     /**
141      * Override default FilterInputStream method.
142      * @see FilterInputStream#read(byte[])
143      */

144     public synchronized int read(byte b[]) throws IOException {
145         return read(b, 0, b.length);
146     }
147
148     /**
149      * Override default FilterInputStream method.
150      * @see FilterInputStream#read()
151      */

152     public synchronized int read() throws IOException {
153         if (readPos >= length) {
154             return -1;
155         }
156         int num = super.read();
157
158         if (num != -1) {
159             readPos++;
160         }
161         return num;
162     }
163
164     /**
165      * Override default available method.
166      * @see FilterInputStream#available()
167      */

168     public synchronized int available() throws IOException {
169         if (readPos < length) // just in case
170
return (length - readPos);
171         return 0;
172     }
173
174     /**
175      * Override default skip method.
176      * @see FilterInputStream#skip(long)
177      */

178     public synchronized long skip(long n) throws IOException {
179         long len = super.skip(n);
180         if (readPos + len > length)
181             len = length - readPos;
182         readPos += len;
183         return len;
184     }
185
186     /**
187      * Override default markSupported method.
188      * @see FilterInputStream#markSupported()
189      */

190     public boolean markSupported() {
191         return false;
192     }
193
194     /**
195      * Override default mark method.
196      * @see FilterInputStream#mark(int)
197      */

198     public void mark(int readlimit) {
199         //ignore
200
}
201
202     /**
203      * Override default reset method.
204      * @see FilterInputStream#reset()
205      */

206     public void reset() throws IOException {
207         throw new IOException("reset not supported."); //$NON-NLS-1$
208
}
209 }
210
Popular Tags