KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > stream > MemoryCacheImageInputStream


1 /*
2  * @(#)MemoryCacheImageInputStream.java 1.23 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.imageio.stream;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.RandomAccessFile JavaDoc;
13
14 import java.util.ArrayList JavaDoc;
15
16 /**
17  * An implementation of <code>ImageInputStream</code> that gets its
18  * input from a regular <code>InputStream</code>. A memory buffer is
19  * used to cache at least the data between the discard position and
20  * the current read position.
21  *
22  * <p> In general, it is preferable to use a
23  * <code>FileCacheImageInputStream</code> when reading from a regular
24  * <code>InputStream</code>. This class is provided for cases where
25  * it is not possible to create a writable temporary file.
26  *
27  * @version 0.5
28  */

29 public class MemoryCacheImageInputStream extends ImageInputStreamImpl JavaDoc {
30
31     private InputStream JavaDoc stream;
32
33     private MemoryCache JavaDoc cache = new MemoryCache JavaDoc();
34
35     /**
36      * Constructs a <code>MemoryCacheImageInputStream</code> that will read
37      * from a given <code>InputStream</code>.
38      *
39      * @param stream an <code>InputStream</code> to read from.
40      *
41      * @exception IllegalArgumentException if <code>stream</code> is
42      * <code>null</code>.
43      */

44     public MemoryCacheImageInputStream(InputStream JavaDoc stream) {
45         if (stream == null) {
46             throw new IllegalArgumentException JavaDoc("stream == null!");
47         }
48         this.stream = stream;
49     }
50
51     public int read() throws IOException JavaDoc {
52         checkClosed();
53         bitOffset = 0;
54         long thisByte = streamPos;
55         long pos = cache.loadFromStream(stream, streamPos+1);
56         if (pos >= streamPos+1) {
57             return cache.read(streamPos++);
58         } else {
59             return -1;
60         }
61     }
62
63     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
64         checkClosed();
65         // Will throw NullPointerException
66
if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
67             throw new IndexOutOfBoundsException JavaDoc
68                 ("off < 0 || len < 0 || off + len > b.length!");
69         }
70
71         bitOffset = 0;
72
73         if (len == 0) {
74             return 0;
75         }
76
77         long pos = cache.loadFromStream(stream, streamPos+len);
78
79         len = (int)(pos - streamPos); // In case stream ended early
80

81         if (len > 0) {
82             cache.read(b, off, len, streamPos);
83             streamPos += len;
84             return len;
85         } else {
86             return -1;
87         }
88     }
89
90     public void flushBefore(long pos) throws IOException JavaDoc {
91         super.flushBefore(pos);
92         cache.disposeBefore(pos);
93     }
94
95     /**
96      * Returns <code>true</code> since this
97      * <code>ImageInputStream</code> caches data in order to allow
98      * seeking backwards.
99      *
100      * @return <code>true</code>.
101      *
102      * @see #isCachedMemory
103      * @see #isCachedFile
104      */

105     public boolean isCached() {
106         return true;
107     }
108
109     /**
110      * Returns <code>false</code> since this
111      * <code>ImageInputStream</code> does not maintain a file cache.
112      *
113      * @return <code>false</code>.
114      *
115      * @see #isCached
116      * @see #isCachedMemory
117      */

118     public boolean isCachedFile() {
119         return false;
120     }
121
122     /**
123      * Returns <code>true</code> since this
124      * <code>ImageInputStream</code> maintains a main memory cache.
125      *
126      * @return <code>true</code>.
127      *
128      * @see #isCached
129      * @see #isCachedFile
130      */

131     public boolean isCachedMemory() {
132         return true;
133     }
134
135     /**
136      * Closes this <code>MemoryCacheImageInputStream</code>, freeing
137      * the cache. The source <code>InputStream</code> is not closed.
138      */

139     public void close() throws IOException JavaDoc {
140         super.close();
141         cache.reset();
142         stream = null;
143     }
144     
145 }
146
Popular Tags