• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package javax.imageio.stream;
19 
20 import java.io.*;
21 
22 /**
23  * The FileCacheImageInputStream class is an implementation of ImageInputStream
24  * which reads from its InputStream and uses a temporary file as a cache.
25  *
26  * @since Android 1.0
27  */
28 public class FileCacheImageInputStream extends ImageInputStreamImpl {
29 
30     /**
31      * The is.
32      */
33     private InputStream is;
34 
35     /**
36      * The file.
37      */
38     private File file;
39 
40     /**
41      * The raf.
42      */
43     private RandomAccessFile raf;
44 
45     /**
46      * Instantiates a new FileCacheImageInputStream from the specified
47      * InputStream and using the specified File as its cache directory.
48      *
49      * @param stream
50      *            the InputStream for reading.
51      * @param cacheDir
52      *            the cache directory where the cache file will be created.
53      * @throws IOException
54      *             if an I/O exception has occurred.
55      */
FileCacheImageInputStream(InputStream stream, File cacheDir)56     public FileCacheImageInputStream(InputStream stream, File cacheDir) throws IOException {
57         if (stream == null) {
58             throw new IllegalArgumentException("stream == null!");
59         }
60         is = stream;
61 
62         if (cacheDir == null || cacheDir.isDirectory()) {
63             file = File.createTempFile(FileCacheImageOutputStream.IIO_TEMP_FILE_PREFIX, null,
64                     cacheDir);
65             file.deleteOnExit();
66         } else {
67             throw new IllegalArgumentException("Not a directory!");
68         }
69 
70         raf = new RandomAccessFile(file, "rw");
71     }
72 
73     @Override
read()74     public int read() throws IOException {
75         bitOffset = 0;
76 
77         if (streamPos >= raf.length()) {
78             int b = is.read();
79 
80             if (b < 0) {
81                 return -1;
82             }
83 
84             raf.seek(streamPos++);
85             raf.write(b);
86             return b;
87         }
88 
89         raf.seek(streamPos++);
90         return raf.read();
91     }
92 
93     @Override
read(byte[] b, int off, int len)94     public int read(byte[] b, int off, int len) throws IOException {
95         bitOffset = 0;
96 
97         if (streamPos >= raf.length()) {
98             int nBytes = is.read(b, off, len);
99 
100             if (nBytes < 0) {
101                 return -1;
102             }
103 
104             raf.seek(streamPos);
105             raf.write(b, off, nBytes);
106             streamPos += nBytes;
107             return nBytes;
108         }
109 
110         raf.seek(streamPos);
111         int nBytes = raf.read(b, off, len);
112         streamPos += nBytes;
113         return nBytes;
114     }
115 
116     @Override
isCached()117     public boolean isCached() {
118         return true;
119     }
120 
121     @Override
isCachedFile()122     public boolean isCachedFile() {
123         return true;
124     }
125 
126     @Override
isCachedMemory()127     public boolean isCachedMemory() {
128         return false;
129     }
130 
131     @Override
close()132     public void close() throws IOException {
133         super.close();
134         raf.close();
135         file.delete();
136     }
137 }
138