annotate src/com/aurellem/capture/audio/SeekableByteArrayOutputStream.java @ 9:5dfc9e768816

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