rlm@3: /*
rlm@3: * @(#)SeekableByteArrayOutputStream.java 1.0 2010-12-27
rlm@3: *
rlm@3: * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland.
rlm@3: * All rights reserved.
rlm@3: *
rlm@3: * You may not use, copy or modify this file, except in compliance with the
rlm@3: * license agreement you entered into with Werner Randelshofer.
rlm@3: * For details see accompanying license terms.
rlm@3: */
rlm@3:
rlm@3: package com.aurellem.capture;
rlm@3:
rlm@3: import java.io.ByteArrayOutputStream;
rlm@3: import java.io.IOException;
rlm@3: import java.io.OutputStream;
rlm@3: import java.util.Arrays;
rlm@3: import static java.lang.Math.*;
rlm@3: /**
rlm@3: * {@code SeekableByteArrayOutputStream}.
rlm@3: *
rlm@3: * @author Werner Randelshofer
rlm@3: * @version 1.0 2010-12-27 Created.
rlm@3: */
rlm@3: public class SeekableByteArrayOutputStream extends ByteArrayOutputStream {
rlm@3:
rlm@3: /**
rlm@3: * The current stream position.
rlm@3: */
rlm@3: private int pos;
rlm@3:
rlm@3: /**
rlm@3: * Creates a new byte array output stream. The buffer capacity is
rlm@3: * initially 32 bytes, though its size increases if necessary.
rlm@3: */
rlm@3: public SeekableByteArrayOutputStream() {
rlm@3: this(32);
rlm@3: }
rlm@3:
rlm@3: /**
rlm@3: * Creates a new byte array output stream, with a buffer capacity of
rlm@3: * the specified size, in bytes.
rlm@3: *
rlm@3: * @param size the initial size.
rlm@3: * @exception IllegalArgumentException if size is negative.
rlm@3: */
rlm@3: public SeekableByteArrayOutputStream(int size) {
rlm@3: if (size < 0) {
rlm@3: throw new IllegalArgumentException("Negative initial size: "
rlm@3: + size);
rlm@3: }
rlm@3: buf = new byte[size];
rlm@3: }
rlm@3:
rlm@3: /**
rlm@3: * Writes the specified byte to this byte array output stream.
rlm@3: *
rlm@3: * @param b the byte to be written.
rlm@3: */
rlm@3: @Override
rlm@3: public synchronized void write(int b) {
rlm@3: int newcount = max(pos + 1, count);
rlm@3: if (newcount > buf.length) {
rlm@3: buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
rlm@3: }
rlm@3: buf[pos++] = (byte)b;
rlm@3: count = newcount;
rlm@3: }
rlm@3:
rlm@3: /**
rlm@3: * Writes len
bytes from the specified byte array
rlm@3: * starting at offset off
to this byte array output stream.
rlm@3: *
rlm@3: * @param b the data.
rlm@3: * @param off the start offset in the data.
rlm@3: * @param len the number of bytes to write.
rlm@3: */
rlm@3: @Override
rlm@3: public synchronized void write(byte b[], int off, int len) {
rlm@3: if ((off < 0) || (off > b.length) || (len < 0) ||
rlm@3: ((off + len) > b.length) || ((off + len) < 0)) {
rlm@3: throw new IndexOutOfBoundsException();
rlm@3: } else if (len == 0) {
rlm@3: return;
rlm@3: }
rlm@3: int newcount = max(pos+len,count);
rlm@3: if (newcount > buf.length) {
rlm@3: buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
rlm@3: }
rlm@3: System.arraycopy(b, off, buf, pos, len);
rlm@3: pos+=len;
rlm@3: count = newcount;
rlm@3: }
rlm@3:
rlm@3: /**
rlm@3: * Resets the count
field of this byte array output
rlm@3: * stream to zero, so that all currently accumulated output in the
rlm@3: * output stream is discarded. The output stream can be used again,
rlm@3: * reusing the already allocated buffer space.
rlm@3: *
rlm@3: * @see java.io.ByteArrayInputStream#count
rlm@3: */
rlm@3: @Override
rlm@3: public synchronized void reset() {
rlm@3: count = 0;
rlm@3: pos=0;
rlm@3: }
rlm@3:
rlm@3: /**
rlm@3: * Sets the current stream position to the desired location. The
rlm@3: * next read will occur at this location. The bit offset is set
rlm@3: * to 0.
rlm@3: *
rlm@3: *
An IndexOutOfBoundsException
will be thrown if
rlm@3: * pos
is smaller than the flushed position (as
rlm@3: * returned by getflushedPosition
).
rlm@3: *
rlm@3: *
It is legal to seek past the end of the file; an
rlm@3: * EOFException
will be thrown only if a read is
rlm@3: * performed.
rlm@3: *
rlm@3: * @param pos a long
containing the desired file
rlm@3: * pointer position.
rlm@3: *
rlm@3: * @exception IndexOutOfBoundsException if pos
is smaller
rlm@3: * than the flushed position.
rlm@3: * @exception IOException if any other I/O error occurs.
rlm@3: */
rlm@3: public void seek(long pos) throws IOException {
rlm@3: this.pos = (int)pos;
rlm@3: }
rlm@3:
rlm@3: /**
rlm@3: * Returns the current byte position of the stream. The next write
rlm@3: * will take place starting at this offset.
rlm@3: *
rlm@3: * @return a long containing the position of the stream.
rlm@3: *
rlm@3: * @exception IOException if an I/O error occurs.
rlm@3: */
rlm@3: public long getStreamPosition() throws IOException {
rlm@3: return pos;
rlm@3: }
rlm@3:
rlm@3: /** Writes the contents of the byte array into the specified output
rlm@3: * stream.
rlm@3: * @param out
rlm@3: */
rlm@3: public void toOutputStream(OutputStream out) throws IOException {
rlm@3: out.write(buf, 0, count);
rlm@3: }
rlm@3:
rlm@3: }