Permalink
Browse files

Ensure proper closing of file input streams.

  • Loading branch information...
luccioman committed Jun 2, 2017
1 parent c53c58f commit d98c04853dd39a452b0b98fb269a2013396cbb42
@@ -2352,14 +2352,27 @@ public InputStream getInputStream(final ClientIdentification.Agent agent) throws
return null;
}
/**
* Read fully the source, close it and return its content as a bytes array.
* @param source the source to read
* @return return the content of the source stream
* @throws IOException when an erro occured
*/
public static byte[] read(final InputStream source) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] buffer = new byte[2048];
int c;
while ((c = source.read(buffer, 0, 2048)) > 0) baos.write(buffer, 0, c);
baos.flush();
baos.close();
return baos.toByteArray();
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] buffer = new byte[2048];
int c;
while ((c = source.read(buffer, 0, 2048)) > 0) baos.write(buffer, 0, c);
baos.flush();
baos.close();
return baos.toByteArray();
} finally {
try {
source.close();
} catch(IOException ignored) {
}
}
}
public Locale getLocale() {
@@ -74,9 +74,10 @@ public OpenGeoDBLocation(final File file, WordCache dymLib) {
if ( file == null || !file.exists() ) {
return;
}
InputStream is = null;
BufferedReader reader = null;
try {
InputStream is = new FileInputStream(file);
is = new FileInputStream(file);
if ( file.getName().endsWith(".gz") ) {
is = new GZIPInputStream(is);
}
@@ -162,14 +163,18 @@ public OpenGeoDBLocation(final File file, WordCache dymLib) {
}
continue;
}
reader.close();
} catch (final IOException e ) {
ConcurrentLog.logException(e);
} finally {
if ( reader != null ) {
try {
reader.close();
} catch (final Exception e ) {
} catch (final Exception ignored ) {
}
} else if(is != null) {
try {
is.close();
} catch (final IOException ignored ) {
}
}
}
@@ -247,15 +247,21 @@ public cl() {
final File f = new File(System.getProperty("user.dir"), classname + ".class");
final int length = (int) f.length();
final byte[] classbytes = new byte[length];
DataInputStream in = null;
try {
final DataInputStream in = new DataInputStream(new FileInputStream(f));
in = new DataInputStream(new FileInputStream(f));
in.readFully(classbytes);
in.close();
c = defineClass(classname, classbytes, 0, classbytes.length);
} catch (final FileNotFoundException ee) {
throw new ClassNotFoundException();
} catch (final IOException ee) {
throw new ClassNotFoundException();
} finally {
try {
in.close();
} catch (IOException ioe) {
log.warn("Could not close input stream on file " + f);
}
}
}
}
@@ -139,13 +139,25 @@ public static void copy(final File from, final File to) throws IOException {
if (to.exists()) to.delete();
final byte[] buffer = new byte[4096];
int bytesRead;
final InputStream in = new BufferedInputStream(new FileInputStream(from));
final OutputStream out = new BufferedOutputStream(new FileOutputStream (to));
while ((bytesRead = in.read(buffer)) >= 0) {
out.write(buffer,0,bytesRead);
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(from));
out = new BufferedOutputStream(new FileOutputStream (to));
while ((bytesRead = in.read(buffer)) >= 0) {
out.write(buffer,0,bytesRead);
}
} finally {
if(in != null) {
try {
in.close();
} catch(IOException ignored) {
}
}
if(out != null) {
out.close();
}
}
in.close();
out.close();
}
}
}
@@ -43,6 +43,7 @@
import net.yacy.cora.date.GenericFormatter;
import net.yacy.cora.document.encoding.ASCII;
import net.yacy.cora.document.id.DigestURL;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.search.index.Fulltext;
import net.yacy.search.schema.CollectionSchema;
@@ -148,17 +149,27 @@ public Revisions(final String hostport, final int depth, final String datehash)
this.pathtoxml[0] = new File(pathToShard(hostport, urlhash, depth), this.urlhash + "." + datestring + ".xml");
String u = null;
if (this.pathtoxml[0].exists()) {
BufferedReader reader = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.pathtoxml[0])));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.pathtoxml[0])));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("<str name=\"sku\">")) {
u = line.substring(16, line.length() - 6);
break;
}
}
reader.close();
} catch (IOException e) {}
} catch (IOException e) {
ConcurrentLog.warn("SNAPSHOTS", "Error while reading file " + this.pathtoxml[0]);
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException ignored) {
ConcurrentLog.warn("SNAPSHOTS", "Could not close input stream on file " + this.pathtoxml[0]);
}
}
}
}
this.url = u;
}
@@ -180,7 +180,6 @@ public boolean translateFile(final File sourceFile, final File destFile, final M
while( (line = br.readLine()) != null){
content.append(line).append(net.yacy.server.serverCore.CRLF_STRING);
}
br.close();
} catch(final IOException e) {
return false;
} finally {
@@ -282,10 +282,12 @@ public double languageProbability() {
public static void main(final String[] args) {
// read a property file and convert them into configuration lines
FileInputStream inStream = null;
try {
final File f = new File(args[0]);
final Properties p = new Properties();
p.load(new FileInputStream(f));
inStream = new FileInputStream(f);
p.load(inStream);
final StringBuilder sb = new StringBuilder();
sb.append("{\n");
for (int i = 0; i <= 15; i++) {
@@ -303,6 +305,14 @@ public static void main(final String[] args) {
ConcurrentLog.logException(e);
} catch (final IOException e) {
ConcurrentLog.logException(e);
} finally {
if(inStream != null) {
try {
inStream.close();
} catch (IOException e) {
ConcurrentLog.logException(e);
}
}
}
}
@@ -767,7 +767,19 @@ public void addSubDocuments(final Document[] docs) throws IOException {
if (!(this.text instanceof ByteArrayOutputStream)) {
this.text = new ByteArrayOutputStream();
}
FileUtils.copy(doc.getTextStream(), (ByteArrayOutputStream) this.text);
InputStream textStream = doc.getTextStream();
try {
FileUtils.copy(textStream, (ByteArrayOutputStream) this.text);
} finally {
try {
if(textStream != null) {
/* textStream can be a FileInputStream : we must close it to ensure releasing system resource */
textStream.close();
}
} catch(IOException e) {
ConcurrentLog.warn("DOCUMENT", "Could not close text input stream");
}
}
this.anchors.addAll(doc.getAnchors());
this.rss.putAll(doc.getRSS());
@@ -958,10 +970,20 @@ public static Document mergeDocuments(final DigestURL location, final String glo
if (doc.getTextLength() > 0) {
if (docTextLength > 0) content.write('\n');
InputStream textStream = doc.getTextStream();
try {
docTextLength += FileUtils.copy(doc.getTextStream(), content);
docTextLength += FileUtils.copy(textStream, content);
} catch (final IOException e) {
ConcurrentLog.logException(e);
} finally {
try {
if(textStream != null) {
/* textStream can be a FileInputStream : we must close it to ensure releasing system resource */
textStream.close();
}
} catch (IOException e) {
ConcurrentLog.warn("DOCUMENT", "Could not close text input stream");
}
}
}
anchors.addAll(doc.getAnchors());
@@ -361,8 +361,10 @@ public static void main(final String[] args) {
// parse
final AbstractParser parser = new pdfParser();
Document document = null;
FileInputStream inStream = null;
try {
document = Document.mergeDocuments(null, "application/pdf", parser.parse(null, "application/pdf", null, new VocabularyScraper(), 0, new FileInputStream(pdfFile)));
inStream = new FileInputStream(pdfFile);
document = Document.mergeDocuments(null, "application/pdf", parser.parse(null, "application/pdf", null, new VocabularyScraper(), 0, inStream));
} catch (final Parser.Failure e) {
System.err.println("Cannot parse file " + pdfFile.getAbsolutePath());
ConcurrentLog.logException(e);
@@ -373,6 +375,14 @@ public static void main(final String[] args) {
System.err.println("class not found: " + e.getMessage());
} catch (final FileNotFoundException e) {
ConcurrentLog.logException(e);
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException e) {
System.err.println("Could not close input stream on file " + pdfFile);
}
}
}
// statistics
@@ -383,12 +393,22 @@ public static void main(final String[] args) {
System.out.println("\t!!!Parsing without result!!!");
} else {
System.out.println("\tParsed text with " + document.getTextLength() + " chars of text and " + document.getAnchors().size() + " anchors");
InputStream textStream = document.getTextStream();
try {
// write file
FileUtils.copy(document.getTextStream(), new File("parsedPdf.txt"));
FileUtils.copy(textStream, new File("parsedPdf.txt"));
} catch (final IOException e) {
System.err.println("error saving parsed document");
ConcurrentLog.logException(e);
} finally {
try {
if(textStream != null) {
/* textStream can be a FileInputStream : we must close it to ensure releasing system resource */
textStream.close();
}
} catch (IOException e) {
ConcurrentLog.warn("PDFPARSER", "Could not close text input stream");
}
}
}
} else {
@@ -442,8 +442,15 @@ private SSLContext initSslContext(Switchboard sb) {
// loading keystore data from file
if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Loading keystore file " + keyStoreFileName);
final FileInputStream stream = new FileInputStream(keyStoreFileName);
ks.load(stream, keyStorePwd.toCharArray());
stream.close();
try {
ks.load(stream, keyStorePwd.toCharArray());
} finally {
try {
stream.close();
} catch(IOException ioe) {
ConcurrentLog.warn("SERVER", "Could not close input stream on file " + keyStoreFileName);
}
}
// creating a keystore factory
if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Initializing key manager factory ...");
@@ -104,25 +104,32 @@ public PublicKey getPublicKeyFromBytes(byte[] keyBuffer) throws InvalidKeySpecEx
}
public byte[] getSignature(PrivateKey privKey, InputStream dataStream) throws InvalidKeyException, SignatureException, IOException {
this.sign.initSign(privKey);
byte[] buffer = new byte[1024];
int count = 0;
while((count = dataStream.read(buffer)) != -1) {
this.sign.update(buffer, 0, count);
}
dataStream.close();
return this.sign.sign();
try {
this.sign.initSign(privKey);
byte[] buffer = new byte[1024];
int count = 0;
while((count = dataStream.read(buffer)) != -1) {
this.sign.update(buffer, 0, count);
}
} finally {
dataStream.close();
}
return this.sign.sign();
}
public boolean verifySignature(PublicKey pubKey, InputStream dataStream, byte[] signBuffer) throws InvalidKeyException, SignatureException, IOException {
this.sign.initVerify(pubKey);
byte[] buffer = new byte[1024];
int count = 0;
while((count = dataStream.read(buffer)) != -1) {
this.sign.update(buffer, 0, count);
}
dataStream.close();
try {
this.sign.initVerify(pubKey);
byte[] buffer = new byte[1024];
int count = 0;
while((count = dataStream.read(buffer)) != -1) {
this.sign.update(buffer, 0, count);
}
} finally {
dataStream.close();
}
return this.sign.verify(signBuffer);
}
Oops, something went wrong.

0 comments on commit d98c048

Please sign in to comment.