package de.vdheide.io;

/**
 * An simulation of RandomAccessFile on a byte array. So far, only
 * read methods are implemented.
 * As some methods in RandomAccessFile are declared final (hail to the
 * developers for this piece of ****), this class cannot extend
 * RandomAccessFile and so cannot be used as a direct replacement.
 *
 * @author Jens Vonderheide <jvonderheide@redlink.de>
 */
public class RandomAccessByteArray implements RandomAccess {

	/**
	 * A flag that is set to true when this stream is closed.
	 */
	private boolean isClosed = false;

	/**
	 * An array of bytes that was provided
	 * by the creator of the stream. Elements <code>buf[0]</code>
	 * through <code>buf[count-1]</code> are the
	 * only bytes that can ever be read from the
	 * stream;  element <code>buf[pos]</code> is
	 * the next byte to be read.
	 */
	protected byte buf[];

	/**
	 * The index of the next character to read from the input stream buffer.
	 * This value should always be nonnegative
	 * and not larger than the value of <code>count</code>.
	 * The next byte to be read from the input stream buffer 
	 * will be <code>buf[pos]</code>.
	 */
	protected int pos;
	
	/**
	 * The maximum index.
	 */
	protected int count;

	/**
	 * Create a new ByteArrayRandomAccess instance.
	 *
	 * @param   buf   the input buffer.
	 */
	public RandomAccessByteArray(byte buf[]) {
		this.buf = buf;
		this.pos = 0;
		this.count = buf.length;
	}

	/**
	 * Reads the next byte of data. If no byte is available 
	 * because the end of the array has been reached, the value 
	 * <code>-1</code> is returned. 
	 *
	 * @return  the next byte of data, or <code>-1</code> if the end of the
	 *		  stream has been reached.
	 */
	public synchronized int read() {
		ensureOpen();
		return (pos < count) ? (buf[pos++] & 0xff) : -1;
	}

	/**
	 * Reads up to <code>len</code> bytes of data into an array of bytes.
	 * If <code>pos</code> equals <code>count</code>,
	 * then <code>-1</code> is returned to indicate
	 * end of file. Otherwise, the  number <code>k</code>
	 * of bytes read is equal to the smaller of
	 * <code>len</code> and <code>count-pos</code>.
	 * If <code>k</code> is positive, then bytes
	 * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
	 * are copied into <code>b[off]</code>  through
	 * <code>b[off+k-1]</code> in the manner performed
	 * by <code>System.arraycopy</code>. The
	 * value <code>k</code> is added into <code>pos</code>
	 * and <code>k</code> is returned.
	 *
	 * @param   b	 the buffer into which the data is read.
	 * @param   off   the start offset of the data.
	 * @param   len   the maximum number of bytes read.
	 * @return  the total number of bytes read into the buffer, or
	 *		  <code>-1</code> if there is no more data because the end of
	 *		  the stream has been reached.
	 */
	public synchronized int read(byte b[], int off, int len) {
		ensureOpen();
		if (b == null) {
			throw new NullPointerException();
		} else if ((off < 0) || (off > b.length) || (len < 0) ||
			((off + len) > b.length) || ((off + len) < 0)) {
			throw new IndexOutOfBoundsException();
		}
		if (pos >= count) {
			return -1;
		}
		if (pos + len > count) {
			len = count - pos;
		}
		if (len <= 0) {
			return 0;
		}
		System.arraycopy(buf, pos, b, off, len);
		pos += len;
		return len;
	}
	
	public synchronized int readUnsignedByte() {
		return read();
	}

	/**
	 * Set the array offset at which the next read occurs. The offset
	 * may not be set beyond the end of the array.
	 *
	 * @param   n   the offset position, measured in bytes from the beginning of the array, at which to set the pointer.
	 */
	public synchronized void seek(long n) {
		ensureOpen();
		if (n > count) {
			n = (long)count;
		}
		if (n < 0) {
			return;
		}
		pos = (int)n;
	}
	
	/**
	 * Get the current pointer position.
	 */
	public long getPointer() {
		return pos;
	}

	/**
	 * Closes this instance and releases any system resources 
	 * associated with it. 
	 */
	public synchronized void close() {
		isClosed = true;
	}
	
	public long length() {
		return buf.length;
	}

	/** Check to make sure that the stream has not been closed */
	private void ensureOpen() {
		/* This method does nothing for now.  Once we add throws clauses
	 * to the I/O methods in this class, it will throw an IOException
	 * if the stream has been closed.
	 */
	}

}
