1 /* 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.sound.sampled; 27 28 import java.io.InputStream; 29 import java.io.PushbackInputStream; 30 import java.io.IOException; 31 32 33 /** 34 * An audio input stream is an input stream with a specified audio format and 35 * length. The length is expressed in sample frames, not bytes. 36 * Several methods are provided for reading a certain number of bytes from 37 * the stream, or an unspecified number of bytes. 38 * The audio input stream keeps track of the last byte that was read. 39 * You can skip over an arbitrary number of bytes to get to a later position 40 * for reading. An audio input stream may support marks. When you set a mark, 41 * the current position is remembered so that you can return to it later. 42 * <p> 43 * The <code>AudioSystem</code> class includes many methods that manipulate 44 * <code>AudioInputStream</code> objects. 45 * For example, the methods let you: 46 * <ul> 47 * <li> obtain an 48 * audio input stream from an external audio file, stream, or URL 49 * <li> write an external file from an audio input stream 50 * <li> convert an audio input stream to a different audio format 51 * </ul> 52 * 53 * @author David Rivas 54 * @author Kara Kytle 55 * @author Florian Bomers 56 * 57 * @see AudioSystem 58 * @see Clip#open(AudioInputStream) Clip.open(AudioInputStream) 59 * @since 1.3 60 */ 61 public class AudioInputStream extends InputStream { 62 63 /** 64 * The <code>InputStream</code> from which this <code>AudioInputStream</code> 65 * object was constructed. 66 */ 67 private InputStream stream; 68 69 /** 70 * The format of the audio data contained in the stream. 71 */ 72 protected AudioFormat format; 73 74 /** 75 * This stream's length, in sample frames. 76 */ 77 protected long frameLength; 78 79 /** 80 * The size of each frame, in bytes. 81 */ 82 protected int frameSize; 83 84 /** 85 * The current position in this stream, in sample frames (zero-based). 86 */ 87 protected long framePos; 88 89 /** 90 * The position where a mark was set. 91 */ 92 private long markpos; 93 94 /** 95 * When the underlying stream could only return 96 * a non-integral number of frames, store 97 * the remainder in a temporary buffer 98 */ 99 private byte[] pushBackBuffer = null; 100 101 /** 102 * number of valid bytes in the pushBackBuffer 103 */ 104 private int pushBackLen = 0; 105 106 /** 107 * MarkBuffer at mark position 108 */ 109 private byte[] markPushBackBuffer = null; 110 111 /** 112 * number of valid bytes in the markPushBackBuffer 113 */ 114 private int markPushBackLen = 0; 115 116 117 /** 118 * Constructs an audio input stream that has the requested format and length in sample frames, 119 * using audio data from the specified input stream. 120 * @param stream the stream on which this <code>AudioInputStream</code> 121 * object is based 122 * @param format the format of this stream's audio data 123 * @param length the length in sample frames of the data in this stream 124 */ 125 public AudioInputStream(InputStream stream, AudioFormat format, long length) { 126 127 super(); 128 129 this.format = format; 130 this.frameLength = length; 131 this.frameSize = format.getFrameSize(); 132 133 // any frameSize that is not well-defined will 134 // cause that this stream will be read in bytes 135 if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) { 136 this.frameSize = 1; 137 } 138 139 this.stream = stream; 140 framePos = 0; 141 markpos = 0; 142 } 143 144 145 /** 146 * Constructs an audio input stream that reads its data from the target 147 * data line indicated. The format of the stream is the same as that of 148 * the target data line, and the length is AudioSystem#NOT_SPECIFIED. 149 * @param line the target data line from which this stream obtains its data. 150 * @see AudioSystem#NOT_SPECIFIED 151 */ 152 public AudioInputStream(TargetDataLine line) { 153 154 TargetDataLineInputStream tstream = new TargetDataLineInputStream(line); 155 format = line.getFormat(); 156 frameLength = AudioSystem.NOT_SPECIFIED; 157 frameSize = format.getFrameSize(); 158 159 if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) { 160 frameSize = 1; 161 } 162 this.stream = tstream; 163 framePos = 0; 164 markpos = 0; 165 } 166 167 168 /** 169 * Obtains the audio format of the sound data in this audio input stream. 170 * @return an audio format object describing this stream's format 171 */ 172 public AudioFormat getFormat() { 173 return format; 174 } 175 176 177 /** 178 * Obtains the length of the stream, expressed in sample frames rather than bytes. 179 * @return the length in sample frames 180 */ 181 public long getFrameLength() { 182 return frameLength; 183 } 184 185 186 /** 187 * Reads the next byte of data from the audio input stream. The audio input 188 * stream's frame size must be one byte, or an <code>IOException</code> 189 * will be thrown. 190 * 191 * @return the next byte of data, or -1 if the end of the stream is reached 192 * @throws IOException if an input or output error occurs 193 * @see #read(byte[], int, int) 194 * @see #read(byte[]) 195 * @see #available 196 */ 197 public int read() throws IOException { 198 if( frameSize != 1 ) { 199 throw new IOException("cannot read a single byte if frame size > 1"); 200 } 201 202 byte[] data = new byte[1]; 203 int temp = read(data); 204 if (temp <= 0) { 205 // we have a weird situation if read(byte[]) returns 0! 206 return -1; 207 } 208 return data[0] & 0xFF; 209 } 210 211 212 /** 213 * Reads some number of bytes from the audio input stream and stores them into 214 * the buffer array <code>b</code>. The number of bytes actually read is 215 * returned as an integer. This method blocks until input data is 216 * available, the end of the stream is detected, or an exception is thrown. 217 * <p>This method will always read an integral number of frames. 218 * If the length of the array is not an integral number 219 * of frames, a maximum of <code>b.length - (b.length % frameSize) 220 * </code> bytes will be read. 221 * 222 * @param b the buffer into which the data is read 223 * @return the total number of bytes read into the buffer, or -1 if there 224 * is no more data because the end of the stream has been reached 225 * @throws IOException if an input or output error occurs 226 * @see #read(byte[], int, int) 227 * @see #read() 228 * @see #available 229 */ 230 public int read(byte[] b) throws IOException { 231 return read(b,0,b.length); 232 } 233 234 235 /** 236 * Reads up to a specified maximum number of bytes of data from the audio 237 * stream, putting them into the given byte array. 238 * <p>This method will always read an integral number of frames. 239 * If <code>len</code> does not specify an integral number 240 * of frames, a maximum of <code>len - (len % frameSize) 241 * </code> bytes will be read. 242 * 243 * @param b the buffer into which the data is read 244 * @param off the offset, from the beginning of array <code>b</code>, at which 245 * the data will be written 246 * @param len the maximum number of bytes to read 247 * @return the total number of bytes read into the buffer, or -1 if there 248 * is no more data because the end of the stream has been reached 249 * @throws IOException if an input or output error occurs 250 * @see #read(byte[]) 251 * @see #read() 252 * @see #skip 253 * @see #available 254 */ 255 public int read(byte[] b, int off, int len) throws IOException { 256 257 // make sure we don't read fractions of a frame. 258 if( (len%frameSize) != 0 ) { 259 len -= (len%frameSize); 260 if (len == 0) { 261 return 0; 262 } 263 } 264 265 if( frameLength != AudioSystem.NOT_SPECIFIED ) { 266 if( framePos >= frameLength ) { 267 return -1; 268 } else { 269 270 // don't try to read beyond our own set length in frames 271 if( (len/frameSize) > (frameLength-framePos) ) { 272 len = (int) (frameLength-framePos) * frameSize; 273 } 274 } 296 bytesRead += thisBytesRead; 297 } 298 if (bytesRead > 0) { 299 pushBackLen = bytesRead % frameSize; 300 if (pushBackLen > 0) { 301 // copy everything we got from the beginning of the frame 302 // to our pushback buffer 303 if (pushBackBuffer == null) { 304 pushBackBuffer = new byte[frameSize]; 305 } 306 System.arraycopy(b, off + bytesRead - pushBackLen, 307 pushBackBuffer, 0, pushBackLen); 308 bytesRead -= pushBackLen; 309 } 310 // make sure to update our framePos 311 framePos += bytesRead/frameSize; 312 } 313 return bytesRead; 314 } 315 316 317 /** 318 * Skips over and discards a specified number of bytes from this 319 * audio input stream. 320 * @param n the requested number of bytes to be skipped 321 * @return the actual number of bytes skipped 322 * @throws IOException if an input or output error occurs 323 * @see #read 324 * @see #available 325 */ 326 public long skip(long n) throws IOException { 327 328 // make sure not to skip fractional frames 329 if( (n%frameSize) != 0 ) { 330 n -= (n%frameSize); 331 } 332 333 if( frameLength != AudioSystem.NOT_SPECIFIED ) { 334 // don't skip more than our set length in frames. 335 if( (n/frameSize) > (frameLength-framePos) ) { 336 n = (frameLength-framePos) * frameSize; 337 } 338 } 339 long temp = stream.skip(n); 340 341 // if no error, update our position. 342 if( temp%frameSize != 0 ) { 343 344 // Throw an IOException if we've skipped a fractional number of frames 345 throw new IOException("Could not skip an integer number of frames."); 346 } 347 if( temp >= 0 ) { 348 framePos += temp/frameSize; 349 } 350 return temp; 351 352 } 353 354 355 /** 356 * Returns the maximum number of bytes that can be read (or skipped over) from this 357 * audio input stream without blocking. This limit applies only to the next invocation of 358 * a <code>read</code> or <code>skip</code> method for this audio input stream; the limit 359 * can vary each time these methods are invoked. 360 * Depending on the underlying stream,an IOException may be thrown if this 361 * stream is closed. 362 * @return the number of bytes that can be read from this audio input stream without blocking 363 * @throws IOException if an input or output error occurs 364 * @see #read(byte[], int, int) 365 * @see #read(byte[]) 366 * @see #read() 367 * @see #skip 368 */ 369 public int available() throws IOException { 370 371 int temp = stream.available(); 372 373 // don't return greater than our set length in frames 374 if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) { 375 return (int) (frameLength-framePos) * frameSize; 376 } else { 377 return temp; 378 } 379 } 380 381 382 /** 383 * Closes this audio input stream and releases any system resources associated 384 * with the stream. 385 * @throws IOException if an input or output error occurs 386 */ 387 public void close() throws IOException { 388 stream.close(); 389 } 390 391 392 /** 393 * Marks the current position in this audio input stream. 394 * @param readlimit the maximum number of bytes that can be read before 395 * the mark position becomes invalid. 396 * @see #reset 397 * @see #markSupported 398 */ 399 400 public void mark(int readlimit) { 401 402 stream.mark(readlimit); 403 if (markSupported()) { 404 markpos = framePos; 405 // remember the pushback buffer 406 markPushBackLen = pushBackLen; 407 if (markPushBackLen > 0) { 408 if (markPushBackBuffer == null) { 409 markPushBackBuffer = new byte[frameSize]; 410 } 411 System.arraycopy(pushBackBuffer, 0, markPushBackBuffer, 0, markPushBackLen); 412 } 413 } 414 } 415 416 417 /** 418 * Repositions this audio input stream to the position it had at the time its 419 * <code>mark</code> method was last invoked. 420 * @throws IOException if an input or output error occurs. 421 * @see #mark 422 * @see #markSupported 423 */ 424 public void reset() throws IOException { 425 426 stream.reset(); 427 framePos = markpos; 428 // re-create the pushback buffer 429 pushBackLen = markPushBackLen; 430 if (pushBackLen > 0) { 431 if (pushBackBuffer == null) { 432 pushBackBuffer = new byte[frameSize - 1]; 433 } 434 System.arraycopy(markPushBackBuffer, 0, pushBackBuffer, 0, pushBackLen); 435 } 436 } 437 438 439 /** 440 * Tests whether this audio input stream supports the <code>mark</code> and 441 * <code>reset</code> methods. 442 * @return <code>true</code> if this stream supports the <code>mark</code> 443 * and <code>reset</code> methods; <code>false</code> otherwise 444 * @see #mark 445 * @see #reset 446 */ 447 public boolean markSupported() { 448 449 return stream.markSupported(); 450 } 451 452 453 /** 454 * Private inner class that makes a TargetDataLine look like an InputStream. 455 */ 456 private class TargetDataLineInputStream extends InputStream { 457 458 /** 459 * The TargetDataLine on which this TargetDataLineInputStream is based. 460 */ 461 TargetDataLine line; 462 463 464 TargetDataLineInputStream(TargetDataLine line) { 465 super(); 466 this.line = line; 467 } 468 469 470 public int available() throws IOException { 471 return line.available(); 472 } 473 474 //$$fb 2001-07-16: added this method to correctly close the underlying TargetDataLine. 475 // fixes bug 4479984 476 public void close() throws IOException { 477 // the line needs to be flushed and stopped to avoid a dead lock... 478 // Probably related to bugs 4417527, 4334868, 4383457 479 if (line.isActive()) { 480 line.flush(); 481 line.stop(); 482 } 483 line.close(); 484 } 485 486 public int read() throws IOException { 487 488 byte[] b = new byte[1]; 489 490 int value = read(b, 0, 1); 491 492 if (value == -1) { 493 return -1; 494 } 495 496 value = (int)b[0]; 497 498 if (line.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) { 499 value += 128; 500 } 501 502 return value; 503 } 504 505 506 public int read(byte[] b, int off, int len) throws IOException { 507 try { 508 return line.read(b, off, len); 509 } catch (IllegalArgumentException e) { 510 throw new IOException(e.getMessage()); 511 } 512 } 513 } 514 } | 1 /* 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.sound.sampled; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 31 32 /** 33 * An audio input stream is an input stream with a specified audio format and 34 * length. The length is expressed in sample frames, not bytes. Several methods 35 * are provided for reading a certain number of bytes from the stream, or an 36 * unspecified number of bytes. The audio input stream keeps track of the last 37 * byte that was read. You can skip over an arbitrary number of bytes to get to 38 * a later position for reading. An audio input stream may support marks. When 39 * you set a mark, the current position is remembered so that you can return to 40 * it later. 41 * <p> 42 * The {@code AudioSystem} class includes many methods that manipulate 43 * {@code AudioInputStream} objects. For example, the methods let you: 44 * <ul> 45 * <li> obtain an audio input stream from an external audio file, stream, or URL 46 * <li> write an external file from an audio input stream 47 * <li> convert an audio input stream to a different audio format 48 * </ul> 49 * 50 * @author David Rivas 51 * @author Kara Kytle 52 * @author Florian Bomers 53 * @see AudioSystem 54 * @see Clip#open(AudioInputStream) 55 * @since 1.3 56 */ 57 public class AudioInputStream extends InputStream { 58 59 /** 60 * The {@code InputStream} from which this {@code AudioInputStream} object 61 * was constructed. 62 */ 63 private InputStream stream; 64 65 /** 66 * The format of the audio data contained in the stream. 67 */ 68 protected AudioFormat format; 69 70 /** 71 * This stream's length, in sample frames. 72 */ 73 protected long frameLength; 74 75 /** 76 * The size of each frame, in bytes. 77 */ 78 protected int frameSize; 79 80 /** 81 * The current position in this stream, in sample frames (zero-based). 82 */ 83 protected long framePos; 84 85 /** 86 * The position where a mark was set. 87 */ 88 private long markpos; 89 90 /** 91 * When the underlying stream could only return a non-integral number of 92 * frames, store the remainder in a temporary buffer. 93 */ 94 private byte[] pushBackBuffer = null; 95 96 /** 97 * number of valid bytes in the pushBackBuffer. 98 */ 99 private int pushBackLen = 0; 100 101 /** 102 * MarkBuffer at mark position. 103 */ 104 private byte[] markPushBackBuffer = null; 105 106 /** 107 * number of valid bytes in the markPushBackBuffer. 108 */ 109 private int markPushBackLen = 0; 110 111 /** 112 * Constructs an audio input stream that has the requested format and length 113 * in sample frames, using audio data from the specified input stream. 114 * 115 * @param stream the stream on which this {@code AudioInputStream} object 116 * is based 117 * @param format the format of this stream's audio data 118 * @param length the length in sample frames of the data in this stream 119 */ 120 public AudioInputStream(InputStream stream, AudioFormat format, long length) { 121 122 super(); 123 124 this.format = format; 125 this.frameLength = length; 126 this.frameSize = format.getFrameSize(); 127 128 // any frameSize that is not well-defined will 129 // cause that this stream will be read in bytes 130 if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) { 131 this.frameSize = 1; 132 } 133 134 this.stream = stream; 135 framePos = 0; 136 markpos = 0; 137 } 138 139 /** 140 * Constructs an audio input stream that reads its data from the target data 141 * line indicated. The format of the stream is the same as that of the 142 * target data line, and the length is AudioSystem#NOT_SPECIFIED. 143 * 144 * @param line the target data line from which this stream obtains its data 145 * @see AudioSystem#NOT_SPECIFIED 146 */ 147 public AudioInputStream(TargetDataLine line) { 148 149 TargetDataLineInputStream tstream = new TargetDataLineInputStream(line); 150 format = line.getFormat(); 151 frameLength = AudioSystem.NOT_SPECIFIED; 152 frameSize = format.getFrameSize(); 153 154 if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) { 155 frameSize = 1; 156 } 157 this.stream = tstream; 158 framePos = 0; 159 markpos = 0; 160 } 161 162 /** 163 * Obtains the audio format of the sound data in this audio input stream. 164 * 165 * @return an audio format object describing this stream's format 166 */ 167 public AudioFormat getFormat() { 168 return format; 169 } 170 171 /** 172 * Obtains the length of the stream, expressed in sample frames rather than 173 * bytes. 174 * 175 * @return the length in sample frames 176 */ 177 public long getFrameLength() { 178 return frameLength; 179 } 180 181 /** 182 * Reads the next byte of data from the audio input stream. The audio input 183 * stream's frame size must be one byte, or an {@code IOException} will be 184 * thrown. 185 * 186 * @return the next byte of data, or -1 if the end of the stream is reached 187 * @throws IOException if an input or output error occurs 188 * @see #read(byte[], int, int) 189 * @see #read(byte[]) 190 * @see #available 191 */ 192 @Override 193 public int read() throws IOException { 194 if( frameSize != 1 ) { 195 throw new IOException("cannot read a single byte if frame size > 1"); 196 } 197 198 byte[] data = new byte[1]; 199 int temp = read(data); 200 if (temp <= 0) { 201 // we have a weird situation if read(byte[]) returns 0! 202 return -1; 203 } 204 return data[0] & 0xFF; 205 } 206 207 /** 208 * Reads some number of bytes from the audio input stream and stores them 209 * into the buffer array {@code b}. The number of bytes actually read is 210 * returned as an integer. This method blocks until input data is available, 211 * the end of the stream is detected, or an exception is thrown. 212 * <p> 213 * This method will always read an integral number of frames. If the length 214 * of the array is not an integral number of frames, a maximum of 215 * {@code b.length - (b.length % frameSize)} bytes will be read. 216 * 217 * @param b the buffer into which the data is read 218 * @return the total number of bytes read into the buffer, or -1 if there is 219 * no more data because the end of the stream has been reached 220 * @throws IOException if an input or output error occurs 221 * @see #read(byte[], int, int) 222 * @see #read() 223 * @see #available 224 */ 225 @Override 226 public int read(byte[] b) throws IOException { 227 return read(b,0,b.length); 228 } 229 230 /** 231 * Reads up to a specified maximum number of bytes of data from the audio 232 * stream, putting them into the given byte array. 233 * <p> 234 * This method will always read an integral number of frames. If {@code len} 235 * does not specify an integral number of frames, a maximum of 236 * {@code len - (len % frameSize)} bytes will be read. 237 * 238 * @param b the buffer into which the data is read 239 * @param off the offset, from the beginning of array {@code b}, at which 240 * the data will be written 241 * @param len the maximum number of bytes to read 242 * @return the total number of bytes read into the buffer, or -1 if there is 243 * no more data because the end of the stream has been reached 244 * @throws IOException if an input or output error occurs 245 * @see #read(byte[]) 246 * @see #read() 247 * @see #skip 248 * @see #available 249 */ 250 @Override 251 public int read(byte[] b, int off, int len) throws IOException { 252 253 // make sure we don't read fractions of a frame. 254 if( (len%frameSize) != 0 ) { 255 len -= (len%frameSize); 256 if (len == 0) { 257 return 0; 258 } 259 } 260 261 if( frameLength != AudioSystem.NOT_SPECIFIED ) { 262 if( framePos >= frameLength ) { 263 return -1; 264 } else { 265 266 // don't try to read beyond our own set length in frames 267 if( (len/frameSize) > (frameLength-framePos) ) { 268 len = (int) (frameLength-framePos) * frameSize; 269 } 270 } 292 bytesRead += thisBytesRead; 293 } 294 if (bytesRead > 0) { 295 pushBackLen = bytesRead % frameSize; 296 if (pushBackLen > 0) { 297 // copy everything we got from the beginning of the frame 298 // to our pushback buffer 299 if (pushBackBuffer == null) { 300 pushBackBuffer = new byte[frameSize]; 301 } 302 System.arraycopy(b, off + bytesRead - pushBackLen, 303 pushBackBuffer, 0, pushBackLen); 304 bytesRead -= pushBackLen; 305 } 306 // make sure to update our framePos 307 framePos += bytesRead/frameSize; 308 } 309 return bytesRead; 310 } 311 312 /** 313 * Skips over and discards a specified number of bytes from this audio input 314 * stream. 315 * 316 * @param n the requested number of bytes to be skipped 317 * @return the actual number of bytes skipped 318 * @throws IOException if an input or output error occurs 319 * @see #read 320 * @see #available 321 */ 322 @Override 323 public long skip(long n) throws IOException { 324 325 // make sure not to skip fractional frames 326 if( (n%frameSize) != 0 ) { 327 n -= (n%frameSize); 328 } 329 330 if( frameLength != AudioSystem.NOT_SPECIFIED ) { 331 // don't skip more than our set length in frames. 332 if( (n/frameSize) > (frameLength-framePos) ) { 333 n = (frameLength-framePos) * frameSize; 334 } 335 } 336 long temp = stream.skip(n); 337 338 // if no error, update our position. 339 if( temp%frameSize != 0 ) { 340 341 // Throw an IOException if we've skipped a fractional number of frames 342 throw new IOException("Could not skip an integer number of frames."); 343 } 344 if( temp >= 0 ) { 345 framePos += temp/frameSize; 346 } 347 return temp; 348 349 } 350 351 /** 352 * Returns the maximum number of bytes that can be read (or skipped over) 353 * from this audio input stream without blocking. This limit applies only 354 * to the next invocation of a {@code read} or {@code skip} method for this 355 * audio input stream; the limit can vary each time these methods are 356 * invoked. Depending on the underlying stream, an IOException may be thrown 357 * if this stream is closed. 358 * 359 * @return the number of bytes that can be read from this audio input stream 360 * without blocking 361 * @throws IOException if an input or output error occurs 362 * @see #read(byte[], int, int) 363 * @see #read(byte[]) 364 * @see #read() 365 * @see #skip 366 */ 367 @Override 368 public int available() throws IOException { 369 370 int temp = stream.available(); 371 372 // don't return greater than our set length in frames 373 if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) { 374 return (int) (frameLength-framePos) * frameSize; 375 } else { 376 return temp; 377 } 378 } 379 380 /** 381 * Closes this audio input stream and releases any system resources 382 * associated with the stream. 383 * 384 * @throws IOException if an input or output error occurs 385 */ 386 @Override 387 public void close() throws IOException { 388 stream.close(); 389 } 390 391 /** 392 * Marks the current position in this audio input stream. 393 * 394 * @param readlimit the maximum number of bytes that can be read before the 395 * mark position becomes invalid. 396 * @see #reset 397 * @see #markSupported 398 */ 399 @Override 400 public void mark(int readlimit) { 401 402 stream.mark(readlimit); 403 if (markSupported()) { 404 markpos = framePos; 405 // remember the pushback buffer 406 markPushBackLen = pushBackLen; 407 if (markPushBackLen > 0) { 408 if (markPushBackBuffer == null) { 409 markPushBackBuffer = new byte[frameSize]; 410 } 411 System.arraycopy(pushBackBuffer, 0, markPushBackBuffer, 0, markPushBackLen); 412 } 413 } 414 } 415 416 /** 417 * Repositions this audio input stream to the position it had at the time 418 * its {@code mark} method was last invoked. 419 * 420 * @throws IOException if an input or output error occurs 421 * @see #mark 422 * @see #markSupported 423 */ 424 @Override 425 public void reset() throws IOException { 426 427 stream.reset(); 428 framePos = markpos; 429 // re-create the pushback buffer 430 pushBackLen = markPushBackLen; 431 if (pushBackLen > 0) { 432 if (pushBackBuffer == null) { 433 pushBackBuffer = new byte[frameSize - 1]; 434 } 435 System.arraycopy(markPushBackBuffer, 0, pushBackBuffer, 0, pushBackLen); 436 } 437 } 438 439 /** 440 * Tests whether this audio input stream supports the {@code mark} and 441 * {@code reset} methods. 442 * 443 * @return {@code true} if this stream supports the {@code mark} and 444 * {@code reset} methods; {@code false} otherwise 445 * @see #mark 446 * @see #reset 447 */ 448 @Override 449 public boolean markSupported() { 450 451 return stream.markSupported(); 452 } 453 454 /** 455 * Private inner class that makes a TargetDataLine look like an InputStream. 456 */ 457 private class TargetDataLineInputStream extends InputStream { 458 459 /** 460 * The TargetDataLine on which this TargetDataLineInputStream is based. 461 */ 462 TargetDataLine line; 463 464 TargetDataLineInputStream(TargetDataLine line) { 465 super(); 466 this.line = line; 467 } 468 469 @Override 470 public int available() throws IOException { 471 return line.available(); 472 } 473 474 //$$fb 2001-07-16: added this method to correctly close the underlying TargetDataLine. 475 // fixes bug 4479984 476 @Override 477 public void close() throws IOException { 478 // the line needs to be flushed and stopped to avoid a dead lock... 479 // Probably related to bugs 4417527, 4334868, 4383457 480 if (line.isActive()) { 481 line.flush(); 482 line.stop(); 483 } 484 line.close(); 485 } 486 487 @Override 488 public int read() throws IOException { 489 490 byte[] b = new byte[1]; 491 492 int value = read(b, 0, 1); 493 494 if (value == -1) { 495 return -1; 496 } 497 498 value = (int)b[0]; 499 500 if (line.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) { 501 value += 128; 502 } 503 504 return value; 505 } 506 507 @Override 508 public int read(byte[] b, int off, int len) throws IOException { 509 try { 510 return line.read(b, off, len); 511 } catch (IllegalArgumentException e) { 512 throw new IOException(e.getMessage()); 513 } 514 } 515 } 516 } |