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