jdk/src/share/classes/javax/imageio/stream/MemoryCacheImageOutputStream.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, 2006, 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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An implementation of <code>ImageOutputStream</code> that writes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * output to a regular <code>OutputStream</code>.  A memory buffer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * used to cache at least the data between the discard position and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the current write position.  The only constructor takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>OutputStream</code>, so this class may not be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * read/modify/write operations.  Reading can occur only on parts of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the stream that have already been written to the cache and not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * yet flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class MemoryCacheImageOutputStream extends ImageOutputStreamImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private OutputStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private MemoryCache cache = new MemoryCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Constructs a <code>MemoryCacheImageOutputStream</code> that will write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * to a given <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param stream an <code>OutputStream</code> to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @exception IllegalArgumentException if <code>stream</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public MemoryCacheImageOutputStream(OutputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new IllegalArgumentException("stream == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        int val = cache.read(streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (val != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new NullPointerException("b == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Fix 4467608: read([B,I,I) works incorrectly if len<=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                ("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // check if we're already at/past EOF i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // no more bytes left to read from cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        long bytesLeftInCache = cache.getLength() - streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (bytesLeftInCache <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return -1; // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // guaranteed by now that bytesLeftInCache > 0 && len > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // and so the rest of the error checking is done by cache.read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // NOTE that alot of error checking is duplicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        len = (int)Math.min(bytesLeftInCache, (long)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        cache.read(b, off, len, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        streamPos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        cache.write(b, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ++streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        flushBits(); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        cache.write(b, off, len, streamPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        streamPos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return cache.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>ImageOutputStream</code> caches data in order to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * seeking backwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public boolean isCached() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Returns <code>false</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <code>ImageOutputStream</code> does not maintain a file cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @see #isCachedMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public boolean isCachedFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns <code>true</code> since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>ImageOutputStream</code> maintains a main memory cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see #isCached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see #isCachedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public boolean isCachedMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Closes this <code>MemoryCacheImageOutputStream</code>.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * pending data is flushed to the output, and the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * is released.  The destination <code>OutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * is not closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        long length = cache.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        seek(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        flushBefore(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        cache.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void flushBefore(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        long oFlushedPos = flushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        super.flushBefore(pos); // this will call checkClosed() for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        long flushBytes = flushedPos - oFlushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        cache.writeToStream(stream, oFlushedPos, flushBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        cache.disposeBefore(flushedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}