Path: blob/master/src/java.desktop/share/classes/javax/imageio/stream/FileImageInputStream.java
41153 views
/*1* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio.stream;2627import java.io.File;28import java.io.FileNotFoundException;29import java.io.IOException;30import java.io.RandomAccessFile;31import com.sun.imageio.stream.CloseableDisposerRecord;32import com.sun.imageio.stream.StreamFinalizer;33import sun.java2d.Disposer;3435/**36* An implementation of {@code ImageInputStream} that gets its37* input from a {@code File} or {@code RandomAccessFile}.38* The file contents are assumed to be stable during the lifetime of39* the object.40*41*/42public class FileImageInputStream extends ImageInputStreamImpl {4344private RandomAccessFile raf;4546/** The referent to be registered with the Disposer. */47private final Object disposerReferent;4849/** The DisposerRecord that closes the underlying RandomAccessFile. */50private final CloseableDisposerRecord disposerRecord;5152/**53* Constructs a {@code FileImageInputStream} that will read54* from a given {@code File}.55*56* <p> The file contents must not change between the time this57* object is constructed and the time of the last call to a read58* method.59*60* @param f a {@code File} to read from.61*62* @exception IllegalArgumentException if {@code f} is63* {@code null}.64* @exception SecurityException if a security manager exists65* and does not allow read access to the file.66* @exception FileNotFoundException if {@code f} is a67* directory or cannot be opened for reading for any other reason.68* @exception IOException if an I/O error occurs.69*/70public FileImageInputStream(File f)71throws FileNotFoundException, IOException {72this(f == null ? null : new RandomAccessFile(f, "r"));73}7475/**76* Constructs a {@code FileImageInputStream} that will read77* from a given {@code RandomAccessFile}.78*79* <p> The file contents must not change between the time this80* object is constructed and the time of the last call to a read81* method.82*83* @param raf a {@code RandomAccessFile} to read from.84*85* @exception IllegalArgumentException if {@code raf} is86* {@code null}.87*/88public FileImageInputStream(RandomAccessFile raf) {89if (raf == null) {90throw new IllegalArgumentException("raf == null!");91}92this.raf = raf;9394disposerRecord = new CloseableDisposerRecord(raf);95if (getClass() == FileImageInputStream.class) {96disposerReferent = new Object();97Disposer.addRecord(disposerReferent, disposerRecord);98} else {99disposerReferent = new StreamFinalizer(this);100}101}102103public int read() throws IOException {104checkClosed();105bitOffset = 0;106int val = raf.read();107if (val != -1) {108++streamPos;109}110return val;111}112113public int read(byte[] b, int off, int len) throws IOException {114checkClosed();115bitOffset = 0;116int nbytes = raf.read(b, off, len);117if (nbytes != -1) {118streamPos += nbytes;119}120return nbytes;121}122123/**124* Returns the length of the underlying file, or {@code -1}125* if it is unknown.126*127* @return the file length as a {@code long}, or128* {@code -1}.129*/130public long length() {131try {132checkClosed();133return raf.length();134} catch (IOException e) {135return -1L;136}137}138139public void seek(long pos) throws IOException {140checkClosed();141if (pos < flushedPos) {142throw new IndexOutOfBoundsException("pos < flushedPos!");143}144bitOffset = 0;145raf.seek(pos);146streamPos = raf.getFilePointer();147}148149public void close() throws IOException {150super.close();151disposerRecord.dispose(); // this closes the RandomAccessFile152raf = null;153}154155/**156* {@inheritDoc}157*158* @deprecated The {@code finalize} method has been deprecated.159* Subclasses that override {@code finalize} in order to perform cleanup160* should be modified to use alternative cleanup mechanisms and161* to remove the overriding {@code finalize} method.162* When overriding the {@code finalize} method, its implementation must explicitly163* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.164* See the specification for {@link Object#finalize()} for further165* information about migration options.166*/167@Deprecated(since="9")168protected void finalize() throws Throwable {169// Empty finalizer: for performance reasons we instead use the170// Disposer mechanism for ensuring that the underlying171// RandomAccessFile is closed prior to garbage collection172}173}174175176