jdk/test/javax/sound/sampled/AudioInputStream/FrameLengthAfterConversion.java
changeset 45341 b7cc7f639ecf
parent 40718 fe2adbe4d101
equal deleted inserted replaced
45023:323c0edaa17b 45341:b7cc7f639ecf
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    92                 }
    92                 }
    93             }
    93             }
    94         }
    94         }
    95     }
    95     }
    96 
    96 
    97     public static void main(final String[] args) {
    97     public static void main(final String[] args) throws IOException {
    98         for (final FormatConversionProvider fcp : load(
    98         for (final FormatConversionProvider fcp : load(
    99                 FormatConversionProvider.class)) {
    99                 FormatConversionProvider.class)) {
   100             System.out.println("fcp = " + fcp);
   100             System.out.println("fcp = " + fcp);
   101             for (final AudioFormat from : formats) {
   101             for (final AudioFormat from : formats) {
   102                 for (final AudioFormat to : formats) {
   102                 for (final AudioFormat to : formats) {
   137      * Verifies the frame length after the stream was saved/read to/from
   137      * Verifies the frame length after the stream was saved/read to/from
   138      * stream.
   138      * stream.
   139      */
   139      */
   140     private static void testAfterSaveToStream(final AudioFileWriter afw,
   140     private static void testAfterSaveToStream(final AudioFileWriter afw,
   141                                               final AudioFileFormat.Type type,
   141                                               final AudioFileFormat.Type type,
   142                                               final AudioInputStream ais) {
   142                                               final AudioInputStream ais)
       
   143             throws IOException {
   143         try {
   144         try {
   144             final ByteArrayOutputStream out = new ByteArrayOutputStream();
   145             final ByteArrayOutputStream out = new ByteArrayOutputStream();
   145             afw.write(ais, type, out);
   146             afw.write(ais, type, out);
   146             final InputStream input = new ByteArrayInputStream(
   147             final InputStream input = new ByteArrayInputStream(
   147                     out.toByteArray());
   148                     out.toByteArray());
   148             validate(AudioSystem.getAudioInputStream(input).getFrameLength());
   149             validate(AudioSystem.getAudioInputStream(input).getFrameLength());
   149         } catch (IllegalArgumentException | UnsupportedAudioFileException
   150         } catch (IllegalArgumentException | UnsupportedAudioFileException
   150                 | IOException ignored) {
   151                 ignored) {
   151         }
   152         }
   152     }
   153     }
   153 
   154 
   154     /**
   155     /**
   155      * Verifies the frame length after the stream was saved/read to/from file.
   156      * Verifies the frame length after the stream was saved/read to/from file.
   156      */
   157      */
   157     private static void testAfterSaveToFile(final AudioFileWriter afw,
   158     private static void testAfterSaveToFile(final AudioFileWriter afw,
   158                                             final AudioFileFormat.Type type,
   159                                             final AudioFileFormat.Type type,
   159                                             AudioInputStream ais) {
   160                                             AudioInputStream ais)
       
   161             throws IOException {
       
   162         final File temp = File.createTempFile("sound", ".tmp");
   160         try {
   163         try {
   161             final File temp = File.createTempFile("sound", ".tmp");
       
   162             temp.deleteOnExit();
       
   163             afw.write(ais, type, temp);
   164             afw.write(ais, type, temp);
   164             ais = AudioSystem.getAudioInputStream(temp);
   165             ais = AudioSystem.getAudioInputStream(temp);
   165             final long frameLength = ais.getFrameLength();
   166             final long frameLength = ais.getFrameLength();
   166             ais.close();
   167             ais.close();
   167             Files.delete(Paths.get(temp.getAbsolutePath()));
       
   168             validate(frameLength);
   168             validate(frameLength);
   169         } catch (IllegalArgumentException | UnsupportedAudioFileException
   169         } catch (IllegalArgumentException | UnsupportedAudioFileException
   170                 | IOException ignored) {
   170                 ignored) {
       
   171         } finally {
       
   172             Files.delete(Paths.get(temp.getAbsolutePath()));
   171         }
   173         }
   172     }
   174     }
   173 
   175 
   174     /**
   176     /**
   175      * Verifies the frame length after the stream was converted to other
   177      * Verifies the frame length after the stream was converted to other