|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -146,34 +146,27 @@ private MidiFileFormat getMidiFileFormatFromStream(InputStream stream,
|
146 | 146 |
|
147 | 147 | @Override
|
148 | 148 | 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; |
156 | 154 | }
|
157 |
| - return fileFormat; |
158 | 155 | }
|
159 | 156 |
|
160 | 157 | @Override
|
161 | 158 | 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; |
175 | 169 | }
|
176 |
| - return fileFormat; |
177 | 170 | }
|
178 | 171 |
|
179 | 172 | @Override
|
@@ -204,28 +197,22 @@ public Sequence getSequence(InputStream stream) throws InvalidMidiDataException,
|
204 | 197 |
|
205 | 198 | @Override
|
206 | 199 | 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; |
214 | 205 | }
|
215 |
| - return seq; |
216 | 206 | }
|
217 | 207 |
|
218 | 208 | @Override
|
219 | 209 | 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; |
227 | 215 | }
|
228 |
| - return seq; |
229 | 216 | }
|
230 | 217 | }
|
231 | 218 |
|
|
0 commit comments