blob: 6680ae0a9738c5984e91cbfeec7c194ceb2e3b61 [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
19package javax.imageio.stream;
20
21import java.io.IOException;
22import java.io.RandomAccessFile;
23import java.io.File;
24import java.io.FileNotFoundException;
25
26/**
27 * The FileImageInputStream class implements ImageInputStream
28 * and obtains its input data from a File or RandomAccessFile.
29 */
30public class FileImageInputStream extends ImageInputStreamImpl {
31
32 /** The raf. */
33 RandomAccessFile raf;
34
35 /**
36 * Instantiates a new FileImageInputStream from the specified File.
37 *
38 * @param f the File of input data.
39 *
40 * @throws FileNotFoundException if the specified file
41 * doesn't exist.
42 * @throws IOException Signals that an I/O exception has occurred.
43 */
44 @SuppressWarnings({"DuplicateThrows"})
45 public FileImageInputStream(File f) throws FileNotFoundException, IOException {
46 if (f == null) {
47 throw new IllegalArgumentException("f == null!");
48 }
49
50 raf = new RandomAccessFile(f, "r");
51 }
52
53 /**
54 * Instantiates a new FileImageInputStream from the specified
55 * RandomAccessFile.
56 *
57 * @param raf the RandomAccessFile of input data.
58 */
59 public FileImageInputStream(RandomAccessFile raf) {
60 if (raf == null) {
61 throw new IllegalArgumentException("raf == null!");
62 }
63
64 this.raf = raf;
65 }
66
67 @Override
68 public int read() throws IOException {
69 bitOffset = 0;
70
71 int res = raf.read();
72 if (res != -1) {
73 streamPos++;
74 }
75 return res;
76 }
77
78 @Override
79 public int read(byte[] b, int off, int len) throws IOException {
80 bitOffset = 0;
81
82 int numRead = raf.read(b, off, len);
83 if (numRead >= 0) {
84 streamPos += numRead;
85 }
86
87 return numRead;
88 }
89
90 @Override
91 public long length() {
92 try {
93 return raf.length();
94 } catch(IOException e) {
95 return -1L;
96 }
97 }
98
99 @Override
100 public void seek(long pos) throws IOException {
101 if (pos < getFlushedPosition()) {
102 throw new IndexOutOfBoundsException();
103 }
104
105 raf.seek(pos);
106 streamPos = raf.getFilePointer();
107 bitOffset = 0;
108 }
109
110 @Override
111 public void close() throws IOException {
112 super.close();
113 raf.close();
114 }
115}