jdk/test/javax/sound/sampled/spi/AudioFileReader/RecognizeHugeAiffFiles.java
author serb
Wed, 04 May 2016 01:03:46 +0300
changeset 38399 bd91ce346b5b
permissions -rw-r--r--
6729836: JavaSound treats large file sizes as negative and cannot read or skip Reviewed-by: prr
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 RecognizeHugeAiffFiles {
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 maximum number of sample frames per AIFF specification.
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 /* 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
    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
     * The supported aiff sample size in bits.
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 byte[] aiffBits = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    46
            1, 2, 4, 8, 11, 16, 20, 24, 27, 32
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
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
     * The list of supported sample rates.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    51
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    52
    private static final int[] sampleRates = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    53
            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
    54
            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
    55
            5644800, Integer.MAX_VALUE
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
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    58
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    59
     * The list of supported channels.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    60
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    61
    private static final int[] channels = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    62
            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
    63
    };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    64
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
     * The list of supported number of frames.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    67
     * <p>
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    68
     * The {@code MAX_UNSIGNED_INT} is a maximum.
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
    private static final long[] numberOfFrames = {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    71
            0, 1, 2, 3, Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    72
            (long) Integer.MAX_VALUE + 1, 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
    73
    };
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
    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
    76
        for (final byte bits : aiffBits) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    77
            for (final int sampleRate : sampleRates) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    78
                for (final int channel : channels) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    79
                    for (final long dataSize : numberOfFrames) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    80
                        testAFF(bits, sampleRate, channel, dataSize);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    81
                        testAIS(bits, sampleRate, channel, dataSize);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    82
                    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    83
                }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    84
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    85
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    86
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    87
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    88
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    89
     * 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
    90
     * <p>
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    91
     * 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
    92
     * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    93
     * NOT_SPECIFIED.
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
    private static void testAFF(final byte bits, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    96
                                final int channel, final long frameLength)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    97
            throws Exception {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    98
        final byte[] header = createHeader(bits, rate, channel, frameLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
    99
        final ByteArrayInputStream fake = new ByteArrayInputStream(header);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   100
        final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
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
        if (aff.getType() != AudioFileFormat.Type.AIFF) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   103
            throw new RuntimeException("Error");
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   104
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   105
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   106
        if (frameLength <= Integer.MAX_VALUE) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   107
            if (aff.getFrameLength() != frameLength) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   108
                System.err.println("Expected: " + frameLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   109
                System.err.println("Actual: " + aff.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   110
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   111
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   112
        } else {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   113
            if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   114
                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
   115
                System.err.println("Actual: " + aff.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   116
                throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   117
            }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   118
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   119
        validateFormat(bits, rate, channel, aff.getFormat());
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
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   122
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   123
     * 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
   124
     * <p>
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   125
     * Note that the frameLength is stored as long which means that {@code
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   126
     * AudioInputStream} must store all possible data from aiff file.
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
    private static void testAIS(final byte bits, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   129
                                final int channel, final long frameLength)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   130
            throws Exception {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   131
        final byte[] header = createHeader(bits, rate, channel, frameLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   132
        final ByteArrayInputStream fake = new ByteArrayInputStream(header);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   133
        final AudioInputStream ais = AudioSystem.getAudioInputStream(fake);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   134
        final AudioFormat format = ais.getFormat();
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
        if (frameLength != ais.getFrameLength()) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   137
            System.err.println("Expected: " + frameLength);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   138
            System.err.println("Actual: " + ais.getFrameLength());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   139
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   140
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   141
        if (ais.available() < 0) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   142
            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
   143
            throw new RuntimeException();
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
        validateFormat(bits, rate, channel, format);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   147
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   148
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   149
    /**
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   150
     * 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
   151
     * stream.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   152
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   153
    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
   154
                                       final int channel,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   155
                                       final AudioFormat format) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   156
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   157
        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
   158
            System.err.println("Expected: " + rate);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   159
            System.err.println("Actual: " + format.getSampleRate());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   160
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   161
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   162
        if (format.getChannels() != channel) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   163
            System.err.println("Expected: " + channel);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   164
            System.err.println("Actual: " + format.getChannels());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   165
            throw new RuntimeException();
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   166
        }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   167
        int frameSize = ((bits + 7) / 8) * channel;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   168
        if (format.getFrameSize() != frameSize) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   169
            System.out.println("Expected: " + frameSize);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   170
            System.err.println("Actual: " + format.getFrameSize());
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   171
            throw new RuntimeException();
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
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   174
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   175
    private static final int DOUBLE_MANTISSA_LENGTH = 52;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   176
    private static final int DOUBLE_EXPONENT_LENGTH = 11;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   177
    private static final long DOUBLE_SIGN_MASK     = 0x8000000000000000L;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   178
    private static final long DOUBLE_EXPONENT_MASK = 0x7FF0000000000000L;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   179
    private static final long DOUBLE_MANTISSA_MASK = 0x000FFFFFFFFFFFFFL;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   180
    private static final int DOUBLE_EXPONENT_OFFSET = 1023;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   181
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   182
    private static final int EXTENDED_EXPONENT_OFFSET = 16383;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   183
    private static final int EXTENDED_MANTISSA_LENGTH = 63;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   184
    private static final int EXTENDED_EXPONENT_LENGTH = 15;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   185
    private static final long EXTENDED_INTEGER_MASK = 0x8000000000000000L;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   186
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
     * Creates the custom header of the AIFF file. It is expected that all
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   189
     * passed data are supported.
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   190
     */
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   191
    private static byte[] createHeader(final byte bits, final int rate,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   192
                                       final int channel, final long frameLength) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   193
        long doubleBits = Double.doubleToLongBits(rate);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   194
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   195
        long sign = (doubleBits & DOUBLE_SIGN_MASK)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   196
                >> (DOUBLE_EXPONENT_LENGTH + DOUBLE_MANTISSA_LENGTH);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   197
        long doubleExponent = (doubleBits & DOUBLE_EXPONENT_MASK)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   198
                >> DOUBLE_MANTISSA_LENGTH;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   199
        long doubleMantissa = doubleBits & DOUBLE_MANTISSA_MASK;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   200
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   201
        long extendedExponent = doubleExponent - DOUBLE_EXPONENT_OFFSET
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   202
                + EXTENDED_EXPONENT_OFFSET;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   203
        long extendedMantissa = doubleMantissa
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   204
                << (EXTENDED_MANTISSA_LENGTH - DOUBLE_MANTISSA_LENGTH);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   205
        long extendedSign = sign << EXTENDED_EXPONENT_LENGTH;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   206
        short extendedBits79To64 = (short) (extendedSign | extendedExponent);
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   207
        long extendedBits63To0 = EXTENDED_INTEGER_MASK | extendedMantissa;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   208
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   209
        return new byte[]{
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   210
                // AIFF_MAGIC
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   211
                0x46, 0x4f, 0x52, 0x4d,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   212
                // fileLength (will use the number of frames for testing)
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   213
                (byte) (frameLength >> 24), (byte) (frameLength >> 16),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   214
                (byte) (frameLength >> 8), (byte) frameLength,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   215
                //  form aiff
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   216
                0x41, 0x49, 0x46, 0x46,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   217
                // COMM_MAGIC
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   218
                0x43, 0x4f, 0x4d, 0x4d,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   219
                // comm chunk size
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   220
                0, 0, 0, 18,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   221
                // channels
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   222
                (byte) (channel >> 8),(byte) channel,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   223
                // numSampleFrames
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   224
                (byte) (frameLength >> 24), (byte) (frameLength >> 16),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   225
                (byte) (frameLength >> 8), (byte) (frameLength),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   226
                // samplesize
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   227
                (byte) (bits >> 8),(byte) (bits),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   228
                // samplerate
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   229
                (byte) (extendedBits79To64 >> 8),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   230
                (byte) extendedBits79To64,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   231
                (byte) (extendedBits63To0 >> 56),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   232
                (byte) (extendedBits63To0 >> 48),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   233
                (byte) (extendedBits63To0 >> 40),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   234
                (byte) (extendedBits63To0 >> 32), (byte) (extendedBits63To0 >> 24),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   235
                (byte) (extendedBits63To0 >> 16), (byte) (extendedBits63To0 >> 8),
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   236
                (byte) extendedBits63To0,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   237
                // SND_MAGIC
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   238
                0x53, 0x53, 0x4e, 0x44,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   239
                // data chunk size
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   240
                0, 0, 0, 0,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   241
                // dataOffset
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   242
                0, 0, 0, 0,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   243
                // blocksize
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   244
                0, 0, 0, 0,
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   245
        };
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   246
    }
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
diff changeset
   247
}