Path: blob/master/test/jdk/javax/sound/midi/File/SMFParserBreak.java
41153 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.FilterInputStream;25import java.io.IOException;26import java.io.InputStream;2728import javax.sound.midi.MidiSystem;29import javax.sound.midi.Sequence;3031/**32* @test33* @bug 491098634* @summary MIDI file parser breaks up on http connection35*/36public class SMFParserBreak {3738public static void main(String[] args) throws Exception {3940InputStream is = new ByteArrayInputStream(midifile);41// create a buffered input stream that seems42// to be on an unfortunate boundary for the43// 1.4.2 SMF parser implementation44is = new ChunkInputStream(is, 32);45Sequence sequence = MidiSystem.getSequence(is);4647long duration = sequence.getMicrosecondLength() / 10000;48System.out.println("Duration: "+duration+" deciseconds ");4950// the test is passed if no exception thrown51System.out.println("Test passed");52}5354// A MIDI file55static byte[] midifile = {5677, 84, 104, 100, 0, 0, 0, 6, 0, 1, 0, 3, -30, 120, 77, 84, 114, 107, 0,570, 0, 123, 0, -112, 30, 100, -113, 49, -128, 50, 100, -114, 69, -112, 31,58100, -114, 33, -128, 51, 100, -114, 55, -112, 32, 100, -114, 120, -128, 52,59100, -114, 40, -112, 33, 100, -114, 26, -128, 53, 100, -114, 26, -112, 34,60100, -114, 76, -128, 54, 100, -114, 12, -112, 35, 100, -114, 91, -128, 55,61100, -114, 69, -112, 36, 100, -114, 33, -128, 56, 100, -114, 55, -112, 37,62100, -114, 84, -128, 57, 100, -114, 40, -112, 38, 100, -114, 26, -128, 58,63100, -114, 26, -112, 39, 100, -113, 24, -128, 59, 100, -113, 60, -112, 40,64100, -113, 110, -128, 60, 100, -113, 96, -112, 41, 100, -113, 39, -128, 61,65100, 0, -1, 47, 0, 77, 84, 114, 107, 0, 0, 0, 4, 0, -1, 47, 0, 77, 84, 114,66107, 0, 0, 0, 4, 0, -1, 47, 067};68}6970/* an input stream that always returns data in chunks */71class ChunkInputStream extends FilterInputStream {72int chunkSize;73int p = 0; // position7475public ChunkInputStream(InputStream is, int chunkSize) {76super(is);77this.chunkSize = chunkSize;78}7980// override to increase counter81public int read() throws IOException {82int ret = super.read();83if (ret >= 0) {84p++;85}86return ret;87}8889// override to make sure that read(byte[], int, int) is used90public int read(byte[] b) throws IOException {91return read(b, 0, b.length);92}9394// override to split the data in chunks95public int read(byte[] b, int off, int len) throws IOException {96// if we would pass a chunk boundary,97// only return up to the chunk boundary98if ( (p / chunkSize) < ( (p+len) / chunkSize)) {99// p+len is in the next chunk100len -= ((p+len) % chunkSize);101}102int ret = super.read(b, off, len);103if (ret >= 0) {104p += ret;105}106return ret;107}108}109110111