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 18package javax.imageio.stream; 19 20import org.apache.harmony.x.imageio.stream.RandomAccessMemoryCache; 21 22import java.io.OutputStream; 23import java.io.IOException; 24 25/** 26 * The MemoryCacheImageOutputStream class implements ImageOutputStream using a 27 * memory buffer for caching the data. 28 * 29 * @since Android 1.0 30 */ 31public class MemoryCacheImageOutputStream extends ImageOutputStreamImpl { 32 33 /** 34 * The os. 35 */ 36 OutputStream os; 37 38 /** 39 * The ramc. 40 */ 41 RandomAccessMemoryCache ramc = new RandomAccessMemoryCache(); 42 43 /** 44 * Instantiates a new MemoryCacheImageOutputStream which writes to the 45 * specified OutputStream. 46 * 47 * @param stream 48 * the OutputStream. 49 */ 50 public MemoryCacheImageOutputStream(OutputStream stream) { 51 if (stream == null) { 52 throw new IllegalArgumentException("stream == null!"); 53 } 54 os = stream; 55 } 56 57 @Override 58 public void write(int b) throws IOException { 59 flushBits(); // See the flushBits method description 60 61 ramc.putData(b, streamPos); 62 streamPos++; 63 } 64 65 @Override 66 public void write(byte[] b, int off, int len) throws IOException { 67 flushBits(); // See the flushBits method description 68 69 ramc.putData(b, off, len, streamPos); 70 streamPos += len; 71 } 72 73 @Override 74 public int read() throws IOException { 75 bitOffset = 0; 76 77 int res = ramc.getData(streamPos); 78 if (res >= 0) { 79 streamPos++; 80 } 81 return res; 82 } 83 84 @Override 85 public int read(byte[] b, int off, int len) throws IOException { 86 bitOffset = 0; 87 88 int res = ramc.getData(b, off, len, streamPos); 89 if (res > 0) { 90 streamPos += res; 91 } 92 return res; 93 } 94 95 @Override 96 public long length() { 97 return ramc.length(); 98 } 99 100 @Override 101 public boolean isCached() { 102 return true; 103 } 104 105 @Override 106 public boolean isCachedMemory() { 107 return true; 108 } 109 110 @Override 111 public boolean isCachedFile() { 112 return false; 113 } 114 115 @Override 116 public void close() throws IOException { 117 flushBefore(length()); 118 super.close(); 119 ramc.close(); 120 } 121 122 @Override 123 public void flushBefore(long pos) throws IOException { 124 long flushedPosition = getFlushedPosition(); 125 super.flushBefore(pos); 126 127 long newFlushedPosition = getFlushedPosition(); 128 int nBytes = (int)(newFlushedPosition - flushedPosition); 129 130 ramc.getData(os, nBytes, flushedPosition); 131 ramc.freeBefore(newFlushedPosition); 132 133 os.flush(); 134 } 135} 136