Skip to content

Commit

Permalink
SAK-29513 close streams in finally blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ottenhoff committed Jun 12, 2015
1 parent 6c676bf commit 0f5b46d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public boolean zipArchive(String siteId, String m_storagePath) throws IOExceptio
log.info("Creating zip file: " + compressedArchivePath);
zipFile.createNewFile();
}

FileOutputStream fOut = null;

FileOutputStream fOut = null;
FileInputStream zip = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream zOut = null;

Expand All @@ -128,20 +129,21 @@ public boolean zipArchive(String siteId, String m_storagePath) throws IOExceptio
bOut = new BufferedOutputStream(fOut);
zOut = new ZipArchiveOutputStream(bOut);
addFileToZip(zOut, archivePath, ""); //add the directory which will then add all files recursively

//create a sha1 hash of the zip
String hashPath = m_storagePath + siteId + "-" + timestamp + ".sha1";
log.info("Creating hash: " + hashPath);
zip = new FileInputStream(compressedArchivePath);
String hash = DigestUtils.sha1Hex(zip);
FileUtils.writeStringToFile(new File(hashPath), hash);
} finally {
zOut.finish();
zOut.close();
bOut.close();
fOut.close();
zip.close();
}

//create a sha1 hash of the zip
String hashPath = m_storagePath + siteId + "-" + timestamp + ".sha1";
log.info("Creating hash: " + hashPath);
FileInputStream zip = new FileInputStream(compressedArchivePath);
String hash = DigestUtils.sha1Hex(zip);
FileUtils.writeStringToFile(new File(hashPath), hash);

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,12 @@ public boolean outputAllFiles (ZipPrintStream out) {
} catch (Exception e) {
log.error("Lessons export error outputting file " + e);
} finally {
if (contentStream != null) {
contentStream.close();
}
if (contentStream != null) {
contentStream.close();
}
if (instream != null) {
instream.close();
}
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4317,7 +4317,8 @@ public void doUpload_Mtrl_Frm_File(RunData data) {
addAlert(state, rb.getString("importFile.choosefile"));
} else {
//Need some other kind of input stream?
ResetOnCloseInputStream fileInput = null;
ResetOnCloseInputStream fileInput = null;
InputStream fileInputStream = null;
long fileSize=0;
try {
// Write to temp file, this should probably be in the velocity util?
Expand All @@ -4326,16 +4327,15 @@ public void doUpload_Mtrl_Frm_File(RunData data) {
// Delete temp file when program exits.
tempFile.deleteOnExit();

InputStream fileInputStream = fileFromUpload.getInputStream();
fileInputStream = fileFromUpload.getInputStream();

FileOutputStream outBuf = new FileOutputStream(tempFile);
byte[] bytes = new byte[102400];
int read = 0;
while ((read = fileInputStream.read(bytes)) != -1) {
outBuf.write(bytes, 0, read);
}

fileInputStream.close();

outBuf.flush();
outBuf.close();

Expand All @@ -4348,6 +4348,16 @@ public void doUpload_Mtrl_Frm_File(RunData data) {
catch (IOException ioe) {
M_log.warn("IOException creating temp import file",ioe);
}
finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
M_log.error("Could not close fileInputStream in doUpload_Mtrl_Frm_File");
}
}

if (fileSize >= max_bytes) {
addAlert(state, rb.getFormattedMessage("importFile.size", new Object[]{max_file_size_mb}));
}
Expand Down Expand Up @@ -4386,6 +4396,14 @@ else if (fileSize > 0) {
addAlert(state, rb.getString("importFile.invalidfile"));
}
}

if (fileInput != null) {
try {
fileInput.close();
} catch (IOException e) {
M_log.error("Could not close fileInput in doUpload_Mtrl_Frm_File");
}
}
}
} // doImportMtrlFrmFile

Expand Down Expand Up @@ -15394,9 +15412,11 @@ public void doUploadArchive(RunData data)
tempZipFile.delete();
}

FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(tempZipFile);
//copy contents into this file
IOUtils.copyLarge(fi.getInputStream(), new FileOutputStream(tempZipFile));
IOUtils.copyLarge(fi.getInputStream(), fileOutputStream);

//set path into state so we can process it later
state.setAttribute(STATE_UPLOADED_ARCHIVE_PATH, tempZipFile.getAbsolutePath());
Expand All @@ -15406,6 +15426,13 @@ public void doUploadArchive(RunData data)
M_log.error(e.getMessage(), e); //general catch all for the various exceptions that occur above. all are failures.
addAlert(state, rb.getString("archive.createsite.failedupload"));
}
finally {
try {
fileOutputStream.close();
} catch (IOException e) {
M_log.error("Could not close fileOutputStream");
}
}

//go to confirm screen
state.setAttribute(STATE_TEMPLATE_INDEX, "10");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,31 @@ protected void generatePDF(Document doc, OutputStream streamOut)
if (currentLocale!=null){
String fullLocale = currentLocale.toString();
xslFileName = "participants-all-attrs_" + fullLocale + ".xsl";
if (getClass().getClassLoader().getResourceAsStream(xslFileName) == null){
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(xslFileName);
if (inputStream == null){
xslFileName = "participants-all-attrs_" + currentLocale.getCountry() + ".xsl";
if (getClass().getClassLoader().getResourceAsStream(xslFileName) == null){
inputStream = getClass().getClassLoader().getResourceAsStream(xslFileName);
if (inputStream == null){
//We use the default file
xslFileName = "participants-all-attrs.xsl";
}
}

if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error("Could not close inputStream in generatePDF");
}
}
}
String configFileName = "userconfig.xml";
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
InputStream configInputStream = null;
try
{
Configuration cfg = cfgBuilder.build(getClass().getClassLoader().getResourceAsStream(configFileName));
configInputStream = getClass().getClassLoader().getResourceAsStream(configFileName);
Configuration cfg = cfgBuilder.build(configInputStream);

FopFactory fopFactory = FopFactory.newInstance();
fopFactory.setUserConfig(cfg);
Expand Down Expand Up @@ -425,12 +437,23 @@ protected void generatePDF(Document doc, OutputStream streamOut)

Source src = new DOMSource(doc);
transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
} catch (Exception e)
}
catch (Exception e)
{
e.printStackTrace();
log.warn(this+".generatePDF(): " + e);
return;
}
finally
{
if (configInputStream != null) {
try {
configInputStream.close();
} catch (IOException e) {
log.error("Could not close the configInputStream in generatePDF");
}
}
}
}

}

0 comments on commit 0f5b46d

Please sign in to comment.