1 /* |
1 /* |
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.media.sound; |
26 package com.sun.media.sound; |
27 |
27 |
|
28 import java.io.BufferedInputStream; |
28 import java.io.DataInputStream; |
29 import java.io.DataInputStream; |
|
30 import java.io.EOFException; |
29 import java.io.File; |
31 import java.io.File; |
30 import java.io.FileInputStream; |
32 import java.io.FileInputStream; |
|
33 import java.io.IOException; |
31 import java.io.InputStream; |
34 import java.io.InputStream; |
32 import java.io.IOException; |
|
33 import java.io.EOFException; |
|
34 import java.io.BufferedInputStream; |
|
35 import java.net.URL; |
35 import java.net.URL; |
36 |
36 |
37 import javax.sound.midi.MidiFileFormat; |
|
38 import javax.sound.midi.InvalidMidiDataException; |
37 import javax.sound.midi.InvalidMidiDataException; |
39 import javax.sound.midi.MetaMessage; |
38 import javax.sound.midi.MetaMessage; |
40 import javax.sound.midi.MidiEvent; |
39 import javax.sound.midi.MidiEvent; |
|
40 import javax.sound.midi.MidiFileFormat; |
41 import javax.sound.midi.MidiMessage; |
41 import javax.sound.midi.MidiMessage; |
42 import javax.sound.midi.Sequence; |
42 import javax.sound.midi.Sequence; |
43 import javax.sound.midi.SysexMessage; |
43 import javax.sound.midi.SysexMessage; |
44 import javax.sound.midi.Track; |
44 import javax.sound.midi.Track; |
45 import javax.sound.midi.spi.MidiFileReader; |
45 import javax.sound.midi.spi.MidiFileReader; |
46 |
|
47 |
|
48 |
46 |
49 /** |
47 /** |
50 * MIDI file reader. |
48 * MIDI file reader. |
51 * |
49 * |
52 * @author Kara Kytle |
50 * @author Kara Kytle |
53 * @author Jan Borgersen |
51 * @author Jan Borgersen |
54 * @author Florian Bomers |
52 * @author Florian Bomers |
55 */ |
53 */ |
56 |
|
57 public final class StandardMidiFileReader extends MidiFileReader { |
54 public final class StandardMidiFileReader extends MidiFileReader { |
58 |
55 |
59 private static final int MThd_MAGIC = 0x4d546864; // 'MThd' |
56 private static final int MThd_MAGIC = 0x4d546864; // 'MThd' |
60 |
57 |
61 private static final int bisBufferSize = 1024; // buffer size in buffered input streams |
58 private static final int bisBufferSize = 1024; // buffer size in buffered input streams |
62 |
59 |
63 public MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOException { |
60 public MidiFileFormat getMidiFileFormat(InputStream stream) |
|
61 throws InvalidMidiDataException, IOException { |
64 return getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, null); |
62 return getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, null); |
65 } |
63 } |
66 |
64 |
67 // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length |
65 // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() |
68 private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, int fileLength, SMFParser smfParser) throws InvalidMidiDataException, IOException { |
66 // returns format having invalid length |
|
67 private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, |
|
68 int fileLength, |
|
69 SMFParser smfParser) |
|
70 throws InvalidMidiDataException, IOException{ |
69 int maxReadLength = 16; |
71 int maxReadLength = 16; |
70 int duration = MidiFileFormat.UNKNOWN_LENGTH; |
72 int duration = MidiFileFormat.UNKNOWN_LENGTH; |
71 DataInputStream dis; |
73 DataInputStream dis; |
72 |
74 |
73 if (stream instanceof DataInputStream) { |
75 if (stream instanceof DataInputStream) { |
295 if (trackLength < 0) { |
297 if (trackLength < 0) { |
296 return false; |
298 return false; |
297 } |
299 } |
298 } |
300 } |
299 // now read track in a byte array |
301 // now read track in a byte array |
300 trackData = new byte[trackLength]; |
302 try { |
|
303 trackData = new byte[trackLength]; |
|
304 } catch (final OutOfMemoryError oom) { |
|
305 throw new IOException("Track length too big", oom); |
|
306 } |
301 try { |
307 try { |
302 // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection |
308 // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection |
303 stream.readFully(trackData); |
309 stream.readFully(trackData); |
304 } catch (EOFException eof) { |
310 } catch (EOFException eof) { |
305 if (!STRICT_PARSER) { |
311 if (!STRICT_PARSER) { |
384 |
390 |
385 case 0xFF: |
391 case 0xFF: |
386 // meta |
392 // meta |
387 int metaType = readUnsigned(); |
393 int metaType = readUnsigned(); |
388 int metaLength = (int) readVarInt(); |
394 int metaLength = (int) readVarInt(); |
389 |
395 final byte[] metaData; |
390 byte[] metaData = new byte[metaLength]; |
396 try { |
|
397 metaData = new byte[metaLength]; |
|
398 } catch (final OutOfMemoryError oom) { |
|
399 throw new IOException("Meta length too big", oom); |
|
400 } |
|
401 |
391 read(metaData); |
402 read(metaData); |
392 |
403 |
393 MetaMessage metaMessage = new MetaMessage(); |
404 MetaMessage metaMessage = new MetaMessage(); |
394 metaMessage.setMessage(metaType, metaData, metaLength); |
405 metaMessage.setMessage(metaType, metaData, metaLength); |
395 message = metaMessage; |
406 message = metaMessage; |