findbugs util (includes ticket #370)

This commit is contained in:
zzz
2011-01-10 15:45:20 +00:00
parent 2a85263259
commit f68c095222
11 changed files with 95 additions and 55 deletions

View File

@@ -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();

View File

@@ -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) {}

View File

@@ -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) {}
}
}

View File

@@ -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);

View File

@@ -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; }

View File

@@ -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 */

View File

@@ -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;
}
}
******/
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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);
}