jdk/src/java.desktop/share/classes/javax/imageio/stream/MemoryCache.java
author yan
Mon, 22 Sep 2014 11:25:33 +0400
changeset 26755 70eafa5df34b
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
6588417: Incorrect javadoc: no @throws or @exception tag in javax.* Summary: Fix remaining missing @throws tags. Reviewed-by: prr Contributed-by: anisha.nagarajan1@gmail.com
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.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
    85
     *
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
    86
     * @throws IOException if there is no more memory for cache
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public long loadFromStream(InputStream stream, long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // We've already got enough data cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (pos < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int offset = (int)(length % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        byte [] buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        long len = pos - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (offset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            buf = getCacheBlock(length/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    buf = new byte[BUFFER_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    throw new IOException("No memory left for cache!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int left = BUFFER_LENGTH - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int nbytes = (int)Math.min(len, (long)left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            nbytes = stream.read(buf, offset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (nbytes == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return length; // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (offset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                cache.add(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            length += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            offset += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (offset >= BUFFER_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // we've filled the current buffer, so a new one will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                // allocated next time around (and offset will be reset to 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Writes out a portion of the cache to an <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This method preserves no state about the output stream, and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * not dispose of any blocks containing bytes written.  To dispose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * blocks, use {@link #disposeBefore <code>disposeBefore()</code>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @exception IndexOutOfBoundsException if any portion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the requested data is not in the cache (including if <code>pos</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * is in a block already disposed), or if either <code>pos</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <code>len</code> is < 0.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   148
     * @throws IOException if there is an I/O exception while writing to the
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   149
     * stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void writeToStream(OutputStream stream, long pos, long len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (pos + len > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IndexOutOfBoundsException("Argument out of cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if ((pos < 0) || (len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IndexOutOfBoundsException("Negative pos or len");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        long bufIndex = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (bufIndex < cacheStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IndexOutOfBoundsException("pos already disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int offset = (int)(pos % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        byte[] buf = getCacheBlock(bufIndex++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                buf = getCacheBlock(bufIndex++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int nbytes = (int)Math.min(len, (long)(BUFFER_LENGTH - offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            stream.write(buf, offset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Ensure that there is space to write a byte at the given position.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   184
     *
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   185
     * throws IOException if there is no more memory left for cache
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void pad(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        long currIndex = cacheStart + cache.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        long lastIndex = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        long numNewBuffers = lastIndex - currIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (long i = 0; i < numNewBuffers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                cache.add(new byte[BUFFER_LENGTH]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                throw new IOException("No memory left for cache!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Overwrites and/or appends the cache from a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * The length of the cache will be extended as needed to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param b an array of bytes containing data to be written.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   206
     * @param off the starting offset within the data array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param len the number of bytes to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param pos the cache position at which to begin writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception NullPointerException if <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception IndexOutOfBoundsException if <code>off</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <code>len</code>, or <code>pos</code> are negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * or if <code>off+len > b.length</code>.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   214
     * @throws IOException if there is an I/O error while writing to the cache
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void write(byte[] b, int off, int len, long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if ((off < 0) || (len < 0) || (pos < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            (off + len > b.length) || (off + len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Ensure there is space for the incoming data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        long lastPos = pos + len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (lastPos >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            pad(lastPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            length = lastPos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // Copy the data into the cache, block by block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int offset = (int)(pos % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            byte[] buf = getCacheBlock(pos/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int nbytes = Math.min(len, BUFFER_LENGTH - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            System.arraycopy(b, off, buf, offset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            pos += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            offset = 0; // Always after the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Overwrites or appends a single byte to the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * The length of the cache will be extended as needed to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * the incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param b an <code>int</code> whose 8 least significant bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * will be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param pos the cache position at which to begin writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @exception IndexOutOfBoundsException if <code>pos</code> is negative.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   258
     * @throws IOException if there is an I/O error while writing to the cache
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void write(int b, long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new ArrayIndexOutOfBoundsException("pos < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Ensure there is space for the incoming data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (pos >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            pad(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            length = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // Insert the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        byte[] buf = getCacheBlock(pos/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int offset = (int)(pos % BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        buf[offset] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Returns the total length of data that has been cached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * regardless of whether any early blocks have been disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * This value will only ever increase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public long getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns the single byte at the given position, as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <code>int</code>.  Returns -1 if this position has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * not been cached or has been disposed.
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   290
     *
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   291
     * @throws IOException if an I/O error occurs while reading from the byte
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   292
     * array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public int read(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (pos >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        byte[] buf = getCacheBlock(pos/BUFFER_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return buf[(int)(pos % BUFFER_LENGTH)] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Copy <code>len</code> bytes from the cache, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * at cache position <code>pos</code>, into the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <code>b</code> at offset <code>off</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @exception NullPointerException if b is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception IndexOutOfBoundsException if <code>off</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <code>len</code> or <code>pos</code> are negative or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <code>off + len > b.length</code> or if any portion of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * requested data is not in the cache (including if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <code>pos</code> is in a block that has already been disposed).
26755
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   318
     * @throws IOException if an I/O exception occurs while reading from the
70eafa5df34b 6588417: Incorrect javadoc: no @throws or @exception tag in javax.*
yan
parents: 25859
diff changeset
   319
     * byte array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void read(byte[] b, int off, int len, long pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if ((off < 0) || (len < 0) || (pos < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            (off + len > b.length) || (off + len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (pos + len > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        long index = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int offset = (int)pos % BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int nbytes = Math.min(len, BUFFER_LENGTH - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            byte[] buf = getCacheBlock(index++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            System.arraycopy(buf, offset, b, off, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            offset = 0; // Always after the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Free the blocks up to the position <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * The byte at <code>pos</code> remains available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @exception IndexOutOfBoundsException if <code>pos</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * is in a block that has already been disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void disposeBefore(long pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        long index = pos/BUFFER_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (index < cacheStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new IndexOutOfBoundsException("pos already disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        long numBlocks = Math.min(index - cacheStart, cache.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        for (long i = 0; i < numBlocks; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            cache.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        this.cacheStart = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Erase the entire cache contents and reset the length to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * The cache object may subsequently be reused as though it had just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * been allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        cacheStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        length = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 }