Skip to content

Commit

Permalink
Merge pull request #579 from lognaturel/close-streams
Browse files Browse the repository at this point in the history
Close streams to avoid resource leaks
  • Loading branch information
seadowg authored Jun 2, 2020
2 parents 9e86375 + 7628ab0 commit f146630
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
30 changes: 10 additions & 20 deletions src/jmh/java/org/javarosa/benchmarks/utils/FormDefCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,38 +119,28 @@ private static File getCacheFile(File formXml, String cachePath) {
}

private static FormDef deserializeFormDef(File serializedFormDef) {
FileInputStream fis;
FormDef fd;
try {
// create new form def
fd = new FormDef();
fis = new FileInputStream(serializedFormDef);
DataInputStream dis = new DataInputStream(fis);

// read serialized formdef into new formdef
try (FileInputStream fis = new FileInputStream(serializedFormDef);
DataInputStream dis = new DataInputStream(fis)) {
FormDef fd = new FormDef();
fd.readExternal(dis, ExtUtil.defaultPrototypes());
dis.close();
return fd;
} catch (Exception e) {
logger.error(e.getMessage(), e);
fd = null;
return null;
}

return fd;
}


public static String getMd5Hash(File file) {
final InputStream is;
try {
is = new FileInputStream(file);

try (InputStream is = new FileInputStream(file)){
return getMd5Hash(is);
} catch (FileNotFoundException e) {
logger.debug(String.format("Cache file %s not found", file.getAbsolutePath()), e);
return null;

} catch (IOException e) {
logger.debug(String.format("Error creating cache file %s", file.getAbsolutePath()), e);
return null;
}

return getMd5Hash(is);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public ReferenceDataSource(String referenceURI) {

@Override
public OrderedMap<String, String> getLocalizedText() {
try {
InputStream is = ReferenceManager.instance().deriveReference(referenceURI).getStream();
try (InputStream is = ReferenceManager.instance().deriveReference(referenceURI).getStream()) {
return LocalizationUtils.parseLocaleInput(is);
} catch (IOException e) {
logger.error("Error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public T read(int id) {
* @see org.javarosa.core.services.storage.IStorageUtility#readBytes(int)
*/
public byte[] readBytes(int id) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
data.get(DataUtil.integer(id)).writeExternal(new DataOutputStream(stream));
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(stream)) {
data.get(DataUtil.integer(id)).writeExternal(dataOutputStream);
return stream.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Couldn't serialize data to return to readBytes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class InternalDataInstanceParser {
*/
public static HashMap<String, DataInstance> buildInstances(String xFormSrc) {
HashMap<String, DataInstance> internalDataInstances = new HashMap<>();
try {
InputStream inputStream = new FileInputStream(xFormSrc);
try (InputStream inputStream = new FileInputStream(xFormSrc)) {
KXmlParser parser = ElementParser.instantiateParser(inputStream);
TreeElementParser treeElementParser =
new TreeElementParser(parser,0, "");
Expand Down
21 changes: 9 additions & 12 deletions src/test/java/org/javarosa/core/test/FormParseInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import static org.javarosa.form.api.FormEntryController.EVENT_END_OF_FORM;
import static org.javarosa.test.utils.ResourcePathHelper.r;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;

import java.util.List;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
Expand All @@ -14,11 +17,6 @@
import org.javarosa.form.api.FormEntryController;
import org.javarosa.form.api.FormEntryModel;
import org.javarosa.xform.util.XFormUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -50,17 +48,16 @@ public FormParseInit(Path form) {

private void init() {
String xf_name = FORM_NAME;
FileInputStream is;
try {
is = new FileInputStream(xf_name);
try (FileInputStream is = new FileInputStream(xf_name)) {
xform = XFormUtils.getFormFromInputStream(is);
} catch (FileNotFoundException e) {
logger.error("Error: the file '{}' could not be found!", xf_name);
throw new RuntimeException("Error: the file '" + xf_name + "' could not be found!");
} catch (IOException e) {
logger.debug(String.format("Error reading form with name %s", xf_name), e);
throw new RuntimeException("Error reading form with name '" + xf_name);
}

// Parse the form
xform = XFormUtils.getFormFromInputStream(is);

femodel = new FormEntryModel(xform);
fec = new FormEntryController(femodel);

Expand Down

0 comments on commit f146630

Please sign in to comment.