diff --git a/htroot/interaction_elements/Document_part.java b/htroot/interaction_elements/Document_part.java index 48425f2a5..ac6b2ac53 100644 --- a/htroot/interaction_elements/Document_part.java +++ b/htroot/interaction_elements/Document_part.java @@ -1,29 +1,19 @@ package interaction_elements; - -import java.util.Collection; - -import net.yacy.cora.lod.vocabulary.Tagging; -import net.yacy.cora.protocol.HeaderFramework; import net.yacy.cora.protocol.RequestHeader; -import net.yacy.document.LibraryProvider; -import net.yacy.search.Switchboard; -import de.anomic.data.UserDB; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; public class Document_part { - + public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) { - - final Switchboard sb = (Switchboard) env; - + final serverObjects prop = new serverObjects(); - + prop.put("hash", post.get("hash", "")); - prop.put("url", post.get("url", "")); + prop.put("url", post.get("url", "")); prop.put("action", post.get("action", "")); - + return prop; } } diff --git a/source/de/anomic/http/server/HTTPDFileHandler.java b/source/de/anomic/http/server/HTTPDFileHandler.java index 85c7a25d0..8d7a1e228 100644 --- a/source/de/anomic/http/server/HTTPDFileHandler.java +++ b/source/de/anomic/http/server/HTTPDFileHandler.java @@ -66,6 +66,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; @@ -946,7 +947,7 @@ public final class HTTPDFileHandler { templatePatterns = new servletProperties((serverObjects) tmp); } // check if the servlets requests authentication - if (templatePatterns.containsKey(servletProperties.ACTION_AUTHENTICATE)) { + if (templatePatterns.containsKey(serverObjects.ACTION_AUTHENTICATE)) { // handle brute-force protection if (realmProp != null) { Log.logInfo("HTTPD", "dynamic log-in for account 'admin' in http file handler for path '" + path + "' from host '" + clientIP + "'"); @@ -958,11 +959,11 @@ public final class HTTPDFileHandler { } // send authentication request to browser final ResponseHeader headers = getDefaultHeaders(path); - headers.put(RequestHeader.WWW_AUTHENTICATE,"Basic realm=\"" + templatePatterns.get(servletProperties.ACTION_AUTHENTICATE, "") + "\""); + headers.put(RequestHeader.WWW_AUTHENTICATE,"Basic realm=\"" + templatePatterns.get(serverObjects.ACTION_AUTHENTICATE, "") + "\""); HTTPDemon.sendRespondHeader(conProp,out,httpVersion,401,headers); return; - } else if (templatePatterns.containsKey(servletProperties.ACTION_LOCATION)) { - String location = templatePatterns.get(servletProperties.ACTION_LOCATION, ""); + } else if (templatePatterns.containsKey(serverObjects.ACTION_LOCATION)) { + String location = templatePatterns.get(serverObjects.ACTION_LOCATION, ""); if (location.length() == 0) location = path; final ResponseHeader headers = getDefaultHeaders(path); @@ -1283,20 +1284,24 @@ public final class HTTPDFileHandler { * CGI scripts. * @param targetFile file to run * @return list of parts of command + * @throws FileNotFoundException * @throws IOException if file can not be accessed */ - private static List assembleCommandFromShebang( - final File targetFile) - throws IOException { + private static List assembleCommandFromShebang(final File targetFile) throws FileNotFoundException { final List ret = new ArrayList(); - final BufferedReader br = - new BufferedReader(new FileReader(targetFile), 512); - final String line = br.readLine(); - if (line.startsWith("#!")) { - ret.addAll(Arrays.asList(line.substring(2).split(" "))); + final BufferedReader br = new BufferedReader(new FileReader(targetFile), 512); + String line; + try { + line = br.readLine(); + if (line.startsWith("#!")) { + ret.addAll(Arrays.asList(line.substring(2).split(" "))); + } + ret.add(targetFile.getAbsolutePath()); + } catch (IOException e) { + Log.logException(e); + } finally { + try {br.close();} catch (IOException e) {} } - ret.add(targetFile.getAbsolutePath()); - return ret; } @@ -1599,16 +1604,13 @@ public final class HTTPDFileHandler { if (outgoingHeader.containsKey(HeaderFramework.TRANSFER_ENCODING)) { HTTPDemon.sendRespondHeader(conProp, out, httpVersion, httpStatus, outgoingHeader); - final ChunkedOutputStream cos = new ChunkedOutputStream(out); - cos.write(sbb); cos.finish(); + cos.close(); } else { outgoingHeader.put(HeaderFramework.CONTENT_LENGTH, Integer.toString(sbb.length)); - HTTPDemon.sendRespondHeader(conProp, out, httpVersion, httpStatus, outgoingHeader); - out.write(sbb); } } else { diff --git a/source/de/anomic/http/server/HTTPDemon.java b/source/de/anomic/http/server/HTTPDemon.java index 6dc088eff..8f5102f9f 100644 --- a/source/de/anomic/http/server/HTTPDemon.java +++ b/source/de/anomic/http/server/HTTPDemon.java @@ -494,6 +494,8 @@ public final class HTTPDemon implements serverHandler, Cloneable { public Boolean POST(final String arg, final Session session) { //System.out.println("POST " + arg); + InputStream sessionIn = null; + boolean retv; try { final HashMap prop = parseRequestLine(HeaderFramework.METHOD_POST, arg, session); @@ -504,7 +506,6 @@ public final class HTTPDemon implements serverHandler, Cloneable { : readHeader(prop, session); // handle transfer-coding - final InputStream sessionIn; final String transferEncoding = header.get(HeaderFramework.TRANSFER_ENCODING); if (transferEncoding != null) { if (!HeaderFramework.HTTP_VERSION_1_1.equals(httpVersion)) { @@ -550,13 +551,17 @@ public final class HTTPDemon implements serverHandler, Cloneable { return serverCore.TERMINATE_CONNECTION; } } - if (sessionIn instanceof ChunkedInputStream) sessionIn.close(); // read to end, but do not close the stream (maybe HTTP/1.1 persistent) - //return serverCore.RESUME_CONNECTION; - return prop.get(HeaderFramework.CONNECTION_PROP_PERSISTENT).equals("keep-alive") ? serverCore.RESUME_CONNECTION : serverCore.TERMINATE_CONNECTION; + retv = prop.get(HeaderFramework.CONNECTION_PROP_PERSISTENT).equals("keep-alive") ? serverCore.RESUME_CONNECTION : serverCore.TERMINATE_CONNECTION; } catch (final Exception e) { logUnexpectedError(e, session.userAddress.getHostAddress()); - return serverCore.TERMINATE_CONNECTION; + retv = serverCore.TERMINATE_CONNECTION; + } finally { + if (sessionIn != null && (sessionIn instanceof ChunkedInputStream)) { + // read to end, but do not close the stream (maybe HTTP/1.1 persistent) + try {sessionIn.close();} catch (IOException e) {} + } } + return retv; } public static void handleTransparentProxySupport(final RequestHeader header, final HashMap prop, final String virtualHost, final boolean isTransparentProxy) { @@ -933,6 +938,7 @@ public final class HTTPDemon implements serverHandler, Cloneable { * @see org.apache.commons.fileupload.RequestContext#getInputStream() */ // @Override + @Override public InputStream getInputStream() throws IOException { return this.inStream; } diff --git a/source/de/anomic/http/server/TemplateEngine.java b/source/de/anomic/http/server/TemplateEngine.java index 0b31aca1c..7837f99dc 100644 --- a/source/de/anomic/http/server/TemplateEngine.java +++ b/source/de/anomic/http/server/TemplateEngine.java @@ -266,7 +266,7 @@ public final class TemplateEngine { // #( } else if ((bb & 0xFF) == lrbr) { //alternative int others=0; - final ByteBuffer text= new ByteBuffer(); + final ByteBuffer text = new ByteBuffer(); transferUntil(pis, keyStream, aClose); key = keyStream.toByteArray(); //Caution: Key does not contain prefix @@ -297,7 +297,10 @@ public final class TemplateEngine { transferUntil(pis, keyStream, appendBytes(PP, patternName, null, null)); if(pis.available()==0){ Log.logSevere("TEMPLATE", "No such Template: %%" + UTF8.String(patternName)); - return structure.getBytes(); + final byte[] sb = structure.getBytes(); + structure.close(); + text.close(); + return sb; } keyStream.reset(); transferUntil(pis, keyStream, dpdpa); @@ -364,7 +367,7 @@ public final class TemplateEngine { } }//while }//if(byName) (else branch) - + text.close(); // #[ } else if ((bb & 0xFF) == lbr) { //normal if (transferUntil(pis, keyStream, pClose)) { @@ -382,7 +385,9 @@ public final class TemplateEngine { } else { // inconsistency, simply finalize this FileUtils.copy(pis, out); - return structure.getBytes(); + final byte[] sb = structure.getBytes(); + structure.close(); + return sb; } // #% @@ -427,7 +432,9 @@ public final class TemplateEngine { out.write(bb); } } - return structure.getBytes(); + final byte[] sb = structure.getBytes(); + structure.close(); + return sb; } private final static byte[] replacePattern(final String key, final Map pattern, final byte dflt[]) { diff --git a/source/de/anomic/tools/CryptoLib.java b/source/de/anomic/tools/CryptoLib.java index 17547ba40..59ac7387a 100644 --- a/source/de/anomic/tools/CryptoLib.java +++ b/source/de/anomic/tools/CryptoLib.java @@ -52,15 +52,15 @@ import net.yacy.kelondro.order.Base64Order; /** * Tool functions to sign and verify files and generate keys - * + * * Start with "java -cp classes de.anomic.tools.CryptoLib --help" * from main folder - * + * * @author flori * */ public class CryptoLib { - + private static final String HELP = "Tool to sign files and verify the signature.\n" + "Usage:\n" + @@ -70,70 +70,70 @@ public class CryptoLib { " --verify publickey file file.sig\n" + " Verify signatur\n" + " --gen-key privatekey publickey\n"; - + public static final String algorithm = "DSA"; public static final int bitkey = 1024; public static final String signAlgorithm = "SHA1with"+algorithm; - + private final KeyFactory keyFact; private final Signature sign; - + public CryptoLib() throws NoSuchAlgorithmException { - keyFact = KeyFactory.getInstance(algorithm); - sign = Signature.getInstance(signAlgorithm); + this.keyFact = KeyFactory.getInstance(algorithm); + this.sign = Signature.getInstance(signAlgorithm); } - + public PrivateKey getPrivateKeyFromBytes(byte[] keyBuffer) throws InvalidKeySpecException { - return keyFact.generatePrivate(new PKCS8EncodedKeySpec(keyBuffer)); + return this.keyFact.generatePrivate(new PKCS8EncodedKeySpec(keyBuffer)); } - + public PublicKey getPublicKeyFromBytes(byte[] keyBuffer) throws InvalidKeySpecException { - return keyFact.generatePublic(new X509EncodedKeySpec(keyBuffer)); + return this.keyFact.generatePublic(new X509EncodedKeySpec(keyBuffer)); } - - public byte[] getBytesOfPrivateKey(PrivateKey privKey) throws InvalidKeySpecException { + + public byte[] getBytesOfPrivateKey(PrivateKey privKey) throws InvalidKeySpecException { EncodedKeySpec keySpec = - keyFact.getKeySpec(privKey, PKCS8EncodedKeySpec.class); + this.keyFact.getKeySpec(privKey, PKCS8EncodedKeySpec.class); return keySpec.getEncoded(); } - - public byte[] getBytesOfPublicKey(PublicKey pubKey) throws InvalidKeySpecException { + + public byte[] getBytesOfPublicKey(PublicKey pubKey) throws InvalidKeySpecException { EncodedKeySpec keySpec = - keyFact.getKeySpec(pubKey, X509EncodedKeySpec.class); + this.keyFact.getKeySpec(pubKey, X509EncodedKeySpec.class); return keySpec.getEncoded(); } - + public byte[] getSignature(PrivateKey privKey, InputStream dataStream) throws InvalidKeyException, SignatureException, IOException { - sign.initSign(privKey); + this.sign.initSign(privKey); byte[] buffer = new byte[1024]; int count = 0; while((count = dataStream.read(buffer)) != -1) { - sign.update(buffer, 0, count); + this.sign.update(buffer, 0, count); } dataStream.close(); - return sign.sign(); + return this.sign.sign(); } - + public boolean verifySignature(PublicKey pubKey, InputStream dataStream, byte[] signBuffer) throws InvalidKeyException, SignatureException, IOException { - sign.initVerify(pubKey); - + this.sign.initVerify(pubKey); + byte[] buffer = new byte[1024]; int count = 0; while((count = dataStream.read(buffer)) != -1) { - sign.update(buffer, 0, count); + this.sign.update(buffer, 0, count); } dataStream.close(); - - return sign.verify(signBuffer); + + return this.sign.verify(signBuffer); } - + public KeyPair genKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm); kpg.initialize(bitkey); - return kpg.generateKeyPair(); + return kpg.generateKeyPair(); } - + public static void main(String[] args) { try { if(args.length < 1 || args[0].contains("help")) { @@ -143,6 +143,7 @@ public class CryptoLib { CryptoLib cl = new CryptoLib(); CharBuffer privKeyBuffer = new CharBuffer(new File(args[1])); byte[] privKeyByteBuffer = Base64Order.standardCoder.decode(privKeyBuffer.toString()); + privKeyBuffer.close(); PrivateKey privKey = cl.getPrivateKeyFromBytes(privKeyByteBuffer); FileInputStream dataStream = new FileInputStream(args[2]); @@ -155,12 +156,14 @@ public class CryptoLib { CryptoLib cl = new CryptoLib(); CharBuffer pubKeyBuffer = new CharBuffer(new File(args[1])); byte[] pubKeyByteBuffer = Base64Order.standardCoder.decode(pubKeyBuffer.toString().trim()); + pubKeyBuffer.close(); PublicKey pubKey = cl.getPublicKeyFromBytes(pubKeyByteBuffer); FileInputStream dataStream = new FileInputStream(args[2]); CharBuffer signBuffer = new CharBuffer(new File(args[2] + ".sig")); byte[] signByteBuffer = Base64Order.standardCoder.decode(signBuffer.toString().trim()); + signBuffer.close(); if(cl.verifySignature(pubKey, dataStream, signByteBuffer)) { System.out.println("Signature OK!"); } else { diff --git a/source/net/yacy/cora/storage/ConfigurationSet.java b/source/net/yacy/cora/storage/ConfigurationSet.java index 2e0ac8ef4..8682fbba4 100644 --- a/source/net/yacy/cora/storage/ConfigurationSet.java +++ b/source/net/yacy/cora/storage/ConfigurationSet.java @@ -38,6 +38,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.yacy.cora.storage.ConfigurationSet.Entry; +import net.yacy.kelondro.logging.Log; import net.yacy.search.index.SolrField; /** * this class reads configuration attributes as a list of keywords from a list @@ -64,8 +65,9 @@ public class ConfigurationSet extends TreeMap implements Serializa public ConfigurationSet(final File file) { this.file = file; + BufferedReader br = null; try { - final BufferedReader br = new BufferedReader(new FileReader(this.file)); + br = new BufferedReader(new FileReader(this.file)); String s; boolean enabled; String comment, key, value; @@ -112,7 +114,11 @@ public class ConfigurationSet extends TreeMap implements Serializa } } } - } catch (final IOException e) {} + } catch (final IOException e) { + Log.logException(e); + } finally { + if (br != null) try {br.close();} catch (IOException e) {} + } } /** diff --git a/source/net/yacy/document/importer/MediawikiImporter.java b/source/net/yacy/document/importer/MediawikiImporter.java index 071a052b2..dc739d5de 100644 --- a/source/net/yacy/document/importer/MediawikiImporter.java +++ b/source/net/yacy/document/importer/MediawikiImporter.java @@ -151,9 +151,15 @@ public class MediawikiImporter extends Thread implements Importer { InputStream is = new BufferedInputStream(new FileInputStream(this.sourcefile), 1024 * 1024); if (this.sourcefile.getName().endsWith(".bz2")) { int b = is.read(); - if (b != 'B') throw new IOException("Invalid bz2 content."); + if (b != 'B') { + try {is.close();} catch (IOException e) {} + throw new IOException("Invalid bz2 content."); + } b = is.read(); - if (b != 'Z') throw new IOException("Invalid bz2 content."); + if (b != 'Z') { + try {is.close();} catch (IOException e) {} + throw new IOException("Invalid bz2 content."); + } is = new BZip2CompressorInputStream(is); } else if (this.sourcefile.getName().endsWith(".gz")) { is = new GZIPInputStream(is); diff --git a/source/net/yacy/document/parser/htmlParser.java b/source/net/yacy/document/parser/htmlParser.java index 0d5f7b9ae..6ee1b3cd2 100644 --- a/source/net/yacy/document/parser/htmlParser.java +++ b/source/net/yacy/document/parser/htmlParser.java @@ -167,6 +167,7 @@ public class htmlParser extends AbstractParser implements Parser { final ScraperInputStream htmlFilter = new ScraperInputStream(sourceStream,documentCharset,location,null,false); sourceStream = htmlFilter; charset = htmlFilter.detectCharset(); + htmlFilter.close(); } catch (final IOException e1) { throw new Parser.Failure("Charset error:" + e1.getMessage(), location); } diff --git a/source/net/yacy/document/parser/tarParser.java b/source/net/yacy/document/parser/tarParser.java index 7005cab1b..8fdda51a9 100644 --- a/source/net/yacy/document/parser/tarParser.java +++ b/source/net/yacy/document/parser/tarParser.java @@ -108,8 +108,9 @@ public class tarParser extends AbstractParser implements Parser { public final static boolean isTar(File f) { if (!f.exists() || f.length() < 0x105) return false; + RandomAccessFile raf = null; try { - RandomAccessFile raf = new RandomAccessFile(f, "r"); + raf = new RandomAccessFile(f, "r"); raf.seek(0x101); byte[] b = new byte[5]; raf.read(b); @@ -118,6 +119,8 @@ public class tarParser extends AbstractParser implements Parser { return false; } catch (IOException e) { return false; + } finally { + if (raf != null) try {raf.close();} catch (IOException e) {} } } } diff --git a/source/net/yacy/document/parser/vcfParser.java b/source/net/yacy/document/parser/vcfParser.java index 336f5a041..138359fb5 100644 --- a/source/net/yacy/document/parser/vcfParser.java +++ b/source/net/yacy/document/parser/vcfParser.java @@ -28,6 +28,7 @@ package net.yacy.document.parser; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; @@ -203,6 +204,7 @@ public class vcfParser extends AbstractParser implements Parser { "\n\tLine-Nr: " + lineNr); } } + try {inputReader.close();} catch (IOException e) {} final String[] sections = parsedNames.toArray(new String[parsedNames.size()]); final byte[] text = UTF8.getBytes(parsedDataText.toString()); diff --git a/source/net/yacy/kelondro/data/meta/URIMetadataRow.java b/source/net/yacy/kelondro/data/meta/URIMetadataRow.java index b46feb0b7..571939634 100644 --- a/source/net/yacy/kelondro/data/meta/URIMetadataRow.java +++ b/source/net/yacy/kelondro/data/meta/URIMetadataRow.java @@ -206,7 +206,9 @@ public class URIMetadataRow implements URIMetadata { if (dc_publisher.length() > 80) s.append(dc_publisher, 0, 80); else s.append(dc_publisher); s.appendLF(); if (lon == 0.0f && lat == 0.0f) s.appendLF(); else s.append(Double.toString(lat)).append(',').append(Double.toString(lon)).appendLF(); - return UTF8.getBytes(s.toString()); + String s0 = s.toString(); + s.close(); + return UTF8.getBytes(s0); } public URIMetadataRow(final Row.Entry entry, final WordReferenceVars searchedWord, final long ranking) { @@ -375,6 +377,7 @@ public class URIMetadataRow implements URIMetadata { return this.entry; } + @Override public byte[] hash() { // return a url-hash, based on the md5 algorithm // the result is a String of 12 bytes within a 72-bit space @@ -390,38 +393,47 @@ public class URIMetadataRow implements URIMetadata { return this.hostHash; } + @Override public long ranking() { return this.ranking; } + @Override public boolean matches(final Pattern matcher) { return this.metadata().matches(matcher); } + @Override public DigestURI url() { return this.metadata().url(); } + @Override public String dc_title() { return this.metadata().dc_title(); } + @Override public String dc_creator() { return this.metadata().dc_creator(); } + @Override public String dc_publisher() { return this.metadata().dc_publisher(); } + @Override public String dc_subject() { return this.metadata().dc_subject(); } + @Override public double lat() { return this.metadata().lat(); } + @Override public double lon() { return this.metadata().lon(); } @@ -443,14 +455,17 @@ public class URIMetadataRow implements URIMetadata { return this.comp; } + @Override public Date moddate() { return decodeDate(col_mod); } + @Override public Date loaddate() { return decodeDate(col_load); } + @Override public Date freshdate() { return decodeDate(col_fresh); } @@ -468,15 +483,18 @@ public class URIMetadataRow implements URIMetadata { return r; } + @Override public String md5() { // returns the md5 in hex representation return Digest.encodeHex(this.entry.getColBytes(col_md5, true)); } + @Override public char doctype() { return (char) this.entry.getColByte(col_dt); } + @Override public byte[] language() { byte[] b = this.entry.getColBytes(col_lang, true); if (b == null || b[0] == (byte)'[') { @@ -487,52 +505,64 @@ public class URIMetadataRow implements URIMetadata { return b; } + @Override public int size() { return (int) this.entry.getColLong(col_size); } + @Override public Bitfield flags() { return new Bitfield(this.entry.getColBytes(col_flags, true)); } + @Override public int wordCount() { return (int) this.entry.getColLong(col_wc); } + @Override public int llocal() { return (int) this.entry.getColLong(col_llocal); } + @Override public int lother() { return (int) this.entry.getColLong(col_lother); } + @Override public int limage() { return (int) this.entry.getColLong(col_limage); } + @Override public int laudio() { return (int) this.entry.getColLong(col_laudio); } + @Override public int lvideo() { return (int) this.entry.getColLong(col_lvideo); } + @Override public int lapp() { return (int) this.entry.getColLong(col_lapp); } + @Override public String snippet() { // the snippet may appear here if the url was transported in a remote search // it will not be saved anywhere, but can only be requested here return this.snippet; } + @Override public WordReferenceVars word() { return this.word; } + @Override public boolean isOlder(final URIMetadata other) { if (other == null) return false; final Date tmoddate = moddate(); @@ -547,6 +577,7 @@ public class URIMetadataRow implements URIMetadata { return false; } + @Override public String toString(final String snippet) { // add information needed for remote transport final StringBuilder core = corePropList(); diff --git a/source/net/yacy/kelondro/index/Row.java b/source/net/yacy/kelondro/index/Row.java index 6005b8503..bd13ee972 100644 --- a/source/net/yacy/kelondro/index/Row.java +++ b/source/net/yacy/kelondro/index/Row.java @@ -27,6 +27,7 @@ package net.yacy.kelondro.index; +import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.Comparator; @@ -619,7 +620,9 @@ public final class Row implements Serializable { } if (includeBraces) bb.append('}'); //System.out.println("DEBUG-ROW " + bb.toString()); - return bb.toString(); + String bbs = bb.toString(); + try {bb.close();} catch (IOException e) {} + return bbs; } @Override diff --git a/source/net/yacy/kelondro/order/Digest.java b/source/net/yacy/kelondro/order/Digest.java index b088a0889..27b10d460 100644 --- a/source/net/yacy/kelondro/order/Digest.java +++ b/source/net/yacy/kelondro/order/Digest.java @@ -72,7 +72,7 @@ public class Digest { public static void cleanup() { md5Cache.clear(); } - + public static String encodeHex(final long in, final int length) { String s = Long.toHexString(in); while (s.length() < length) s = "0" + s; @@ -121,7 +121,7 @@ public class Digest { // generate a hex representation from the md5 of a byte-array return encodeHex(encodeMD5Raw(b)); } - + public static byte[] encodeMD5Raw(final String key) { byte[] h = md5Cache.get(key); @@ -160,7 +160,7 @@ public class Digest { } public static byte[] encodeMD5Raw(final File file) throws IOException { - FileInputStream in; + FileInputStream in = null; try { in = new FileInputStream(file); } catch (final java.io.FileNotFoundException e) { @@ -185,11 +185,12 @@ public class Digest { if (c.n <= 0) break; md5consumer.consume(c); } - in.close(); } catch (final IOException e) { Log.logSevere("Digest", "file error with " + file.toString() + ": " + e.getMessage()); md5consumer.consume(md5FilechunkConsumer.poison); throw e; + } finally { + try {in.close();} catch (IOException e) {} } // put in poison into queue to tell the consumer to stop md5consumer.consume(md5FilechunkConsumer.poison); diff --git a/source/net/yacy/kelondro/util/FileUtils.java b/source/net/yacy/kelondro/util/FileUtils.java index 764fd47ab..52721dd28 100644 --- a/source/net/yacy/kelondro/util/FileUtils.java +++ b/source/net/yacy/kelondro/util/FileUtils.java @@ -547,8 +547,7 @@ public final class FileUtils return set; } - public static void saveSet(final File file, final String format, final Set set, final String sep) - throws IOException { + public static void saveSet(final File file, final String format, final Set set, final String sep) throws IOException { final File tf = new File(file.toString() + ".prt" + (System.currentTimeMillis() % 1000)); OutputStream os = null; if ( (format == null) || (format.equals("plain")) ) { @@ -556,6 +555,7 @@ public final class FileUtils } else if ( format.equals("gzip") ) { os = new GZIPOutputStream(new FileOutputStream(tf)); } else if ( format.equals("zip") ) { + @SuppressWarnings("resource") final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file)); String name = file.getName(); if ( name.endsWith(".zip") ) { @@ -564,7 +564,8 @@ public final class FileUtils zos.putNextEntry(new ZipEntry(name + ".txt")); os = zos; } - if ( os != null ) { + IOException ex = null; + if ( os != null ) try { for ( final byte[] b : set ) { os.write(b); if ( sep != null ) { @@ -572,12 +573,16 @@ public final class FileUtils } } os.close(); + } catch (IOException e) { + ex = e; + } finally { + os.close(); } + if (ex != null) throw ex; forceMove(tf, file); } - public static void saveSet(final File file, final String format, final RowSet set, final String sep) - throws IOException { + public static void saveSet(final File file, final String format, final RowSet set, final String sep) throws IOException { final File tf = new File(file.toString() + ".prt" + (System.currentTimeMillis() % 1000)); OutputStream os = null; if ( (format == null) || (format.equals("plain")) ) { @@ -585,6 +590,7 @@ public final class FileUtils } else if ( format.equals("gzip") ) { os = new GZIPOutputStream(new FileOutputStream(tf)); } else if ( format.equals("zip") ) { + @SuppressWarnings("resource") final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file)); String name = file.getName(); if ( name.endsWith(".zip") ) { @@ -593,7 +599,8 @@ public final class FileUtils zos.putNextEntry(new ZipEntry(name + ".txt")); os = zos; } - if ( os != null ) { + IOException ex = null; + if ( os != null ) try { final Iterator i = set.iterator(); if ( i.hasNext() ) { os.write(i.next().getPrimaryKeyBytes()); @@ -604,8 +611,12 @@ public final class FileUtils } os.write(i.next().getPrimaryKeyBytes()); } + } catch (IOException e) { + ex = e; + } finally { os.close(); } + if (ex != null) throw ex; forceMove(tf, file); } diff --git a/source/net/yacy/search/Switchboard.java b/source/net/yacy/search/Switchboard.java index 38bdf9aa0..51a236e57 100644 --- a/source/net/yacy/search/Switchboard.java +++ b/source/net/yacy/search/Switchboard.java @@ -1686,9 +1686,10 @@ public final class Switchboard extends serverSwitch boolean moved = false; if ( s.endsWith("xml.zip") ) { // open the zip file with all the xml files in it + ZipInputStream zis = null; try { final InputStream is = new BufferedInputStream(new FileInputStream(infile)); - final ZipInputStream zis = new ZipInputStream(is); + zis = new ZipInputStream(is); ZipEntry entry; while ( (entry = zis.getNextEntry()) != null ) { int size; @@ -1705,6 +1706,7 @@ public final class Switchboard extends serverSwitch Log.logException(e); } finally { moved = infile.renameTo(outfile); + if (zis != null) try {zis.close();} catch (IOException e) {} } return moved; } else { diff --git a/source/net/yacy/search/query/AccessTracker.java b/source/net/yacy/search/query/AccessTracker.java index ceeee6edb..7533bfb33 100644 --- a/source/net/yacy/search/query/AccessTracker.java +++ b/source/net/yacy/search/query/AccessTracker.java @@ -115,8 +115,9 @@ public class AccessTracker { while (localSearches.size() > 0) { addToDump(localSearches.removeFirst()); } + RandomAccessFile raf = null; try { - final RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf = new RandomAccessFile(file, "rw"); raf.seek(raf.length()); for (final String s: log) { raf.write(UTF8.getBytes(s)); @@ -126,8 +127,9 @@ public class AccessTracker { } catch (final FileNotFoundException e) { Log.logException(e); } catch (final IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + Log.logException(e); + } finally { + if (raf != null) try {raf.close();} catch (IOException e) {} } } }