1 /*
   2  * Copyright (c) 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.applet.AudioClip;
  25 import java.io.ByteArrayInputStream;
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.nio.file.Files;
  30 import java.util.ArrayList;
  31 import java.util.LinkedList;
  32 import java.util.List;
  33 
  34 import javax.sound.sampled.AudioFileFormat;
  35 import javax.sound.sampled.AudioFormat;
  36 import javax.sound.sampled.AudioInputStream;
  37 import javax.sound.sampled.AudioSystem;
  38 
  39 import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
  40 import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
  41 import static javax.sound.sampled.AudioFileFormat.Type.AU;
  42 import static javax.sound.sampled.AudioFileFormat.Type.SND;
  43 import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
  44 
  45 /**
  46  * @test
  47  * @bug 8204454
  48  * @summary URL.getContent() should return AudioClip for supported formats
  49  * @run main/othervm -mx128m AudioContentHandlers
  50  */
  51 public final class AudioContentHandlers {
  52 
  53     private static final List<AudioFormat> formats = new ArrayList<>();
  54 
  55     private static final AudioFormat.Encoding[] encodings =
  56             {AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
  57                     AudioFormat.Encoding.PCM_SIGNED,
  58                     AudioFormat.Encoding.PCM_UNSIGNED,
  59                     AudioFormat.Encoding.PCM_FLOAT};
  60 
  61     private static final AudioFileFormat.Type[] types =
  62             {WAVE, AU, AIFF, AIFC, SND};
  63 
  64     static {
  65         for (final AudioFormat.Encoding enc : encodings) {
  66             formats.add(new AudioFormat(enc, 44100, 8, 1, 1, 44100, true));
  67             formats.add(new AudioFormat(enc, 44100, 8, 1, 1, 44100, false));
  68         }
  69     }
  70 
  71     public static void main(final String[] args) throws Exception {
  72         for (final AudioFileFormat.Type type : types) {
  73             for (final AudioFormat format : formats) {
  74                 File file = new File("audio." + type.getExtension());
  75                 try {
  76                     AudioSystem.write(getStream(format), type, file);
  77                 } catch (IOException | IllegalArgumentException ignored) {
  78                     continue;
  79                 }
  80                 AudioClip content;
  81                 try {
  82                     content = (AudioClip) file.toURL().getContent();
  83                     // We need to generate OOM because the stream in AudioClip
  84                     // will be closed in finalize().
  85                     generateOOME();
  86                 } finally {
  87                     Files.delete(file.toPath());
  88                 }
  89                 if (content == null) {
  90                     throw new RuntimeException("Content is null");
  91                 }
  92             }
  93         }
  94     }
  95 
  96     private static AudioInputStream getStream(final AudioFormat format) {
  97         final InputStream in = new ByteArrayInputStream(new byte[100]);
  98         return new AudioInputStream(in, format, 10);
  99     }
 100 
 101     private static void generateOOME() throws Exception {
 102         List<Object> leak = new LinkedList<>();
 103         try {
 104             while (true) {
 105                 leak.add(new byte[1024 * 1024]);
 106             }
 107         } catch (OutOfMemoryError ignored) {
 108         }
 109         // Give the GC a chance at that weakref in case of slow systems
 110         Thread.sleep(2000);
 111     }
 112 }