jdk/src/share/classes/javax/imageio/stream/FileCacheImageOutputStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3448 1ccef37a150f
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructs a <code>FileCacheImageOutputStream</code> that will write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * to a given <code>outputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <p> A temporary file is used as a cache.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * <code>cacheDir</code>is non-<code>null</code> and is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * directory, the file will be created there.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * <code>null</code>, the system-dependent default temporary-file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * directory will be used (see the documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <code>File.createTempFile</code> for details).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param stream an <code>OutputStream</code> to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param cacheDir a <code>File</code> indicating where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * cache file should be created, or <code>null</code> to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * system directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @exception IllegalArgumentException if <code>stream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @exception IllegalArgumentException if <code>cacheDir</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * non-<code>null</code> but is not a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @exception IOException if a cache file cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new IllegalArgumentException("stream == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if ((cacheDir != null) && !(cacheDir.isDirectory())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new IllegalArgumentException("Not a directory!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.cacheFile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            File.createTempFile("imageio", ".tmp", cacheDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.cache = new RandomAccessFile(cacheFile, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        StreamCloser.addToQueue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int val =  cache.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (val != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return val;
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(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                ("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return 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
        int nbytes = cache.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (nbytes != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            streamPos += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return nbytes;
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(int b) 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
        cache.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        maxStreamPos = Math.max(maxStreamPos, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        cache.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        streamPos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        maxStreamPos = Math.max(maxStreamPos, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return cache.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Sets the current stream position and resets the bit offset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * 0.  It is legal to seek past the end of the file; an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>EOFException</code> will be thrown only if a read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * performed.  The file length will not be increased until a write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * than the flushed position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @exception IOException if any other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void seek(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (pos < flushedPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        cache.seek(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.streamPos = cache.getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        maxStreamPos = Math.max(maxStreamPos, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>ImageOutputStream</code> caches data in order to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * seeking backwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public boolean isCached() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code>ImageOutputStream</code> maintains a file cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public boolean isCachedFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns <code>false</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>ImageOutputStream</code> does not maintain a main memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public boolean isCachedMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Closes this <code>FileCacheImageOututStream</code>.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * pending data is flushed to the output, and the cache file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * is closed and removed.  The destination <code>OutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * is not closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception IOException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        maxStreamPos = cache.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        seek(maxStreamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        flushBefore(maxStreamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        cache.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        cacheFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        cacheFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        StreamCloser.removeFromQueue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void flushBefore(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        long oFlushedPos = flushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        super.flushBefore(pos); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        long flushBytes = flushedPos - oFlushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (flushBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int bufLen = 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            byte[] buf = new byte[bufLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            cache.seek(oFlushedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            while (flushBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                int len = (int)Math.min(flushBytes, bufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                cache.readFully(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                stream.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                flushBytes -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}