test/jdk/javax/sound/sampled/spi/AudioFileReader/RecognizeHugeAuFiles.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38399
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     1
/*
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     4
 *
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     8
 *
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    13
 * accompanied this code).
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    14
 *
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    18
 *
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    21
 * questions.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    22
 */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    23
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    25
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    26
import javax.sound.sampled.AudioFileFormat;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    27
import javax.sound.sampled.AudioFormat;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    28
import javax.sound.sampled.AudioInputStream;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    29
import javax.sound.sampled.AudioSystem;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    30
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    31
/**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    32
 * @test
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    33
 * @bug 6729836
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    34
 */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    35
public final class RecognizeHugeAuFiles {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    36
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    37
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    38
     * The size of the header's data.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    39
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    40
    private static final byte AU_HEADER = 44;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    41
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    42
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    43
     * This value should be used if the size in bytes is unknown.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    44
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    45
    private static final /* unsigned int */ long MAX_UNSIGNED_INT = 0xffffffffL;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    46
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    47
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    48
     * The list of supported au formats and sample size in bits per format.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    49
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    50
    private static final byte[][] auTypeBits = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    51
            {1, 8}, {2, 8}, {3, 16}, {4, 24}, {5, 32}, {6, 32}, {27, 8}
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    52
    };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    53
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    54
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    55
     * The list of supported sample rates(stored as unsigned int).
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    56
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    57
    private static final int[] sampleRates = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    58
            8000, 11025, 16000, 22050, 32000, 37800, 44056, 44100, 47250, 48000,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    59
            50000, 50400, 88200, 96000, 176400, 192000, 352800, 2822400,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    60
            5644800, Integer.MAX_VALUE
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    61
    };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    62
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    63
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    64
     * The list of supported channels (stored as unsigned int).
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    65
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    66
    private static final int[] channels = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    67
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    68
    };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    69
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    70
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    71
     * The list of supported size of data (stored as unsigned int).
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    72
     * <p>
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    73
     * The {@code MAX_UNSIGNED_INT} used if the size in bytes is unknown.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    74
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    75
    private static final long[] dataSizes = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    76
            0, 1, 2, 3, Integer.MAX_VALUE - AU_HEADER, Integer.MAX_VALUE - 1,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    77
            Integer.MAX_VALUE, (long) Integer.MAX_VALUE + 1,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    78
            (long) Integer.MAX_VALUE + AU_HEADER, MAX_UNSIGNED_INT - AU_HEADER,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    79
            MAX_UNSIGNED_INT - 1, MAX_UNSIGNED_INT
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    80
    };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    81
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    82
    public static void main(final String[] args) throws Exception {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    83
        for (final byte[] type : auTypeBits) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    84
            for (final int sampleRate : sampleRates) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    85
                for (final int channel : channels) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    86
                    for (final long dataSize : dataSizes) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    87
                        testAFF(type, sampleRate, channel, dataSize);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    88
                        testAIS(type, sampleRate, channel, dataSize);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    89
                    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    90
                }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    91
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    92
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    93
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    94
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    95
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    96
     * Tests the {@code AudioFileFormat} fetched from the fake header.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    97
     * <p>
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    98
     * Note that the frameLength and byteLength are stored as int which means
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    99
     * that {@code AudioFileFormat} will store the data above {@code  MAX_INT}
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   100
     * as NOT_SPECIFIED.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   101
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   102
    private static void testAFF(final byte[] type, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   103
                                final int channel, final long size)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   104
            throws Exception {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   105
        final byte[] header = createHeader(type, rate, channel, size);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   106
        final ByteArrayInputStream fake = new ByteArrayInputStream(header);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   107
        final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   108
        final AudioFormat format = aff.getFormat();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   109
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   110
        if (aff.getType() != AudioFileFormat.Type.AU) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   111
            throw new RuntimeException("Error");
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   112
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   113
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   114
        final long frameLength = size / format.getFrameSize();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   115
        if (size != MAX_UNSIGNED_INT && frameLength <= Integer.MAX_VALUE) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   116
            if (aff.getFrameLength() != frameLength) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   117
                System.err.println("Expected: " + frameLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   118
                System.err.println("Actual: " + aff.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   119
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   120
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   121
        } else {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   122
            if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   123
                System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   124
                System.err.println("Actual: " + aff.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   125
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   126
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   127
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   128
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   129
        final long byteLength = size + AU_HEADER;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   130
        if (byteLength <= Integer.MAX_VALUE) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   131
            if (aff.getByteLength() != byteLength) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   132
                System.err.println("Expected: " + byteLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   133
                System.err.println("Actual: " + aff.getByteLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   134
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   135
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   136
        } else {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   137
            if (aff.getByteLength() != AudioSystem.NOT_SPECIFIED) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   138
                System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   139
                System.err.println("Actual: " + aff.getByteLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   140
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   141
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   142
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   143
        validateFormat(type[1], rate, channel, aff.getFormat());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   144
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   145
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   146
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   147
     * Tests the {@code AudioInputStream} fetched from the fake header.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   148
     * <p>
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   149
     * Note that the frameLength is stored as long which means
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   150
     * that {@code AudioInputStream} must store all possible data from au file.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   151
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   152
    private static void testAIS(final byte[] type, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   153
                                final int channel, final long size)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   154
            throws Exception {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   155
        final byte[] header = createHeader(type, rate, channel, size);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   156
        final ByteArrayInputStream fake = new ByteArrayInputStream(header);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   157
        final AudioInputStream ais = AudioSystem.getAudioInputStream(fake);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   158
        final AudioFormat format = ais.getFormat();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   159
        final long frameLength = size / format.getFrameSize();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   160
        if (size != MAX_UNSIGNED_INT) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   161
            if (frameLength != ais.getFrameLength()) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   162
                System.err.println("Expected: " + frameLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   163
                System.err.println("Actual: " + ais.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   164
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   165
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   166
        } else {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   167
            if (ais.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   168
                System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   169
                System.err.println("Actual: " + ais.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   170
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   171
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   172
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   173
        if (ais.available() < 0) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   174
            System.err.println("available should be >=0: " + ais.available());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   175
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   176
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   177
        validateFormat(type[1], rate, channel, format);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   178
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   179
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   180
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   181
     * Tests that format contains the same data as were provided to the fake
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   182
     * stream.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   183
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   184
    private static void validateFormat(final byte bits, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   185
                                       final int channel,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   186
                                       final AudioFormat format) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   187
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   188
        if (Float.compare(format.getSampleRate(), rate) != 0) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   189
            System.out.println("Expected: " + rate);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   190
            System.out.println("Actual: " + format.getSampleRate());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   191
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   192
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   193
        if (format.getChannels() != channel) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   194
            System.out.println("Expected: " + channel);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   195
            System.out.println("Actual: " + format.getChannels());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   196
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   197
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   198
        int frameSize = ((bits + 7) / 8) * channel;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   199
        if (format.getFrameSize() != frameSize) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   200
            System.out.println("Expected: " + frameSize);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   201
            System.out.println("Actual: " + format.getFrameSize());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   202
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   203
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   204
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   205
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   206
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   207
     * Creates the custom header of the AU file. It is expected that all passed
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   208
     * data are supported.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   209
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   210
    private static byte[] createHeader(final byte[] type, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   211
                                       final int channel, final long size) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   212
        return new byte[]{
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   213
                // AU_SUN_MAGIC
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   214
                0x2e, 0x73, 0x6e, 0x64,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   215
                // headerSize
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   216
                0, 0, 0, AU_HEADER,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   217
                // dataSize
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   218
                (byte) (size >> 24), (byte) (size >> 16), (byte) (size >> 8),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   219
                (byte) size,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   220
                // encoding
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   221
                0, 0, 0, type[0],
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   222
                // sampleRate
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   223
                (byte) (rate >> 24), (byte) (rate >> 16), (byte) (rate >> 8),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   224
                (byte) (rate),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   225
                // channels
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   226
                (byte) (channel >> 24), (byte) (channel >> 16),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   227
                (byte) (channel >> 8), (byte) (channel),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   228
                // data
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   229
                0, 0, 0, 0, 0, 0
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   230
        };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   231
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   232
}