Replace try finally with try with resources
This commit is contained in:
parent
595406ab68
commit
3f8b35343c
@ -50,17 +50,13 @@ public class ResourceDecalImage implements DecalImage {
|
|||||||
@Override
|
@Override
|
||||||
public void exportImage(File file) throws IOException {
|
public void exportImage(File file) throws IOException {
|
||||||
InputStream is = getBytes();
|
InputStream is = getBytes();
|
||||||
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
|
|
||||||
|
|
||||||
if (is == null) {
|
if (is == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try (is; OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||||
FileUtils.copy(is, os);
|
FileUtils.copy(is, os);
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
os.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,12 +166,9 @@ public class DecalRegistry {
|
|||||||
if (!exportedFile.exists()) {
|
if (!exportedFile.exists()) {
|
||||||
throw new DecalNotFoundException(exportedFile.getAbsolutePath(), this);
|
throw new DecalNotFoundException(exportedFile.getAbsolutePath(), this);
|
||||||
}
|
}
|
||||||
InputStream rawIs = new FileInputStream(exportedFile);
|
try (InputStream rawIs = new FileInputStream(exportedFile)) {
|
||||||
try {
|
|
||||||
byte[] bytes = FileUtils.readBytes(rawIs);
|
byte[] bytes = FileUtils.readBytes(rawIs);
|
||||||
return new ByteArrayInputStream(bytes);
|
return new ByteArrayInputStream(bytes);
|
||||||
} finally {
|
|
||||||
rawIs.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,7 @@ public class ZipFileAttachment extends Attachment {
|
|||||||
public InputStream getBytes() throws DecalNotFoundException, IOException {
|
public InputStream getBytes() throws DecalNotFoundException, IOException {
|
||||||
String name = getName();
|
String name = getName();
|
||||||
|
|
||||||
ZipInputStream zis = new ZipInputStream(zipFileLocation.openStream());
|
try (ZipInputStream zis = new ZipInputStream(zipFileLocation.openStream())) {
|
||||||
|
|
||||||
try {
|
|
||||||
ZipEntry entry = zis.getNextEntry();
|
ZipEntry entry = zis.getNextEntry();
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
if (entry.getName().equals(name)) {
|
if (entry.getName().equals(name)) {
|
||||||
@ -36,8 +34,6 @@ public class ZipFileAttachment extends Attachment {
|
|||||||
entry = zis.getNextEntry();
|
entry = zis.getNextEntry();
|
||||||
}
|
}
|
||||||
throw new DecalNotFoundException(name, null);
|
throw new DecalNotFoundException(name, null);
|
||||||
} finally {
|
|
||||||
zis.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -211,9 +211,9 @@ public class GeneralRocketSaver {
|
|||||||
|
|
||||||
// Open a zip stream to write to.
|
// Open a zip stream to write to.
|
||||||
ZipOutputStream zos = new ZipOutputStream(output);
|
ZipOutputStream zos = new ZipOutputStream(output);
|
||||||
zos.setLevel(9);
|
|
||||||
// big try block to close the zos.
|
// big try block to close the zos.
|
||||||
try {
|
try (zos) {
|
||||||
|
zos.setLevel(9);
|
||||||
|
|
||||||
ZipEntry mainFile = new ZipEntry("rocket.ork");
|
ZipEntry mainFile = new ZipEntry("rocket.ork");
|
||||||
zos.putNextEntry(mainFile);
|
zos.putNextEntry(mainFile);
|
||||||
@ -241,8 +241,6 @@ public class GeneralRocketSaver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
zos.flush();
|
zos.flush();
|
||||||
} finally {
|
|
||||||
zos.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -128,9 +128,7 @@ public abstract class RockSimComponentFileLoader {
|
|||||||
if (is == null) {
|
if (is == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
InputStreamReader r = null;
|
try (InputStreamReader r = new InputStreamReader(is)) {
|
||||||
try {
|
|
||||||
r = new InputStreamReader(is);
|
|
||||||
|
|
||||||
// Create the CSV reader. Use comma separator.
|
// Create the CSV reader. Use comma separator.
|
||||||
CSVParser parser = new CSVParserBuilder()
|
CSVParser parser = new CSVParserBuilder()
|
||||||
@ -160,13 +158,6 @@ public abstract class RockSimComponentFileLoader {
|
|||||||
return;
|
return;
|
||||||
} catch (IOException | CsvValidationException e) {
|
} catch (IOException | CsvValidationException e) {
|
||||||
throw new BugException("Could not read component file", e);
|
throw new BugException("Could not read component file", e);
|
||||||
} finally {
|
|
||||||
if (r != null) {
|
|
||||||
try {
|
|
||||||
r.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -65,13 +65,10 @@ public class SlideSetLoader {
|
|||||||
*/
|
*/
|
||||||
public SlideSet load(String filename) throws IOException {
|
public SlideSet load(String filename) throws IOException {
|
||||||
String file = baseDir + filename;
|
String file = baseDir + filename;
|
||||||
InputStream in = getLocalizedFile(file);
|
|
||||||
|
|
||||||
try {
|
try (InputStream in = getLocalizedFile(file)) {
|
||||||
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
|
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
|
||||||
return load(reader);
|
return load(reader);
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,12 +120,12 @@ public class SlideSetManager {
|
|||||||
|
|
||||||
private List<String> loadTourList() throws IOException {
|
private List<String> loadTourList() throws IOException {
|
||||||
InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE);
|
InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE);
|
||||||
|
|
||||||
|
try (in) {
|
||||||
if (in == null) {
|
if (in == null) {
|
||||||
throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found.");
|
throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
List<String> tours = new ArrayList<>();
|
List<String> tours = new ArrayList<>();
|
||||||
TextLineReader reader = new TextLineReader(in);
|
TextLineReader reader = new TextLineReader(in);
|
||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
@ -133,20 +133,18 @@ public class SlideSetManager {
|
|||||||
}
|
}
|
||||||
return tours;
|
return tours;
|
||||||
|
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private StyleSheet loadStyleSheet() throws IOException {
|
private StyleSheet loadStyleSheet() throws IOException {
|
||||||
InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE);
|
InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE);
|
||||||
|
|
||||||
|
try (in) {
|
||||||
if (in == null) {
|
if (in == null) {
|
||||||
throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found.");
|
throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
StyleSheet ss = new StyleSheet();
|
StyleSheet ss = new StyleSheet();
|
||||||
ss.addRule(String.format("p { color: rgb(%d, %d, %d, %d)",
|
ss.addRule(String.format("p { color: rgb(%d, %d, %d, %d)",
|
||||||
textColor.getRed(), textColor.getGreen(), textColor.getBlue(), textColor.getAlpha()));
|
textColor.getRed(), textColor.getGreen(), textColor.getBlue(), textColor.getAlpha()));
|
||||||
@ -154,8 +152,6 @@ public class SlideSetManager {
|
|||||||
ss.loadRules(reader, null);
|
ss.loadRules(reader, null);
|
||||||
return ss;
|
return ss;
|
||||||
|
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -153,9 +153,7 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterate over JAR entries searching for designs
|
// Iterate over JAR entries searching for designs
|
||||||
JarFile jarFile = null;
|
try (JarFile jarFile = new JarFile(file)) {
|
||||||
try {
|
|
||||||
jarFile = new JarFile(file);
|
|
||||||
|
|
||||||
// Loop through JAR entries searching for files to load
|
// Loop through JAR entries searching for files to load
|
||||||
Enumeration<JarEntry> entries = jarFile.entries();
|
Enumeration<JarEntry> entries = jarFile.entries();
|
||||||
@ -174,13 +172,6 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
|
|||||||
logger.error("IOException when processing jarFile", e);
|
logger.error("IOException when processing jarFile", e);
|
||||||
// Could be normal condition if not package in JAR
|
// Could be normal condition if not package in JAR
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
if (jarFile != null) {
|
|
||||||
try {
|
|
||||||
jarFile.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list.toArray(new ExampleDesignFile[0]);
|
return list.toArray(new ExampleDesignFile[0]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user