Mercurial > jmeCapture
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/com/aurellem/capture/audio/SeekableByteArrayOutputStream.java Wed Oct 26 08:54:12 2011 -0700 1.3 @@ -0,0 +1,153 @@ 1.4 +/* 1.5 + * @(#)SeekableByteArrayOutputStream.java 1.0 2010-12-27 1.6 + * 1.7 + * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland. 1.8 + * All rights reserved. 1.9 + * 1.10 + * You may not use, copy or modify this file, except in compliance with the 1.11 + * license agreement you entered into with Werner Randelshofer. 1.12 + * For details see accompanying license terms. 1.13 + */ 1.14 + 1.15 +package com.aurellem.capture.audio; 1.16 + 1.17 +import java.io.ByteArrayOutputStream; 1.18 +import java.io.IOException; 1.19 +import java.io.OutputStream; 1.20 +import java.util.Arrays; 1.21 +import static java.lang.Math.*; 1.22 +/** 1.23 + * {@code SeekableByteArrayOutputStream}. 1.24 + * 1.25 + * @author Werner Randelshofer 1.26 + * @version 1.0 2010-12-27 Created. 1.27 + */ 1.28 +public class SeekableByteArrayOutputStream extends ByteArrayOutputStream { 1.29 + 1.30 + /** 1.31 + * The current stream position. 1.32 + */ 1.33 + private int pos; 1.34 + 1.35 + /** 1.36 + * Creates a new byte array output stream. The buffer capacity is 1.37 + * initially 32 bytes, though its size increases if necessary. 1.38 + */ 1.39 + public SeekableByteArrayOutputStream() { 1.40 + this(32); 1.41 + } 1.42 + 1.43 + /** 1.44 + * Creates a new byte array output stream, with a buffer capacity of 1.45 + * the specified size, in bytes. 1.46 + * 1.47 + * @param size the initial size. 1.48 + * @exception IllegalArgumentException if size is negative. 1.49 + */ 1.50 + public SeekableByteArrayOutputStream(int size) { 1.51 + if (size < 0) { 1.52 + throw new IllegalArgumentException("Negative initial size: " 1.53 + + size); 1.54 + } 1.55 + buf = new byte[size]; 1.56 + } 1.57 + 1.58 + /** 1.59 + * Writes the specified byte to this byte array output stream. 1.60 + * 1.61 + * @param b the byte to be written. 1.62 + */ 1.63 + @Override 1.64 + public synchronized void write(int b) { 1.65 + int newcount = max(pos + 1, count); 1.66 + if (newcount > buf.length) { 1.67 + buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); 1.68 + } 1.69 + buf[pos++] = (byte)b; 1.70 + count = newcount; 1.71 + } 1.72 + 1.73 + /** 1.74 + * Writes <code>len</code> bytes from the specified byte array 1.75 + * starting at offset <code>off</code> to this byte array output stream. 1.76 + * 1.77 + * @param b the data. 1.78 + * @param off the start offset in the data. 1.79 + * @param len the number of bytes to write. 1.80 + */ 1.81 + @Override 1.82 + public synchronized void write(byte b[], int off, int len) { 1.83 + if ((off < 0) || (off > b.length) || (len < 0) || 1.84 + ((off + len) > b.length) || ((off + len) < 0)) { 1.85 + throw new IndexOutOfBoundsException(); 1.86 + } else if (len == 0) { 1.87 + return; 1.88 + } 1.89 + int newcount = max(pos+len,count); 1.90 + if (newcount > buf.length) { 1.91 + buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); 1.92 + } 1.93 + System.arraycopy(b, off, buf, pos, len); 1.94 + pos+=len; 1.95 + count = newcount; 1.96 + } 1.97 + 1.98 + /** 1.99 + * Resets the <code>count</code> field of this byte array output 1.100 + * stream to zero, so that all currently accumulated output in the 1.101 + * output stream is discarded. The output stream can be used again, 1.102 + * reusing the already allocated buffer space. 1.103 + * 1.104 + * @see java.io.ByteArrayInputStream#count 1.105 + */ 1.106 + @Override 1.107 + public synchronized void reset() { 1.108 + count = 0; 1.109 + pos=0; 1.110 + } 1.111 + 1.112 + /** 1.113 + * Sets the current stream position to the desired location. The 1.114 + * next read will occur at this location. The bit offset is set 1.115 + * to 0. 1.116 + * 1.117 + * <p> An <code>IndexOutOfBoundsException</code> will be thrown if 1.118 + * <code>pos</code> is smaller than the flushed position (as 1.119 + * returned by <code>getflushedPosition</code>). 1.120 + * 1.121 + * <p> It is legal to seek past the end of the file; an 1.122 + * <code>EOFException</code> will be thrown only if a read is 1.123 + * performed. 1.124 + * 1.125 + * @param pos a <code>long</code> containing the desired file 1.126 + * pointer position. 1.127 + * 1.128 + * @exception IndexOutOfBoundsException if <code>pos</code> is smaller 1.129 + * than the flushed position. 1.130 + * @exception IOException if any other I/O error occurs. 1.131 + */ 1.132 + public void seek(long pos) throws IOException { 1.133 + this.pos = (int)pos; 1.134 + } 1.135 + 1.136 + /** 1.137 + * Returns the current byte position of the stream. The next write 1.138 + * will take place starting at this offset. 1.139 + * 1.140 + * @return a long containing the position of the stream. 1.141 + * 1.142 + * @exception IOException if an I/O error occurs. 1.143 + */ 1.144 + public long getStreamPosition() throws IOException { 1.145 + return pos; 1.146 + } 1.147 + 1.148 + /** Writes the contents of the byte array into the specified output 1.149 + * stream. 1.150 + * @param out 1.151 + */ 1.152 + public void toOutputStream(OutputStream out) throws IOException { 1.153 + out.write(buf, 0, count); 1.154 + } 1.155 + 1.156 +}