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