KickJava   Java API By Example, From Geeks To Geeks.

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


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

6 /*
7  * Copyright 2005-2006 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.EOFException JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * <p>
29  * Please note that subclasses of this class often implement their own virtual
30  * file pointer.
31  * Thus, if you would like to access the underlying <code>ReadOnlyFile</code>
32  * again after you have finished working with the
33  * <code>FilteredReadOnlyFile</code>, you should synchronize their file
34  * pointers a'la:
35  * <pre>
36  * ReadOnlyFile rof = new SimpleReadOnlyFile(new File("HelloWorld.java"));
37  * try {
38  * ReadOnlyFile frof = new FilteredReadOnlyFile(rof);
39  * try {
40  * // Do any file input on brof here...
41  * frof.seek(1);
42  * } finally {
43  * // Synchronize the file pointers.
44  * rof.seek(frof.getFilePointer());
45  * }
46  * // This assertion would fail if we hadn't done the file pointer
47  * // synchronization!
48  * assert rof.getFilePointer() == 1;
49  * } finally {
50  * rof.close();
51  * }
52  * </pre>
53  * This does not apply to this base class, however.
54  * <p>
55  * Subclasses implemententing their own virtual file pointer should add a note
56  * referring to this classes Javadoc like this:
57  * <blockquote>
58  * <b>Note:</b> This class implements its own virtual file pointer.
59  * Thus, if you would like to access the underlying <code>ReadOnlyFile</code>
60  * again after you have finished working with an instance of this class,
61  * you should synchronize their file pointers using the pattern as described
62  * in {@link FilterReadOnlyFile}.
63  * </blockquote>
64  *
65  * @author Christian Schlichtherle
66  */

67 public class FilterReadOnlyFile implements ReadOnlyFile {
68
69     /** The read only file to be filtered. */
70     protected ReadOnlyFile rof;
71     
72     /**
73      * Creates a new instance of <tt>FilterReadOnlyFile</tt>,
74      * which filters the given read only file.
75      */

76     public FilterReadOnlyFile(ReadOnlyFile rof) {
77         this.rof = rof;
78     }
79
80     public long length() throws IOException JavaDoc {
81         return rof.length();
82     }
83
84     public long getFilePointer() throws IOException JavaDoc {
85         return rof.getFilePointer();
86     }
87
88     public void seek(long pos) throws IOException JavaDoc {
89         rof.seek(pos);
90     }
91
92     public int read() throws IOException JavaDoc {
93         return rof.read();
94     }
95
96     public int read(byte[] b) throws IOException JavaDoc {
97         return read(b, 0, b.length);
98     }
99
100     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
101         return rof.read(b, off, len);
102     }
103
104     public void readFully(byte[] b) throws IOException JavaDoc {
105         readFully(b, 0, b.length);
106     }
107
108     public void readFully(byte[] buf, int off, int len) throws IOException JavaDoc {
109         int n = 0;
110     do {
111         int count = read(buf, off + n, len - n);
112         if (count < 0)
113         throw new EOFException JavaDoc();
114         n += count;
115     } while (n < len);
116     }
117
118     public int skipBytes(int n) throws IOException JavaDoc {
119         return rof.skipBytes(n);
120     }
121
122     public void close() throws IOException JavaDoc {
123         rof.close();
124     }
125 }
126
Popular Tags