1 /*
2 * $Id: ByteBufferInputStream.java,v 1.1 2006/09/14 14:34:52 oeuillot Exp $
3 */
4 package org.rcfaces.core.internal.lang;
5
6 import java.io.InputStream;
7
8 /**
9 *
10 * @author Java team (latest modification by $Author: oeuillot $)
11 * @version $Revision: 1.1 $ $Date: 2006/09/14 14:34:52 $
12 */
13 public final class ByteBufferInputStream extends InputStream {
14 private static final String REVISION = "$Revision: 1.1 $";
15
16 /**
17 * An array of bytes that was provided by the creator of the stream.
18 * Elements <code>buf[0]</code> through <code>buf[count-1]</code> are
19 * the only bytes that can ever be read from the stream; element
20 * <code>buf[pos]</code> is the next byte to be read.
21 */
22 private byte buf[];
23
24 /**
25 * The index of the next character to read from the input stream buffer.
26 * This value should always be nonnegative and not larger than the value of
27 * <code>count</code>. The next byte to be read from the input stream
28 * buffer will be <code>buf[pos]</code>.
29 */
30 private int pos;
31
32 /**
33 * The index one greater than the last valid character in the input stream
34 * buffer. This value should always be nonnegative and not larger than the
35 * length of <code>buf</code>. It is one greater than the position of the
36 * last byte within <code>buf</code> that can ever be read from the input
37 * stream buffer.
38 */
39 private int count;
40
41 /**
42 * Creates a <code>ByteBufferInputStream</code> so that it uses
43 * <code>buf</code> as its buffer array. The buffer array is not copied.
44 * The initial value of <code>pos</code> is <code>0</code> and the
45 * initial value of <code>count</code> is the length of <code>buf</code>.
46 *
47 * @param buf
48 * the input buffer.
49 */
50 public ByteBufferInputStream(byte buf[]) {
51 this.buf = buf;
52 this.pos = 0;
53 this.count = buf.length;
54 }
55
56 /**
57 * Creates <code>ByteBufferInputStream</code> that uses <code>buf</code>
58 * as its buffer array. The initial value of <code>pos</code> is
59 * <code>offset</code> and the initial value of <code>count</code> is
60 * the minimum of <code>offset+length</code> and <code>buf.length</code>.
61 * The buffer array is not copied. The buffer's mark is set to the specified
62 * offset.
63 *
64 * @param buf
65 * the input buffer.
66 * @param offset
67 * the offset in the buffer of the first byte to read.
68 * @param length
69 * the maximum number of bytes to read from the buffer.
70 */
71 public ByteBufferInputStream(byte buf[], int offset, int length) {
72 this.buf = buf;
73 this.pos = offset;
74 this.count = Math.min(offset + length, buf.length);
75 }
76
77 /**
78 * Reads the next byte of data from this input stream. The value byte is
79 * returned as an <code>int</code> in the range <code>0</code> to
80 * <code>255</code>. If no byte is available because the end of the
81 * stream has been reached, the value <code>-1</code> is returned.
82 * <p>
83 * This <code>read</code> method cannot block.
84 *
85 * @return the next byte of data, or <code>-1</code> if the end of the
86 * stream has been reached.
87 */
88 public int read() {
89 return (pos < count) ? (buf[pos++] & 0xff) : -1;
90 }
91
92 /**
93 * Reads up to <code>len</code> bytes of data into an array of bytes from
94 * this input stream. If <code>pos</code> equals <code>count</code>,
95 * then <code>-1</code> is returned to indicate end of file. Otherwise,
96 * the number <code>k</code> of bytes read is equal to the smaller of
97 * <code>len</code> and <code>count-pos</code>. If <code>k</code> is
98 * positive, then bytes <code>buf[pos]</code> through
99 * <code>buf[pos+k-1]</code> are copied into <code>b[off]</code> through
100 * <code>b[off+k-1]</code> in the manner performed by
101 * <code>System.arraycopy</code>. The value <code>k</code> is added
102 * into <code>pos</code> and <code>k</code> is returned.
103 * <p>
104 * This <code>read</code> method cannot block.
105 *
106 * @param b
107 * the buffer into which the data is read.
108 * @param off
109 * the start offset of the data.
110 * @param len
111 * the maximum number of bytes read.
112 * @return the total number of bytes read into the buffer, or
113 * <code>-1</code> if there is no more data because the end of the
114 * stream has been reached.
115 */
116 public int read(byte b[], int off, int len) {
117 if (b == null) {
118 throw new NullPointerException();
119 }
120
121 if ((off < 0) || (off > b.length) || (len < 0)
122 || ((off + len) > b.length) || ((off + len) < 0)) {
123 throw new IndexOutOfBoundsException();
124 }
125
126 if (pos >= count) {
127 return -1;
128 }
129 if (pos + len > count) {
130 len = count - pos;
131 }
132 if (len <= 0) {
133 return 0;
134 }
135 System.arraycopy(buf, pos, b, off, len);
136 pos += len;
137 return len;
138 }
139
140 public long skip(long n) {
141 if (pos + n > count) {
142 n = count - pos;
143 }
144 if (n < 0) {
145 return 0;
146 }
147 pos += n;
148 return n;
149 }
150
151 public int available() {
152 return count - pos;
153 }
154
155 public void flush() {
156 }
157
158 public void close() {
159 }
160 }