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