8272255: Completely handle MIDI files

Reviewed-by: jdv, rhalade, mschoene, ahgross, prr
This commit is contained in:
Alexander Zuev 2021-11-10 05:15:13 +00:00 committed by Henry Jen
parent 11faf5395f
commit d492b07f11

@ -76,12 +76,26 @@ public final class AudioFileSoundbankReader extends SoundbankReader {
public Soundbank getSoundbank(AudioInputStream ais)
throws InvalidMidiDataException, IOException {
int MEGABYTE = 1048576;
int DEFAULT_BUFFER_SIZE = 65536;
int MAX_FRAME_SIZE = 1024;
try {
byte[] buffer;
if (ais.getFrameLength() == -1) {
int frameSize = ais.getFormat().getFrameSize();
if (frameSize <= 0 || frameSize > MAX_FRAME_SIZE) {
throw new InvalidMidiDataException("Formats with frame size "
+ frameSize + " are not supported");
}
long totalSize = ais.getFrameLength() * frameSize;
if (totalSize >= Integer.MAX_VALUE - 2) {
throw new InvalidMidiDataException(
"Can not allocate enough memory to read audio data.");
}
if (ais.getFrameLength() == -1 || totalSize > MEGABYTE) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[1024
- (1024 % ais.getFormat().getFrameSize())];
byte[] buff = new byte[DEFAULT_BUFFER_SIZE - (DEFAULT_BUFFER_SIZE % frameSize)];
int ret;
while ((ret = ais.read(buff)) != -1) {
baos.write(buff, 0, ret);
@ -89,8 +103,7 @@ public final class AudioFileSoundbankReader extends SoundbankReader {
ais.close();
buffer = baos.toByteArray();
} else {
buffer = new byte[(int) (ais.getFrameLength()
* ais.getFormat().getFrameSize())];
buffer = new byte[(int) totalSize];
new DataInputStream(ais).readFully(buffer);
}
ModelByteBufferWavetable osc = new ModelByteBufferWavetable(