Java 7: 'try finally' replaceable with 'try' with resources

This commit is contained in:
Lars Grefer
2019-08-27 02:12:16 +02:00
committed by Rossen Stoyanchev
parent 78e8f7a3f8
commit ef48c15603
2 changed files with 4 additions and 19 deletions

View File

@@ -93,9 +93,7 @@ public class DefaultDocumentLoader implements DocumentLoader {
}
public Document loadDocument(Resource resource) throws IOException, ParserConfigurationException, SAXException {
InputStream is = null;
try {
is = resource.getInputStream();
try (InputStream is = resource.getInputStream()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(isValidating());
factory.setNamespaceAware(true);
@@ -111,10 +109,6 @@ public class DefaultDocumentLoader implements DocumentLoader {
docBuilder.setErrorHandler(new SimpleSaxErrorHandler(logger));
docBuilder.setEntityResolver(getEntityResolver());
return docBuilder.parse(is);
} finally {
if (is != null) {
is.close();
}
}
}
}

View File

@@ -168,13 +168,10 @@ public class SerializedFlowExecutionSnapshot extends FlowExecutionSnapshot imple
*/
protected byte[] serialize(FlowExecution flowExecution) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
try {
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(flowExecution);
oos.flush();
return baos.toByteArray();
} finally {
oos.close();
}
}
@@ -189,11 +186,8 @@ public class SerializedFlowExecutionSnapshot extends FlowExecutionSnapshot imple
*/
protected FlowExecution deserialize(byte[] data, ClassLoader classLoader) throws IOException,
ClassNotFoundException {
ObjectInputStream ois = new ConfigurableObjectInputStream(new ByteArrayInputStream(data), classLoader);
try {
try (ObjectInputStream ois = new ConfigurableObjectInputStream(new ByteArrayInputStream(data), classLoader)) {
return (FlowExecution) ois.readObject();
} finally {
ois.close();
}
}
@@ -203,12 +197,9 @@ public class SerializedFlowExecutionSnapshot extends FlowExecutionSnapshot imple
*/
protected byte[] compress(byte[] dataToCompress) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipos = new GZIPOutputStream(baos);
try {
try (GZIPOutputStream gzipos = new GZIPOutputStream(baos)) {
gzipos.write(dataToCompress);
gzipos.flush();
} finally {
gzipos.close();
}
return baos.toByteArray();
}