jdk/src/java.desktop/share/classes/javax/imageio/stream/MemoryCacheImageInputStream.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 5506 jdk/src/share/classes/javax/imageio/stream/MemoryCacheImageInputStream.java@202f599c92aa
child 35667 ed476aba94de
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.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, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.imageio.stream.StreamFinalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.java2d.Disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.java2d.DisposerRecord;
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>ImageInputStream</code> that gets its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * input from a regular <code>InputStream</code>.  A memory buffer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * used to cache at least the data between the discard position and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the current read position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> In general, it is preferable to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>FileCacheImageInputStream</code> when reading from a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>InputStream</code>.  This class is provided for cases where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * it is not possible to create a writable temporary file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class MemoryCacheImageInputStream extends ImageInputStreamImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private InputStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private MemoryCache cache = new MemoryCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** The referent to be registered with the Disposer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final Object disposerReferent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** The DisposerRecord that resets the underlying MemoryCache. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final DisposerRecord disposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Constructs a <code>MemoryCacheImageInputStream</code> that will read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * from a given <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param stream an <code>InputStream</code> to read from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception IllegalArgumentException if <code>stream</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public MemoryCacheImageInputStream(InputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new IllegalArgumentException("stream == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        disposerRecord = new StreamDisposerRecord(cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (getClass() == MemoryCacheImageInputStream.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            disposerReferent = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Disposer.addRecord(disposerReferent, disposerRecord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            disposerReferent = new StreamFinalizer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        long pos = cache.loadFromStream(stream, streamPos+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (pos >= streamPos+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return cache.read(streamPos++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                ("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        long pos = cache.loadFromStream(stream, streamPos+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        len = (int)(pos - streamPos);  // In case stream ended early
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            cache.read(b, off, len, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            streamPos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void flushBefore(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        super.flushBefore(pos); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        cache.disposeBefore(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>ImageInputStream</code> caches data in order to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * seeking backwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public boolean isCached() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Returns <code>false</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>ImageInputStream</code> does not maintain a file cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public boolean isCachedFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>ImageInputStream</code> maintains a main memory cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public boolean isCachedMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Closes this <code>MemoryCacheImageInputStream</code>, freeing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the cache.  The source <code>InputStream</code> is not closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        disposerRecord.dispose(); // this resets the MemoryCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Empty finalizer: for performance reasons we instead use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Disposer mechanism for ensuring that the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // MemoryCache is reset prior to garbage collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private static class StreamDisposerRecord implements DisposerRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        private MemoryCache cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        public StreamDisposerRecord(MemoryCache cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.cache = cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        public synchronized void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                cache.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}