blob: b9b6002b321da51ab3190e124aff6743bdff28c3 [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
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070018package javax.imageio.stream;
19
20import java.io.IOException;
21import java.io.RandomAccessFile;
22import java.io.File;
23import java.io.FileNotFoundException;
24
25/**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080026 * The FileImageInputStream class implements ImageInputStream and obtains its
27 * input data from a File or RandomAccessFile.
28 *
29 * @since Android 1.0
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070030 */
31public class FileImageInputStream extends ImageInputStreamImpl {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080032
33 /**
34 * The raf.
35 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036 RandomAccessFile raf;
37
38 /**
39 * Instantiates a new FileImageInputStream from the specified File.
40 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080041 * @param f
42 * the File of input data.
43 * @throws FileNotFoundException
44 * if the specified file doesn't exist.
45 * @throws IOException
46 * if an I/O exception has occurred.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047 */
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080048 @SuppressWarnings( {
49 "DuplicateThrows"
50 })
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051 public FileImageInputStream(File f) throws FileNotFoundException, IOException {
52 if (f == null) {
53 throw new IllegalArgumentException("f == null!");
54 }
55
56 raf = new RandomAccessFile(f, "r");
57 }
58
59 /**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080060 * Instantiates a new FileImageInputStream from the specified
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061 * RandomAccessFile.
62 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080063 * @param raf
64 * the RandomAccessFile of input data.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070065 */
66 public FileImageInputStream(RandomAccessFile raf) {
67 if (raf == null) {
68 throw new IllegalArgumentException("raf == null!");
69 }
70
71 this.raf = raf;
72 }
73
74 @Override
75 public int read() throws IOException {
76 bitOffset = 0;
77
78 int res = raf.read();
79 if (res != -1) {
80 streamPos++;
81 }
82 return res;
83 }
84
85 @Override
86 public int read(byte[] b, int off, int len) throws IOException {
87 bitOffset = 0;
88
89 int numRead = raf.read(b, off, len);
90 if (numRead >= 0) {
91 streamPos += numRead;
92 }
93
94 return numRead;
95 }
96
97 @Override
98 public long length() {
99 try {
100 return raf.length();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800101 } catch (IOException e) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700102 return -1L;
103 }
104 }
105
106 @Override
107 public void seek(long pos) throws IOException {
108 if (pos < getFlushedPosition()) {
109 throw new IndexOutOfBoundsException();
110 }
111
112 raf.seek(pos);
113 streamPos = raf.getFilePointer();
114 bitOffset = 0;
115 }
116
117 @Override
118 public void close() throws IOException {
119 super.close();
120 raf.close();
121 }
122}