Skip to content

Commit 70c6df6

Browse files
turbanoffmrserb
authored andcommittedDec 24, 2021
8274893: Update java.desktop classes to use try-with-resources
Reviewed-by: serb
1 parent d52392c commit 70c6df6

24 files changed

+129
-200
lines changed
 

‎src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -669,18 +669,9 @@ private void parse_tRNS_chunk(int chunkLength) throws IOException {
669669

670670
private static byte[] inflate(byte[] b) throws IOException {
671671
InputStream bais = new ByteArrayInputStream(b);
672-
InputStream iis = new InflaterInputStream(bais);
673-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
674-
675-
int c;
676-
try {
677-
while ((c = iis.read()) != -1) {
678-
baos.write(c);
679-
}
680-
} finally {
681-
iis.close();
672+
try (InputStream iis = new InflaterInputStream(bais)) {
673+
return iis.readAllBytes();
682674
}
683-
return baos.toByteArray();
684675
}
685676

686677
private void parse_zTXt_chunk(int chunkLength) throws IOException {

‎src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -583,15 +583,16 @@ public Object run() {
583583
URL url = new URL(new File(userHome).toURI().toURL(),
584584
".gconf/apps/metacity/general/%25gconf.xml");
585585
// Pending: verify character encoding spec for gconf
586-
Reader reader = new InputStreamReader(url.openStream(),
587-
ISO_8859_1);
588-
char[] buf = new char[1024];
589586
StringBuilder sb = new StringBuilder();
590-
int n;
591-
while ((n = reader.read(buf)) >= 0) {
592-
sb.append(buf, 0, n);
587+
try (InputStream in = url.openStream();
588+
Reader reader = new InputStreamReader(in, ISO_8859_1))
589+
{
590+
char[] buf = new char[1024];
591+
int n;
592+
while ((n = reader.read(buf)) >= 0) {
593+
sb.append(buf, 0, n);
594+
}
593595
}
594-
reader.close();
595596
String str = sb.toString();
596597
if (str != null) {
597598
String strLowerCase = str.toLowerCase();

‎src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -48,10 +48,8 @@ public final class AudioFileSoundbankReader extends SoundbankReader {
4848
@Override
4949
public Soundbank getSoundbank(URL url)
5050
throws InvalidMidiDataException, IOException {
51-
try {
52-
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
51+
try (AudioInputStream ais = AudioSystem.getAudioInputStream(url)) {
5352
Soundbank sbk = getSoundbank(ais);
54-
ais.close();
5553
return sbk;
5654
} catch (UnsupportedAudioFileException e) {
5755
return null;

‎src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,16 @@ public DLSSoundbank() {
191191
}
192192

193193
public DLSSoundbank(URL url) throws IOException {
194-
InputStream is = url.openStream();
195-
try {
194+
try (InputStream is = url.openStream()) {
196195
readSoundbank(is);
197-
} finally {
198-
is.close();
199196
}
200197
}
201198

202199
public DLSSoundbank(File file) throws IOException {
203200
largeFormat = true;
204201
sampleFile = file;
205-
InputStream is = new FileInputStream(file);
206-
try {
202+
try (InputStream is = new FileInputStream(file)) {
207203
readSoundbank(is);
208-
} finally {
209-
is.close();
210204
}
211205
}
212206

@@ -875,15 +869,21 @@ private void readWaveInfoChunk(DLSSample dlssample, RIFFReader riff)
875869
}
876870

877871
public void save(String name) throws IOException {
878-
writeSoundbank(new RIFFWriter(name, "DLS "));
872+
try (RIFFWriter writer = new RIFFWriter(name, "DLS ")) {
873+
writeSoundbank(writer);
874+
}
879875
}
880876

881877
public void save(File file) throws IOException {
882-
writeSoundbank(new RIFFWriter(file, "DLS "));
878+
try (RIFFWriter writer = new RIFFWriter(file, "DLS ")) {
879+
writeSoundbank(writer);
880+
}
883881
}
884882

885883
public void save(OutputStream out) throws IOException {
886-
writeSoundbank(new RIFFWriter(out, "DLS "));
884+
try (RIFFWriter writer = new RIFFWriter(out, "DLS ")) {
885+
writeSoundbank(writer);
886+
}
887887
}
888888

889889
private void writeSoundbank(RIFFWriter writer) throws IOException {
@@ -923,8 +923,6 @@ private void writeSoundbank(RIFFWriter writer) throws IOException {
923923
writer.seek(bak);
924924

925925
writeInfo(writer.writeList("INFO"), info);
926-
927-
writer.close();
928926
}
929927

930928
private void writeSample(RIFFWriter writer, DLSSample sample)

‎src/java.desktop/share/classes/com/sun/media/sound/JARSoundbankReader.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,8 +51,7 @@ public final class JARSoundbankReader extends SoundbankReader {
5151
private static boolean isZIP(URL url) {
5252
boolean ok = false;
5353
try {
54-
InputStream stream = url.openStream();
55-
try {
54+
try (InputStream stream = url.openStream()) {
5655
byte[] buff = new byte[4];
5756
ok = stream.read(buff) == 4;
5857
if (ok) {
@@ -61,8 +60,6 @@ private static boolean isZIP(URL url) {
6160
&& buff[2] == 0x03
6261
&& buff[3] == 0x04);
6362
}
64-
} finally {
65-
stream.close();
6663
}
6764
} catch (IOException e) {
6865
}
@@ -81,8 +78,7 @@ public Soundbank getSoundbank(URL url)
8178
"META-INF/services/javax.sound.midi.Soundbank");
8279
if (stream == null)
8380
return null;
84-
try
85-
{
81+
try (stream) {
8682
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
8783
String line = r.readLine();
8884
while (line != null) {
@@ -100,10 +96,6 @@ public Soundbank getSoundbank(URL url)
10096
line = r.readLine();
10197
}
10298
}
103-
finally
104-
{
105-
stream.close();
106-
}
10799
if (soundbanks.size() == 0)
108100
return null;
109101
if (soundbanks.size() == 1)

‎src/java.desktop/share/classes/com/sun/media/sound/ModelByteBuffer.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,13 @@ public void load() throws IOException {
315315
"No file associated with this ByteBuffer!");
316316
}
317317

318-
DataInputStream is = new DataInputStream(getInputStream());
319-
buffer = new byte[(int) capacity()];
320-
offset = 0;
321-
is.readFully(buffer);
322-
is.close();
318+
try (InputStream is = getInputStream();
319+
DataInputStream dis = new DataInputStream(is))
320+
{
321+
buffer = new byte[(int) capacity()];
322+
offset = 0;
323+
dis.readFully(buffer);
324+
}
323325

324326
}
325327

‎src/java.desktop/share/classes/com/sun/media/sound/ModelByteBufferWavetable.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -182,18 +182,12 @@ public AudioFormat getFormat() {
182182
if (format == null) {
183183
if (buffer == null)
184184
return null;
185-
InputStream is = buffer.getInputStream();
186185
AudioFormat format = null;
187-
try {
186+
try (InputStream is = buffer.getInputStream()) {
188187
format = AudioSystem.getAudioFileFormat(is).getFormat();
189188
} catch (Exception e) {
190189
//e.printStackTrace();
191190
}
192-
try {
193-
is.close();
194-
} catch (IOException e) {
195-
//e.printStackTrace();
196-
}
197191
return format;
198192
}
199193
return format;

‎src/java.desktop/share/classes/com/sun/media/sound/SF2Soundbank.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,16 @@ public SF2Soundbank() {
9292
}
9393

9494
public SF2Soundbank(URL url) throws IOException {
95-
96-
InputStream is = url.openStream();
97-
try {
95+
try (InputStream is = url.openStream()) {
9896
readSoundbank(is);
99-
} finally {
100-
is.close();
10197
}
10298
}
10399

104100
public SF2Soundbank(File file) throws IOException {
105101
largeFormat = true;
106102
sampleFile = file;
107-
InputStream is = new FileInputStream(file);
108-
try {
103+
try (InputStream is = new FileInputStream(file)) {
109104
readSoundbank(is);
110-
} finally {
111-
is.close();
112105
}
113106
}
114107

@@ -517,22 +510,27 @@ private void readPdtaChunk(RIFFReader riff) throws IOException {
517510
}
518511

519512
public void save(String name) throws IOException {
520-
writeSoundbank(new RIFFWriter(name, "sfbk"));
513+
try (RIFFWriter writer = new RIFFWriter(name, "sfbk")) {
514+
writeSoundbank(writer);
515+
}
521516
}
522517

523518
public void save(File file) throws IOException {
524-
writeSoundbank(new RIFFWriter(file, "sfbk"));
519+
try (RIFFWriter writer = new RIFFWriter(file, "sfbk")) {
520+
writeSoundbank(writer);
521+
}
525522
}
526523

527524
public void save(OutputStream out) throws IOException {
528-
writeSoundbank(new RIFFWriter(out, "sfbk"));
525+
try (RIFFWriter writer = new RIFFWriter(out, "sfbk")) {
526+
writeSoundbank(writer);
527+
}
529528
}
530529

531530
private void writeSoundbank(RIFFWriter writer) throws IOException {
532531
writeInfo(writer.writeList("INFO"));
533532
writeSdtaChunk(writer.writeList("sdta"));
534533
writePdtaChunk(writer.writeList("pdta"));
535-
writer.close();
536534
}
537535

538536
private void writeInfoStringChunk(RIFFWriter writer, String name,

‎src/java.desktop/share/classes/com/sun/media/sound/SoftSynthesizer.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -755,10 +755,8 @@ public InputStream run() {
755755
InputStream is = AccessController.doPrivileged(action);
756756
if(is == null) continue;
757757
Soundbank sbk;
758-
try {
758+
try (is) {
759759
sbk = MidiSystem.getSoundbank(new BufferedInputStream(is));
760-
} finally {
761-
is.close();
762760
}
763761
if (sbk != null) {
764762
defaultSoundBank = sbk;
@@ -802,9 +800,8 @@ public InputStream run() {
802800
return null;
803801
});
804802
if (out != null) {
805-
try {
803+
try (out) {
806804
((SF2Soundbank) defaultSoundBank).save(out);
807-
out.close();
808805
} catch (final IOException ignored) {
809806
}
810807
}

‎src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileReader.java

+26-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -146,34 +146,27 @@ private MidiFileFormat getMidiFileFormatFromStream(InputStream stream,
146146

147147
@Override
148148
public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException {
149-
InputStream urlStream = url.openStream(); // throws IOException
150-
BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize );
151-
MidiFileFormat fileFormat = null;
152-
try {
153-
fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException
154-
} finally {
155-
bis.close();
149+
try (InputStream urlStream = url.openStream(); // throws IOException
150+
BufferedInputStream bis = new BufferedInputStream(urlStream, bisBufferSize))
151+
{
152+
MidiFileFormat fileFormat = getMidiFileFormat(bis); // throws InvalidMidiDataException
153+
return fileFormat;
156154
}
157-
return fileFormat;
158155
}
159156

160157
@Override
161158
public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException {
162-
FileInputStream fis = new FileInputStream(file); // throws IOException
163-
BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize);
164-
165-
// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
166-
long length = file.length();
167-
if (length > Integer.MAX_VALUE) {
168-
length = MidiFileFormat.UNKNOWN_LENGTH;
169-
}
170-
MidiFileFormat fileFormat = null;
171-
try {
172-
fileFormat = getMidiFileFormatFromStream(bis, (int) length, null);
173-
} finally {
174-
bis.close();
159+
try (FileInputStream fis = new FileInputStream(file); // throws IOException
160+
BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize))
161+
{
162+
// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
163+
long length = file.length();
164+
if (length > Integer.MAX_VALUE) {
165+
length = MidiFileFormat.UNKNOWN_LENGTH;
166+
}
167+
MidiFileFormat fileFormat = getMidiFileFormatFromStream(bis, (int) length, null);
168+
return fileFormat;
175169
}
176-
return fileFormat;
177170
}
178171

179172
@Override
@@ -204,28 +197,22 @@ public Sequence getSequence(InputStream stream) throws InvalidMidiDataException,
204197

205198
@Override
206199
public Sequence getSequence(URL url) throws InvalidMidiDataException, IOException {
207-
InputStream is = url.openStream(); // throws IOException
208-
is = new BufferedInputStream(is, bisBufferSize);
209-
Sequence seq = null;
210-
try {
211-
seq = getSequence(is);
212-
} finally {
213-
is.close();
200+
try (InputStream is = url.openStream(); // throws IOException
201+
BufferedInputStream bis = new BufferedInputStream(is, bisBufferSize))
202+
{
203+
Sequence seq = getSequence(bis);
204+
return seq;
214205
}
215-
return seq;
216206
}
217207

218208
@Override
219209
public Sequence getSequence(File file) throws InvalidMidiDataException, IOException {
220-
InputStream is = new FileInputStream(file); // throws IOException
221-
is = new BufferedInputStream(is, bisBufferSize);
222-
Sequence seq = null;
223-
try {
224-
seq = getSequence(is);
225-
} finally {
226-
is.close();
210+
try (InputStream is = new FileInputStream(file); // throws IOException
211+
BufferedInputStream bis = new BufferedInputStream(is, bisBufferSize))
212+
{
213+
Sequence seq = getSequence(bis);
214+
return seq;
227215
}
228-
return seq;
229216
}
230217
}
231218

0 commit comments

Comments
 (0)
Please sign in to comment.