1 7 8 package com.sun.imageio.plugins.common; 9 10 import java.io.IOException ; 11 import javax.imageio.stream.ImageInputStreamImpl ; 12 import javax.imageio.stream.ImageInputStream ; 13 14 17 public class SubImageInputStream extends ImageInputStreamImpl { 18 19 ImageInputStream stream; 20 long startingPos; 21 int startingLength; 22 int length; 23 24 public SubImageInputStream(ImageInputStream stream, int length) 25 throws IOException { 26 this.stream = stream; 27 this.startingPos = stream.getStreamPosition(); 28 this.startingLength = this.length = length; 29 } 30 31 public int read() throws IOException { 32 if (length == 0) { return -1; 34 } else { 35 --length; 36 return stream.read(); 37 } 38 } 39 40 public int read(byte[] b, int off, int len) throws IOException { 41 if (length == 0) { return -1; 43 } 44 45 len = Math.min(len, length); 46 int bytes = stream.read(b, off, len); 47 length -= bytes; 48 return bytes; 49 } 50 51 public long length() { 52 return startingLength; 53 } 54 55 public void seek(long pos) throws IOException { 56 stream.seek(pos - startingPos); 57 streamPos = pos; 58 } 59 } 60 | Popular Tags |