|
From: <ls...@us...> - 2008-03-28 21:02:47
|
Revision: 3899
http://jnode.svn.sourceforge.net/jnode/?rev=3899&view=rev
Author: lsantha
Date: 2008-03-28 14:02:46 -0700 (Fri, 28 Mar 2008)
Log Message:
-----------
OpenJDK integration.
Added Paths:
-----------
trunk/core/src/openjdk/com/com/sun/imageio/stream/
trunk/core/src/openjdk/com/com/sun/imageio/stream/CloseableDisposerRecord.java
trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamCloser.java
trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamFinalizer.java
trunk/core/src/openjdk/javax/javax/imageio/stream/
trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageInputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageOutputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageInputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageOutputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/IIOByteBuffer.java
trunk/core/src/openjdk/javax/javax/imageio/stream/ImageInputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/ImageInputStreamImpl.java
trunk/core/src/openjdk/javax/javax/imageio/stream/ImageOutputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/ImageOutputStreamImpl.java
trunk/core/src/openjdk/javax/javax/imageio/stream/MemoryCache.java
trunk/core/src/openjdk/javax/javax/imageio/stream/MemoryCacheImageInputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/MemoryCacheImageOutputStream.java
trunk/core/src/openjdk/javax/javax/imageio/stream/package.html
Added: trunk/core/src/openjdk/com/com/sun/imageio/stream/CloseableDisposerRecord.java
===================================================================
--- trunk/core/src/openjdk/com/com/sun/imageio/stream/CloseableDisposerRecord.java (rev 0)
+++ trunk/core/src/openjdk/com/com/sun/imageio/stream/CloseableDisposerRecord.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package com.sun.imageio.stream;
+
+import java.io.Closeable;
+import java.io.IOException;
+import sun.java2d.DisposerRecord;
+
+/**
+ * Convenience class that closes a given resource (e.g. RandomAccessFile),
+ * typically associated with an Image{Input,Output}Stream, prior to the
+ * stream being garbage collected.
+ */
+public class CloseableDisposerRecord implements DisposerRecord {
+ private Closeable closeable;
+
+ public CloseableDisposerRecord(Closeable closeable) {
+ this.closeable = closeable;
+ }
+
+ public synchronized void dispose() {
+ if (closeable != null) {
+ try {
+ closeable.close();
+ } catch (IOException e) {
+ } finally {
+ closeable = null;
+ }
+ }
+ }
+}
Added: trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamCloser.java
===================================================================
--- trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamCloser.java (rev 0)
+++ trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamCloser.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package com.sun.imageio.stream;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.WeakHashMap;
+import javax.imageio.stream.ImageInputStream;
+
+/**
+ * This class provide means to properly close hanging
+ * image input/output streams on VM shutdown.
+ * This might be useful for proper cleanup such as removal
+ * of temporary files.
+ *
+ * Addition of stream do not prevent it from being garbage collected
+ * if no other references to it exists. Stream can be closed
+ * explicitly without removal from StreamCloser queue.
+ * Explicit removal from the queue only helps to save some memory.
+ */
+public class StreamCloser {
+
+ private static WeakHashMap<ImageInputStream, Object> toCloseQueue;
+ private static Thread streamCloser;
+
+ public static void addToQueue(ImageInputStream iis) {
+ synchronized (StreamCloser.class) {
+ if (toCloseQueue == null) {
+ toCloseQueue =
+ new WeakHashMap<ImageInputStream, Object>();
+ }
+
+ toCloseQueue.put(iis, null);
+
+ if (streamCloser == null) {
+ final Runnable streamCloserRunnable = new Runnable() {
+ public void run() {
+ if (toCloseQueue != null) {
+ synchronized (StreamCloser.class) {
+ Set<ImageInputStream> set =
+ toCloseQueue.keySet();
+ // Make a copy of the set in order to avoid
+ // concurrent modification (the is.close()
+ // will in turn call removeFromQueue())
+ ImageInputStream[] streams =
+ new ImageInputStream[set.size()];
+ streams = set.toArray(streams);
+ for (ImageInputStream is : streams) {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+ }
+ }
+ }
+ };
+
+ java.security.AccessController.doPrivileged(
+ new java.security.PrivilegedAction() {
+ public Object run() {
+ /* The thread must be a member of a thread group
+ * which will not get GCed before VM exit.
+ * Make its parent the top-level thread group.
+ */
+ ThreadGroup tg =
+ Thread.currentThread().getThreadGroup();
+ for (ThreadGroup tgn = tg;
+ tgn != null;
+ tg = tgn, tgn = tg.getParent());
+ streamCloser = new Thread(tg, streamCloserRunnable);
+ Runtime.getRuntime().addShutdownHook(streamCloser);
+ return null;
+ }
+ });
+ }
+ }
+ }
+
+ public static void removeFromQueue(ImageInputStream iis) {
+ synchronized (StreamCloser.class) {
+ if (toCloseQueue != null) {
+ toCloseQueue.remove(iis);
+ }
+ }
+ }
+}
Added: trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamFinalizer.java
===================================================================
--- trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamFinalizer.java (rev 0)
+++ trunk/core/src/openjdk/com/com/sun/imageio/stream/StreamFinalizer.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package com.sun.imageio.stream;
+
+import java.io.IOException;
+import javax.imageio.stream.ImageInputStream;
+
+/**
+ * Small class to assist in properly closing an ImageInputStream instance
+ * prior to garbage collection. The ImageInputStreamImpl class defines a
+ * finalize() method, but in a number of its public subclasses
+ * (e.g. FileImageInputStream) we override the finalize() method to be
+ * empty for performance reasons, and instead rely on the Disposer mechanism
+ * for closing/disposing resources. This is fine when one of these classes
+ * is instantiated directly (e.g. new FileImageInputStream()) but in the
+ * unlikely case where a user defines their own subclass of one of those
+ * streams, we need some way to get back to the behavior of
+ * ImageInputStreamImpl, which will call close() as part of finalization.
+ *
+ * Typically an Image{Input,Output}Stream will construct an instance of
+ * StreamFinalizer in its constructor if it detects that it has been
+ * subclassed by the user. The ImageInputStream instance will hold a
+ * reference to the StreamFinalizer, and the StreamFinalizer will hold a
+ * reference back to the ImageInputStream from which it was created. When
+ * both are no longer reachable, the StreamFinalizer.finalize() method will
+ * be called, which will take care of closing down the ImageInputStream.
+ *
+ * Clearly this is a bit of a hack, but it will likely only be used in the
+ * rarest of circumstances: when a user has subclassed one of the public
+ * stream classes. (It should be no worse than the old days when the public
+ * stream classes had non-empty finalize() methods.)
+ */
+public class StreamFinalizer {
+ private ImageInputStream stream;
+
+ public StreamFinalizer(ImageInputStream stream) {
+ this.stream = stream;
+ }
+
+ protected void finalize() throws Throwable {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ } finally {
+ stream = null;
+ super.finalize();
+ }
+ }
+}
Added: trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageInputStream.java
===================================================================
--- trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageInputStream.java (rev 0)
+++ trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageInputStream.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,286 @@
+/*
+ * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.imageio.stream;
+
+import java.io.DataInput;
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import com.sun.imageio.stream.StreamCloser;
+import com.sun.imageio.stream.StreamFinalizer;
+import sun.java2d.Disposer;
+import sun.java2d.DisposerRecord;
+
+/**
+ * An implementation of <code>ImageInputStream</code> that gets its
+ * input from a regular <code>InputStream</code>. A file is used to
+ * cache previously read data.
+ *
+ * @version 0.5
+ */
+public class FileCacheImageInputStream extends ImageInputStreamImpl {
+
+ private InputStream stream;
+
+ private File cacheFile;
+
+ private RandomAccessFile cache;
+
+ private static final int BUFFER_LENGTH = 1024;
+
+ private byte[] buf = new byte[BUFFER_LENGTH];
+
+ private long length = 0L;
+
+ private boolean foundEOF = false;
+
+ /** The referent to be registered with the Disposer. */
+ private final Object disposerReferent;
+
+ /** The DisposerRecord that closes the underlying cache. */
+ private final DisposerRecord disposerRecord;
+
+ /**
+ * Constructs a <code>FileCacheImageInputStream</code> that will read
+ * from a given <code>InputStream</code>.
+ *
+ * <p> A temporary file is used as a cache. If
+ * <code>cacheDir</code>is non-<code>null</code> and is a
+ * directory, the file will be created there. If it is
+ * <code>null</code>, the system-dependent default temporary-file
+ * directory will be used (see the documentation for
+ * <code>File.createTempFile</code> for details).
+ *
+ * @param stream an <code>InputStream</code> to read from.
+ * @param cacheDir a <code>File</code> indicating where the
+ * cache file should be created, or <code>null</code> to use the
+ * system directory.
+ *
+ * @exception IllegalArgumentException if <code>stream</code> is
+ * <code>null</code>.
+ * @exception IllegalArgumentException if <code>cacheDir</code> is
+ * non-<code>null</code> but is not a directory.
+ * @exception IOException if a cache file cannot be created.
+ */
+ public FileCacheImageInputStream(InputStream stream, File cacheDir)
+ throws IOException {
+ if (stream == null) {
+ throw new IllegalArgumentException("stream == null!");
+ }
+ if ((cacheDir != null) && !(cacheDir.isDirectory())) {
+ throw new IllegalArgumentException("Not a directory!");
+ }
+ this.stream = stream;
+ this.cacheFile =
+ File.createTempFile("imageio", ".tmp", cacheDir);
+ this.cache = new RandomAccessFile(cacheFile, "rw");
+ StreamCloser.addToQueue(this);
+
+ disposerRecord = new StreamDisposerRecord(cacheFile, cache);
+ if (getClass() == FileCacheImageInputStream.class) {
+ disposerReferent = new Object();
+ Disposer.addRecord(disposerReferent, disposerRecord);
+ } else {
+ disposerReferent = new StreamFinalizer(this);
+ }
+ }
+
+ /**
+ * Ensures that at least <code>pos</code> bytes are cached,
+ * or the end of the source is reached. The return value
+ * is equal to the smaller of <code>pos</code> and the
+ * length of the source file.
+ */
+ private long readUntil(long pos) throws IOException {
+ // We've already got enough data cached
+ if (pos < length) {
+ return pos;
+ }
+ // pos >= length but length isn't getting any bigger, so return it
+ if (foundEOF) {
+ return length;
+ }
+
+ long len = pos - length;
+ cache.seek(length);
+ while (len > 0) {
+ // Copy a buffer's worth of data from the source to the cache
+ // BUFFER_LENGTH will always fit into an int so this is safe
+ int nbytes =
+ stream.read(buf, 0, (int)Math.min(len, (long)BUFFER_LENGTH));
+ if (nbytes == -1) {
+ foundEOF = true;
+ return length;
+ }
+
+ cache.write(buf, 0, nbytes);
+ len -= nbytes;
+ length += nbytes;
+ }
+
+ return pos;
+ }
+
+ public int read() throws IOException {
+ checkClosed();
+ bitOffset = 0;
+ long next = streamPos + 1;
+ long pos = readUntil(next);
+ if (pos >= next) {
+ cache.seek(streamPos++);
+ return cache.read();
+ } else {
+ return -1;
+ }
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ checkClosed();
+
+ if (b == null) {
+ throw new NullPointerException("b == null!");
+ }
+ // Fix 4430357 - if off + len < 0, overflow occurred
+ if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
+ throw new IndexOutOfBoundsException
+ ("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
+ }
+
+ bitOffset = 0;
+
+ if (len == 0) {
+ return 0;
+ }
+
+ long pos = readUntil(streamPos + len);
+
+ // len will always fit into an int so this is safe
+ len = (int)Math.min((long)len, pos - streamPos);
+ if (len > 0) {
+ cache.seek(streamPos);
+ cache.readFully(b, off, len);
+ streamPos += len;
+ return len;
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns <code>true</code> since this
+ * <code>ImageInputStream</code> caches data in order to allow
+ * seeking backwards.
+ *
+ * @return <code>true</code>.
+ *
+ * @see #isCachedMemory
+ * @see #isCachedFile
+ */
+ public boolean isCached() {
+ return true;
+ }
+
+ /**
+ * Returns <code>true</code> since this
+ * <code>ImageInputStream</code> maintains a file cache.
+ *
+ * @return <code>true</code>.
+ *
+ * @see #isCached
+ * @see #isCachedMemory
+ */
+ public boolean isCachedFile() {
+ return true;
+ }
+
+ /**
+ * Returns <code>false</code> since this
+ * <code>ImageInputStream</code> does not maintain a main memory
+ * cache.
+ *
+ * @return <code>false</code>.
+ *
+ * @see #isCached
+ * @see #isCachedFile
+ */
+ public boolean isCachedMemory() {
+ return false;
+ }
+
+ /**
+ * Closes this <code>FileCacheImageInputStream</code>, closing
+ * and removing the cache file. The source <code>InputStream</code>
+ * is not closed.
+ *
+ * @exception IOException if an error occurs.
+ */
+ public void close() throws IOException {
+ super.close();
+ disposerRecord.dispose(); // this will close/delete the cache file
+ stream = null;
+ cache = null;
+ cacheFile = null;
+ StreamCloser.removeFromQueue(this);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void finalize() throws Throwable {
+ // Empty finalizer: for performance reasons we instead use the
+ // Disposer mechanism for ensuring that the underlying
+ // RandomAccessFile is closed/deleted prior to garbage collection
+ }
+
+ private static class StreamDisposerRecord implements DisposerRecord {
+ private File cacheFile;
+ private RandomAccessFile cache;
+
+ public StreamDisposerRecord(File cacheFile, RandomAccessFile cache) {
+ this.cacheFile = cacheFile;
+ this.cache = cache;
+ }
+
+ public synchronized void dispose() {
+ if (cache != null) {
+ try {
+ cache.close();
+ } catch (IOException e) {
+ } finally {
+ cache = null;
+ }
+ }
+ if (cacheFile != null) {
+ cacheFile.delete();
+ cacheFile = null;
+ }
+ // Note: Explicit removal of the stream from the StreamCloser
+ // queue is not mandatory in this case, as it will be removed
+ // automatically by GC shortly after this method is called.
+ }
+ }
+}
Added: trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageOutputStream.java
===================================================================
--- trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageOutputStream.java (rev 0)
+++ trunk/core/src/openjdk/javax/javax/imageio/stream/FileCacheImageOutputStream.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,254 @@
+/*
+ * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.imageio.stream;
+
+import java.io.DataInput;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.RandomAccessFile;
+import com.sun.imageio.stream.StreamCloser;
+
+/**
+ * An implementation of <code>ImageOutputStream</code> that writes its
+ * output to a regular <code>OutputStream</code>. A file is used to
+ * cache data until it is flushed to the output stream.
+ *
+ * @version 0.5
+ */
+public class FileCacheImageOutputStream extends ImageOutputStreamImpl {
+
+ private OutputStream stream;
+
+ private File cacheFile;
+
+ private RandomAccessFile cache;
+
+ // Pos after last (rightmost) byte written
+ private long maxStreamPos = 0L;
+
+ /**
+ * Constructs a <code>FileCacheImageOutputStream</code> that will write
+ * to a given <code>outputStream</code>.
+ *
+ * <p> A temporary file is used as a cache. If
+ * <code>cacheDir</code>is non-<code>null</code> and is a
+ * directory, the file will be created there. If it is
+ * <code>null</code>, the system-dependent default temporary-file
+ * directory will be used (see the documentation for
+ * <code>File.createTempFile</code> for details).
+ *
+ * @param stream an <code>OutputStream</code> to write to.
+ * @param cacheDir a <code>File</code> indicating where the
+ * cache file should be created, or <code>null</code> to use the
+ * system directory.
+ *
+ * @exception IllegalArgumentException if <code>stream</code>
+ * is <code>null</code>.
+ * @exception IllegalArgumentException if <code>cacheDir</code> is
+ * non-<code>null</code> but is not a directory.
+ * @exception IOException if a cache file cannot be created.
+ */
+ public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
+ throws IOException {
+ if (stream == null) {
+ throw new IllegalArgumentException("stream == null!");
+ }
+ if ((cacheDir != null) && !(cacheDir.isDirectory())) {
+ throw new IllegalArgumentException("Not a directory!");
+ }
+ this.stream = stream;
+ this.cacheFile =
+ File.createTempFile("imageio", ".tmp", cacheDir);
+ this.cache = new RandomAccessFile(cacheFile, "rw");
+ StreamCloser.addToQueue(this);
+ }
+
+ public int read() throws IOException {
+ checkClosed();
+ bitOffset = 0;
+ int val = cache.read();
+ if (val != -1) {
+ ++streamPos;
+ }
+ return val;
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ checkClosed();
+
+ if (b == null) {
+ throw new NullPointerException("b == null!");
+ }
+ if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
+ throw new IndexOutOfBoundsException
+ ("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
+ }
+
+ bitOffset = 0;
+
+ if (len == 0) {
+ return 0;
+ }
+
+ int nbytes = cache.read(b, off, len);
+ if (nbytes != -1) {
+ streamPos += nbytes;
+ }
+ return nbytes;
+ }
+
+ public void write(int b) throws IOException {
+ flushBits(); // this will call checkClosed() for us
+ cache.write(b);
+ ++streamPos;
+ maxStreamPos = Math.max(maxStreamPos, streamPos);
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException {
+ flushBits(); // this will call checkClosed() for us
+ cache.write(b, off, len);
+ streamPos += len;
+ maxStreamPos = Math.max(maxStreamPos, streamPos);
+ }
+
+ public long length() {
+ try {
+ checkClosed();
+ return cache.length();
+ } catch (IOException e) {
+ return -1L;
+ }
+ }
+
+ /**
+ * Sets the current stream position and resets the bit offset to
+ * 0. It is legal to seek past the end of the file; an
+ * <code>EOFException</code> will be thrown only if a read is
+ * performed. The file length will not be increased until a write
+ * is performed.
+ *
+ * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
+ * than the flushed position.
+ * @exception IOException if any other I/O error occurs.
+ */
+ public void seek(long pos) throws IOException {
+ checkClosed();
+
+ if (pos < flushedPos) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ cache.seek(pos);
+ this.streamPos = cache.getFilePointer();
+ maxStreamPos = Math.max(maxStreamPos, streamPos);
+ this.bitOffset = 0;
+ }
+
+ /**
+ * Returns <code>true</code> since this
+ * <code>ImageOutputStream</code> caches data in order to allow
+ * seeking backwards.
+ *
+ * @return <code>true</code>.
+ *
+ * @see #isCachedMemory
+ * @see #isCachedFile
+ */
+ public boolean isCached() {
+ return true;
+ }
+
+ /**
+ * Returns <code>true</code> since this
+ * <code>ImageOutputStream</code> maintains a file cache.
+ *
+ * @return <code>true</code>.
+ *
+ * @see #isCached
+ * @see #isCachedMemory
+ */
+ public boolean isCachedFile() {
+ return true;
+ }
+
+ /**
+ * Returns <code>false</code> since this
+ * <code>ImageOutputStream</code> does not maintain a main memory
+ * cache.
+ *
+ * @return <code>false</code>.
+ *
+ * @see #isCached
+ * @see #isCachedFile
+ */
+ public boolean isCachedMemory() {
+ return false;
+ }
+
+ /**
+ * Closes this <code>FileCacheImageOututStream</code>. All
+ * pending data is flushed to the output, and the cache file
+ * is closed and removed. The destination <code>OutputStream</code>
+ * is not closed.
+ *
+ * @exception IOException if an error occurs.
+ */
+ public void close() throws IOException {
+ maxStreamPos = cache.length();
+
+ seek(maxStreamPos);
+ flushBefore(maxStreamPos);
+ super.close();
+ cache.close();
+ cache = null;
+ cacheFile.delete();
+ cacheFile = null;
+ stream.flush();
+ stream = null;
+ StreamCloser.removeFromQueue(this);
+ }
+
+ public void flushBefore(long pos) throws IOException {
+ long oFlushedPos = flushedPos;
+ super.flushBefore(pos); // this will call checkClosed() for us
+
+ long flushBytes = flushedPos - oFlushedPos;
+ if (flushBytes > 0) {
+ int bufLen = 512;
+ byte[] buf = new byte[bufLen];
+ cache.seek(oFlushedPos);
+ while (flushBytes > 0) {
+ int len = (int)Math.min(flushBytes, bufLen);
+ cache.readFully(buf, 0, len);
+ stream.write(buf, 0, len);
+ flushBytes -= len;
+ }
+ stream.flush();
+ }
+ }
+}
Added: trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageInputStream.java
===================================================================
--- trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageInputStream.java (rev 0)
+++ trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageInputStream.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.imageio.stream;
+
+import java.io.DataInput;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import com.sun.imageio.stream.CloseableDisposerRecord;
+import com.sun.imageio.stream.StreamFinalizer;
+import sun.java2d.Disposer;
+
+/**
+ * An implementation of <code>ImageInputStream</code> that gets its
+ * input from a <code>File</code> or <code>RandomAccessFile</code>.
+ * The file contents are assumed to be stable during the lifetime of
+ * the object.
+ *
+ * @version 0.5
+ */
+public class FileImageInputStream extends ImageInputStreamImpl {
+
+ private RandomAccessFile raf;
+
+ /** The referent to be registered with the Disposer. */
+ private final Object disposerReferent;
+
+ /** The DisposerRecord that closes the underlying RandomAccessFile. */
+ private final CloseableDisposerRecord disposerRecord;
+
+ /**
+ * Constructs a <code>FileImageInputStream</code> that will read
+ * from a given <code>File</code>.
+ *
+ * <p> The file contents must not change between the time this
+ * object is constructed and the time of the last call to a read
+ * method.
+ *
+ * @param f a <code>File</code> to read from.
+ *
+ * @exception IllegalArgumentException if <code>f</code> is
+ * <code>null</code>.
+ * @exception SecurityException if a security manager exists
+ * and does not allow read access to the file.
+ * @exception FileNotFoundException if <code>f</code> is a
+ * directory or cannot be opened for reading for any other reason.
+ * @exception IOException if an I/O error occurs.
+ */
+ public FileImageInputStream(File f)
+ throws FileNotFoundException, IOException {
+ this(f == null ? null : new RandomAccessFile(f, "r"));
+ }
+
+ /**
+ * Constructs a <code>FileImageInputStream</code> that will read
+ * from a given <code>RandomAccessFile</code>.
+ *
+ * <p> The file contents must not change between the time this
+ * object is constructed and the time of the last call to a read
+ * method.
+ *
+ * @param raf a <code>RandomAccessFile</code> to read from.
+ *
+ * @exception IllegalArgumentException if <code>raf</code> is
+ * <code>null</code>.
+ */
+ public FileImageInputStream(RandomAccessFile raf) {
+ if (raf == null) {
+ throw new IllegalArgumentException("raf == null!");
+ }
+ this.raf = raf;
+
+ disposerRecord = new CloseableDisposerRecord(raf);
+ if (getClass() == FileImageInputStream.class) {
+ disposerReferent = new Object();
+ Disposer.addRecord(disposerReferent, disposerRecord);
+ } else {
+ disposerReferent = new StreamFinalizer(this);
+ }
+ }
+
+ public int read() throws IOException {
+ checkClosed();
+ bitOffset = 0;
+ int val = raf.read();
+ if (val != -1) {
+ ++streamPos;
+ }
+ return val;
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ checkClosed();
+ bitOffset = 0;
+ int nbytes = raf.read(b, off, len);
+ if (nbytes != -1) {
+ streamPos += nbytes;
+ }
+ return nbytes;
+ }
+
+ /**
+ * Returns the length of the underlying file, or <code>-1</code>
+ * if it is unknown.
+ *
+ * @return the file length as a <code>long</code>, or
+ * <code>-1</code>.
+ */
+ public long length() {
+ try {
+ checkClosed();
+ return raf.length();
+ } catch (IOException e) {
+ return -1L;
+ }
+ }
+
+ public void seek(long pos) throws IOException {
+ checkClosed();
+ if (pos < flushedPos) {
+ throw new IndexOutOfBoundsException("pos < flushedPos!");
+ }
+ bitOffset = 0;
+ raf.seek(pos);
+ streamPos = raf.getFilePointer();
+ }
+
+ public void close() throws IOException {
+ super.close();
+ disposerRecord.dispose(); // this closes the RandomAccessFile
+ raf = null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void finalize() throws Throwable {
+ // Empty finalizer: for performance reasons we instead use the
+ // Disposer mechanism for ensuring that the underlying
+ // RandomAccessFile is closed prior to garbage collection
+ }
+}
Added: trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageOutputStream.java
===================================================================
--- trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageOutputStream.java (rev 0)
+++ trunk/core/src/openjdk/javax/javax/imageio/stream/FileImageOutputStream.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.imageio.stream;
+
+import java.io.DataInput;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import com.sun.imageio.stream.CloseableDisposerRecord;
+import com.sun.imageio.stream.StreamFinalizer;
+import sun.java2d.Disposer;
+
+/**
+ * An implementation of <code>ImageOutputStream</code> that writes its
+ * output directly to a <code>File</code> or
+ * <code>RandomAccessFile</code>.
+ *
+ * @version 0.5
+ */
+public class FileImageOutputStream extends ImageOutputStreamImpl {
+
+ private RandomAccessFile raf;
+
+ /** The referent to be registered with the Disposer. */
+ private final Object disposerReferent;
+
+ /** The DisposerRecord that closes the underlying RandomAccessFile. */
+ private final CloseableDisposerRecord disposerRecord;
+
+ /**
+ * Constructs a <code>FileImageOutputStream</code> that will write
+ * to a given <code>File</code>.
+ *
+ * @param f a <code>File</code> to write to.
+ *
+ * @exception IllegalArgumentException if <code>f</code> is
+ * <code>null</code>.
+ * @exception SecurityException if a security manager exists
+ * and does not allow write access to the file.
+ * @exception FileNotFoundException if <code>f</code> does not denote
+ * a regular file or it cannot be opened for reading and writing for any
+ * other reason.
+ * @exception IOException if an I/O error occurs.
+ */
+ public FileImageOutputStream(File f)
+ throws FileNotFoundException, IOException {
+ this(f == null ? null : new RandomAccessFile(f, "rw"));
+ }
+
+ /**
+ * Constructs a <code>FileImageOutputStream</code> that will write
+ * to a given <code>RandomAccessFile</code>.
+ *
+ * @param raf a <code>RandomAccessFile</code> to write to.
+ *
+ * @exception IllegalArgumentException if <code>raf</code> is
+ * <code>null</code>.
+ */
+ public FileImageOutputStream(RandomAccessFile raf) {
+ if (raf == null) {
+ throw new IllegalArgumentException("raf == null!");
+ }
+ this.raf = raf;
+
+ disposerRecord = new CloseableDisposerRecord(raf);
+ if (getClass() == FileImageOutputStream.class) {
+ disposerReferent = new Object();
+ Disposer.addRecord(disposerReferent, disposerRecord);
+ } else {
+ disposerReferent = new StreamFinalizer(this);
+ }
+ }
+
+ public int read() throws IOException {
+ checkClosed();
+ bitOffset = 0;
+ int val = raf.read();
+ if (val != -1) {
+ ++streamPos;
+ }
+ return val;
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ checkClosed();
+ bitOffset = 0;
+ int nbytes = raf.read(b, off, len);
+ if (nbytes != -1) {
+ streamPos += nbytes;
+ }
+ return nbytes;
+ }
+
+ public void write(int b) throws IOException {
+ flushBits(); // this will call checkClosed() for us
+ raf.write(b);
+ ++streamPos;
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException {
+ flushBits(); // this will call checkClosed() for us
+ raf.write(b, off, len);
+ streamPos += len;
+ }
+
+ public long length() {
+ try {
+ checkClosed();
+ return raf.length();
+ } catch (IOException e) {
+ return -1L;
+ }
+ }
+
+ /**
+ * Sets the current stream position and resets the bit offset to
+ * 0. It is legal to seeking past the end of the file; an
+ * <code>EOFException</code> will be thrown only if a read is
+ * performed. The file length will not be increased until a write
+ * is performed.
+ *
+ * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
+ * than the flushed position.
+ * @exception IOException if any other I/O error occurs.
+ */
+ public void seek(long pos) throws IOException {
+ checkClosed();
+ if (pos < flushedPos) {
+ throw new IndexOutOfBoundsException("pos < flushedPos!");
+ }
+ bitOffset = 0;
+ raf.seek(pos);
+ streamPos = raf.getFilePointer();
+ }
+
+ public void close() throws IOException {
+ super.close();
+ disposerRecord.dispose(); // this closes the RandomAccessFile
+ raf = null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void finalize() throws Throwable {
+ // Empty finalizer: for performance reasons we instead use the
+ // Disposer mechanism for ensuring that the underlying
+ // RandomAccessFile is closed prior to garbage collection
+ }
+}
Added: trunk/core/src/openjdk/javax/javax/imageio/stream/IIOByteBuffer.java
===================================================================
--- trunk/core/src/openjdk/javax/javax/imageio/stream/IIOByteBuffer.java (rev 0)
+++ trunk/core/src/openjdk/javax/javax/imageio/stream/IIOByteBuffer.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,144 @@
+/*
+ * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.imageio.stream;
+
+/**
+ * A class representing a mutable reference to an array of bytes and
+ * an offset and length within that array. <code>IIOByteBuffer</code>
+ * is used by <code>ImageInputStream</code> to supply a sequence of bytes
+ * to the caller, possibly with fewer copies than using the conventional
+ * <code>read</code> methods that take a user-supplied byte array.
+ *
+ * <p> The byte array referenced by an <code>IIOByteBuffer</code> will
+ * generally be part of an internal data structure belonging to an
+ * <code>ImageReader</code> implementation; its contents should be
+ * considered read-only and must not be modified.
+ *
+ * @version 0.5
+ */
+public class IIOByteBuffer {
+
+ private byte[] data;
+
+ private int offset;
+
+ private int length;
+
+ /**
+ * Constructs an <code>IIOByteBuffer</code> that references a
+ * given byte array, offset, and length.
+ *
+ * @param data a byte array.
+ * @param offset an int offset within the array.
+ * @param length an int specifying the length of the data of
+ * interest within byte array, in bytes.
+ */
+ public IIOByteBuffer(byte[] data, int offset, int length) {
+ this.data = data;
+ this.offset = offset;
+ this.length = length;
+ }
+
+ /**
+ * Returns a reference to the byte array. The returned value should
+ * be treated as read-only, and only the portion specified by the
+ * values of <code>getOffset</code> and <code>getLength</code> should
+ * be used.
+ *
+ * @return a byte array reference.
+ *
+ * @see #getOffset
+ * @see #getLength
+ * @see #setData
+ */
+ public byte[] getData() {
+ return data;
+ }
+
+ /**
+ * Updates the array reference that will be returned by subsequent calls
+ * to the <code>getData</code> method.
+ *
+ * @param data a byte array reference containing the new data value.
+ *
+ * @see #getData
+ */
+ public void setData(byte[] data) {
+ this.data = data;
+ }
+
+ /**
+ * Returns the offset within the byte array returned by
+ * <code>getData</code> at which the data of interest start.
+ *
+ * @return an int offset.
+ *
+ * @see #getData
+ * @see #getLength
+ * @see #setOffset
+ */
+ public int getOffset() {
+ return offset;
+ }
+
+ /**
+ * Updates the value that will be returned by subsequent calls
+ * to the <code>getOffset</code> method.
+ *
+ * @param offset an int containing the new offset value.
+ *
+ * @see #getOffset
+ */
+ public void setOffset(int offset) {
+ this.offset = offset;
+ }
+
+ /**
+ * Returns the length of the data of interest within the byte
+ * array returned by <code>getData</code>.
+ *
+ * @return an int length.
+ *
+ * @see #getData
+ * @see #getOffset
+ * @see #setLength
+ */
+ public int getLength() {
+ return length;
+ }
+
+ /**
+ * Updates the value that will be returned by subsequent calls
+ * to the <code>getLength</code> method.
+ *
+ * @param length an int containing the new length value.
+ *
+ * @see #getLength
+ */
+ public void setLength(int length) {
+ this.length = length;
+ }
+}
Added: trunk/core/src/openjdk/javax/javax/imageio/stream/ImageInputStream.java
===================================================================
--- trunk/core/src/openjdk/javax/javax/imageio/stream/ImageInputStream.java (rev 0)
+++ trunk/core/src/openjdk/javax/javax/imageio/stream/ImageInputStream.java 2008-03-28 21:02:46 UTC (rev 3899)
@@ -0,0 +1,1002 @@
+/*
+ * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.imageio.stream;
+
+import java.io.DataInput;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.UTFDataFormatException;
+import java.nio.ByteOrder;
+
+/**
+ * A seekable input stream interface for use by
+ * <code>ImageReader</code>s. Various input sources, such as
+ * <code>InputStream</code>s and <code>File</code>s,
+ * as well as future fast I/O sources may be "wrapped" by a suitable
+ * implementation of this interface for use by the Image I/O API.
+ *
+ * @see ImageInputStreamImpl
+ * @see FileImageInputStream
+ * @see FileCacheImageInputStream
+ * @see MemoryCacheImageInputStream
+ *
+ * @version 0.5
+ */
+public interface ImageInputStream extends DataInput {
+
+ /**
+ * Sets the desired byte order for future reads of data values
+ * from this stream. For example, the sequence of bytes '0x01
+ * 0x02 0x03 0x04' if read as a 4-byte integer would have the
+ * value '0x01020304' using network byte order and the value
+ * '0x04030201' under the reverse byte order.
+ *
+ * <p> The enumeration class <code>java.nio.ByteOrder</code> is
+ * used to specify the byte order. A value of
+ * <code>ByteOrder.BIG_ENDIAN</code> specifies so-called
+ * big-endian or network byte order, in which the high-order byte
+ * comes first. Motorola and Sparc processors store data in this
+ * format, while Intel processors store data in the reverse
+ * <code>ByteOrder.LITTLE_ENDIAN</code> order.
+ *
+ * <p> The byte order has no effect on the results returned from
+ * the <code>readBits</code> method (or the value written by
+ * <code>ImageOutputStream.writeBits</code>).
+ *
+ * @param byteOrder one of <code>ByteOrder.BIG_ENDIAN</code> or
+ * <code>java.nio.ByteOrder.LITTLE_ENDIAN</code>, indicating whether
+ * network byte order or its reverse will be used for future
+ * reads.
+ *
+ * @see java.nio.ByteOrder
+ * @see #getByteOrder
+ * @see #readBits(int)
+ */
+ void setByteOrder(ByteOrder byteOrder);
+
+ /**
+ * Returns the byte order with which data values will be read from
+ * this stream as an instance of the
+ * <code>java.nio.ByteOrder</code> enumeration.
+ *
+ * @return one of <code>ByteOrder.BIG_ENDIAN</code> or
+ * <code>ByteOrder.LITTLE_ENDIAN</code>, indicating which byte
+ * order is being used.
+ *
+ * @see java.nio.ByteOrder
+ * @see #setByteOrder
+ */
+ ByteOrder getByteOrder();
+
+ /**
+ * Reads a single byte from the stream and returns it as an
+ * integer between 0 and 255. If the end of the stream is
+ * reached, -1 is returned.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return a byte value from the stream, as an int, or -1 to
+ * indicate EOF.
+ *
+ * @exception IOException if an I/O error occurs.
+ */
+ int read() throws IOException;
+
+ /**
+ * Reads up to <code>b.length</code> bytes from the stream, and
+ * stores them into <code>b</code> starting at index 0. The
+ * number of bytes read is returned. If no bytes can be read
+ * because the end of the stream has been reached, -1 is returned.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @param b an array of bytes to be written to.
+ *
+ * @return the number of bytes actually read, or <code>-1</code>
+ * to indicate EOF.
+ *
+ * @exception NullPointerException if <code>b</code> is
+ * <code>null</code>.
+ *
+ * @exception IOException if an I/O error occurs.
+ */
+ int read(byte[] b) throws IOException;
+
+ /**
+ * Reads up to <code>len</code> bytes from the stream, and stores
+ * them into <code>b</code> starting at index <code>off</code>.
+ * The number of bytes read is returned. If no bytes can be read
+ * because the end of the stream has been reached, <code>-1</code>
+ * is returned.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @param b an array of bytes to be written to.
+ * @param off the starting position within <code>b</code> to write to.
+ * @param len the maximum number of <code>byte</code>s to read.
+ *
+ * @return the number of bytes actually read, or <code>-1</code>
+ * to indicate EOF.
+ *
+ * @exception NullPointerException if <code>b</code> is
+ * <code>null</code>.
+ * @exception IndexOutOfBoundsException if <code>off</code> is
+ * negative, <code>len</code> is negative, or <code>off +
+ * len</code> is greater than <code>b.length</code>.
+ * @exception IOException if an I/O error occurs.
+ */
+ int read(byte[] b, int off, int len) throws IOException;
+
+ /**
+ * Reads up to <code>len</code> bytes from the stream, and
+ * modifies the supplied <code>IIOByteBuffer</code> to indicate
+ * the byte array, offset, and length where the data may be found.
+ * The caller should not attempt to modify the data found in the
+ * <code>IIOByteBuffer</code>.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @param buf an IIOByteBuffer object to be modified.
+ * @param len the maximum number of <code>byte</code>s to read.
+ *
+ * @exception IndexOutOfBoundsException if <code>len</code> is
+ * negative.
+ * @exception NullPointerException if <code>buf</code> is
+ * <code>null</code>.
+ *
+ * @exception IOException if an I/O error occurs.
+ */
+ void readBytes(IIOByteBuffer buf, int len) throws IOException;
+
+ /**
+ * Reads a byte from the stream and returns a <code>boolean</code>
+ * value of <code>true</code> if it is nonzero, <code>false</code>
+ * if it is zero.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return a boolean value from the stream.
+ *
+ * @exception EOFException if the end of the stream is reached.
+ * @exception IOException if an I/O error occurs.
+ */
+ boolean readBoolean() throws IOException;
+
+ /**
+ * Reads a byte from the stream and returns it as a
+ * <code>byte</code> value. Byte values between <code>0x00</code>
+ * and <code>0x7f</code> represent integer values between
+ * <code>0</code> and <code>127</code>. Values between
+ * <code>0x80</code> and <code>0xff</code> represent negative
+ * values from <code>-128</code> to <code>/1</code>.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return a signed byte value from the stream.
+ *
+ * @exception EOFException if the end of the stream is reached.
+ * @exception IOException if an I/O error occurs.
+ */
+ byte readByte() throws IOException;
+
+ /**
+ * Reads a byte from the stream, and (conceptually) converts it to
+ * an int, masks it with <code>0xff</code> in order to strip off
+ * any sign-extension bits, and returns it as a <code>byte</code>
+ * value.
+ *
+ * <p> Thus, byte values between <code>0x00</code> and
+ * <code>0x7f</code> are simply returned as integer values between
+ * <code>0</code> and <code>127</code>. Values between
+ * <code>0x80</code> and <code>0xff</code>, which normally
+ * represent negative <code>byte</code>values, will be mapped into
+ * positive integers between <code>128</code> and
+ * <code>255</code>.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return an unsigned byte value from the stream.
+ *
+ * @exception EOFException if the end of the stream is reached.
+ * @exception IOException if an I/O error occurs.
+ */
+ int readUnsignedByte() throws IOException;
+
+ /**
+ * Reads two bytes from the stream, and (conceptually)
+ * concatenates them according to the current byte order, and
+ * returns the result as a <code>short</code> value.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return a signed short value from the stream.
+ *
+ * @exception EOFException if the stream reaches the end before
+ * reading all the bytes.
+ * @exception IOException if an I/O error occurs.
+ *
+ * @see #getByteOrder
+ */
+ short readShort() throws IOException;
+
+ /**
+ * Reads two bytes from the stream, and (conceptually)
+ * concatenates them according to the current byte order, converts
+ * the resulting value to an <code>int</code>, masks it with
+ * <code>0xffff</code> in order to strip off any sign-extension
+ * buts, and returns the result as an unsigned <code>int</code>
+ * value.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return an unsigned short value from the stream, as an int.
+ *
+ * @exception EOFException if the stream reaches the end before
+ * reading all the bytes.
+ * @exception IOException if an I/O error occurs.
+ *
+ * @see #getByteOrder
+ */
+ int readUnsignedShort() throws IOException;
+
+ /**
+ * Equivalent to <code>readUnsignedShort</code>, except that the
+ * result is returned using the <code>char</code> datatype.
+ *
+ * <p> The bit offset within the stream is reset to zero before
+ * the read occurs.
+ *
+ * @return an unsigned char value from the stream.
+ *
+ * @exception EOFException if the stream reaches the end before
+ * reading all the bytes.
+ * @exception IOException if an I/O error occurs.
+ *
+ * @see #readUnsignedShort
+ */
+ char readChar() throws IOExcept...
[truncated message content] |