Ensure proper closing of file input streams.

pull/122/head
luccioman 8 years ago
parent c53c58fa85
commit d98c04853d

@ -2352,14 +2352,27 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
return null; 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 { public static byte[] read(final InputStream source) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {
final byte[] buffer = new byte[2048]; final ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c; final byte[] buffer = new byte[2048];
while ((c = source.read(buffer, 0, 2048)) > 0) baos.write(buffer, 0, c); int c;
baos.flush(); while ((c = source.read(buffer, 0, 2048)) > 0) baos.write(buffer, 0, c);
baos.close(); baos.flush();
return baos.toByteArray(); baos.close();
return baos.toByteArray();
} finally {
try {
source.close();
} catch(IOException ignored) {
}
}
} }
public Locale getLocale() { public Locale getLocale() {

@ -74,9 +74,10 @@ public class OpenGeoDBLocation implements Locations
if ( file == null || !file.exists() ) { if ( file == null || !file.exists() ) {
return; return;
} }
InputStream is = null;
BufferedReader reader = null; BufferedReader reader = null;
try { try {
InputStream is = new FileInputStream(file); is = new FileInputStream(file);
if ( file.getName().endsWith(".gz") ) { if ( file.getName().endsWith(".gz") ) {
is = new GZIPInputStream(is); is = new GZIPInputStream(is);
} }
@ -162,14 +163,18 @@ public class OpenGeoDBLocation implements Locations
} }
continue; continue;
} }
reader.close();
} catch (final IOException e ) { } catch (final IOException e ) {
ConcurrentLog.logException(e); ConcurrentLog.logException(e);
} finally { } finally {
if ( reader != null ) { if ( reader != null ) {
try { try {
reader.close(); 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 class FTPClient {
final File f = new File(System.getProperty("user.dir"), classname + ".class"); final File f = new File(System.getProperty("user.dir"), classname + ".class");
final int length = (int) f.length(); final int length = (int) f.length();
final byte[] classbytes = new byte[length]; final byte[] classbytes = new byte[length];
DataInputStream in = null;
try { try {
final DataInputStream in = new DataInputStream(new FileInputStream(f)); in = new DataInputStream(new FileInputStream(f));
in.readFully(classbytes); in.readFully(classbytes);
in.close();
c = defineClass(classname, classbytes, 0, classbytes.length); c = defineClass(classname, classbytes, 0, classbytes.length);
} catch (final FileNotFoundException ee) { } catch (final FileNotFoundException ee) {
throw new ClassNotFoundException(); throw new ClassNotFoundException();
} catch (final IOException ee) { } catch (final IOException ee) {
throw new ClassNotFoundException(); 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 class Files {
if (to.exists()) to.delete(); if (to.exists()) to.delete();
final byte[] buffer = new byte[4096]; final byte[] buffer = new byte[4096];
int bytesRead; int bytesRead;
final InputStream in = new BufferedInputStream(new FileInputStream(from)); InputStream in = null;
final OutputStream out = new BufferedOutputStream(new FileOutputStream (to)); OutputStream out = null;
while ((bytesRead = in.read(buffer)) >= 0) { try {
out.write(buffer,0,bytesRead); 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 org.apache.solr.common.SolrDocument;
import net.yacy.cora.date.GenericFormatter; import net.yacy.cora.date.GenericFormatter;
import net.yacy.cora.document.encoding.ASCII; import net.yacy.cora.document.encoding.ASCII;
import net.yacy.cora.document.id.DigestURL; import net.yacy.cora.document.id.DigestURL;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.search.index.Fulltext; import net.yacy.search.index.Fulltext;
import net.yacy.search.schema.CollectionSchema; import net.yacy.search.schema.CollectionSchema;
@ -148,8 +149,9 @@ public class Snapshots {
this.pathtoxml[0] = new File(pathToShard(hostport, urlhash, depth), this.urlhash + "." + datestring + ".xml"); this.pathtoxml[0] = new File(pathToShard(hostport, urlhash, depth), this.urlhash + "." + datestring + ".xml");
String u = null; String u = null;
if (this.pathtoxml[0].exists()) { if (this.pathtoxml[0].exists()) {
BufferedReader reader = null;
try { try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.pathtoxml[0]))); reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.pathtoxml[0])));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.startsWith("<str name=\"sku\">")) { if (line.startsWith("<str name=\"sku\">")) {
@ -157,8 +159,17 @@ public class Snapshots {
break; 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; this.url = u;
} }

@ -180,7 +180,6 @@ public class Translator {
while( (line = br.readLine()) != null){ while( (line = br.readLine()) != null){
content.append(line).append(net.yacy.server.serverCore.CRLF_STRING); content.append(line).append(net.yacy.server.serverCore.CRLF_STRING);
} }
br.close();
} catch(final IOException e) { } catch(final IOException e) {
return false; return false;
} finally { } finally {

@ -282,10 +282,12 @@ public final class Condenser extends Tokenizer {
public static void main(final String[] args) { public static void main(final String[] args) {
// read a property file and convert them into configuration lines // read a property file and convert them into configuration lines
FileInputStream inStream = null;
try { try {
final File f = new File(args[0]); final File f = new File(args[0]);
final Properties p = new Properties(); final Properties p = new Properties();
p.load(new FileInputStream(f)); inStream = new FileInputStream(f);
p.load(inStream);
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append("{\n"); sb.append("{\n");
for (int i = 0; i <= 15; i++) { for (int i = 0; i <= 15; i++) {
@ -303,6 +305,14 @@ public final class Condenser extends Tokenizer {
ConcurrentLog.logException(e); ConcurrentLog.logException(e);
} catch (final IOException e) { } catch (final IOException e) {
ConcurrentLog.logException(e); ConcurrentLog.logException(e);
} finally {
if(inStream != null) {
try {
inStream.close();
} catch (IOException e) {
ConcurrentLog.logException(e);
}
}
} }
} }

@ -767,7 +767,19 @@ dc_rights
if (!(this.text instanceof ByteArrayOutputStream)) { if (!(this.text instanceof ByteArrayOutputStream)) {
this.text = new 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.anchors.addAll(doc.getAnchors());
this.rss.putAll(doc.getRSS()); this.rss.putAll(doc.getRSS());
@ -958,10 +970,20 @@ dc_rights
if (doc.getTextLength() > 0) { if (doc.getTextLength() > 0) {
if (docTextLength > 0) content.write('\n'); if (docTextLength > 0) content.write('\n');
InputStream textStream = doc.getTextStream();
try { try {
docTextLength += FileUtils.copy(doc.getTextStream(), content); docTextLength += FileUtils.copy(textStream, content);
} catch (final IOException e) { } catch (final IOException e) {
ConcurrentLog.logException(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()); anchors.addAll(doc.getAnchors());

@ -361,8 +361,10 @@ public class pdfParser extends AbstractParser implements Parser {
// parse // parse
final AbstractParser parser = new pdfParser(); final AbstractParser parser = new pdfParser();
Document document = null; Document document = null;
FileInputStream inStream = null;
try { 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) { } catch (final Parser.Failure e) {
System.err.println("Cannot parse file " + pdfFile.getAbsolutePath()); System.err.println("Cannot parse file " + pdfFile.getAbsolutePath());
ConcurrentLog.logException(e); ConcurrentLog.logException(e);
@ -373,6 +375,14 @@ public class pdfParser extends AbstractParser implements Parser {
System.err.println("class not found: " + e.getMessage()); System.err.println("class not found: " + e.getMessage());
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
ConcurrentLog.logException(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 // statistics
@ -383,12 +393,22 @@ public class pdfParser extends AbstractParser implements Parser {
System.out.println("\t!!!Parsing without result!!!"); System.out.println("\t!!!Parsing without result!!!");
} else { } else {
System.out.println("\tParsed text with " + document.getTextLength() + " chars of text and " + document.getAnchors().size() + " anchors"); System.out.println("\tParsed text with " + document.getTextLength() + " chars of text and " + document.getAnchors().size() + " anchors");
InputStream textStream = document.getTextStream();
try { try {
// write file // write file
FileUtils.copy(document.getTextStream(), new File("parsedPdf.txt")); FileUtils.copy(textStream, new File("parsedPdf.txt"));
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("error saving parsed document"); System.err.println("error saving parsed document");
ConcurrentLog.logException(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("PDFPARSER", "Could not close text input stream");
}
} }
} }
} else { } else {

@ -442,8 +442,15 @@ public class Jetty9HttpServerImpl implements YaCyHttpServer {
// loading keystore data from file // loading keystore data from file
if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Loading keystore file " + keyStoreFileName); if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Loading keystore file " + keyStoreFileName);
final FileInputStream stream = new FileInputStream(keyStoreFileName); final FileInputStream stream = new FileInputStream(keyStoreFileName);
ks.load(stream, keyStorePwd.toCharArray()); try {
stream.close(); 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 // creating a keystore factory
if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Initializing key manager factory ..."); if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Initializing key manager factory ...");

@ -104,25 +104,32 @@ public class CryptoLib {
} }
public byte[] getSignature(PrivateKey privKey, InputStream dataStream) throws InvalidKeyException, SignatureException, IOException { public byte[] getSignature(PrivateKey privKey, InputStream dataStream) throws InvalidKeyException, SignatureException, IOException {
this.sign.initSign(privKey); try {
byte[] buffer = new byte[1024]; this.sign.initSign(privKey);
int count = 0; byte[] buffer = new byte[1024];
while((count = dataStream.read(buffer)) != -1) { int count = 0;
this.sign.update(buffer, 0, count); while((count = dataStream.read(buffer)) != -1) {
} this.sign.update(buffer, 0, count);
dataStream.close(); }
return this.sign.sign(); } finally {
dataStream.close();
}
return this.sign.sign();
} }
public boolean verifySignature(PublicKey pubKey, InputStream dataStream, byte[] signBuffer) throws InvalidKeyException, SignatureException, IOException { public boolean verifySignature(PublicKey pubKey, InputStream dataStream, byte[] signBuffer) throws InvalidKeyException, SignatureException, IOException {
this.sign.initVerify(pubKey); try {
this.sign.initVerify(pubKey);
byte[] buffer = new byte[1024];
int count = 0; byte[] buffer = new byte[1024];
while((count = dataStream.read(buffer)) != -1) { int count = 0;
this.sign.update(buffer, 0, count); while((count = dataStream.read(buffer)) != -1) {
} this.sign.update(buffer, 0, count);
dataStream.close(); }
} finally {
dataStream.close();
}
return this.sign.verify(signBuffer); return this.sign.verify(signBuffer);
} }

@ -53,12 +53,21 @@ public class PKCS12Tool {
// creating PKCS12 keystore // creating PKCS12 keystore
this.kspkcs12 = KeyStore.getInstance("PKCS12"); this.kspkcs12 = KeyStore.getInstance("PKCS12");
// load pkcs12 file into keystore object FileInputStream fileIn = null;
final FileInputStream fileIn = new FileInputStream(pkcs12FileName); try {
this.kspkcs12.load(fileIn,(pkcs12Pwd!=null)?pkcs12Pwd.toCharArray():null); // load pkcs12 file into keystore object
fileIn = new FileInputStream(pkcs12FileName);
// close stream this.kspkcs12.load(fileIn,(pkcs12Pwd!=null)?pkcs12Pwd.toCharArray():null);
fileIn.close(); } finally {
if(fileIn != null) {
try {
// close stream
fileIn.close();
} catch(IOException ioe) {
System.err.println("Could not close file " + pkcs12FileName);
}
}
}
} }
public Enumeration<String> aliases() throws KeyStoreException { public Enumeration<String> aliases() throws KeyStoreException {
@ -108,9 +117,19 @@ public class PKCS12Tool {
// storing jdk into file // storing jdk into file
System.err.print("Storing java keystore"); System.err.print("Storing java keystore");
final FileOutputStream jksFileOut = new FileOutputStream(jksName); FileOutputStream jksFileOut = null;
jks.store(jksFileOut,(jksPassword!=null)?jksPassword.toCharArray():null); try {
jksFileOut.close(); jksFileOut = new FileOutputStream(jksName);
jks.store(jksFileOut,(jksPassword!=null)?jksPassword.toCharArray():null);
} finally {
if(jksFileOut != null) {
try {
jksFileOut.close();
} catch(IOException ioe) {
System.err.println("Could not close file " + jksFileOut);
}
}
}
System.err.print("Import finished."); System.err.print("Import finished.");
} }

@ -327,10 +327,6 @@ public class cryptbig {
// read and decrypt the file // read and decrypt the file
copy(fout, fin, 512); copy(fout, fin, 512);
// close the files
fin.close();
fout.close();
// do postprocessing // do postprocessing
} catch (final BadPaddingException e) { } catch (final BadPaddingException e) {
System.err.println("ERROR: decryption of '" + inFileName + "' not possible: " + e.getMessage()); System.err.println("ERROR: decryption of '" + inFileName + "' not possible: " + e.getMessage());
@ -340,6 +336,7 @@ public class cryptbig {
System.err.println("ERROR: file '" + inFileName + "' not found"); System.err.println("ERROR: file '" + inFileName + "' not found");
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("ERROR: IO trouble"); System.err.println("ERROR: IO trouble");
} finally {
try { if(fin != null) { try { if(fin != null) {
fin.close(); fin.close();
}} catch (final Exception ee) {} }} catch (final Exception ee) {}

@ -48,17 +48,33 @@ public class gzip {
private final static ConcurrentLog logger = new ConcurrentLog("GZIP"); private final static ConcurrentLog logger = new ConcurrentLog("GZIP");
public static void gzipFile(final String inFile, final String outFile) { public static void gzipFile(final String inFile, final String outFile) {
try { InputStream fin = null;
final InputStream fin = new BufferedInputStream(new FileInputStream(inFile)); OutputStream fout = null;
final OutputStream fout = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)), 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}}; try {
copy(fout, fin, 1024); fin = new BufferedInputStream(new FileInputStream(inFile));
fin.close(); fout = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)), 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}};
fout.close(); copy(fout, fin, 1024);
} catch (final FileNotFoundException e) {
logger.warn("ERROR: file '" + inFile + "' not found", e); } catch (final FileNotFoundException e) {
} catch (final IOException e) { logger.warn("ERROR: file '" + inFile + "' not found", e);
} catch (final IOException e) {
logger.warn("ERROR: IO trouble ",e); logger.warn("ERROR: IO trouble ",e);
} } finally {
if(fin != null) {
try {
fin.close();
} catch(IOException e) {
logger.warn("ERROR: Could not close file " + inFile);
}
}
if(fout != null) {
try {
fout.close();
} catch(IOException e) {
logger.warn("ERROR: Could not close file " + outFile);
}
}
}
} }
public static File gunzipFile(final File in) { public static File gunzipFile(final File in) {

@ -345,12 +345,18 @@ public final class yacy {
for (String tmplang : langlist) { for (String tmplang : langlist) {
if (!tmplang.equals("") && !tmplang.equals("default") && !tmplang.equals("browser")) { //locale is used if (!tmplang.equals("") && !tmplang.equals("default") && !tmplang.equals("browser")) { //locale is used
String currentRev = null; String currentRev = null;
BufferedReader br = null;
try { try {
final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(sb.getDataPath("locale.translated_html", "DATA/LOCALE/htroot"), tmplang + "/version")))); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(sb.getDataPath("locale.translated_html", "DATA/LOCALE/htroot"), tmplang + "/version"))));
currentRev = br.readLine(); // may return null currentRev = br.readLine(); // may return null
br.close();
} catch (final IOException e) { } catch (final IOException e) {
//Error //Error
} finally {
try {
br.close();
} catch(IOException ioe) {
ConcurrentLog.warn("STARTUP", "Could not close " + tmplang + " version file");
}
} }
if (currentRev == null || !currentRev.equals(sb.getConfig(Seed.VERSION, ""))) { if (currentRev == null || !currentRev.equals(sb.getConfig(Seed.VERSION, ""))) {
@ -696,10 +702,10 @@ public final class yacy {
if (configFile.exists()) { if (configFile.exists()) {
Properties p = new Properties(); Properties p = new Properties();
FileInputStream fis = null;
try { try {
FileInputStream fis = new FileInputStream(configFile); fis = new FileInputStream(configFile);
p.load(fis); p.load(fis);
fis.close();
// Test for server access restriction (is implemented using Jetty IPaccessHandler which does not support IPv6 // Test for server access restriction (is implemented using Jetty IPaccessHandler which does not support IPv6
// try to disable IPv6 // try to disable IPv6
String teststr = p.getProperty("serverClient", "*"); String teststr = p.getProperty("serverClient", "*");
@ -736,6 +742,12 @@ public final class yacy {
System.err.println(configFile.getAbsolutePath()); System.err.println(configFile.getAbsolutePath());
ConcurrentLog.logException(ex); ConcurrentLog.logException(ex);
ConcurrentLog.severe("Startup", "cannot read " + configFile.toString() + ", please delete the corrupted file if problem persits"); ConcurrentLog.severe("Startup", "cannot read " + configFile.toString() + ", please delete the corrupted file if problem persits");
} finally {
try {
fis.close();
} catch (IOException e) {
ConcurrentLog.warn("Startup", "Could not close file " + configFile);
}
} }
} }
} }

@ -29,28 +29,51 @@ public class ParserTest {
}; };
for (final String[] testFile : testFiles) { for (final String[] testFile : testFiles) {
FileInputStream inStream = null;
final String filename = "test/parsertest/" + testFile[0];
try { try {
final String filename = "test/parsertest/" + testFile[0];
final File file = new File(filename); final File file = new File(filename);
final String mimetype = testFile[1]; final String mimetype = testFile[1];
final AnchorURL url = new AnchorURL("http://localhost/"+filename); final AnchorURL url = new AnchorURL("http://localhost/"+filename);
AbstractParser p = new ooxmlParser(); AbstractParser p = new ooxmlParser();
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); inStream = new FileInputStream(file);
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
for (final Document doc: docs) { for (final Document doc: docs) {
final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); Reader content = null;
final StringBuilder str = new StringBuilder(); try {
int c; content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
while( (c = content.read()) != -1 ) final StringBuilder str = new StringBuilder();
str.append((char)c); int c;
while( (c = content.read()) != -1 )
str.append((char)c);
System.out.println("Parsed " + filename + ": " + str); System.out.println("Parsed " + filename + ": " + str);
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
assertThat(doc.dc_title(), containsString(testFile[2])); assertThat(doc.dc_title(), containsString(testFile[2]));
assertThat(doc.dc_creator(), containsString(testFile[3])); assertThat(doc.dc_creator(), containsString(testFile[3]));
if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4]));
} finally {
if(content != null) {
try {
content.close();
} catch(IOException ioe) {
System.out.println("Could not close text input stream");
}
}
}
} }
} catch (final InterruptedException ex) {} } catch (final InterruptedException ex) {
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException ioe) {
System.out.println("Could not close input stream on file " + filename);
}
}
}
} }
} }
@ -63,28 +86,50 @@ public class ParserTest {
}; };
for (final String[] testFile : testFiles) { for (final String[] testFile : testFiles) {
FileInputStream inStream = null;
final String filename = "test/parsertest/" + testFile[0];
try { try {
final String filename = "test/parsertest/" + testFile[0];
final File file = new File(filename); final File file = new File(filename);
final String mimetype = testFile[1]; final String mimetype = testFile[1];
final AnchorURL url = new AnchorURL("http://localhost/"+filename); final AnchorURL url = new AnchorURL("http://localhost/"+filename);
AbstractParser p = new odtParser(); AbstractParser p = new odtParser();
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); inStream = new FileInputStream(file);
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
for (final Document doc: docs) { for (final Document doc: docs) {
final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); Reader content = null;
final StringBuilder str = new StringBuilder(); try {
int c; content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
while( (c = content.read()) != -1 ) final StringBuilder str = new StringBuilder();
int c;
while( (c = content.read()) != -1 )
str.append((char)c); str.append((char)c);
System.out.println("Parsed " + filename + ": " + str); System.out.println("Parsed " + filename + ": " + str);
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
assertThat(doc.dc_title(), containsString(testFile[2])); assertThat(doc.dc_title(), containsString(testFile[2]));
assertThat(doc.dc_creator(), containsString(testFile[3])); assertThat(doc.dc_creator(), containsString(testFile[3]));
if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4]));
} finally {
if(content != null) {
try {
content.close();
} catch(IOException ioe) {
System.out.println("Could not close text input stream");
}
}
}
} }
} catch (final InterruptedException ex) {} } catch (final InterruptedException ex) {
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException ioe) {
System.out.println("Could not close input stream on file " + filename);
}
}
}
} }
} }
@ -95,28 +140,50 @@ public class ParserTest {
}; };
for (final String[] testFile : testFiles) { for (final String[] testFile : testFiles) {
final String filename = "test/parsertest/" + testFile[0];
FileInputStream inStream = null;
try { try {
final String filename = "test/parsertest/" + testFile[0];
final File file = new File(filename); final File file = new File(filename);
final String mimetype = testFile[1]; final String mimetype = testFile[1];
final AnchorURL url = new AnchorURL("http://localhost/"+filename); final AnchorURL url = new AnchorURL("http://localhost/"+filename);
AbstractParser p = new pdfParser(); AbstractParser p = new pdfParser();
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); inStream = new FileInputStream(file);
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
for (final Document doc: docs) { for (final Document doc: docs) {
final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); Reader content = null;
final StringBuilder str = new StringBuilder(); try {
int c; content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
while( (c = content.read()) != -1 ) final StringBuilder str = new StringBuilder();
int c;
while( (c = content.read()) != -1 )
str.append((char)c); str.append((char)c);
System.out.println("Parsed " + filename + ": " + str); System.out.println("Parsed " + filename + ": " + str);
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
assertThat(doc.dc_title(), containsString(testFile[2])); assertThat(doc.dc_title(), containsString(testFile[2]));
assertThat(doc.dc_creator(), containsString(testFile[3])); assertThat(doc.dc_creator(), containsString(testFile[3]));
if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4]));
} finally {
if(content != null) {
try {
content.close();
} catch(IOException ioe) {
System.out.println("Could not close text input stream");
}
}
}
} }
} catch (final InterruptedException ex) {} } catch (final InterruptedException ex) {
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException ioe) {
System.out.println("Could not close input stream on file " + filename);
}
}
}
} }
} }
@ -127,30 +194,52 @@ public class ParserTest {
}; };
for (final String[] testFile : testFiles) { for (final String[] testFile : testFiles) {
final String filename = "test/parsertest/" + testFile[0];
FileInputStream inStream = null;
try { try {
final String filename = "test/parsertest/" + testFile[0];
final File file = new File(filename); final File file = new File(filename);
final String mimetype = testFile[1]; final String mimetype = testFile[1];
final AnchorURL url = new AnchorURL("http://localhost/"+filename); final AnchorURL url = new AnchorURL("http://localhost/"+filename);
AbstractParser p = new docParser(); AbstractParser p = new docParser();
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); inStream = new FileInputStream(file);
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
for (final Document doc: docs) { for (final Document doc: docs) {
final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); Reader content = null;
final StringBuilder str = new StringBuilder(); try {
int c; content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
while( (c = content.read()) != -1 ) final StringBuilder str = new StringBuilder();
int c;
while( (c = content.read()) != -1 )
str.append((char)c); str.append((char)c);
System.out.println("Parsed " + filename + ": " + str); System.out.println("Parsed " + filename + ": " + str);
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
assertThat(doc.dc_title(), containsString(testFile[2])); assertThat(doc.dc_title(), containsString(testFile[2]));
assertThat(doc.dc_creator(), containsString(testFile[3])); assertThat(doc.dc_creator(), containsString(testFile[3]));
if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4]));
} finally {
if(content != null) {
try {
content.close();
} catch(IOException ioe) {
System.out.println("Could not close text input stream");
}
}
}
} }
} catch (final InterruptedException ex) {} } catch (final InterruptedException ex) {
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException ioe) {
System.out.println("Could not close input stream on file " + filename);
}
}
} }
} }
}
/** /**
* Powerpoint parser test * * Powerpoint parser test *
@ -172,22 +261,46 @@ public class ParserTest {
final AnchorURL url = new AnchorURL("http://localhost/" + filename); final AnchorURL url = new AnchorURL("http://localhost/" + filename);
AbstractParser p = new pptParser(); AbstractParser p = new pptParser();
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); FileInputStream inStream = null;
for (final Document doc : docs) { try {
final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); inStream = new FileInputStream(file);
final StringBuilder str = new StringBuilder(); final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
int c; for (final Document doc : docs) {
while ((c = content.read()) != -1) { Reader content = null;
str.append((char) c); try {
} content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
final StringBuilder str = new StringBuilder();
System.out.println("Parsed " + filename + ": " + str); int c;
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); while ((c = content.read()) != -1) {
assertThat(doc.dc_title(), containsString(testFile[2])); str.append((char) c);
assertThat(doc.dc_creator(), containsString(testFile[3])); }
if (testFile[4].length() > 0) {
assertThat(doc.dc_description()[0], containsString(testFile[4])); System.out.println("Parsed " + filename + ": " + str);
} assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
assertThat(doc.dc_title(), containsString(testFile[2]));
assertThat(doc.dc_creator(), containsString(testFile[3]));
if (testFile[4].length() > 0) {
assertThat(doc.dc_description()[0], containsString(testFile[4]));
}
} finally {
if(content != null) {
try {
content.close();
} catch(IOException ioe) {
System.out.println("Could not close text input stream");
}
}
}
}
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException ioe) {
System.out.println("Could not close input stream on file " + filename);
}
}
} }
} }
} }

@ -2,6 +2,7 @@ package net.yacy.document.parser;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import net.yacy.cora.document.id.AnchorURL; import net.yacy.cora.document.id.AnchorURL;
@ -35,21 +36,44 @@ public class xlsParserTest {
final AnchorURL url = new AnchorURL("http://localhost/" + filename); final AnchorURL url = new AnchorURL("http://localhost/" + filename);
AbstractParser p = new xlsParser(); AbstractParser p = new xlsParser();
final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); FileInputStream inStream = null;
for (final Document doc : docs) { try {
final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); inStream = new FileInputStream(file);
final StringBuilder str = new StringBuilder(); final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
int c; for (final Document doc : docs) {
while ((c = content.read()) != -1) { Reader content = null;
str.append((char) c); try {
} content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
final StringBuilder str = new StringBuilder();
System.out.println("Parsed " + filename + ": " + str); int c;
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); while ((c = content.read()) != -1) {
assertThat(doc.dc_creator(), containsString(testFile[3])); str.append((char) c);
if (testFile[4].length() > 0) { }
assertThat(doc.dc_description()[0], containsString(testFile[4]));
System.out.println("Parsed " + filename + ": " + str);
assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
assertThat(doc.dc_creator(), containsString(testFile[3]));
if (testFile[4].length() > 0) {
assertThat(doc.dc_description()[0], containsString(testFile[4]));
}
} finally {
if(content != null) {
try {
content.close();
} catch(IOException ioe) {
System.out.println("Could not close text input stream");
}
}
}
} }
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException ioe) {
System.out.println("Could not close input stream on file " + filename);
}
}
} }
} }
} }

Loading…
Cancel
Save