KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > util > ChunkInputStream


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ChunkInputStream.java,v 1.2 2002/09/18 06:54:18 per_nyfelt Exp $
8

9 package org.ozoneDB.xml.util;
10
11 import java.io.InputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.Serializable JavaDoc;
14
15
16 /**
17  */

18 public final class ChunkInputStream extends InputStream JavaDoc implements Serializable JavaDoc {
19     
20     /**
21      * The internal buffer array where the data is stored. When necessary,
22      * it may be replaced by another array of
23      * a different size.
24      */

25     protected byte[] buf;
26     
27     /**
28      * The current position in the buffer. This is the index of the next
29      * character to be read from the <code>buf</code> array.
30      * <p>
31      * This value is always in the range <code>0</code>
32      * through <code>count</code>. If it is less
33      * than <code>count</code>, then <code>buf[pos]</code>
34      * is the next byte to be supplied as input;
35      * if it is equal to <code>count</code>, then
36      * the next <code>read</code> or <code>skip</code>
37      * operation will require more bytes to be
38      * read from the contained input stream.
39      *
40      * @see java.io.BufferedInputStream#buf
41      */

42     protected int pos;
43     
44     
45     /**
46      * Creates a <code>BufferedInputStream</code>
47      * and saves its argument, the input stream
48      * <code>in</code>, for later use. An internal
49      * buffer array is created and stored in <code>buf</code>.
50      *
51      * @param data the underlying input stream.
52      */

53     public ChunkInputStream( byte[] data ) {
54         this.buf = data;
55         this.pos = 0;
56     }
57     
58     
59     /**
60      * See
61      * the general contract of the <code>read</code>
62      * method of <code>InputStream</code>.
63      *
64      * @return the next byte of data, or <code>-1</code> if the end of the
65      * stream is reached.
66      * @exception IOException if an I/O error occurs.
67      * @see java.io.FilterInputStream#in
68      */

69     public synchronized int read() throws IOException JavaDoc {
70         if (this.pos >= this.buf.length) {
71             return -1;
72         }
73
74         return this.buf[this.pos++] & 0xff;
75     }
76     
77     
78     /**
79      * Reads bytes from this byte-input stream into the specified byte array,
80      * starting at the given offset.
81      *
82      * @param b destination buffer.
83      * @param off offset at which to start storing bytes.
84      * @param len maximum number of bytes to read.
85      * @return the number of bytes read, or <code>-1</code> if the end of
86      * the stream has been reached.
87      * @exception IOException if an I/O error occurs.
88      */

89     public synchronized final int read( byte[] b, int off, int len ) throws IOException JavaDoc {
90         if (this.pos >= this.buf.length) {
91             return -1;
92         }
93         
94         if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) {
95             throw new IndexOutOfBoundsException JavaDoc();
96         } else {
97             if (len == 0) {
98                 return 0;
99             }
100         }
101         
102         int toRead = this.buf.length - this.pos;
103         len = toRead <= len ? toRead : len;
104         System.arraycopy( this.buf, this.pos, b, off, len );
105         
106         this.pos += len;
107
108         return len;
109     }
110     
111     
112     /**
113      * See the general contract of the <code>skip</code>
114      * method of <code>InputStream</code>.
115      *
116      * @param n the number of bytes to be skipped.
117      * @return the actual number of bytes skipped.
118      * @exception IOException if an I/O error occurs.
119      */

120     public synchronized final long skip( long n ) throws IOException JavaDoc {
121         int toRead = this.buf.length - this.pos;
122         n = toRead <= n ? toRead : n;
123         pos += n;
124         return n;
125     }
126     
127     
128     /**
129      * Returns the number of bytes that can be read from this input
130      * stream without blocking.
131      *
132      * @return the number of bytes that can be read from this input
133      * stream without blocking.
134      * @exception IOException if an I/O error occurs.
135      */

136     public synchronized final int available() throws IOException JavaDoc {
137         return this.buf.length - pos;
138     }
139     
140     
141     /**
142      * Tests if this input stream supports the <code>mark</code>
143      * and <code>reset</code> methods. The <code>markSupported</code>
144      * method of <code>ChunkInputStream</code> returns <code>false</code>.
145      *
146      * @return a <code>boolean</code> indicating if this stream type supports
147      * the <code>mark</code> and <code>reset</code> methods.
148      */

149     public final boolean markSupported() {
150         return false;
151     }
152     
153     
154     /**
155      * Closes this input stream and releases any system resources
156      * associated with the stream.
157      *
158      * @exception IOException if an I/O error occurs.
159      */

160     public synchronized final void close() throws IOException JavaDoc {
161         this.buf = null;
162     }
163     
164     
165     /**
166      * Set a new buffer for this input stream.
167      * @param buffer the data buffer
168      */

169     public final void setBuffer( byte[] buffer ) {
170         this.buf = buffer;
171         this.pos = 0;
172     }
173     
174 }
175
Popular Tags