blob: 2730ba6a971d595986126539a4ddfdfc4b3df4c2 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18 * @author Rustem V. Rafikov
19 * @version $Revision: 1.3 $
20 */
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070022package javax.imageio.stream;
23
24import java.io.*;
25
26/**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080027 * The FileImageOutputStream class implements ImageOutputStream and writes the
28 * output data to a File or RandomAccessFile.
29 *
30 * @since Android 1.0
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031 */
32public class FileImageOutputStream extends ImageOutputStreamImpl {
33
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080034 /**
35 * The file.
36 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037 RandomAccessFile file;
38
39 /**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080040 * Instantiates a new FileImageOutputStream with the specified File.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080042 * @param f
43 * the output File.
44 * @throws FileNotFoundException
45 * if the file not found.
46 * @throws IOException
47 * if an I/O exception has occurred.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048 */
49 public FileImageOutputStream(File f) throws FileNotFoundException, IOException {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080050 this(f != null ? new RandomAccessFile(f, "rw") : null);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051 }
52
53 /**
54 * Instantiates a new FileImageOutputStream with the specified
55 * RandomAccessFile.
56 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080057 * @param raf
58 * the output RandomAccessFile.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070059 */
60 public FileImageOutputStream(RandomAccessFile raf) {
61 if (raf == null) {
62 throw new IllegalArgumentException("file should not be NULL");
63 }
64 file = raf;
65 }
66
67 @Override
68 public void write(int b) throws IOException {
69 checkClosed();
70 // according to the spec for ImageOutputStreamImpl#flushBits()
71 flushBits();
72 file.write(b);
73 streamPos++;
74 }
75
76 @Override
77 public void write(byte[] b, int off, int len) throws IOException {
78 checkClosed();
79 // according to the spec for ImageOutputStreamImpl#flushBits()
80 flushBits();
81 file.write(b, off, len);
82 streamPos += len;
83 }
84
85 @Override
86 public int read() throws IOException {
87 checkClosed();
88 int rt = file.read();
89 if (rt != -1) {
90 streamPos++;
91 }
92 return rt;
93 }
94
95 @Override
96 public int read(byte[] b, int off, int len) throws IOException {
97 checkClosed();
98 int rt = file.read(b, off, len);
99 if (rt != -1) {
100 streamPos += rt;
101 }
102 return rt;
103 }
104
105 @Override
106 public long length() {
107 try {
108 checkClosed();
109 return file.length();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800110 } catch (IOException e) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 return super.length(); // -1L
112 }
113 }
114
115 @Override
116 public void seek(long pos) throws IOException {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800117 // -- checkClosed() is performed in super.seek()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700118 super.seek(pos);
119 file.seek(pos);
120 streamPos = file.getFilePointer();
121 }
122
123 @Override
124 public void close() throws IOException {
125 super.close();
126 file.close();
127 }
128}