KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > stream > FileImageInputStream


1 /*
2  * @(#)FileImageInputStream.java 1.21 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 javax.imageio.stream;
9
10 import java.io.DataInput JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.RandomAccessFile JavaDoc;
15
16 /**
17  * An implementation of <code>ImageInputStream</code> that gets its
18  * input from a <code>File</code> or <code>RandomAccessFile</code>.
19  * The file contents are assumed to be stable during the lifetime of
20  * the object.
21  *
22  * @version 0.5
23  */

24 public class FileImageInputStream extends ImageInputStreamImpl JavaDoc {
25
26     private RandomAccessFile JavaDoc raf;
27
28     /**
29      * Constructs a <code>FileImageInputStream</code> that will read
30      * from a given <code>File</code>.
31      *
32      * <p> The file contents must not change between the time this
33      * object is constructed and the time of the last call to a read
34      * method.
35      *
36      * @param f a <code>File</code> to read from.
37      *
38      * @exception IllegalArgumentException if <code>f</code> is
39      * <code>null</code>.
40      * @exception SecurityException if a security manager exists
41      * and does not allow read access to the file.
42      * @exception FileNotFoundException if <code>f</code> is a
43      * directory or cannot be opened for reading for any other reason.
44      * @exception IOException if an I/O error occurs.
45      */

46     public FileImageInputStream(File JavaDoc f)
47         throws FileNotFoundException JavaDoc, IOException JavaDoc {
48         if (f == null) {
49             throw new IllegalArgumentException JavaDoc("f == null!");
50         }
51         this.raf = new RandomAccessFile JavaDoc(f, "r");
52     }
53
54     /**
55      * Constructs a <code>FileImageInputStream</code> that will read
56      * from a given <code>RandomAccessFile</code>.
57      *
58      * <p> The file contents must not change between the time this
59      * object is constructed and the time of the last call to a read
60      * method.
61      *
62      * @param raf a <code>RandomAccessFile</code> to read from.
63      *
64      * @exception IllegalArgumentException if <code>raf</code> is
65      * <code>null</code>.
66      */

67     public FileImageInputStream(RandomAccessFile JavaDoc raf) {
68         if (raf == null) {
69             throw new IllegalArgumentException JavaDoc("raf == null!");
70         }
71         this.raf = raf;
72     }
73
74     public int read() throws IOException JavaDoc {
75         checkClosed();
76         bitOffset = 0;
77         int val = raf.read();
78         if (val != -1) {
79             ++streamPos;
80         }
81         return val;
82     }
83
84     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
85         checkClosed();
86         bitOffset = 0;
87         int nbytes = raf.read(b, off, len);
88         if (nbytes != -1) {
89             streamPos += nbytes;
90         }
91         return nbytes;
92     }
93
94     /**
95      * Returns the length of the underlying file, or <code>-1</code>
96      * if it is unknown.
97      *
98      * @return the file length as a <code>long</code>, or
99      * <code>-1</code>.
100      */

101     public long length() {
102         try {
103             checkClosed();
104             return raf.length();
105         } catch (IOException JavaDoc e) {
106             return -1L;
107         }
108     }
109
110     public void seek(long pos) throws IOException JavaDoc {
111         checkClosed();
112         if (pos < flushedPos) {
113             throw new IndexOutOfBoundsException JavaDoc("pos < flushedPos!");
114         }
115         bitOffset = 0;
116         raf.seek(pos);
117         streamPos = raf.getFilePointer();
118     }
119
120     public void close() throws IOException JavaDoc {
121         super.close();
122         raf.close();
123     }
124 }
125
Popular Tags