package com.fr.third.JAI;
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduct the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
import java.io.InputStream;
import java.io.IOException;
/**
* A subclass of SeekableStream
that takes input from an
* array of bytes. Seeking backwards is supported. The
* mark()
and resest()
methods are
* supported.
*
*
This class is not a committed part of the JAI API. It may
* be removed or changed in future releases of JAI.
*/
public class ByteArraySeekableStream extends SeekableStream {
/** Array holding the source data. */
private byte[] src;
/** The starting offset within the array. */
private int offset;
/** The length of the valid segment of the array. */
private int length;
/** The current output position. */
private int pointer;
/**
* Constructs a ByteArraySeekableStream
taking
* input from a given segment of an input byte
array.
*/
public ByteArraySeekableStream(byte[] src, int offset, int length)
throws IOException {
this.src = src;
this.offset = offset;
this.length = length;
}
/**
* Constructs a ByteArraySeekableStream
taking
* input from an entire input byte
array.
*/
public ByteArraySeekableStream(byte[] src) throws IOException {
this(src, 0, src.length);
}
/**
* Returns true
since this object supports seeking
* backwards.
*/
public boolean canSeekBackwards() {
return true;
}
/**
* Returns the current offset in this stream.
*
* @return the offset from the beginning of the stream, in bytes,
* at which the next read occurs.
*/
public long getFilePointer() {
return (long)pointer;
}
/**
* Sets the offset, measured from the beginning of this
* stream, at which the next read occurs. Seeking backwards is
* allowed.
*
* @param pos the offset position, measured in bytes from the
* beginning of the stream, at which to set the stream
* pointer.
*/
public void seek(long pos) {
pointer = (int)pos;
}
/**
* Reads the next byte of data from the input array. The value byte is
* returned as an int
in the range 0
to
* 255
. If no byte is available because the end of the stream
* has been reached, the value -1
is returned.
*/
public int read() {
if (pointer < length + offset) {
return (int)(src[pointer++ + offset] & 0xff);
} else {
return -1;
}
}
/**
* Copies up to len
bytes of data from the input array into
* an array of bytes. An attempt is made to copy as many as
* len
bytes, but a smaller number may be copied, possibly
* zero. The number of bytes actually copied is returned as an integer.
*
*
If b
is null
, a
* NullPointerException
is thrown.
*
*
If off
is negative, or len
is negative, or
* off+len
is greater than the length of the array
* b
, then an IndexOutOfBoundsException
is
* thrown.
*
*
If len
is zero, then no bytes are copied and
* 0
is returned; otherwise, there is an attempt to copy at
* least one byte. If no byte is available because the stream is at end of
* stream, the value -1
is returned; otherwise, at least one
* byte is copied into b
.
*
*
The first byte copied is stored into element
* b[off]
, the next one into b[off+1]
,
* and so on. The number of bytes copied is, at most, equal to
* len
. Let k be the number of bytes actually
* copied; these bytes will be stored in elements
* b[off]
through
* b[off+
k-1]
, leaving elements
* b[off+
k]
through
* b[off+len-1]
unaffected.
*
*
In every case, elements b[0]
through
* b[off]
and elements b[off+len]
through
* b[b.length-1]
are unaffected.
*
* @param b the buffer into which the data is copied.
* @param off the start offset in array b
* at which the data is written.
* @param len the maximum number of bytes to copy.
* @return the total number of bytes read into the buffer, or
* -1
if there is no more data because the end of
* the stream has been reached.
*/
public int read(byte[] b, int off, int len) {
if (b == null) {
throw new NullPointerException();
}
if ((off < 0) || (len < 0) || (off + len > b.length)) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return 0;
}
int oldPointer = pointer;
pointer = Math.min(pointer + len, length + offset);
if (pointer == oldPointer) {
return -1;
} else {
System.arraycopy(src, oldPointer, b, off, pointer - oldPointer);
return pointer - oldPointer;
}
}
/**
* Attempts to skip over n
bytes of input discarding the
* skipped bytes.
*
*
* This method may skip over some smaller number of bytes, possibly zero.
* This may result from any of a number of conditions; reaching end of
* stream before n
bytes have been skipped is only one
* possibility. This method never throws an EOFException
.
* The actual number of bytes skipped is returned. If n
* is negative, no bytes are skipped.
*
* @param n the number of bytes to be skipped.
* @return the actual number of bytes skipped.
*/
public int skipBytes(int n) {
int oldPointer = pointer;
pointer = Math.min(pointer + n, length + offset);
return pointer - oldPointer;
}
/** Does nothing. */
public void close() {
}
/** Returns the number of valid bytes in the input array. */
public long length() {
return length;
}
}