jdk/src/share/classes/javax/imageio/stream/FileCacheImageOutputStream.java
author henryjen
Thu, 20 Feb 2014 16:23:45 -0800
changeset 23306 679ac7841e8d
parent 13041 8477cb6992be
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
/*
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 3448
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: 3448
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: 3448
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3448
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3448
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.RandomAccessFile;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
    32
import java.nio.file.Files;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.imageio.stream.StreamCloser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * An implementation of <code>ImageOutputStream</code> that writes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * output to a regular <code>OutputStream</code>.  A file is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * cache data until it is flushed to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class FileCacheImageOutputStream extends ImageOutputStreamImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private OutputStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private File cacheFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private RandomAccessFile cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Pos after last (rightmost) byte written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private long maxStreamPos = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
3448
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    52
    /** The CloseAction that closes the stream in
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    53
     *  the StreamCloser's shutdown hook                     */
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    54
    private final StreamCloser.CloseAction closeAction;
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs a <code>FileCacheImageOutputStream</code> that will write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * to a given <code>outputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <p> A temporary file is used as a cache.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <code>cacheDir</code>is non-<code>null</code> and is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * directory, the file will be created there.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <code>null</code>, the system-dependent default temporary-file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * directory will be used (see the documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>File.createTempFile</code> for details).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param stream an <code>OutputStream</code> to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param cacheDir a <code>File</code> indicating where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * cache file should be created, or <code>null</code> to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * system directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @exception IllegalArgumentException if <code>stream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @exception IllegalArgumentException if <code>cacheDir</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * non-<code>null</code> but is not a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @exception IOException if a cache file cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new IllegalArgumentException("stream == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if ((cacheDir != null) && !(cacheDir.isDirectory())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new IllegalArgumentException("Not a directory!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.stream = stream;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
    87
        if (cacheDir == null)
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
    88
            this.cacheFile = Files.createTempFile("imageio", ".tmp").toFile();
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
    89
        else
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
    90
            this.cacheFile = Files.createTempFile(cacheDir.toPath(), "imageio", ".tmp")
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9192
diff changeset
    91
                                  .toFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.cache = new RandomAccessFile(cacheFile, "rw");
3448
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    93
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    94
        this.closeAction = StreamCloser.createCloseAction(this);
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
    95
        StreamCloser.addToQueue(closeAction);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int val =  cache.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (val != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                ("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int nbytes = cache.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (nbytes != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            streamPos += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        cache.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        maxStreamPos = Math.max(maxStreamPos, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        cache.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        streamPos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        maxStreamPos = Math.max(maxStreamPos, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return cache.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Sets the current stream position and resets the bit offset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * 0.  It is legal to seek past the end of the file; an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>EOFException</code> will be thrown only if a read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * performed.  The file length will not be increased until a write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * than the flushed position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception IOException if any other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public void seek(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (pos < flushedPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        cache.seek(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.streamPos = cache.getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        maxStreamPos = Math.max(maxStreamPos, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        this.bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>ImageOutputStream</code> caches data in order to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * seeking backwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public boolean isCached() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <code>ImageOutputStream</code> maintains a file cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public boolean isCachedFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns <code>false</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <code>ImageOutputStream</code> does not maintain a main memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public boolean isCachedMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
9192
8221ad7f7950 6983666: Typo in JavaDoc comments within FileCacheImageOutputStream
prr
parents: 5506
diff changeset
   221
     * Closes this <code>FileCacheImageOutputStream</code>.  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * pending data is flushed to the output, and the cache file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * is closed and removed.  The destination <code>OutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * is not closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @exception IOException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        maxStreamPos = cache.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        seek(maxStreamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        flushBefore(maxStreamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        cache.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        cacheFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        cacheFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        stream = null;
3448
1ccef37a150f 6657133: Mutable statics in imageio plugins (findbugs)
bae
parents: 2
diff changeset
   240
        StreamCloser.removeFromQueue(closeAction);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void flushBefore(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        long oFlushedPos = flushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        super.flushBefore(pos); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        long flushBytes = flushedPos - oFlushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (flushBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int bufLen = 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            byte[] buf = new byte[bufLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            cache.seek(oFlushedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            while (flushBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                int len = (int)Math.min(flushBytes, bufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                cache.readFully(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                stream.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                flushBytes -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}