Skip to content

Commit

Permalink
Merge branch 'master' into goetz_backport_8346828
Browse files Browse the repository at this point in the history
  • Loading branch information
GoeLin authored Feb 22, 2025
2 parents 1255ae4 + 715d37e commit 9fbf8b9
Show file tree
Hide file tree
Showing 30 changed files with 191 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -668,18 +668,9 @@ private void parse_tRNS_chunk(int chunkLength) throws IOException {

private static byte[] inflate(byte[] b) throws IOException {
InputStream bais = new ByteArrayInputStream(b);
InputStream iis = new InflaterInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int c;
try {
while ((c = iis.read()) != -1) {
baos.write(c);
}
} finally {
iis.close();
try (InputStream iis = new InflaterInputStream(bais)) {
return iis.readAllBytes();
}
return baos.toByteArray();
}

private void parse_zTXt_chunk(int chunkLength) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,16 @@ public Object run() {
URL url = new URL(new File(userHome).toURI().toURL(),
".gconf/apps/metacity/general/%25gconf.xml");
// Pending: verify character encoding spec for gconf
Reader reader = new InputStreamReader(url.openStream(),
ISO_8859_1);
char[] buf = new char[1024];
StringBuilder sb = new StringBuilder();
int n;
while ((n = reader.read(buf)) >= 0) {
sb.append(buf, 0, n);
try (InputStream in = url.openStream();
Reader reader = new InputStreamReader(in, ISO_8859_1))
{
char[] buf = new char[1024];
int n;
while ((n = reader.read(buf)) >= 0) {
sb.append(buf, 0, n);
}
}
reader.close();
String str = sb.toString();
if (str != null) {
String strLowerCase = str.toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public final class AudioFileSoundbankReader extends SoundbankReader {
@Override
public Soundbank getSoundbank(URL url)
throws InvalidMidiDataException, IOException {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
try (AudioInputStream ais = AudioSystem.getAudioInputStream(url)) {
Soundbank sbk = getSoundbank(ais);
ais.close();
return sbk;
} catch (UnsupportedAudioFileException e) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,16 @@ public DLSSoundbank() {
}

public DLSSoundbank(URL url) throws IOException {
InputStream is = url.openStream();
try {
try (InputStream is = url.openStream()) {
readSoundbank(is);
} finally {
is.close();
}
}

public DLSSoundbank(File file) throws IOException {
largeFormat = true;
sampleFile = file;
InputStream is = new FileInputStream(file);
try {
try (InputStream is = new FileInputStream(file)) {
readSoundbank(is);
} finally {
is.close();
}
}

Expand Down Expand Up @@ -875,15 +869,21 @@ private void readWaveInfoChunk(DLSSample dlssample, RIFFReader riff)
}

public void save(String name) throws IOException {
writeSoundbank(new RIFFWriter(name, "DLS "));
try (RIFFWriter writer = new RIFFWriter(name, "DLS ")) {
writeSoundbank(writer);
}
}

public void save(File file) throws IOException {
writeSoundbank(new RIFFWriter(file, "DLS "));
try (RIFFWriter writer = new RIFFWriter(file, "DLS ")) {
writeSoundbank(writer);
}
}

public void save(OutputStream out) throws IOException {
writeSoundbank(new RIFFWriter(out, "DLS "));
try (RIFFWriter writer = new RIFFWriter(out, "DLS ")) {
writeSoundbank(writer);
}
}

private void writeSoundbank(RIFFWriter writer) throws IOException {
Expand Down Expand Up @@ -923,8 +923,6 @@ private void writeSoundbank(RIFFWriter writer) throws IOException {
writer.seek(bak);

writeInfo(writer.writeList("INFO"), info);

writer.close();
}

private void writeSample(RIFFWriter writer, DLSSample sample)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public final class JARSoundbankReader extends SoundbankReader {
private static boolean isZIP(URL url) {
boolean ok = false;
try {
InputStream stream = url.openStream();
try {
try (InputStream stream = url.openStream()) {
byte[] buff = new byte[4];
ok = stream.read(buff) == 4;
if (ok) {
Expand All @@ -73,8 +72,6 @@ private static boolean isZIP(URL url) {
&& buff[2] == 0x03
&& buff[3] == 0x04);
}
} finally {
stream.close();
}
} catch (IOException e) {
}
Expand All @@ -95,8 +92,7 @@ public Soundbank getSoundbank(URL url)
"META-INF/services/javax.sound.midi.Soundbank");
if (stream == null)
return null;
try
{
try (stream) {
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
String line = r.readLine();
while (line != null) {
Expand All @@ -114,10 +110,6 @@ public Soundbank getSoundbank(URL url)
line = r.readLine();
}
}
finally
{
stream.close();
}
if (soundbanks.size() == 0)
return null;
if (soundbanks.size() == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,13 @@ public void load() throws IOException {
"No file associated with this ByteBuffer!");
}

DataInputStream is = new DataInputStream(getInputStream());
buffer = new byte[(int) capacity()];
offset = 0;
is.readFully(buffer);
is.close();
try (InputStream is = getInputStream();
DataInputStream dis = new DataInputStream(is))
{
buffer = new byte[(int) capacity()];
offset = 0;
dis.readFully(buffer);
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -182,18 +182,12 @@ public AudioFormat getFormat() {
if (format == null) {
if (buffer == null)
return null;
InputStream is = buffer.getInputStream();
AudioFormat format = null;
try {
try (InputStream is = buffer.getInputStream()) {
format = AudioSystem.getAudioFileFormat(is).getFormat();
} catch (Exception e) {
//e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
//e.printStackTrace();
}
return format;
}
return format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,16 @@ public SF2Soundbank() {
}

public SF2Soundbank(URL url) throws IOException {

InputStream is = url.openStream();
try {
try (InputStream is = url.openStream()) {
readSoundbank(is);
} finally {
is.close();
}
}

public SF2Soundbank(File file) throws IOException {
largeFormat = true;
sampleFile = file;
InputStream is = new FileInputStream(file);
try {
try (InputStream is = new FileInputStream(file)) {
readSoundbank(is);
} finally {
is.close();
}
}

Expand Down Expand Up @@ -517,22 +510,27 @@ private void readPdtaChunk(RIFFReader riff) throws IOException {
}

public void save(String name) throws IOException {
writeSoundbank(new RIFFWriter(name, "sfbk"));
try (RIFFWriter writer = new RIFFWriter(name, "sfbk")) {
writeSoundbank(writer);
}
}

public void save(File file) throws IOException {
writeSoundbank(new RIFFWriter(file, "sfbk"));
try (RIFFWriter writer = new RIFFWriter(file, "sfbk")) {
writeSoundbank(writer);
}
}

public void save(OutputStream out) throws IOException {
writeSoundbank(new RIFFWriter(out, "sfbk"));
try (RIFFWriter writer = new RIFFWriter(out, "sfbk")) {
writeSoundbank(writer);
}
}

private void writeSoundbank(RIFFWriter writer) throws IOException {
writeInfo(writer.writeList("INFO"));
writeSdtaChunk(writer.writeList("sdta"));
writePdtaChunk(writer.writeList("pdta"));
writer.close();
}

private void writeInfoStringChunk(RIFFWriter writer, String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,8 @@ public InputStream run() {
InputStream is = AccessController.doPrivileged(action);
if(is == null) continue;
Soundbank sbk;
try {
try (is) {
sbk = MidiSystem.getSoundbank(new BufferedInputStream(is));
} finally {
is.close();
}
if (sbk != null) {
defaultSoundBank = sbk;
Expand Down Expand Up @@ -802,9 +800,8 @@ public InputStream run() {
return null;
});
if (out != null) {
try {
try (out) {
((SF2Soundbank) defaultSoundBank).save(out);
out.close();
} catch (final IOException ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -146,34 +146,27 @@ private MidiFileFormat getMidiFileFormatFromStream(InputStream stream,

@Override
public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException {
InputStream urlStream = url.openStream(); // throws IOException
BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize );
MidiFileFormat fileFormat = null;
try {
fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException
} finally {
bis.close();
try (InputStream urlStream = url.openStream(); // throws IOException
BufferedInputStream bis = new BufferedInputStream(urlStream, bisBufferSize))
{
MidiFileFormat fileFormat = getMidiFileFormat(bis); // throws InvalidMidiDataException
return fileFormat;
}
return fileFormat;
}

@Override
public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException {
FileInputStream fis = new FileInputStream(file); // throws IOException
BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize);

// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
long length = file.length();
if (length > Integer.MAX_VALUE) {
length = MidiFileFormat.UNKNOWN_LENGTH;
}
MidiFileFormat fileFormat = null;
try {
fileFormat = getMidiFileFormatFromStream(bis, (int) length, null);
} finally {
bis.close();
try (FileInputStream fis = new FileInputStream(file); // throws IOException
BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize))
{
// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
long length = file.length();
if (length > Integer.MAX_VALUE) {
length = MidiFileFormat.UNKNOWN_LENGTH;
}
MidiFileFormat fileFormat = getMidiFileFormatFromStream(bis, (int) length, null);
return fileFormat;
}
return fileFormat;
}

@Override
Expand Down Expand Up @@ -204,28 +197,22 @@ public Sequence getSequence(InputStream stream) throws InvalidMidiDataException,

@Override
public Sequence getSequence(URL url) throws InvalidMidiDataException, IOException {
InputStream is = url.openStream(); // throws IOException
is = new BufferedInputStream(is, bisBufferSize);
Sequence seq = null;
try {
seq = getSequence(is);
} finally {
is.close();
try (InputStream is = url.openStream(); // throws IOException
BufferedInputStream bis = new BufferedInputStream(is, bisBufferSize))
{
Sequence seq = getSequence(bis);
return seq;
}
return seq;
}

@Override
public Sequence getSequence(File file) throws InvalidMidiDataException, IOException {
InputStream is = new FileInputStream(file); // throws IOException
is = new BufferedInputStream(is, bisBufferSize);
Sequence seq = null;
try {
seq = getSequence(is);
} finally {
is.close();
try (InputStream is = new FileInputStream(file); // throws IOException
BufferedInputStream bis = new BufferedInputStream(is, bisBufferSize))
{
Sequence seq = getSequence(bis);
return seq;
}
return seq;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ public int write(Sequence in, int type, OutputStream out) throws IOException {
@Override
public int write(Sequence in, int type, File out) throws IOException {
Objects.requireNonNull(in);
FileOutputStream fos = new FileOutputStream(out); // throws IOException
int bytesWritten = write( in, type, fos );
fos.close();
return bytesWritten;
try (FileOutputStream fos = new FileOutputStream(out)) { // throws IOException
int bytesWritten = write(in, type, fos);
return bytesWritten;
}
}

//=================================================================================
Expand Down
Loading

0 comments on commit 9fbf8b9

Please sign in to comment.