Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/ydp/jai/ForwardSeekableStream.java


1   /*
2    * The contents of this file are subject to the  JAVA ADVANCED IMAGING
3    * SAMPLE INPUT-OUTPUT CODECS AND WIDGET HANDLING SOURCE CODE  License
4    * Version 1.0 (the "License"); You may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    * http://www.sun.com/software/imaging/JAI/index.html
7    *
8    * Software distributed under the License is distributed on an "AS IS"
9    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10   * the License for the specific language governing rights and limitations
11   * under the License. 
12   *
13   * The Original Code is JAVA ADVANCED IMAGING SAMPLE INPUT-OUTPUT CODECS
14   * AND WIDGET HANDLING SOURCE CODE. 
15   * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
16   * Portions created by: _______________________________________
17   * are Copyright (C): _______________________________________
18   * All Rights Reserved.
19   * Contributor(s): _______________________________________
20   */
21  
22  package org.ydp.jai;
23  
24  import java.io.InputStream;
25  import java.io.IOException;
26  
27  /**
28   * A subclass of <code>SeekableStream</code> that may be used
29   * to wrap a regular <code>InputStream</code> efficiently.
30   * Seeking backwards is not supported.
31   *
32   * <p><b> This class is not a committed part of the JAI API.  It may
33   * be removed or changed in future releases of JAI.</b>
34   */
35  public class ForwardSeekableStream extends SeekableStream {
36  
37      /** The source <code>InputStream</code>. */
38      private InputStream src;
39  
40      /** The current position. */
41      long pointer = 0L;
42  
43      /** The marked position. */
44      long markPos = -1L;
45  
46      /** 
47       * Constructs a <code>InputStreamForwardSeekableStream</code> from a
48       * regular <code>InputStream</code>.
49       */
50      public ForwardSeekableStream(InputStream src) {
51          this.src = src;
52      }
53  
54      /** Forwards the request to the real <code>InputStream</code>. */
55      public final int read() throws IOException {
56          int result = src.read();
57          if (result != -1) {
58              ++pointer;
59          }
60          return result;
61      }
62  
63      /** Forwards the request to the real <code>InputStream</code>. */
64      public final int read(byte[] b, int off, int len) throws IOException {
65          int result = src.read(b, off, len);
66          if (result != -1) {
67              pointer += result;
68          }
69          return result;
70      }
71  
72      /** Forwards the request to the real <code>InputStream</code>. */
73      public final long skip(long n) throws IOException {
74          long skipped = src.skip(n);
75          pointer += skipped;
76          return skipped;
77      }
78  
79      /** Forwards the request to the real <code>InputStream</code>. */
80      public final int available() throws IOException {
81          return src.available();
82      }
83  
84      /** Forwards the request to the real <code>InputStream</code>. */
85      public final void close() throws IOException {
86          src.close();
87      }
88  
89      /** Forwards the request to the real <code>InputStream</code>. */
90      public synchronized final void mark(int readLimit) {
91          markPos = pointer;
92          src.mark(readLimit);
93      }
94  
95      /** Forwards the request to the real <code>InputStream</code>. */
96      public synchronized final void reset() throws IOException {
97          if (markPos != -1) {
98              pointer = markPos;
99          }
100         src.reset();
101     }
102 
103     /** Forwards the request to the real <code>InputStream</code>. */
104     public boolean markSupported() {
105         return src.markSupported();
106     }
107 
108     /** Returns <code>false</code> since seking backwards is not supported. */
109     public final boolean canSeekBackwards() {
110         return false;
111     }
112 
113     /** Returns the current position in the stream (bytes read). */
114     public final long getFilePointer() {
115         return (long)pointer;
116     }
117 
118     /**
119      * Seeks forward to the given position in the stream.
120      * If <code>pos</code> is smaller than the current position
121      * as returned by <code>getFilePointer()</code>, nothing
122      * happens.
123      */
124     public final void seek(long pos) throws IOException {
125         while (pos - pointer > 0) {
126             pointer += src.skip(pos - pointer);
127         }
128     }
129 }