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 sun.audio; 27 28 import java.io.InputStream; 29 import java.io.FilterInputStream; 30 import java.io.BufferedInputStream; 31 import java.io.IOException; 32 33 import javax.sound.sampled.*; 34 import javax.sound.midi.*; 35 36 /** 37 * Convert an InputStream to an AudioStream. 38 * 39 */ 40 41 42 public final class AudioStream extends FilterInputStream { 43 44 // AudioContainerInputStream acis; 45 AudioInputStream ais = null; 46 AudioFormat format = null; 47 MidiFileFormat midiformat = null; 48 InputStream stream = null; 49 50 51 /* 52 * create the AudioStream; if we survive without throwing 53 * an exception, we should now have some subclass of 54 * ACIS with all the header info already read 55 */ 56 57 public AudioStream(InputStream in) throws IOException { 58 59 super(in); 60 61 stream = in; 62 63 if( in.markSupported() == false ) { 64 65 stream = new BufferedInputStream( in, 1024 ); 66 } 67 68 try { 69 ais = AudioSystem.getAudioInputStream( stream ); 70 format = ais.getFormat(); 71 this.in = ais; 72 73 } catch (UnsupportedAudioFileException e ) { 74 75 // not an audio file, see if it's midi... 76 try { 77 midiformat = MidiSystem.getMidiFileFormat( stream ); 78 79 } catch (InvalidMidiDataException e1) { 80 throw new IOException("could not create audio stream from input stream"); 81 } 82 } 83 } 84 85 86 87 88 /** 89 * A blocking read. 90 */ 91 /* public int read(byte buf[], int pos, int len) throws IOException { 92 93 return(acis.readFully(buf, pos, len)); 94 } 95 */ 96 97 /** 98 * Get the data. 99 */ 100 public AudioData getData() throws IOException { 101 int length = getLength(); 102 103 //limit the memory to 1M, so too large au file won't load 104 if (length < 1024*1024) { 105 byte [] buffer = new byte[length]; 106 try { 107 ais.read(buffer, 0, length); 108 } catch (IOException ex) { 109 throw new IOException("Could not create AudioData Object"); 110 } 111 return new AudioData(format, buffer); 112 } 113 114 /* acis.setData(); 115 116 if (acis.stream instanceof ByteArrayInputStream) { 117 Format[] format = acis.getFormat(); 118 byte[] bytes = acis.getBytes(); 119 if (bytes == null) 120 throw new IOException("could not create AudioData object: no data received"); 121 return new AudioData((AudioFormat)format[0], bytes); 122 } 123 */ 124 125 throw new IOException("could not create AudioData object"); 126 } 127 128 129 public int getLength() { 130 131 if( ais != null && format != null ) { 132 return (int) (ais.getFrameLength() * 133 ais.getFormat().getFrameSize() ); 134 135 } else if ( midiformat != null ) { 136 return (int) midiformat.getByteLength(); 137 138 } else { 139 return -1; 140 } 141 } 142 }