blob: d7fc79139cec3021aa5cd2c7bad5eff5060e1720 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
18package javax.imageio.stream;
19
20import org.apache.harmony.x.imageio.stream.RandomAccessMemoryCache;
21
22import java.io.IOException;
23import java.io.InputStream;
24
25/**
26 * The MemoryCacheImageInputStream class implements ImageInputStream using a
27 * memory buffer for caching the data.
28 *
29 * @since Android 1.0
30 */
31public class MemoryCacheImageInputStream extends ImageInputStreamImpl {
32
33 /**
34 * The is.
35 */
36 private InputStream is;
37
38 /**
39 * The ramc.
40 */
41 private RandomAccessMemoryCache ramc = new RandomAccessMemoryCache();
42
43 /**
44 * Instantiates a new MemoryCacheImageInputStream which reads from the
45 * specified InputStream.
46 *
47 * @param stream
48 * the InputStream to be read.
49 */
50 public MemoryCacheImageInputStream(InputStream stream) {
51 if (stream == null) {
52 throw new IllegalArgumentException("stream == null!");
53 }
54 is = stream;
55 }
56
57 @Override
58 public int read() throws IOException {
59 bitOffset = 0;
60
61 if (streamPos >= ramc.length()) {
62 int count = (int)(streamPos - ramc.length() + 1);
63 int bytesAppended = ramc.appendData(is, count);
64
65 if (bytesAppended < count) {
66 return -1;
67 }
68 }
69
70 int res = ramc.getData(streamPos);
71 if (res >= 0) {
72 streamPos++;
73 }
74 return res;
75 }
76
77 @Override
78 public int read(byte[] b, int off, int len) throws IOException {
79 bitOffset = 0;
80
81 if (streamPos >= ramc.length()) {
82 int count = (int)(streamPos - ramc.length() + len);
83 ramc.appendData(is, count);
84 }
85
86 int res = ramc.getData(b, off, len, streamPos);
87 if (res > 0) {
88 streamPos += res;
89 }
90 return res;
91 }
92
93 @Override
94 public boolean isCached() {
95 return true;
96 }
97
98 @Override
99 public boolean isCachedFile() {
100 return false;
101 }
102
103 @Override
104 public boolean isCachedMemory() {
105 return true;
106 }
107
108 @Override
109 public void close() throws IOException {
110 super.close();
111 ramc.close();
112 }
113
114 @Override
115 public void flushBefore(long pos) throws IOException {
116 super.flushBefore(pos);
117 ramc.freeBefore(getFlushedPosition());
118 }
119}