1 11 package org.eclipse.swt.internal.image; 12 13 import java.io.*; 14 15 public class PngInputStream extends InputStream { 16 PngChunkReader reader; 17 PngChunk chunk; 18 int offset, length; 19 20 final static int DATA_OFFSET = 8; 21 22 public PngInputStream(PngIdatChunk chunk, PngChunkReader reader) { 23 this.chunk = chunk; 24 this.reader = reader; 25 length = chunk.getLength(); 26 offset = 0; 27 } 28 29 private boolean checkChunk() throws IOException { 30 while (offset == length) { 31 chunk = reader.readNextChunk(); 32 if (chunk == null) throw new IOException(); 33 if (chunk.getChunkType() == PngChunk.CHUNK_IEND) return false; 34 if (chunk.getChunkType() != PngChunk.CHUNK_IDAT) throw new IOException(); 35 length = chunk.getLength(); 36 offset = 0; 37 } 38 return true; 39 } 40 41 public void close() throws IOException { 42 chunk = null; 43 } 44 45 public int read() throws IOException { 46 if (chunk == null) throw new IOException(); 47 if (offset == length && !checkChunk()) return -1; 48 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF; 49 offset++; 50 return b; 51 } 52 53 public int read(byte[] b, int off, int len) throws IOException { 54 if (chunk == null) throw new IOException(); 55 if (offset == length && !checkChunk()) return -1; 56 len = Math.min(len, length - offset); 57 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len); 58 offset += len; 59 return len; 60 } 61 } 62 | Popular Tags |