KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > codec > ForwardSeekableStream


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

18 package org.apache.batik.ext.awt.image.codec;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 /**
24  * A subclass of <code>SeekableStream</code> that may be used
25  * to wrap a regular <code>InputStream</code> efficiently.
26  * Seeking backwards is not supported.
27  *
28  */

29 public class ForwardSeekableStream extends SeekableStream {
30
31     /** The source <code>InputStream</code>. */
32     private InputStream JavaDoc src;
33
34     /** The current position. */
35     long pointer = 0L;
36
37     /** The marked position. */
38     long markPos = -1L;
39
40     /**
41      * Constructs a <code>InputStreamForwardSeekableStream</code> from a
42      * regular <code>InputStream</code>.
43      */

44     public ForwardSeekableStream(InputStream JavaDoc src) {
45         this.src = src;
46     }
47
48     /** Forwards the request to the real <code>InputStream</code>. */
49     public final int read() throws IOException JavaDoc {
50         int result = src.read();
51         if (result != -1) {
52             ++pointer;
53         }
54         return result;
55     }
56
57     /** Forwards the request to the real <code>InputStream</code>. */
58     public final int read(byte[] b, int off, int len) throws IOException JavaDoc {
59         int result = src.read(b, off, len);
60         if (result != -1) {
61             pointer += result;
62         }
63         return result;
64     }
65
66     /** Forwards the request to the real <code>InputStream</code>. */
67     public final long skip(long n) throws IOException JavaDoc {
68         long skipped = src.skip(n);
69         pointer += skipped;
70         return skipped;
71     }
72
73     /** Forwards the request to the real <code>InputStream</code>. */
74     public final int available() throws IOException JavaDoc {
75         return src.available();
76     }
77
78     /** Forwards the request to the real <code>InputStream</code>. */
79     public final void close() throws IOException JavaDoc {
80         src.close();
81     }
82
83     /** Forwards the request to the real <code>InputStream</code>. */
84     public synchronized final void mark(int readLimit) {
85         markPos = pointer;
86         src.mark(readLimit);
87     }
88
89     /** Forwards the request to the real <code>InputStream</code>. */
90     public synchronized final void reset() throws IOException JavaDoc {
91         if (markPos != -1) {
92             pointer = markPos;
93         }
94         src.reset();
95     }
96
97     /** Forwards the request to the real <code>InputStream</code>. */
98     public boolean markSupported() {
99         return src.markSupported();
100     }
101
102     /** Returns <code>false</code> since seking backwards is not supported. */
103     public final boolean canSeekBackwards() {
104         return false;
105     }
106
107     /** Returns the current position in the stream (bytes read). */
108     public final long getFilePointer() {
109         return pointer;
110     }
111
112     /**
113      * Seeks forward to the given position in the stream.
114      * If <code>pos</code> is smaller than the current position
115      * as returned by <code>getFilePointer()</code>, nothing
116      * happens.
117      */

118     public final void seek(long pos) throws IOException JavaDoc {
119         while (pos - pointer > 0) {
120             pointer += src.skip(pos - pointer);
121         }
122     }
123 }
124
Popular Tags