KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ReadOnlyFileInputStream.java
3  *
4  * Created on 12. Dezember 2005, 17:23
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.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 /**
30  * An adapter class turning a provided {@link ReadOnlyFile} into
31  * an {@link InputStream}.
32  * Note that this stream supports marking.
33  * Note that any of the methods in this class throw a
34  * {@link NullPointerException} if {@link #rof} hasn't been initialized.
35  *
36  * @author Christian Schlichtherle
37  * @version @version@
38  * @since TrueZIP 6.4 Support for marking.
39  */

40 public class ReadOnlyFileInputStream extends InputStream JavaDoc {
41
42     /**
43      * The underlying {@link ReadOnlyFile}.
44      * Any of the methods in this class throw a {@link NullPointerException}
45      * if this hasn't been initialized.
46      */

47     protected ReadOnlyFile rof;
48
49     /**
50      * The position of the last mark.
51      * Initialized to <code>-1</code> to indicate that no mark has been set.
52      */

53     private long mark = -1;
54
55     /**
56      * Adapts the given <code>ReadOnlyFile</code>.
57      *
58      * @param rof The underlying <code>ReadOnlyFile</code>. May be
59      * <code>null</code>, but must be initialized before any method
60      * of this class can be used.
61      */

62     public ReadOnlyFileInputStream(ReadOnlyFile rof) {
63         this.rof = rof;
64     }
65
66     public int read() throws IOException JavaDoc {
67         return rof.read();
68     }
69
70     public int read(byte[] b) throws IOException JavaDoc {
71         return rof.read(b);
72     }
73
74     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
75         return rof.read(b, off, len);
76     }
77
78     public long skip(long n) throws IOException JavaDoc {
79         if (n > Integer.MAX_VALUE)
80             throw new IllegalArgumentException JavaDoc("Too many bytes to skip!");
81         return rof.skipBytes((int) n);
82     }
83
84     public int available() throws IOException JavaDoc {
85         final long avail = rof.length() - rof.getFilePointer();
86         return avail > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) avail;
87     }
88
89     public void close() throws IOException JavaDoc {
90         rof.close();
91     }
92
93     public void mark(int readlimit) {
94         try {
95             mark = rof.getFilePointer();
96         } catch (IOException JavaDoc failure) {
97             Logger.getLogger(ReadOnlyFileInputStream.class.getName())
98                 .log(Level.WARNING, "mark()/reset() not supported!", failure);
99             mark = -2;
100         }
101     }
102
103     public void reset() throws IOException JavaDoc {
104         if (mark < 0)
105             throw new IOException JavaDoc(mark == -1
106                     ? "No mark set!"
107                     : "mark()/reset() not supported by underlying ReadOnlyFile!");
108         rof.seek(mark);
109     }
110
111     public boolean markSupported() {
112         try {
113             rof.seek(rof.getFilePointer());
114             return true;
115         } catch (IOException JavaDoc failure) {
116             return false;
117         }
118     }
119 }
120
Popular Tags