KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > common > SubImageInputStream


1 /*
2  * @(#)SubImageInputStream.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.common;
9
10 import java.io.IOException JavaDoc;
11 import javax.imageio.stream.ImageInputStreamImpl JavaDoc;
12 import javax.imageio.stream.ImageInputStream JavaDoc;
13
14 /**
15  * @version 0.5
16  */

17 public class SubImageInputStream extends ImageInputStreamImpl JavaDoc {
18
19     ImageInputStream JavaDoc stream;
20     long startingPos;
21     int startingLength;
22     int length;
23
24     public SubImageInputStream(ImageInputStream JavaDoc stream, int length)
25         throws IOException JavaDoc {
26         this.stream = stream;
27         this.startingPos = stream.getStreamPosition();
28         this.startingLength = this.length = length;
29     }
30
31     public int read() throws IOException JavaDoc {
32         if (length == 0) { // Local EOF
33
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 JavaDoc {
41         if (length == 0) { // Local EOF
42
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 JavaDoc {
56         stream.seek(pos - startingPos);
57         streamPos = pos;
58     }
59 }
60
Popular Tags