01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package javax.imageio.stream;
19:
20: import java.io.IOException;
21: import java.io.RandomAccessFile;
22: import java.io.File;
23: import java.io.FileNotFoundException;
24:
25: public class FileImageInputStream extends ImageInputStreamImpl {
26: RandomAccessFile raf;
27:
28: @SuppressWarnings({"DuplicateThrows"})
29: public FileImageInputStream(File f) throws FileNotFoundException,
30: IOException {
31: if (f == null) {
32: throw new IllegalArgumentException("f == null!");
33: }
34:
35: raf = new RandomAccessFile(f, "r");
36: }
37:
38: public FileImageInputStream(RandomAccessFile raf) {
39: if (raf == null) {
40: throw new IllegalArgumentException("raf == null!");
41: }
42:
43: this .raf = raf;
44: }
45:
46: @Override
47: public int read() throws IOException {
48: bitOffset = 0;
49:
50: int res = raf.read();
51: if (res != -1) {
52: streamPos++;
53: }
54: return res;
55: }
56:
57: @Override
58: public int read(byte[] b, int off, int len) throws IOException {
59: bitOffset = 0;
60:
61: int numRead = raf.read(b, off, len);
62: if (numRead >= 0) {
63: streamPos += numRead;
64: }
65:
66: return numRead;
67: }
68:
69: @Override
70: public long length() {
71: try {
72: return raf.length();
73: } catch (IOException e) {
74: return -1L;
75: }
76: }
77:
78: @Override
79: public void seek(long pos) throws IOException {
80: if (pos < getFlushedPosition()) {
81: throw new IndexOutOfBoundsException();
82: }
83:
84: raf.seek(pos);
85: streamPos = raf.getFilePointer();
86: bitOffset = 0;
87: }
88:
89: @Override
90: public void close() throws IOException {
91: super.close();
92: raf.close();
93: }
94: }
|