From f68c09522230a218888a2c61c29769674f6cab19 Mon Sep 17 00:00:00 2001 From: zzz Date: Mon, 10 Jan 2011 15:45:20 +0000 Subject: [PATCH] findbugs util (includes ticket #370) --- core/java/src/net/i2p/util/EepGet.java | 62 ++++++++++++------- core/java/src/net/i2p/util/EepPost.java | 5 +- core/java/src/net/i2p/util/FileUtil.java | 29 +++++---- core/java/src/net/i2p/util/Log.java | 3 +- core/java/src/net/i2p/util/LogManager.java | 11 ++-- .../src/net/i2p/util/LogRecordFormatter.java | 3 + .../net/i2p/util/LookaheadInputStream.java | 2 + core/java/src/net/i2p/util/SSLEepGet.java | 23 ++++--- core/java/src/net/i2p/util/ShellCommand.java | 8 +-- core/java/src/net/i2p/util/SimpleTimer.java | 2 +- core/java/src/net/i2p/util/SimpleTimer2.java | 2 +- 11 files changed, 95 insertions(+), 55 deletions(-) diff --git a/core/java/src/net/i2p/util/EepGet.java b/core/java/src/net/i2p/util/EepGet.java index f055ce98c..3ad84e6bb 100644 --- a/core/java/src/net/i2p/util/EepGet.java +++ b/core/java/src/net/i2p/util/EepGet.java @@ -430,29 +430,33 @@ public class EepGet { _log.debug("Fetching (proxied? " + _shouldProxy + ") url=" + _actualURL); while (_keepFetching) { SocketTimeout timeout = null; - if (_fetchHeaderTimeout > 0) + if (_fetchHeaderTimeout > 0) { timeout = new SocketTimeout(_fetchHeaderTimeout); - final SocketTimeout stimeout = timeout; // ugly - why not use sotimeout? - timeout.setTimeoutCommand(new Runnable() { - public void run() { - if (_log.shouldLog(Log.DEBUG)) - _log.debug("timeout reached on " + _url + ": " + stimeout); - _aborted = true; - } - }); - timeout.setTotalTimeoutPeriod(_fetchEndTime); + final SocketTimeout stimeout = timeout; // ugly - why not use sotimeout? + timeout.setTimeoutCommand(new Runnable() { + public void run() { + if (_log.shouldLog(Log.DEBUG)) + _log.debug("timeout reached on " + _url + ": " + stimeout); + _aborted = true; + } + }); + timeout.setTotalTimeoutPeriod(_fetchEndTime); + } try { for (int i = 0; i < _listeners.size(); i++) _listeners.get(i).attempting(_url); sendRequest(timeout); - timeout.resetTimer(); + if (timeout != null) + timeout.resetTimer(); doFetch(timeout); - timeout.cancel(); + if (timeout != null) + timeout.cancel(); if (!_transferFailed) return true; break; } catch (IOException ioe) { - timeout.cancel(); + if (timeout != null) + timeout.cancel(); for (int i = 0; i < _listeners.size(); i++) _listeners.get(i).attemptFailed(_url, _bytesTransferred, _bytesRemaining, _currentAttempt, _numRetries, ioe); if (_log.shouldLog(Log.WARN)) @@ -492,7 +496,10 @@ public class EepGet { return false; } - /** single fetch */ + /** + * single fetch + * @param timeout may be null + */ protected void doFetch(SocketTimeout timeout) throws IOException { _headersRead = false; _aborted = false; @@ -504,11 +511,13 @@ public class EepGet { if (_aborted) throw new IOException("Timed out reading the HTTP headers"); - timeout.resetTimer(); - if (_fetchInactivityTimeout > 0) - timeout.setInactivityTimeout(_fetchInactivityTimeout); - else - timeout.setInactivityTimeout(INACTIVITY_TIMEOUT); + if (timeout != null) { + timeout.resetTimer(); + if (_fetchInactivityTimeout > 0) + timeout.setInactivityTimeout(_fetchInactivityTimeout); + else + timeout.setInactivityTimeout(INACTIVITY_TIMEOUT); + } if (_redirectLocation != null) { //try { @@ -571,7 +580,8 @@ public class EepGet { int read = _proxyIn.read(buf, 0, toRead); if (read == -1) break; - timeout.resetTimer(); + if (timeout != null) + timeout.resetTimer(); _out.write(buf, 0, read); _bytesTransferred += read; if ((_maxSize > -1) && (_alreadyTransferred + read > _maxSize)) // could transfer a little over maxSize @@ -597,7 +607,8 @@ public class EepGet { read++; } } - timeout.resetTimer(); + if (timeout != null) + timeout.resetTimer(); if (_bytesRemaining >= read) // else chunked? _bytesRemaining -= read; if (read > 0) { @@ -622,7 +633,8 @@ public class EepGet { if (_aborted) throw new IOException("Timed out reading the HTTP data"); - timeout.cancel(); + if (timeout != null) + timeout.cancel(); if (_log.shouldLog(Log.DEBUG)) _log.debug("Done transferring " + _bytesTransferred + " (ok? " + !_transferFailed + ")"); @@ -867,6 +879,9 @@ public class EepGet { private static final byte NL = '\n'; private static boolean isNL(byte b) { return (b == NL); } + /** + * @param timeout may be null + */ protected void sendRequest(SocketTimeout timeout) throws IOException { if (_outputStream != null) { // We are reading into a stream supplied by a caller, @@ -907,7 +922,8 @@ public class EepGet { _proxyIn = _proxy.getInputStream(); _proxyOut = _proxy.getOutputStream(); - timeout.setSocket(_proxy); + if (timeout != null) + timeout.setSocket(_proxy); _proxyOut.write(DataHelper.getUTF8(req)); _proxyOut.flush(); diff --git a/core/java/src/net/i2p/util/EepPost.java b/core/java/src/net/i2p/util/EepPost.java index 615361e20..2e7836f62 100644 --- a/core/java/src/net/i2p/util/EepPost.java +++ b/core/java/src/net/i2p/util/EepPost.java @@ -31,6 +31,7 @@ public class EepPost { _log = ctx.logManager().getLog(EepPost.class); } +/***** public static void main(String args[]) { EepPost e = new EepPost(); Map fields = new HashMap(); @@ -47,6 +48,8 @@ public class EepPost { //e.postFiles("http://localhost/cgi-bin/read.pl", null, -1, fields, null); //e.postFiles("http://localhost:2001/import.jsp", null, -1, fields, null); } +*****/ + /** * Submit an HTTP POST to the given URL (using the proxy if specified), * uploading the given fields. If the field's value is a File object, then @@ -117,7 +120,7 @@ public class EepPost { } } out.close(); - } catch (Exception e) { + } catch (IOException e) { e.printStackTrace(); } finally { if (s != null) try { s.close(); } catch (IOException ioe) {} diff --git a/core/java/src/net/i2p/util/FileUtil.java b/core/java/src/net/i2p/util/FileUtil.java index b5bb48a43..68ce4e4f6 100644 --- a/core/java/src/net/i2p/util/FileUtil.java +++ b/core/java/src/net/i2p/util/FileUtil.java @@ -122,24 +122,24 @@ public class FileUtil { } } } else { + InputStream in = null; + FileOutputStream fos = null; + JarOutputStream jos = null; try { - InputStream in = zip.getInputStream(entry); + in = zip.getInputStream(entry); if (entry.getName().endsWith(".jar.pack") || entry.getName().endsWith(".war.pack")) { target = new File(targetDir, entry.getName().substring(0, entry.getName().length() - ".pack".length())); - JarOutputStream fos = new JarOutputStream(new FileOutputStream(target)); - unpack(in, fos); - fos.close(); + jos = new JarOutputStream(new FileOutputStream(target)); + unpack(in, jos); System.err.println("INFO: File [" + entry.getName() + "] extracted and unpacked"); } else { - FileOutputStream fos = new FileOutputStream(target); + fos = new FileOutputStream(target); int read = 0; while ( (read = in.read(buf)) != -1) { fos.write(buf, 0, read); } - fos.close(); System.err.println("INFO: File [" + entry.getName() + "] extracted"); } - in.close(); } catch (IOException ioe) { System.err.println("ERROR: Error extracting the zip entry (" + entry.getName() + ')'); if (ioe.getMessage() != null && ioe.getMessage().indexOf("CAFED00D") >= 0) @@ -151,6 +151,10 @@ public class FileUtil { System.err.println("ERROR: Error unpacking the zip entry (" + entry.getName() + "), your JVM does not support unpack200"); return false; + } finally { + try { if (in != null) in.close(); } catch (IOException ioe) {} + try { if (fos != null) fos.close(); } catch (IOException ioe) {} + try { if (jos != null) jos.close(); } catch (IOException ioe) {} } } } @@ -401,21 +405,24 @@ public class FileUtil { if (dst.exists() && !overwriteExisting) return false; byte buf[] = new byte[4096]; + InputStream in = null; + OutputStream out = null; try { - FileInputStream in = new FileInputStream(src); - FileOutputStream out = new FileOutputStream(dst); + in = new FileInputStream(src); + out = new FileOutputStream(dst); int read = 0; while ( (read = in.read(buf)) != -1) out.write(buf, 0, read); - in.close(); - out.close(); return true; } catch (IOException ioe) { if (!quiet) ioe.printStackTrace(); return false; + } finally { + try { if (in != null) in.close(); } catch (IOException ioe) {} + try { if (out != null) out.close(); } catch (IOException ioe) {} } } diff --git a/core/java/src/net/i2p/util/Log.java b/core/java/src/net/i2p/util/Log.java index b22325adb..6ce320c9d 100644 --- a/core/java/src/net/i2p/util/Log.java +++ b/core/java/src/net/i2p/util/Log.java @@ -205,7 +205,8 @@ public class Log { } @Override public boolean equals(Object obj) { - if (obj == null) throw new NullPointerException("Null object scope?"); + if (obj == null) + return false; if (obj instanceof LogScope) { LogScope s = (LogScope)obj; return s._scopeCache.equals(_scopeCache); diff --git a/core/java/src/net/i2p/util/LogManager.java b/core/java/src/net/i2p/util/LogManager.java index 49eaf291f..8a9549724 100644 --- a/core/java/src/net/i2p/util/LogManager.java +++ b/core/java/src/net/i2p/util/LogManager.java @@ -164,8 +164,10 @@ public class LogManager { Log rv = _logs.get(scope); if (rv == null) { rv = new Log(this, cls, name); - _logs.putIfAbsent(scope, rv); - isNew = true; + Log old = _logs.putIfAbsent(scope, rv); + isNew = old == null; + if (!isNew) + rv = old; } if (isNew) updateLimit(rv); @@ -178,8 +180,9 @@ public class LogManager { } void addLog(Log log) { - _logs.putIfAbsent(log.getScope(), log); - updateLimit(log); + Log old = _logs.putIfAbsent(log.getScope(), log); + if (old == null) + updateLimit(log); } public LogConsoleBuffer getBuffer() { return _consoleBuffer; } diff --git a/core/java/src/net/i2p/util/LogRecordFormatter.java b/core/java/src/net/i2p/util/LogRecordFormatter.java index 52583b63c..58d08af74 100644 --- a/core/java/src/net/i2p/util/LogRecordFormatter.java +++ b/core/java/src/net/i2p/util/LogRecordFormatter.java @@ -92,10 +92,13 @@ class LogRecordFormatter { } /** don't translate */ +/**** private static String getPriority(LogRecord rec) { return toString(Log.toLevelString(rec.getPriority()), MAX_PRIORITY_LENGTH); } +****/ + /** */ private static final String BUNDLE_NAME = "net.i2p.router.web.messages"; /** translate @since 0.7.14 */ diff --git a/core/java/src/net/i2p/util/LookaheadInputStream.java b/core/java/src/net/i2p/util/LookaheadInputStream.java index 7b23d26ae..d9a1d9f93 100644 --- a/core/java/src/net/i2p/util/LookaheadInputStream.java +++ b/core/java/src/net/i2p/util/LookaheadInputStream.java @@ -78,6 +78,7 @@ public class LookaheadInputStream extends FilterInputStream { /** grab the lookahead footer */ public byte[] getFooter() { return _footerLookahead; } +/******* public static void main(String args[]) { byte buf[] = new byte[32]; for (int i = 0; i < 32; i++) @@ -128,4 +129,5 @@ public class LookaheadInputStream extends FilterInputStream { return false; } } +******/ } diff --git a/core/java/src/net/i2p/util/SSLEepGet.java b/core/java/src/net/i2p/util/SSLEepGet.java index e3ba43ae7..d35545ae7 100644 --- a/core/java/src/net/i2p/util/SSLEepGet.java +++ b/core/java/src/net/i2p/util/SSLEepGet.java @@ -482,12 +482,14 @@ public class SSLEepGet extends EepGet { if (_aborted) throw new IOException("Timed out reading the HTTP headers"); - timeout.resetTimer(); - if (_fetchInactivityTimeout > 0) - timeout.setInactivityTimeout(_fetchInactivityTimeout); - else - timeout.setInactivityTimeout(60*1000); - + if (timeout != null) { + timeout.resetTimer(); + if (_fetchInactivityTimeout > 0) + timeout.setInactivityTimeout(_fetchInactivityTimeout); + else + timeout.setInactivityTimeout(60*1000); + } + if (_redirectLocation != null) { throw new IOException("Server redirect to " + _redirectLocation + " not allowed"); } @@ -506,7 +508,8 @@ public class SSLEepGet extends EepGet { int read = _proxyIn.read(buf, 0, toRead); if (read == -1) break; - timeout.resetTimer(); + if (timeout != null) + timeout.resetTimer(); _out.write(buf, 0, read); _bytesTransferred += read; @@ -531,7 +534,8 @@ public class SSLEepGet extends EepGet { read++; } } - timeout.resetTimer(); + if (timeout != null) + timeout.resetTimer(); if (_bytesRemaining >= read) // else chunked? _bytesRemaining -= read; if (read > 0) { @@ -556,7 +560,8 @@ public class SSLEepGet extends EepGet { if (_aborted) throw new IOException("Timed out reading the HTTP data"); - timeout.cancel(); + if (timeout != null) + timeout.cancel(); if (_transferFailed) { // 404, etc - transferFailed is called after all attempts fail, by fetch() above diff --git a/core/java/src/net/i2p/util/ShellCommand.java b/core/java/src/net/i2p/util/ShellCommand.java index 12b668f67..bb0aec5f8 100644 --- a/core/java/src/net/i2p/util/ShellCommand.java +++ b/core/java/src/net/i2p/util/ShellCommand.java @@ -89,7 +89,7 @@ public class ShellCommand { * * @author hypercubus */ - private class StreamConsumer extends Thread { + private static class StreamConsumer extends Thread { private BufferedReader bufferedReader; private InputStreamReader inputStreamReader; @@ -123,7 +123,7 @@ public class ShellCommand { * * @author hypercubus */ - private class StreamReader extends Thread { + private static class StreamReader extends Thread { private BufferedReader bufferedReader; private InputStreamReader inputStreamReader; @@ -159,7 +159,7 @@ public class ShellCommand { * * @author hypercubus */ - private class StreamWriter extends Thread { + private static class StreamWriter extends Thread { private BufferedWriter bufferedWriter; private BufferedReader in; @@ -183,7 +183,7 @@ public class ShellCommand { bufferedWriter.write(input, 0, input.length()); bufferedWriter.flush(); } - } catch (Exception e) { + } catch (IOException e) { try { bufferedWriter.flush(); } catch (IOException e1) { diff --git a/core/java/src/net/i2p/util/SimpleTimer.java b/core/java/src/net/i2p/util/SimpleTimer.java index 0b5430711..f428afceb 100644 --- a/core/java/src/net/i2p/util/SimpleTimer.java +++ b/core/java/src/net/i2p/util/SimpleTimer.java @@ -90,7 +90,7 @@ public class SimpleTimer { int totalEvents = 0; long now = System.currentTimeMillis(); long eventTime = now + timeoutMs; - Long time = new Long(eventTime); + Long time = Long.valueOf(eventTime); synchronized (_events) { // remove the old scheduled position, then reinsert it Long oldTime = (Long)_eventTimes.get(event); diff --git a/core/java/src/net/i2p/util/SimpleTimer2.java b/core/java/src/net/i2p/util/SimpleTimer2.java index bda41e621..44e405b24 100644 --- a/core/java/src/net/i2p/util/SimpleTimer2.java +++ b/core/java/src/net/i2p/util/SimpleTimer2.java @@ -55,7 +55,7 @@ public class SimpleTimer2 { _executor.shutdownNow(); } - private class CustomScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor { + private static class CustomScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor { public CustomScheduledThreadPoolExecutor(int threads, ThreadFactory factory) { super(threads, factory); }