jdk/src/share/classes/javax/imageio/stream/MemoryCache.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, 2003, 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.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Package-visible class consolidating common code for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>MemoryCacheImageInputStream</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>MemoryCacheImageOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class keeps an <code>ArrayList</code> of 8K blocks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * loaded sequentially.  Blocks may only be disposed of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * from the index 0 forward.  As blocks are freed, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * corresponding entries in the array list are set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>null</code>, but no compacting is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This allows the index for each block to never change,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and the length of the cache is always the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * total amount of data ever cached.  Cached data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * therefore always contiguous from the point of last
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * disposal to the current length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p> The total number of blocks resident in the cache must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * exceed <code>Integer.MAX_VALUE</code>.  In practice, the limit of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * available memory will be exceeded long before this becomes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * issue, since a full cache would contain 8192*2^31 = 16 terabytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * A <code>MemoryCache</code> may be reused after a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to <code>reset()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class MemoryCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final int BUFFER_LENGTH = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
23306
679ac7841e8d 8034998: Fix raw and unchecked lint warnings in javax.imageio
henryjen
parents: 5506
diff changeset
    61
    private ArrayList<byte[]> cache = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private long cacheStart = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The largest position ever written to the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private long length = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private byte[] getCacheBlock(long blockNum) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        long blockOffset = blockNum - cacheStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (blockOffset > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // This can only happen when the cache hits 16 terabytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // contiguous data...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new IOException("Cache addressing limit exceeded!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
23306
679ac7841e8d 8034998: Fix raw and unchecked lint warnings in javax.imageio
henryjen
parents: 5506
diff changeset
    77
        return cache.get((int)blockOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Ensures that at least <code>pos</code> bytes are cached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * or the end of the source is reached.  The return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * is equal to the smaller of <code>pos</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * length of the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public long loadFromStream(InputStream stream, long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // We've already got enough data cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (pos < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        int offset = (int)(length % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        byte [] buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        long len = pos - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (offset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            buf = getCacheBlock(length/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    buf = new byte[BUFFER_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    throw new IOException("No memory left for cache!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int left = BUFFER_LENGTH - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int nbytes = (int)Math.min(len, (long)left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            nbytes = stream.read(buf, offset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (nbytes == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return length; // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (offset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                cache.add(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            length += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            offset += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (offset >= BUFFER_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                // we've filled the current buffer, so a new one will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                // allocated next time around (and offset will be reset to 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return pos;
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
     * Writes out a portion of the cache to an <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * This method preserves no state about the output stream, and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * not dispose of any blocks containing bytes written.  To dispose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * blocks, use {@link #disposeBefore <code>disposeBefore()</code>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception IndexOutOfBoundsException if any portion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the requested data is not in the cache (including if <code>pos</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * is in a block already disposed), or if either <code>pos</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>len</code> is < 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void writeToStream(OutputStream stream, long pos, long len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (pos + len > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new IndexOutOfBoundsException("Argument out of cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if ((pos < 0) || (len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new IndexOutOfBoundsException("Negative pos or len");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        long bufIndex = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (bufIndex < cacheStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new IndexOutOfBoundsException("pos already disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int offset = (int)(pos % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        byte[] buf = getCacheBlock(bufIndex++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                buf = getCacheBlock(bufIndex++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int nbytes = (int)Math.min(len, (long)(BUFFER_LENGTH - offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            stream.write(buf, offset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Ensure that there is space to write a byte at the given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private void pad(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        long currIndex = cacheStart + cache.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        long lastIndex = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        long numNewBuffers = lastIndex - currIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        for (long i = 0; i < numNewBuffers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                cache.add(new byte[BUFFER_LENGTH]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throw new IOException("No memory left for cache!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Overwrites and/or appends the cache from a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The length of the cache will be extended as needed to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * the incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param b an array of bytes containing data to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param off the starting offset withing the data array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param len the number of bytes to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param pos the cache position at which to begin writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception NullPointerException if <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @exception IndexOutOfBoundsException if <code>off</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>len</code>, or <code>pos</code> are negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * or if <code>off+len > b.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void write(byte[] b, int off, int len, long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if ((off < 0) || (len < 0) || (pos < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            (off + len > b.length) || (off + len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Ensure there is space for the incoming data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        long lastPos = pos + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (lastPos >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            pad(lastPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            length = lastPos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Copy the data into the cache, block by block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int offset = (int)(pos % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            byte[] buf = getCacheBlock(pos/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            int nbytes = Math.min(len, BUFFER_LENGTH - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            System.arraycopy(b, off, buf, offset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            pos += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            offset = 0; // Always after the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Overwrites or appends a single byte to the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * The length of the cache will be extended as needed to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param b an <code>int</code> whose 8 least significant bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * will be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param pos the cache position at which to begin writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception IndexOutOfBoundsException if <code>pos</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public void write(int b, long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new ArrayIndexOutOfBoundsException("pos < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // Ensure there is space for the incoming data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (pos >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            pad(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            length = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // Insert the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        byte[] buf = getCacheBlock(pos/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        int offset = (int)(pos % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        buf[offset] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns the total length of data that has been cached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * regardless of whether any early blocks have been disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * This value will only ever increase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public long getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Returns the single byte at the given position, as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <code>int</code>.  Returns -1 if this position has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * not been cached or has been disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public int read(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (pos >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        byte[] buf = getCacheBlock(pos/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return buf[(int)(pos % BUFFER_LENGTH)] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Copy <code>len</code> bytes from the cache, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * at cache position <code>pos</code>, into the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <code>b</code> at offset <code>off</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception NullPointerException if b is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @exception IndexOutOfBoundsException if <code>off</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>len</code> or <code>pos</code> are negative or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <code>off + len > b.length</code> or if any portion of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * requested data is not in the cache (including if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>pos</code> is in a block that has already been disposed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void read(byte[] b, int off, int len, long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if ((off < 0) || (len < 0) || (pos < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            (off + len > b.length) || (off + len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (pos + len > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        long index = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int offset = (int)pos % BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            int nbytes = Math.min(len, BUFFER_LENGTH - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            byte[] buf = getCacheBlock(index++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            System.arraycopy(buf, offset, b, off, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            offset = 0; // Always after the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Free the blocks up to the position <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * The byte at <code>pos</code> remains available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @exception IndexOutOfBoundsException if <code>pos</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * is in a block that has already been disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public void disposeBefore(long pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        long index = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (index < cacheStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IndexOutOfBoundsException("pos already disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        long numBlocks = Math.min(index - cacheStart, cache.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        for (long i = 0; i < numBlocks; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            cache.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        this.cacheStart = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Erase the entire cache contents and reset the length to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * The cache object may subsequently be reused as though it had just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * been allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        cacheStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        length = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 }