Mercurial > projects > dwt-mac
comparison dwt/internal/image/LEDataInputStream.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
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 module dwt.internal.image; | |
12 | |
13 | |
14 import java.io.IOException; | |
15 import java.io.InputStream; | |
16 | |
17 final class LEDataInputStream : InputStream { | |
18 int position; | |
19 InputStream in; | |
20 | |
21 /** | |
22 * The byte array containing the bytes to read. | |
23 */ | |
24 protected byte[] buf; | |
25 | |
26 /** | |
27 * The current position within the byte array <code>buf</code>. A value | |
28 * equal to buf.length indicates no bytes available. A value of | |
29 * 0 indicates the buffer is full. | |
30 */ | |
31 protected int pos; | |
32 | |
33 | |
34 public LEDataInputStream(InputStream input) { | |
35 this(input, 512); | |
36 } | |
37 | |
38 public LEDataInputStream(InputStream input, int bufferSize) { | |
39 this.in = input; | |
40 if (bufferSize > 0) { | |
41 buf = new byte[bufferSize]; | |
42 pos = bufferSize; | |
43 } | |
44 else throw new IllegalArgumentException(); | |
45 } | |
46 | |
47 public void close() throws IOException { | |
48 buf = null; | |
49 if (in !is null) { | |
50 in.close(); | |
51 in = null; | |
52 } | |
53 } | |
54 | |
55 /** | |
56 * Answer how many bytes were read. | |
57 */ | |
58 public int getPosition() { | |
59 return position; | |
60 } | |
61 | |
62 /** | |
63 * Answers how many bytes are available for reading without blocking | |
64 */ | |
65 public int available() throws IOException { | |
66 if (buf is null) throw new IOException(); | |
67 return (buf.length - pos) + in.available(); | |
68 } | |
69 | |
70 /** | |
71 * Answer the next byte of the input stream. | |
72 */ | |
73 public int read() throws IOException { | |
74 if (buf is null) throw new IOException(); | |
75 if (pos < buf.length) { | |
76 position++; | |
77 return (buf[pos++] & 0xFF); | |
78 } | |
79 int c = in.read(); | |
80 if (c !is -1) position++; | |
81 return c; | |
82 } | |
83 | |
84 /** | |
85 * Don't imitate the JDK behaviour of reading a random number | |
86 * of bytes when you can actually read them all. | |
87 */ | |
88 public int read(byte b[], int off, int len) throws IOException { | |
89 int read = 0, count; | |
90 while (read !is len && (count = readData(b, off, len - read)) !is -1) { | |
91 off += count; | |
92 read += count; | |
93 } | |
94 position += read; | |
95 if (read is 0 && read !is len) return -1; | |
96 return read; | |
97 } | |
98 | |
99 /** | |
100 * Reads at most <code>length</code> bytes from this LEDataInputStream and | |
101 * stores them in byte array <code>buffer</code> starting at <code>offset</code>. | |
102 * <p> | |
103 * Answer the number of bytes actually read or -1 if no bytes were read and | |
104 * end of stream was encountered. This implementation reads bytes from | |
105 * the pushback buffer first, then the target stream if more bytes are required | |
106 * to satisfy <code>count</code>. | |
107 * </p> | |
108 * @param buffer the byte array in which to store the read bytes. | |
109 * @param offset the offset in <code>buffer</code> to store the read bytes. | |
110 * @param length the maximum number of bytes to store in <code>buffer</code>. | |
111 * | |
112 * @return int the number of bytes actually read or -1 if end of stream. | |
113 * | |
114 * @exception java.io.IOException if an IOException occurs. | |
115 */ | |
116 private int readData(byte[] buffer, int offset, int length) throws IOException { | |
117 if (buf is null) throw new IOException(); | |
118 if (offset < 0 || offset > buffer.length || | |
119 length < 0 || (length > buffer.length - offset)) { | |
120 throw new ArrayIndexOutOfBoundsException(); | |
121 } | |
122 | |
123 int cacheCopied = 0; | |
124 int newOffset = offset; | |
125 | |
126 // Are there pushback bytes available? | |
127 int available = buf.length - pos; | |
128 if (available > 0) { | |
129 cacheCopied = (available >= length) ? length : available; | |
130 System.arraycopy(buf, pos, buffer, newOffset, cacheCopied); | |
131 newOffset += cacheCopied; | |
132 pos += cacheCopied; | |
133 } | |
134 | |
135 // Have we copied enough? | |
136 if (cacheCopied is length) return length; | |
137 | |
138 int inCopied = in.read(buffer, newOffset, length - cacheCopied); | |
139 | |
140 if (inCopied > 0) return inCopied + cacheCopied; | |
141 if (cacheCopied is 0) return inCopied; | |
142 return cacheCopied; | |
143 } | |
144 | |
145 /** | |
146 * Answer an integer comprised of the next | |
147 * four bytes of the input stream. | |
148 */ | |
149 public int readInt() throws IOException { | |
150 byte[] buf = new byte[4]; | |
151 read(buf); | |
152 return ((buf[3] & 0xFF) << 24) | | |
153 ((buf[2] & 0xFF) << 16) | | |
154 ((buf[1] & 0xFF) << 8) | | |
155 (buf[0] & 0xFF); | |
156 } | |
157 | |
158 /** | |
159 * Answer a short comprised of the next | |
160 * two bytes of the input stream. | |
161 */ | |
162 public short readShort() throws IOException { | |
163 byte[] buf = new byte[2]; | |
164 read(buf); | |
165 return (short)(((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF)); | |
166 } | |
167 | |
168 /** | |
169 * Push back the entire content of the given buffer <code>b</code>. | |
170 * <p> | |
171 * The bytes are pushed so that they would be read back b[0], b[1], etc. | |
172 * If the push back buffer cannot handle the bytes copied from <code>b</code>, | |
173 * an IOException will be thrown and no byte will be pushed back. | |
174 * </p> | |
175 * | |
176 * @param b the byte array containing bytes to push back into the stream | |
177 * | |
178 * @exception java.io.IOException if the pushback buffer is too small | |
179 */ | |
180 public void unread(byte[] b) throws IOException { | |
181 int length = b.length; | |
182 if (length > pos) throw new IOException(); | |
183 position -= length; | |
184 pos -= length; | |
185 System.arraycopy(b, 0, buf, pos, length); | |
186 } | |
187 } |