001 /*
002 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.imageio.stream;
027
028 import java.util.ArrayList;
029 import java.io.InputStream;
030 import java.io.OutputStream;
031 import java.io.IOException;
032
033 /**
034 * Package-visible class consolidating common code for
035 * <code>MemoryCacheImageInputStream</code> and
036 * <code>MemoryCacheImageOutputStream</code>.
037 * This class keeps an <code>ArrayList</code> of 8K blocks,
038 * loaded sequentially. Blocks may only be disposed of
039 * from the index 0 forward. As blocks are freed, the
040 * corresponding entries in the array list are set to
041 * <code>null</code>, but no compacting is performed.
042 * This allows the index for each block to never change,
043 * and the length of the cache is always the same as the
044 * total amount of data ever cached. Cached data is
045 * therefore always contiguous from the point of last
046 * disposal to the current length.
047 *
048 * <p> The total number of blocks resident in the cache must not
049 * exceed <code>Integer.MAX_VALUE</code>. In practice, the limit of
050 * available memory will be exceeded long before this becomes an
051 * issue, since a full cache would contain 8192*2^31 = 16 terabytes of
052 * data.
053 *
054 * A <code>MemoryCache</code> may be reused after a call
055 * to <code>reset()</code>.
056 */
057 class MemoryCache {
058
059 private static final int BUFFER_LENGTH = 8192;
060
061 private ArrayList cache = new ArrayList();
062
063 private long cacheStart = 0L;
064
065 /**
066 * The largest position ever written to the cache.
067 */
068 private long length = 0L;
069
070 private byte[] getCacheBlock(long blockNum) throws IOException {
071 long blockOffset = blockNum - cacheStart;
072 if (blockOffset > Integer.MAX_VALUE) {
073 // This can only happen when the cache hits 16 terabytes of
074 // contiguous data...
075 throw new IOException("Cache addressing limit exceeded!");
076 }
077 return (byte[]) cache.get((int) blockOffset);
078 }
079
080 /**
081 * Ensures that at least <code>pos</code> bytes are cached,
082 * or the end of the source is reached. The return value
083 * is equal to the smaller of <code>pos</code> and the
084 * length of the source.
085 */
086 public long loadFromStream(InputStream stream, long pos)
087 throws IOException {
088 // We've already got enough data cached
089 if (pos < length) {
090 return pos;
091 }
092
093 int offset = (int) (length % BUFFER_LENGTH);
094 byte[] buf = null;
095
096 long len = pos - length;
097 if (offset != 0) {
098 buf = getCacheBlock(length / BUFFER_LENGTH);
099 }
100
101 while (len > 0) {
102 if (buf == null) {
103 try {
104 buf = new byte[BUFFER_LENGTH];
105 } catch (OutOfMemoryError e) {
106 throw new IOException("No memory left for cache!");
107 }
108 offset = 0;
109 }
110
111 int left = BUFFER_LENGTH - offset;
112 int nbytes = (int) Math.min(len, (long) left);
113 nbytes = stream.read(buf, offset, nbytes);
114 if (nbytes == -1) {
115 return length; // EOF
116 }
117
118 if (offset == 0) {
119 cache.add(buf);
120 }
121
122 len -= nbytes;
123 length += nbytes;
124 offset += nbytes;
125
126 if (offset >= BUFFER_LENGTH) {
127 // we've filled the current buffer, so a new one will be
128 // allocated next time around (and offset will be reset to 0)
129 buf = null;
130 }
131 }
132
133 return pos;
134 }
135
136 /**
137 * Writes out a portion of the cache to an <code>OutputStream</code>.
138 * This method preserves no state about the output stream, and does
139 * not dispose of any blocks containing bytes written. To dispose
140 * blocks, use {@link #disposeBefore <code>disposeBefore()</code>}.
141 *
142 * @exception IndexOutOfBoundsException if any portion of
143 * the requested data is not in the cache (including if <code>pos</code>
144 * is in a block already disposed), or if either <code>pos</code> or
145 * <code>len</code> is < 0.
146 */
147 public void writeToStream(OutputStream stream, long pos, long len)
148 throws IOException {
149 if (pos + len > length) {
150 throw new IndexOutOfBoundsException("Argument out of cache");
151 }
152 if ((pos < 0) || (len < 0)) {
153 throw new IndexOutOfBoundsException("Negative pos or len");
154 }
155 if (len == 0) {
156 return;
157 }
158
159 long bufIndex = pos / BUFFER_LENGTH;
160 if (bufIndex < cacheStart) {
161 throw new IndexOutOfBoundsException("pos already disposed");
162 }
163 int offset = (int) (pos % BUFFER_LENGTH);
164
165 byte[] buf = getCacheBlock(bufIndex++);
166 while (len > 0) {
167 if (buf == null) {
168 buf = getCacheBlock(bufIndex++);
169 offset = 0;
170 }
171 int nbytes = (int) Math.min(len,
172 (long) (BUFFER_LENGTH - offset));
173 stream.write(buf, offset, nbytes);
174 buf = null;
175 len -= nbytes;
176 }
177 }
178
179 /**
180 * Ensure that there is space to write a byte at the given position.
181 */
182 private void pad(long pos) throws IOException {
183 long currIndex = cacheStart + cache.size() - 1;
184 long lastIndex = pos / BUFFER_LENGTH;
185 long numNewBuffers = lastIndex - currIndex;
186 for (long i = 0; i < numNewBuffers; i++) {
187 try {
188 cache.add(new byte[BUFFER_LENGTH]);
189 } catch (OutOfMemoryError e) {
190 throw new IOException("No memory left for cache!");
191 }
192 }
193 }
194
195 /**
196 * Overwrites and/or appends the cache from a byte array.
197 * The length of the cache will be extended as needed to hold
198 * the incoming data.
199 *
200 * @param b an array of bytes containing data to be written.
201 * @param off the starting offset withing the data array.
202 * @param len the number of bytes to be written.
203 * @param pos the cache position at which to begin writing.
204 *
205 * @exception NullPointerException if <code>b</code> is <code>null</code>.
206 * @exception IndexOutOfBoundsException if <code>off</code>,
207 * <code>len</code>, or <code>pos</code> are negative,
208 * or if <code>off+len > b.length</code>.
209 */
210 public void write(byte[] b, int off, int len, long pos)
211 throws IOException {
212 if (b == null) {
213 throw new NullPointerException("b == null!");
214 }
215 // Fix 4430357 - if off + len < 0, overflow occurred
216 if ((off < 0) || (len < 0) || (pos < 0)
217 || (off + len > b.length) || (off + len < 0)) {
218 throw new IndexOutOfBoundsException();
219 }
220
221 // Ensure there is space for the incoming data
222 long lastPos = pos + len - 1;
223 if (lastPos >= length) {
224 pad(lastPos);
225 length = lastPos + 1;
226 }
227
228 // Copy the data into the cache, block by block
229 int offset = (int) (pos % BUFFER_LENGTH);
230 while (len > 0) {
231 byte[] buf = getCacheBlock(pos / BUFFER_LENGTH);
232 int nbytes = Math.min(len, BUFFER_LENGTH - offset);
233 System.arraycopy(b, off, buf, offset, nbytes);
234
235 pos += nbytes;
236 off += nbytes;
237 len -= nbytes;
238 offset = 0; // Always after the first time
239 }
240 }
241
242 /**
243 * Overwrites or appends a single byte to the cache.
244 * The length of the cache will be extended as needed to hold
245 * the incoming data.
246 *
247 * @param b an <code>int</code> whose 8 least significant bits
248 * will be written.
249 * @param pos the cache position at which to begin writing.
250 *
251 * @exception IndexOutOfBoundsException if <code>pos</code> is negative.
252 */
253 public void write(int b, long pos) throws IOException {
254 if (pos < 0) {
255 throw new ArrayIndexOutOfBoundsException("pos < 0");
256 }
257
258 // Ensure there is space for the incoming data
259 if (pos >= length) {
260 pad(pos);
261 length = pos + 1;
262 }
263
264 // Insert the data.
265 byte[] buf = getCacheBlock(pos / BUFFER_LENGTH);
266 int offset = (int) (pos % BUFFER_LENGTH);
267 buf[offset] = (byte) b;
268 }
269
270 /**
271 * Returns the total length of data that has been cached,
272 * regardless of whether any early blocks have been disposed.
273 * This value will only ever increase.
274 */
275 public long getLength() {
276 return length;
277 }
278
279 /**
280 * Returns the single byte at the given position, as an
281 * <code>int</code>. Returns -1 if this position has
282 * not been cached or has been disposed.
283 */
284 public int read(long pos) throws IOException {
285 if (pos >= length) {
286 return -1;
287 }
288
289 byte[] buf = getCacheBlock(pos / BUFFER_LENGTH);
290 if (buf == null) {
291 return -1;
292 }
293
294 return buf[(int) (pos % BUFFER_LENGTH)] & 0xff;
295 }
296
297 /**
298 * Copy <code>len</code> bytes from the cache, starting
299 * at cache position <code>pos</code>, into the array
300 * <code>b</code> at offset <code>off</code>.
301 *
302 * @exception NullPointerException if b is <code>null</code>
303 * @exception IndexOutOfBoundsException if <code>off</code>,
304 * <code>len</code> or <code>pos</code> are negative or if
305 * <code>off + len > b.length</code> or if any portion of the
306 * requested data is not in the cache (including if
307 * <code>pos</code> is in a block that has already been disposed).
308 */
309 public void read(byte[] b, int off, int len, long pos)
310 throws IOException {
311 if (b == null) {
312 throw new NullPointerException("b == null!");
313 }
314 // Fix 4430357 - if off + len < 0, overflow occurred
315 if ((off < 0) || (len < 0) || (pos < 0)
316 || (off + len > b.length) || (off + len < 0)) {
317 throw new IndexOutOfBoundsException();
318 }
319 if (pos + len > length) {
320 throw new IndexOutOfBoundsException();
321 }
322
323 long index = pos / BUFFER_LENGTH;
324 int offset = (int) pos % BUFFER_LENGTH;
325 while (len > 0) {
326 int nbytes = Math.min(len, BUFFER_LENGTH - offset);
327 byte[] buf = getCacheBlock(index++);
328 System.arraycopy(buf, offset, b, off, nbytes);
329
330 len -= nbytes;
331 off += nbytes;
332 offset = 0; // Always after the first time
333 }
334 }
335
336 /**
337 * Free the blocks up to the position <code>pos</code>.
338 * The byte at <code>pos</code> remains available.
339 *
340 * @exception IndexOutOfBoundsException if <code>pos</code>
341 * is in a block that has already been disposed.
342 */
343 public void disposeBefore(long pos) {
344 long index = pos / BUFFER_LENGTH;
345 if (index < cacheStart) {
346 throw new IndexOutOfBoundsException("pos already disposed");
347 }
348 long numBlocks = Math.min(index - cacheStart, cache.size());
349 for (long i = 0; i < numBlocks; i++) {
350 cache.remove(0);
351 }
352 this .cacheStart = index;
353 }
354
355 /**
356 * Erase the entire cache contents and reset the length to 0.
357 * The cache object may subsequently be reused as though it had just
358 * been allocated.
359 */
360 public void reset() {
361 cache.clear();
362 cacheStart = 0;
363 length = 0L;
364 }
365 }
|