001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.imageio.stream;
019:
020: import org.apache.harmony.x.imageio.stream.RandomAccessMemoryCache;
021:
022: import java.io.OutputStream;
023: import java.io.IOException;
024:
025: public class MemoryCacheImageOutputStream extends ImageOutputStreamImpl {
026: OutputStream os;
027: RandomAccessMemoryCache ramc = new RandomAccessMemoryCache();
028:
029: public MemoryCacheImageOutputStream(OutputStream stream) {
030: if (stream == null) {
031: throw new IllegalArgumentException("stream == null!");
032: }
033: os = stream;
034: }
035:
036: @Override
037: public void write(int b) throws IOException {
038: flushBits(); // See the flushBits method description
039:
040: ramc.putData(b, streamPos);
041: streamPos++;
042: }
043:
044: @Override
045: public void write(byte[] b, int off, int len) throws IOException {
046: flushBits(); // See the flushBits method description
047:
048: ramc.putData(b, off, len, streamPos);
049: streamPos += len;
050: }
051:
052: @Override
053: public int read() throws IOException {
054: bitOffset = 0;
055:
056: int res = ramc.getData(streamPos);
057: if (res >= 0) {
058: streamPos++;
059: }
060: return res;
061: }
062:
063: @Override
064: public int read(byte[] b, int off, int len) throws IOException {
065: bitOffset = 0;
066:
067: int res = ramc.getData(b, off, len, streamPos);
068: if (res > 0) {
069: streamPos += res;
070: }
071: return res;
072: }
073:
074: @Override
075: public long length() {
076: return ramc.length();
077: }
078:
079: @Override
080: public boolean isCached() {
081: return true;
082: }
083:
084: @Override
085: public boolean isCachedMemory() {
086: return true;
087: }
088:
089: @Override
090: public boolean isCachedFile() {
091: return false;
092: }
093:
094: @Override
095: public void close() throws IOException {
096: flushBefore(length());
097: super .close();
098: ramc.close();
099: }
100:
101: @Override
102: public void flushBefore(long pos) throws IOException {
103: long flushedPosition = getFlushedPosition();
104: super .flushBefore(pos);
105:
106: long newFlushedPosition = getFlushedPosition();
107: int nBytes = (int) (newFlushedPosition - flushedPosition);
108:
109: ramc.getData(os, nBytes, flushedPosition);
110: ramc.freeBefore(newFlushedPosition);
111:
112: os.flush();
113: }
114: }
|