various sonarqube fixes - mainly resource closing

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1774705 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-12-17 02:35:30 +00:00
parent ea0a511f00
commit 5684ee90a4
6 changed files with 51 additions and 55 deletions

View File

@ -243,7 +243,6 @@ public class CopyCompare
throws NoPropertySetStreamException, MarkUnsupportedException, throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException UnsupportedEncodingException, IOException
{ {
boolean equal = true;
final DocumentInputStream dis1 = new DocumentInputStream(d1); final DocumentInputStream dis1 = new DocumentInputStream(d1);
final DocumentInputStream dis2 = new DocumentInputStream(d2); final DocumentInputStream dis2 = new DocumentInputStream(d2);
try { try {
@ -251,23 +250,20 @@ public class CopyCompare
PropertySet.isPropertySetStream(dis2)) { PropertySet.isPropertySetStream(dis2)) {
final PropertySet ps1 = PropertySetFactory.create(dis1); final PropertySet ps1 = PropertySetFactory.create(dis1);
final PropertySet ps2 = PropertySetFactory.create(dis2); final PropertySet ps2 = PropertySetFactory.create(dis2);
equal = ps1.equals(ps2); if (!ps1.equals(ps2)) {
if (!equal) {
msg.append("Property sets are not equal.\n"); msg.append("Property sets are not equal.\n");
return equal; return false;
} }
} else { } else {
int i1; int i1, i2;
int i2;
do { do {
i1 = dis1.read(); i1 = dis1.read();
i2 = dis2.read(); i2 = dis2.read();
if (i1 != i2) { if (i1 != i2) {
equal = false;
msg.append("Documents are not equal.\n"); msg.append("Documents are not equal.\n");
break; return false;
} }
} while (equal && i1 == -1); } while (i1 > -1);
} }
} finally { } finally {
dis2.close(); dis2.close();

View File

@ -166,53 +166,42 @@ public class WriteAuthorAndTitle
Throwable t = null; Throwable t = null;
try try {
{
/* Find out whether the current document is a property set /* Find out whether the current document is a property set
* stream or not. */ * stream or not. */
if (PropertySet.isPropertySetStream(stream)) if (PropertySet.isPropertySetStream(stream)) {
{ try {
/* Yes, the current document is a property set stream. /* Yes, the current document is a property set stream.
* Let's create a PropertySet instance from it. */ * Let's create a PropertySet instance from it. */
PropertySet ps = null; PropertySet ps = PropertySetFactory.create(stream);
try
{ /* Now we know that we really have a property set. The next
ps = PropertySetFactory.create(stream); * step is to find out whether it is a summary information
} * or not. */
catch (NoPropertySetStreamException ex) if (ps.isSummaryInformation()) {
{ /* Yes, it is a summary information. We will modify it
* and write the result to the destination POIFS. */
editSI(poiFs, path, name, ps);
} else {
/* No, it is not a summary information. We don't care
* about its internals and copy it unmodified to the
* destination POIFS. */
copy(poiFs, path, name, ps);
}
} catch (NoPropertySetStreamException ex) {
/* This exception will not be thrown because we already /* This exception will not be thrown because we already
* checked above. */ * checked above. */
} }
} else {
/* Now we know that we really have a property set. The next
* step is to find out whether it is a summary information
* or not. */
if (ps.isSummaryInformation())
/* Yes, it is a summary information. We will modify it
* and write the result to the destination POIFS. */
editSI(poiFs, path, name, ps);
else
/* No, it is not a summary information. We don't care
* about its internals and copy it unmodified to the
* destination POIFS. */
copy(poiFs, path, name, ps);
}
else
/* No, the current document is not a property set stream. We /* No, the current document is not a property set stream. We
* copy it unmodified to the destination POIFS. */ * copy it unmodified to the destination POIFS. */
copy(poiFs, event.getPath(), event.getName(), stream); copy(poiFs, event.getPath(), event.getName(), stream);
} }
catch (MarkUnsupportedException ex) } catch (MarkUnsupportedException ex) {
{
t = ex; t = ex;
} } catch (IOException ex) {
catch (IOException ex)
{
t = ex; t = ex;
} } catch (WritingNotSupportedException ex) {
catch (WritingNotSupportedException ex)
{
t = ex; t = ex;
} }
@ -221,8 +210,7 @@ public class WriteAuthorAndTitle
* lines check whether a checked exception occured and throws an * lines check whether a checked exception occured and throws an
* unchecked exception. The message of that exception is that of * unchecked exception. The message of that exception is that of
* the underlying checked exception. */ * the underlying checked exception. */
if (t != null) if (t != null) {
{
throw new HPSFRuntimeException throw new HPSFRuntimeException
("Could not read file \"" + path + "/" + name + ("Could not read file \"" + path + "/" + name +
"\". Reason: " + Util.toString(t)); "\". Reason: " + Util.toString(t));

View File

@ -97,7 +97,10 @@ public class WriteTitle
poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME); poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
/* Write the whole POI file system to a disk file. */ /* Write the whole POI file system to a disk file. */
poiFs.writeFilesystem(new FileOutputStream(fileName)); FileOutputStream fos = new FileOutputStream(fileName);
poiFs.writeFilesystem(fos);
fos.close();
poiFs.close();
} }
} }

View File

@ -424,7 +424,7 @@ public abstract class POIDocument implements Closeable {
@Internal @Internal
protected boolean initDirectory() { protected boolean initDirectory() {
if (directory == null) { if (directory == null) {
directory = new NPOIFSFileSystem().getRoot(); directory = new NPOIFSFileSystem().getRoot(); // NOSONAR
return true; return true;
} }
return false; return false;

View File

@ -108,10 +108,14 @@ public final class PublisherTextExtractor extends POIOLE2TextExtractor {
} }
for(int i=0; i<args.length; i++) { for(int i=0; i<args.length; i++) {
PublisherTextExtractor te = new PublisherTextExtractor( FileInputStream fis = new FileInputStream(args[i]);
new FileInputStream(args[i]) try {
); PublisherTextExtractor te = new PublisherTextExtractor(fis);
System.out.println(te.getText()); System.out.println(te.getText());
te.close();
} finally {
fis.close();
}
} }
} }
} }

View File

@ -20,6 +20,7 @@ package org.apache.poi.hslf.blip;
import java.awt.Dimension; import java.awt.Dimension;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.util.zip.InflaterInputStream; import java.util.zip.InflaterInputStream;
@ -56,7 +57,11 @@ public final class PICT extends Metafile {
ByteArrayInputStream bis = new ByteArrayInputStream(data); ByteArrayInputStream bis = new ByteArrayInputStream(data);
Header header = new Header(); Header header = new Header();
header.read(data, pos); header.read(data, pos);
bis.skip(pos + header.getSize()); long bs_exp = pos + header.getSize();
long bs_act = bis.skip(bs_exp);
if (bs_exp != bs_act) {
throw new EOFException();
}
byte[] chunk = new byte[4096]; byte[] chunk = new byte[4096];
ByteArrayOutputStream out = new ByteArrayOutputStream(header.getWmfSize()); ByteArrayOutputStream out = new ByteArrayOutputStream(header.getWmfSize());
InflaterInputStream inflater = new InflaterInputStream( bis ); InflaterInputStream inflater = new InflaterInputStream( bis );