Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileReader/AudioInputStreamClose.java
40527 views
/*1* Copyright (c) 2017, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedInputStream;24import java.io.ByteArrayInputStream;25import java.io.ByteArrayOutputStream;26import java.io.IOException;27import java.io.InputStream;28import java.util.ArrayList;29import java.util.List;3031import javax.sound.sampled.AudioFileFormat;32import javax.sound.sampled.AudioFormat;33import javax.sound.sampled.AudioInputStream;34import javax.sound.sampled.UnsupportedAudioFileException;35import javax.sound.sampled.spi.AudioFileReader;36import javax.sound.sampled.spi.AudioFileWriter;3738import static java.util.ServiceLoader.load;39import static javax.sound.sampled.AudioFileFormat.Type.AIFC;40import static javax.sound.sampled.AudioFileFormat.Type.AIFF;41import static javax.sound.sampled.AudioFileFormat.Type.AU;42import static javax.sound.sampled.AudioFileFormat.Type.SND;43import static javax.sound.sampled.AudioFileFormat.Type.WAVE;44import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;4546/**47* @test48* @bug 819138449* @summary the stream returned by AudioFileReader should close its data stream50*/51public final class AudioInputStreamClose {5253static final class StreamWrapper extends BufferedInputStream {5455private boolean open = true;5657StreamWrapper(final InputStream in) {58super(in);59}6061@Override62public void close() throws IOException {63super.close();64open = false;65}6667boolean isOpen() {68return open;69}70}7172/**73* We will try to use all formats, in this case all our providers will be74* covered by supported/unsupported formats.75*/76private static final List<AudioFormat> formats = new ArrayList<>(23000);7778private static final AudioFormat.Encoding[] encodings = {79AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,80AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,81AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")82};8384private static final int[] sampleBits = {1, 4, 8, 11, 16, 20, 24, 32};8586private static final int[] channels = {1, 2, 3, 4, 5};8788private static final AudioFileFormat.Type[] types = {89WAVE, AU, AIFF, AIFC, SND,90new AudioFileFormat.Type("TestName", "TestExt")91};9293private static final int FRAME_LENGTH = 10;9495static {96for (final int sampleSize : sampleBits) {97for (final int channel : channels) {98for (final AudioFormat.Encoding enc : encodings) {99final int frameSize = ((sampleSize + 7) / 8) * channel;100formats.add(new AudioFormat(enc, 44100, sampleSize, channel,101frameSize, 44100, true));102formats.add(new AudioFormat(enc, 44100, sampleSize, channel,103frameSize, 44100, false));104}105}106}107}108109public static void main(final String[] args) throws IOException {110for (final AudioFileWriter afw : load(AudioFileWriter.class)) {111for (final AudioFileReader afr : load(AudioFileReader.class)) {112for (final AudioFileFormat.Type type : types) {113for (final AudioFormat from : formats) {114test(afw, afr, type, getStream(from, true));115test(afw, afr, type, getStream(from, false));116}117}118}119}120}121122/**123* Writes and reads the data to/from the stream.124*/125private static void test(final AudioFileWriter afw,126final AudioFileReader afr,127final AudioFileFormat.Type type,128final AudioInputStream ais)129throws IOException {130try {131final ByteArrayOutputStream out = new ByteArrayOutputStream();132afw.write(ais, type, out);133final InputStream input = new ByteArrayInputStream(out.toByteArray());134final StreamWrapper wrapper = new StreamWrapper(input);135136// the wrapper should be closed as well137afr.getAudioInputStream(wrapper).close();138139if (wrapper.isOpen()) {140System.err.println("Writer = " + afw);141System.err.println("Reader = " + afr);142throw new RuntimeException("Stream was not closed");143}144} catch (IOException | IllegalArgumentException |145UnsupportedAudioFileException ignored) {146}147}148149private static AudioInputStream getStream(final AudioFormat format,150final boolean frameLength) {151final int dataSize = FRAME_LENGTH * format.getFrameSize();152byte[] buf = new byte[dataSize];153final InputStream in = new ByteArrayInputStream(buf);154if (frameLength) {155return new AudioInputStream(in, format, FRAME_LENGTH);156} else {157return new AudioInputStream(in, format, NOT_SPECIFIED);158}159}160}161162163