The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 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 | |
| 19 | package javax.imageio.stream; |
| 20 | |
| 21 | import java.io.*; |
| 22 | |
| 23 | /** |
| 24 | * The FileCacheImageInputStream class is an implementation of |
| 25 | * ImageInputStream which reads from its InputStream |
| 26 | * and uses a temporary file as a cache. |
| 27 | */ |
| 28 | public class FileCacheImageInputStream extends ImageInputStreamImpl { |
| 29 | |
| 30 | /** The is. */ |
| 31 | private InputStream is; |
| 32 | |
| 33 | /** The file. */ |
| 34 | private File file; |
| 35 | |
| 36 | /** The raf. */ |
| 37 | private RandomAccessFile raf; |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Instantiates a new FileCacheImageInputStream from |
| 42 | * the specified InputStream and using the specified |
| 43 | * File as its cache directory. |
| 44 | * |
| 45 | * @param stream the InputStream for reading. |
| 46 | * @param cacheDir the cache directory where the chache file |
| 47 | * will be created. |
| 48 | * |
| 49 | * @throws IOException Signals that an I/O exception has occurred. |
| 50 | */ |
| 51 | public FileCacheImageInputStream(InputStream stream, File cacheDir) throws IOException { |
| 52 | if (stream == null) { |
| 53 | throw new IllegalArgumentException("stream == null!"); |
| 54 | } |
| 55 | is = stream; |
| 56 | |
| 57 | if (cacheDir == null || cacheDir.isDirectory()) { |
| 58 | file = File.createTempFile(FileCacheImageOutputStream.IIO_TEMP_FILE_PREFIX, null, cacheDir); |
| 59 | file.deleteOnExit(); |
| 60 | } else { |
| 61 | throw new IllegalArgumentException("Not a directory!"); |
| 62 | } |
| 63 | |
| 64 | raf = new RandomAccessFile(file, "rw"); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int read() throws IOException { |
| 69 | bitOffset = 0; |
| 70 | |
| 71 | if (streamPos >= raf.length()) { |
| 72 | int b = is.read(); |
| 73 | |
| 74 | if (b < 0) { |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | raf.seek(streamPos++); |
| 79 | raf.write(b); |
| 80 | return b; |
| 81 | } |
| 82 | |
| 83 | raf.seek(streamPos++); |
| 84 | return raf.read(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int read(byte[] b, int off, int len) throws IOException { |
| 89 | bitOffset = 0; |
| 90 | |
| 91 | if (streamPos >= raf.length()) { |
| 92 | int nBytes = is.read(b, off, len); |
| 93 | |
| 94 | if (nBytes < 0) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | raf.seek(streamPos); |
| 99 | raf.write(b, off, nBytes); |
| 100 | streamPos += nBytes; |
| 101 | return nBytes; |
| 102 | } |
| 103 | |
| 104 | raf.seek(streamPos); |
| 105 | int nBytes = raf.read(b, off, len); |
| 106 | streamPos += nBytes; |
| 107 | return nBytes; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean isCached() { |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean isCachedFile() { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean isCachedMemory() { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void close() throws IOException { |
| 127 | super.close(); |
| 128 | raf.close(); |
| 129 | file.delete(); |
| 130 | } |
| 131 | } |