001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ForwardSeekableStream.java 496556 2007-01-16 00:59:48Z cam $ */
019:
020: package org.apache.xmlgraphics.image.codec.util;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024:
025: /**
026: * A subclass of <code>SeekableStream</code> that may be used
027: * to wrap a regular <code>InputStream</code> efficiently.
028: * Seeking backwards is not supported.
029: *
030: */
031: public class ForwardSeekableStream extends SeekableStream {
032:
033: /** The source <code>InputStream</code>. */
034: private InputStream src;
035:
036: /** The current position. */
037: long pointer = 0L;
038:
039: /** The marked position. */
040: long markPos = -1L;
041:
042: /**
043: * Constructs a <code>InputStreamForwardSeekableStream</code> from a
044: * regular <code>InputStream</code>.
045: */
046: public ForwardSeekableStream(InputStream src) {
047: this .src = src;
048: }
049:
050: /** Forwards the request to the real <code>InputStream</code>. */
051: public final int read() throws IOException {
052: int result = src.read();
053: if (result != -1) {
054: ++pointer;
055: }
056: return result;
057: }
058:
059: /** Forwards the request to the real <code>InputStream</code>. */
060: public final int read(byte[] b, int off, int len)
061: throws IOException {
062: int result = src.read(b, off, len);
063: if (result != -1) {
064: pointer += result;
065: }
066: return result;
067: }
068:
069: /** Forwards the request to the real <code>InputStream</code>. */
070: public final long skip(long n) throws IOException {
071: long skipped = src.skip(n);
072: pointer += skipped;
073: return skipped;
074: }
075:
076: /** Forwards the request to the real <code>InputStream</code>. */
077: public final int available() throws IOException {
078: return src.available();
079: }
080:
081: /** Forwards the request to the real <code>InputStream</code>. */
082: public final void close() throws IOException {
083: src.close();
084: }
085:
086: /** Forwards the request to the real <code>InputStream</code>. */
087: public final synchronized void mark(int readLimit) {
088: markPos = pointer;
089: src.mark(readLimit);
090: }
091:
092: /** Forwards the request to the real <code>InputStream</code>. */
093: public final synchronized void reset() throws IOException {
094: if (markPos != -1) {
095: pointer = markPos;
096: }
097: src.reset();
098: }
099:
100: /** Forwards the request to the real <code>InputStream</code>. */
101: public boolean markSupported() {
102: return src.markSupported();
103: }
104:
105: /** Returns <code>false</code> since seking backwards is not supported. */
106: public final boolean canSeekBackwards() {
107: return false;
108: }
109:
110: /** Returns the current position in the stream (bytes read). */
111: public final long getFilePointer() {
112: return pointer;
113: }
114:
115: /**
116: * Seeks forward to the given position in the stream.
117: * If <code>pos</code> is smaller than the current position
118: * as returned by <code>getFilePointer()</code>, nothing
119: * happens.
120: */
121: public final void seek(long pos) throws IOException {
122: while (pos - pointer > 0) {
123: pointer += src.skip(pos - pointer);
124: }
125: }
126: }
|