annotate src/com/aurellem/capture/SeekableByteArrayOutputStream.java @ 3:a92de00f0414

migrating files
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 11:55:55 -0700
parents
children
rev   line source
rlm@3 1 /*
rlm@3 2 * @(#)SeekableByteArrayOutputStream.java 1.0 2010-12-27
rlm@3 3 *
rlm@3 4 * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland.
rlm@3 5 * All rights reserved.
rlm@3 6 *
rlm@3 7 * You may not use, copy or modify this file, except in compliance with the
rlm@3 8 * license agreement you entered into with Werner Randelshofer.
rlm@3 9 * For details see accompanying license terms.
rlm@3 10 */
rlm@3 11
rlm@3 12 package com.aurellem.capture;
rlm@3 13
rlm@3 14 import java.io.ByteArrayOutputStream;
rlm@3 15 import java.io.IOException;
rlm@3 16 import java.io.OutputStream;
rlm@3 17 import java.util.Arrays;
rlm@3 18 import static java.lang.Math.*;
rlm@3 19 /**
rlm@3 20 * {@code SeekableByteArrayOutputStream}.
rlm@3 21 *
rlm@3 22 * @author Werner Randelshofer
rlm@3 23 * @version 1.0 2010-12-27 Created.
rlm@3 24 */
rlm@3 25 public class SeekableByteArrayOutputStream extends ByteArrayOutputStream {
rlm@3 26
rlm@3 27 /**
rlm@3 28 * The current stream position.
rlm@3 29 */
rlm@3 30 private int pos;
rlm@3 31
rlm@3 32 /**
rlm@3 33 * Creates a new byte array output stream. The buffer capacity is
rlm@3 34 * initially 32 bytes, though its size increases if necessary.
rlm@3 35 */
rlm@3 36 public SeekableByteArrayOutputStream() {
rlm@3 37 this(32);
rlm@3 38 }
rlm@3 39
rlm@3 40 /**
rlm@3 41 * Creates a new byte array output stream, with a buffer capacity of
rlm@3 42 * the specified size, in bytes.
rlm@3 43 *
rlm@3 44 * @param size the initial size.
rlm@3 45 * @exception IllegalArgumentException if size is negative.
rlm@3 46 */
rlm@3 47 public SeekableByteArrayOutputStream(int size) {
rlm@3 48 if (size < 0) {
rlm@3 49 throw new IllegalArgumentException("Negative initial size: "
rlm@3 50 + size);
rlm@3 51 }
rlm@3 52 buf = new byte[size];
rlm@3 53 }
rlm@3 54
rlm@3 55 /**
rlm@3 56 * Writes the specified byte to this byte array output stream.
rlm@3 57 *
rlm@3 58 * @param b the byte to be written.
rlm@3 59 */
rlm@3 60 @Override
rlm@3 61 public synchronized void write(int b) {
rlm@3 62 int newcount = max(pos + 1, count);
rlm@3 63 if (newcount > buf.length) {
rlm@3 64 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
rlm@3 65 }
rlm@3 66 buf[pos++] = (byte)b;
rlm@3 67 count = newcount;
rlm@3 68 }
rlm@3 69
rlm@3 70 /**
rlm@3 71 * Writes <code>len</code> bytes from the specified byte array
rlm@3 72 * starting at offset <code>off</code> to this byte array output stream.
rlm@3 73 *
rlm@3 74 * @param b the data.
rlm@3 75 * @param off the start offset in the data.
rlm@3 76 * @param len the number of bytes to write.
rlm@3 77 */
rlm@3 78 @Override
rlm@3 79 public synchronized void write(byte b[], int off, int len) {
rlm@3 80 if ((off < 0) || (off > b.length) || (len < 0) ||
rlm@3 81 ((off + len) > b.length) || ((off + len) < 0)) {
rlm@3 82 throw new IndexOutOfBoundsException();
rlm@3 83 } else if (len == 0) {
rlm@3 84 return;
rlm@3 85 }
rlm@3 86 int newcount = max(pos+len,count);
rlm@3 87 if (newcount > buf.length) {
rlm@3 88 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
rlm@3 89 }
rlm@3 90 System.arraycopy(b, off, buf, pos, len);
rlm@3 91 pos+=len;
rlm@3 92 count = newcount;
rlm@3 93 }
rlm@3 94
rlm@3 95 /**
rlm@3 96 * Resets the <code>count</code> field of this byte array output
rlm@3 97 * stream to zero, so that all currently accumulated output in the
rlm@3 98 * output stream is discarded. The output stream can be used again,
rlm@3 99 * reusing the already allocated buffer space.
rlm@3 100 *
rlm@3 101 * @see java.io.ByteArrayInputStream#count
rlm@3 102 */
rlm@3 103 @Override
rlm@3 104 public synchronized void reset() {
rlm@3 105 count = 0;
rlm@3 106 pos=0;
rlm@3 107 }
rlm@3 108
rlm@3 109 /**
rlm@3 110 * Sets the current stream position to the desired location. The
rlm@3 111 * next read will occur at this location. The bit offset is set
rlm@3 112 * to 0.
rlm@3 113 *
rlm@3 114 * <p> An <code>IndexOutOfBoundsException</code> will be thrown if
rlm@3 115 * <code>pos</code> is smaller than the flushed position (as
rlm@3 116 * returned by <code>getflushedPosition</code>).
rlm@3 117 *
rlm@3 118 * <p> It is legal to seek past the end of the file; an
rlm@3 119 * <code>EOFException</code> will be thrown only if a read is
rlm@3 120 * performed.
rlm@3 121 *
rlm@3 122 * @param pos a <code>long</code> containing the desired file
rlm@3 123 * pointer position.
rlm@3 124 *
rlm@3 125 * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
rlm@3 126 * than the flushed position.
rlm@3 127 * @exception IOException if any other I/O error occurs.
rlm@3 128 */
rlm@3 129 public void seek(long pos) throws IOException {
rlm@3 130 this.pos = (int)pos;
rlm@3 131 }
rlm@3 132
rlm@3 133 /**
rlm@3 134 * Returns the current byte position of the stream. The next write
rlm@3 135 * will take place starting at this offset.
rlm@3 136 *
rlm@3 137 * @return a long containing the position of the stream.
rlm@3 138 *
rlm@3 139 * @exception IOException if an I/O error occurs.
rlm@3 140 */
rlm@3 141 public long getStreamPosition() throws IOException {
rlm@3 142 return pos;
rlm@3 143 }
rlm@3 144
rlm@3 145 /** Writes the contents of the byte array into the specified output
rlm@3 146 * stream.
rlm@3 147 * @param out
rlm@3 148 */
rlm@3 149 public void toOutputStream(OutputStream out) throws IOException {
rlm@3 150 out.write(buf, 0, count);
rlm@3 151 }
rlm@3 152
rlm@3 153 }