KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)FileImageOutputStream.java 1.14 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>ImageOutputStream</code> that writes its
18  * output directly to a <code>File</code> or
19  * <code>RandomAccessFile</code>.
20  *
21  * @version 0.5
22  */

23 public class FileImageOutputStream extends ImageOutputStreamImpl JavaDoc {
24
25     private RandomAccessFile JavaDoc raf;
26
27     /**
28      * Constructs a <code>FileImageOutputStream</code> that will write
29      * to a given <code>File</code>.
30      *
31      * @param f a <code>File</code> to write to.
32      *
33      * @exception IllegalArgumentException if <code>f</code> is
34      * <code>null</code>.
35      * @exception SecurityException if a security manager exists
36      * and does not allow write access to the file.
37      * @exception FileNotFoundException if <code>f</code> does not denote
38      * a regular file or it cannot be opened for reading and writing for any
39      * other reason.
40      * @exception IOException if an I/O error occurs.
41      */

42     public FileImageOutputStream(File JavaDoc f)
43         throws FileNotFoundException JavaDoc, IOException JavaDoc {
44         this(f == null ? null : new RandomAccessFile JavaDoc(f, "rw"));
45     }
46
47     /**
48      * Constructs a <code>FileImageOutputStream</code> that will write
49      * to a given <code>RandomAccessFile</code>.
50      *
51      * @param raf a <code>RandomAccessFile</code> to write to.
52      *
53      * @exception IllegalArgumentException if <code>raf</code> is
54      * <code>null</code>.
55      */

56     public FileImageOutputStream(RandomAccessFile JavaDoc raf) {
57         if (raf == null) {
58             throw new IllegalArgumentException JavaDoc("raf == null!");
59         }
60         this.raf = raf;
61     }
62
63     public int read() throws IOException JavaDoc {
64         checkClosed();
65         bitOffset = 0;
66         int val = raf.read();
67         if (val != -1) {
68             ++streamPos;
69         }
70         return val;
71     }
72
73     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
74         checkClosed();
75         bitOffset = 0;
76         int nbytes = raf.read(b, off, len);
77         if (nbytes != -1) {
78             streamPos += nbytes;
79         }
80         return nbytes;
81     }
82
83     public void write(int b) throws IOException JavaDoc {
84         checkClosed();
85         flushBits();
86         raf.write(b);
87         ++streamPos;
88     }
89
90     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
91         checkClosed();
92         flushBits();
93         raf.write(b, off, len);
94         streamPos += len;
95     }
96
97     public long length() {
98         try {
99             checkClosed();
100             return raf.length();
101         } catch (IOException JavaDoc e) {
102             return -1L;
103         }
104     }
105
106     /**
107      * Sets the current stream position and resets the bit offset to
108      * 0. It is legal to seeking past the end of the file; an
109      * <code>EOFException</code> will be thrown only if a read is
110      * performed. The file length will not be increased until a write
111      * is performed.
112      *
113      * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
114      * than the flushed position.
115      * @exception IOException if any other I/O error occurs.
116      */

117     public void seek(long pos) throws IOException JavaDoc {
118         checkClosed();
119         if (pos < flushedPos) {
120             throw new IndexOutOfBoundsException JavaDoc("pos < flushedPos!");
121         }
122         bitOffset = 0;
123         raf.seek(pos);
124         streamPos = raf.getFilePointer();
125     }
126
127     public void close() throws IOException JavaDoc {
128         super.close();
129         raf.close();
130     }
131 }
132
Popular Tags