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