KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > rof > FastReadOnlyFile


1 /*
2  * FastReadOnlyFile.java
3  *
4  * Created on 14. Oktober 2005, 20:36
5  */

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

21
22 package de.schlichtherle.io.rof;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.RandomAccessFile JavaDoc;
28
29 /**
30  * A {@link RandomAccessFile} subclassed to implement the {@link ReadOnlyFile}
31  * interface.
32  * This implementation uses an internal file pointer to skip unnecessary
33  * file pointer operations for the operating system.
34  *
35  * @deprecated Depending on the implementation of the J2SE API, this class
36  * most likely does not provide any performance improvement.
37  * Use where necessary only!
38  *
39  * @author Christian Schlichtherle
40  */

41 public class FastReadOnlyFile
42         extends RandomAccessFile JavaDoc
43         implements ReadOnlyFile
44 {
45     /** The current file pointer in the file. */
46     private long fp;
47
48     private boolean closed;
49     
50     public FastReadOnlyFile(File JavaDoc file)
51     throws FileNotFoundException JavaDoc {
52         super(file, "r");
53     }
54
55     public void seek(long pos) throws IOException JavaDoc {
56         if (pos == fp) {
57             if (closed)
58                 throw new IOException JavaDoc("File has been closed!");
59
60             return;
61         }
62
63         super.seek(pos);
64         fp = pos;
65     }
66
67     public int read() throws IOException JavaDoc {
68         final int ret = super.read();
69         if (ret != -1)
70             fp++;
71         return ret;
72     }
73
74     public final int read(byte b[]) throws IOException JavaDoc {
75         return read(b, 0, b.length);
76     }
77
78     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
79         final int ret = super.read(b, off, len);
80         if (ret != -1)
81             fp += ret;
82         return ret;
83     }
84
85     public int skipBytes(int n) throws IOException JavaDoc {
86         final int ret = super.skipBytes(n);
87         fp += ret;
88         return ret;
89     }
90     
91     public void close() throws IOException JavaDoc {
92         super.close();
93         closed = true;
94     }
95 }
96
Popular Tags