jdk/src/share/classes/javax/imageio/stream/FileImageOutputStream.java
author henryjen
Thu, 20 Feb 2014 16:23:45 -0800
changeset 23306 679ac7841e8d
parent 5506 202f599c92aa
permissions -rw-r--r--
8034998: Fix raw and unchecked lint warnings in javax.imageio Reviewed-by: prr, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.imageio.stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.imageio.stream.CloseableDisposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.imageio.stream.StreamFinalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.java2d.Disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An implementation of <code>ImageOutputStream</code> that writes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * output directly to a <code>File</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>RandomAccessFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class FileImageOutputStream extends ImageOutputStreamImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private RandomAccessFile raf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /** The referent to be registered with the Disposer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final Object disposerReferent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** The DisposerRecord that closes the underlying RandomAccessFile. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final CloseableDisposerRecord disposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Constructs a <code>FileImageOutputStream</code> that will write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * to a given <code>File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param f a <code>File</code> to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @exception IllegalArgumentException if <code>f</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @exception SecurityException if a security manager exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * and does not allow write access to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @exception FileNotFoundException if <code>f</code> does not denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * a regular file or it cannot be opened for reading and writing for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public FileImageOutputStream(File f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        throws FileNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this(f == null ? null : new RandomAccessFile(f, "rw"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a <code>FileImageOutputStream</code> that will write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * to a given <code>RandomAccessFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param raf a <code>RandomAccessFile</code> to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @exception IllegalArgumentException if <code>raf</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public FileImageOutputStream(RandomAccessFile raf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (raf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new IllegalArgumentException("raf == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.raf = raf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        disposerRecord = new CloseableDisposerRecord(raf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (getClass() == FileImageOutputStream.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            disposerReferent = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Disposer.addRecord(disposerReferent, disposerRecord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            disposerReferent = new StreamFinalizer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int val = raf.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (val != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int nbytes = raf.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (nbytes != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            streamPos += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        raf.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        raf.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        streamPos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return raf.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Sets the current stream position and resets the bit offset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * 0.  It is legal to seeking past the end of the file; an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>EOFException</code> will be thrown only if a read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * performed.  The file length will not be increased until a write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * than the flushed position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @exception IOException if any other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void seek(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (pos < flushedPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IndexOutOfBoundsException("pos < flushedPos!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        raf.seek(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        streamPos = raf.getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        disposerRecord.dispose(); // this closes the RandomAccessFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        raf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // Empty finalizer: for performance reasons we instead use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // Disposer mechanism for ensuring that the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // RandomAccessFile is closed prior to garbage collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}