KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > LEDataInputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.internal.image;
12
13
14 import java.io.*;
15
16 final class LEDataInputStream extends InputStream {
17     int position;
18     InputStream in;
19
20     /**
21      * The byte array containing the bytes to read.
22      */

23     protected byte[] buf;
24     
25     /**
26      * The current position within the byte array <code>buf</code>. A value
27      * equal to buf.length indicates no bytes available. A value of
28      * 0 indicates the buffer is full.
29      */

30     protected int pos;
31     
32
33     public LEDataInputStream(InputStream input) {
34         this(input, 512);
35     }
36     
37     public LEDataInputStream(InputStream input, int bufferSize) {
38         this.in = input;
39         if (bufferSize > 0) {
40             buf = new byte[bufferSize];
41             pos = bufferSize;
42         }
43         else throw new IllegalArgumentException JavaDoc();
44     }
45     
46     public void close() throws IOException {
47         buf = null;
48         if (in != null) {
49             in.close();
50             in = null;
51         }
52     }
53     
54     /**
55      * Answer how many bytes were read.
56      */

57     public int getPosition() {
58         return position;
59     }
60     
61     /**
62      * Answers how many bytes are available for reading without blocking
63      */

64     public int available() throws IOException {
65         if (buf == null) throw new IOException();
66         return (buf.length - pos) + in.available();
67     }
68     
69     /**
70      * Answer the next byte of the input stream.
71      */

72     public int read() throws IOException {
73         if (buf == null) throw new IOException();
74         if (pos < buf.length) {
75             position++;
76             return (buf[pos++] & 0xFF);
77         }
78         int c = in.read();
79         if (c != -1) position++;
80         return c;
81     }
82     
83     /**
84      * Don't imitate the JDK behaviour of reading a random number
85      * of bytes when you can actually read them all.
86      */

87     public int read(byte b[], int off, int len) throws IOException {
88         int read = 0, count;
89         while (read != len && (count = readData(b, off, len - read)) != -1) {
90             off += count;
91             read += count;
92         }
93         position += read;
94         if (read == 0 && read != len) return -1;
95         return read;
96     }
97     
98     /**
99      * Reads at most <code>length</code> bytes from this LEDataInputStream and
100      * stores them in byte array <code>buffer</code> starting at <code>offset</code>.
101      * <p>
102      * Answer the number of bytes actually read or -1 if no bytes were read and
103      * end of stream was encountered. This implementation reads bytes from
104      * the pushback buffer first, then the target stream if more bytes are required
105      * to satisfy <code>count</code>.
106      * </p>
107      * @param buffer the byte array in which to store the read bytes.
108      * @param offset the offset in <code>buffer</code> to store the read bytes.
109      * @param length the maximum number of bytes to store in <code>buffer</code>.
110      *
111      * @return int the number of bytes actually read or -1 if end of stream.
112      *
113      * @exception java.io.IOException if an IOException occurs.
114      */

115     private int readData(byte[] buffer, int offset, int length) throws IOException {
116         if (buf == null) throw new IOException();
117         if (offset < 0 || offset > buffer.length ||
118             length < 0 || (length > buffer.length - offset)) {
119             throw new ArrayIndexOutOfBoundsException JavaDoc();
120             }
121                 
122         int cacheCopied = 0;
123         int newOffset = offset;
124     
125         // Are there pushback bytes available?
126
int available = buf.length - pos;
127         if (available > 0) {
128             cacheCopied = (available >= length) ? length : available;
129             System.arraycopy(buf, pos, buffer, newOffset, cacheCopied);
130             newOffset += cacheCopied;
131             pos += cacheCopied;
132         }
133     
134         // Have we copied enough?
135
if (cacheCopied == length) return length;
136
137         int inCopied = in.read(buffer, newOffset, length - cacheCopied);
138
139         if (inCopied > 0) return inCopied + cacheCopied;
140         if (cacheCopied == 0) return inCopied;
141         return cacheCopied;
142     }
143     
144     /**
145      * Answer an integer comprised of the next
146      * four bytes of the input stream.
147      */

148     public int readInt() throws IOException {
149         byte[] buf = new byte[4];
150         read(buf);
151         return ((((((buf[3] & 0xFF) << 24) |
152             (buf[2] & 0xFF)) << 16) |
153             (buf[1] & 0xFF)) << 8) |
154             (buf[0] & 0xFF);
155     }
156     
157     /**
158      * Answer a short comprised of the next
159      * two bytes of the input stream.
160      */

161     public short readShort() throws IOException {
162         byte[] buf = new byte[2];
163         read(buf);
164         return (short)(((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF));
165     }
166     
167     /**
168      * Push back the entire content of the given buffer <code>b</code>.
169      * <p>
170      * The bytes are pushed so that they would be read back b[0], b[1], etc.
171      * If the push back buffer cannot handle the bytes copied from <code>b</code>,
172      * an IOException will be thrown and no byte will be pushed back.
173      * </p>
174      *
175      * @param b the byte array containing bytes to push back into the stream
176      *
177      * @exception java.io.IOException if the pushback buffer is too small
178      */

179     public void unread(byte[] b) throws IOException {
180         int length = b.length;
181         if (length > pos) throw new IOException();
182         position -= length;
183         pos -= length;
184         System.arraycopy(b, 0, buf, pos, length);
185     }
186 }
187
Popular Tags