Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/audio/AudioStream.java
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.audio;2627import java.io.InputStream;28import java.io.FilterInputStream;29import java.io.BufferedInputStream;30import java.io.IOException;3132import javax.sound.sampled.*;33import javax.sound.midi.*;3435/**36* Convert an InputStream to an AudioStream.37*38*/394041public final class AudioStream extends FilterInputStream {4243// AudioContainerInputStream acis;44AudioInputStream ais = null;45AudioFormat format = null;46MidiFileFormat midiformat = null;47InputStream stream = null;484950/*51* create the AudioStream; if we survive without throwing52* an exception, we should now have some subclass of53* ACIS with all the header info already read54*/5556public AudioStream(InputStream in) throws IOException {5758super(in);5960stream = in;6162if( in.markSupported() == false ) {6364stream = new BufferedInputStream( in, 1024 );65}6667try {68ais = AudioSystem.getAudioInputStream( stream );69format = ais.getFormat();70this.in = ais;7172} catch (UnsupportedAudioFileException e ) {7374// not an audio file, see if it's midi...75try {76midiformat = MidiSystem.getMidiFileFormat( stream );7778} catch (InvalidMidiDataException e1) {79throw new IOException("could not create audio stream from input stream");80}81}82}8384858687/**88* A blocking read.89*/90/* public int read(byte buf[], int pos, int len) throws IOException {9192return(acis.readFully(buf, pos, len));93}94*/9596/**97* Get the data.98*/99public AudioData getData() throws IOException {100int length = getLength();101102//limit the memory to 1M, so too large au file won't load103if (length < 1024*1024) {104byte [] buffer = new byte[length];105try {106ais.read(buffer, 0, length);107} catch (IOException ex) {108throw new IOException("Could not create AudioData Object");109}110return new AudioData(format, buffer);111}112113/* acis.setData();114115if (acis.stream instanceof ByteArrayInputStream) {116Format[] format = acis.getFormat();117byte[] bytes = acis.getBytes();118if (bytes == null)119throw new IOException("could not create AudioData object: no data received");120return new AudioData((AudioFormat)format[0], bytes);121}122*/123124throw new IOException("could not create AudioData object");125}126127128public int getLength() {129130if( ais != null && format != null ) {131return (int) (ais.getFrameLength() *132ais.getFormat().getFrameSize() );133134} else if ( midiformat != null ) {135return (int) midiformat.getByteLength();136137} else {138return -1;139}140}141}142143144