findbugs core,client,crypto,data,stat,i2np

This commit is contained in:
zzz
2011-01-07 00:15:35 +00:00
parent 0129051063
commit 087c7b86de
15 changed files with 76 additions and 51 deletions

View File

@ -63,7 +63,7 @@ import net.i2p.util.SecureDirectory;
*/ */
public class I2PAppContext { public class I2PAppContext {
/** the context that components without explicit root are bound */ /** the context that components without explicit root are bound */
protected static I2PAppContext _globalAppContext; protected static volatile I2PAppContext _globalAppContext;
private Properties _overrideProps; private Properties _overrideProps;
@ -117,7 +117,8 @@ public class I2PAppContext {
* *
*/ */
public static I2PAppContext getGlobalContext() { public static I2PAppContext getGlobalContext() {
// skip the global lock // skip the global lock - _gAC must be volatile
// http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
I2PAppContext rv = _globalAppContext; I2PAppContext rv = _globalAppContext;
if (rv != null) if (rv != null)
return rv; return rv;
@ -474,6 +475,9 @@ public class I2PAppContext {
* provided during the context construction, as well as the ones included in * provided during the context construction, as well as the ones included in
* System.getProperties. * System.getProperties.
* *
* WARNING - not overridden in RouterContext, doesn't contain router config settings,
* use getProperties() instead.
*
* @return set of Strings containing the names of defined system properties * @return set of Strings containing the names of defined system properties
*/ */
public Set getPropertyNames() { public Set getPropertyNames() {

View File

@ -421,7 +421,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
* *
*/ */
public byte[] receiveMessage(int msgId) throws I2PSessionException { public byte[] receiveMessage(int msgId) throws I2PSessionException {
MessagePayloadMessage msg = _availableMessages.remove(new Long(msgId)); MessagePayloadMessage msg = _availableMessages.remove(Long.valueOf(msgId));
if (msg == null) { if (msg == null) {
_log.error("Receive message " + msgId + " had no matches"); _log.error("Receive message " + msgId + " had no matches");
return null; return null;
@ -468,7 +468,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
* Recieve a payload message and let the app know its available * Recieve a payload message and let the app know its available
*/ */
public void addNewMessage(MessagePayloadMessage msg) { public void addNewMessage(MessagePayloadMessage msg) {
Long mid = new Long(msg.getMessageId()); Long mid = Long.valueOf(msg.getMessageId());
_availableMessages.put(mid, msg); _availableMessages.put(mid, msg);
long id = msg.getMessageId(); long id = msg.getMessageId();
byte data[] = msg.getPayload().getUnencryptedData(); byte data[] = msg.getPayload().getUnencryptedData();
@ -518,7 +518,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
public void available(long msgId, int size) { public void available(long msgId, int size) {
synchronized (AvailabilityNotifier.this) { synchronized (AvailabilityNotifier.this) {
_pendingIds.add(new Long(msgId)); _pendingIds.add(Long.valueOf(msgId));
_pendingSizes.add(Integer.valueOf(size)); _pendingSizes.add(Integer.valueOf(size));
AvailabilityNotifier.this.notifyAll(); AvailabilityNotifier.this.notifyAll();
} }

View File

@ -191,7 +191,7 @@ class I2PSessionMuxedImpl extends I2PSessionImpl2 implements I2PSession {
*/ */
@Override @Override
public void addNewMessage(MessagePayloadMessage msg) { public void addNewMessage(MessagePayloadMessage msg) {
Long mid = new Long(msg.getMessageId()); Long mid = Long.valueOf(msg.getMessageId());
_availableMessages.put(mid, msg); _availableMessages.put(mid, msg);
long id = msg.getMessageId(); long id = msg.getMessageId();
byte data[] = msg.getPayload().getUnencryptedData(); byte data[] = msg.getPayload().getUnencryptedData();

View File

@ -108,7 +108,7 @@ class I2PSimpleSession extends I2PSessionImpl2 {
/** /**
* Only map message handlers that we will use * Only map message handlers that we will use
*/ */
class SimpleMessageHandlerMap extends I2PClientMessageHandlerMap { private static class SimpleMessageHandlerMap extends I2PClientMessageHandlerMap {
public SimpleMessageHandlerMap(I2PAppContext context) { public SimpleMessageHandlerMap(I2PAppContext context) {
int highest = Math.max(DestReplyMessage.MESSAGE_TYPE, BandwidthLimitsMessage.MESSAGE_TYPE); int highest = Math.max(DestReplyMessage.MESSAGE_TYPE, BandwidthLimitsMessage.MESSAGE_TYPE);
_handlers = new I2CPMessageHandler[highest+1]; _handlers = new I2CPMessageHandler[highest+1];

View File

@ -149,7 +149,7 @@ public class CryptixAESEngine extends AESEngine {
@Override @Override
public final void decryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte rv[], int outIndex) { public final void decryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte rv[], int outIndex) {
if ( (payload == null) || (rv == null) ) if ( (payload == null) || (rv == null) )
throw new IllegalArgumentException("null block args [payload=" + payload + " rv="+rv); throw new IllegalArgumentException("null block args");
if (payload.length - inIndex > rv.length - outIndex) if (payload.length - inIndex > rv.length - outIndex)
throw new IllegalArgumentException("bad block args [payload.len=" + payload.length throw new IllegalArgumentException("bad block args [payload.len=" + payload.length
+ " inIndex=" + inIndex + " rv.len=" + rv.length + " inIndex=" + inIndex + " rv.len=" + rv.length

View File

@ -72,13 +72,13 @@ public class Base32 {
} }
private static void runApp(String args[]) { private static void runApp(String args[]) {
if ("encodestring".equalsIgnoreCase(args[0])) {
System.out.println(encode(args[1].getBytes()));
return;
}
InputStream in = System.in;
OutputStream out = System.out;
try { try {
if ("encodestring".equalsIgnoreCase(args[0])) {
System.out.println(encode(args[1].getBytes()));
return;
}
InputStream in = System.in;
OutputStream out = System.out;
if (args.length >= 3) { if (args.length >= 3) {
out = new FileOutputStream(args[2]); out = new FileOutputStream(args[2]);
} }
@ -95,6 +95,9 @@ public class Base32 {
} }
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(System.err); ioe.printStackTrace(System.err);
} finally {
try { in.close(); } catch (IOException e) {}
try { out.close(); } catch (IOException e) {}
} }
} }

View File

@ -178,13 +178,13 @@ public class Base64 {
} }
private static void runApp(String args[]) { private static void runApp(String args[]) {
if ("encodestring".equalsIgnoreCase(args[0])) {
System.out.println(encode(args[1].getBytes()));
return;
}
InputStream in = System.in;
OutputStream out = System.out;
try { try {
if ("encodestring".equalsIgnoreCase(args[0])) {
System.out.println(encode(args[1].getBytes()));
return;
}
InputStream in = System.in;
OutputStream out = System.out;
if (args.length >= 3) { if (args.length >= 3) {
out = new FileOutputStream(args[2]); out = new FileOutputStream(args[2]);
} }
@ -201,6 +201,9 @@ public class Base64 {
} }
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(System.err); ioe.printStackTrace(System.err);
} finally {
try { in.close(); } catch (IOException e) {}
try { out.close(); } catch (IOException e) {}
} }
} }

View File

@ -844,7 +844,7 @@ public class DataHelper {
*/ */
public final static void xor(byte lhs[], int startLeft, byte rhs[], int startRight, byte out[], int startOut, int len) { public final static void xor(byte lhs[], int startLeft, byte rhs[], int startRight, byte out[], int startOut, int len) {
if ( (lhs == null) || (rhs == null) || (out == null) ) if ( (lhs == null) || (rhs == null) || (out == null) )
throw new NullPointerException("Invalid params to xor (" + lhs + ", " + rhs + ", " + out + ")"); throw new NullPointerException("Null params to xor");
if (lhs.length < startLeft + len) if (lhs.length < startLeft + len)
throw new IllegalArgumentException("Left hand side is too short"); throw new IllegalArgumentException("Left hand side is too short");
if (rhs.length < startRight + len) if (rhs.length < startRight + len)

View File

@ -133,9 +133,15 @@ public class PrivateKeyFile {
*/ */
public Destination createIfAbsent() throws I2PException, IOException, DataFormatException { public Destination createIfAbsent() throws I2PException, IOException, DataFormatException {
if(!this.file.exists()) { if(!this.file.exists()) {
FileOutputStream out = new FileOutputStream(this.file); FileOutputStream out = null;
this.client.createDestination(out); try {
out.close(); out = new FileOutputStream(this.file);
this.client.createDestination(out);
} finally {
if (out != null) {
try { out.close(); } catch (IOException ioe) {}
}
}
} }
return getDestination(); return getDestination();
} }
@ -243,29 +249,36 @@ public class PrivateKeyFile {
public I2PSession open() throws I2PSessionException, IOException { public I2PSession open() throws I2PSessionException, IOException {
return this.open(new Properties()); return this.open(new Properties());
} }
public I2PSession open(Properties opts) throws I2PSessionException, IOException { public I2PSession open(Properties opts) throws I2PSessionException, IOException {
// open input file FileInputStream in = null;
FileInputStream in = new FileInputStream(this.file); try {
in = new FileInputStream(this.file);
// create sesssion I2PSession s = this.client.createSession(in, opts);
I2PSession s = this.client.createSession(in, opts); return s;
} finally {
// close file if (in != null) {
in.close(); try { in.close(); } catch (IOException ioe) {}
}
return s; }
} }
/** /**
* Copied from I2PClientImpl.createDestination() * Copied from I2PClientImpl.createDestination()
*/ */
public void write() throws IOException, DataFormatException { public void write() throws IOException, DataFormatException {
FileOutputStream out = new FileOutputStream(this.file); FileOutputStream out = null;
this.dest.writeBytes(out); try {
this.privKey.writeBytes(out); out = new FileOutputStream(this.file);
this.signingPrivKey.writeBytes(out); this.dest.writeBytes(out);
out.flush(); this.privKey.writeBytes(out);
out.close(); this.signingPrivKey.writeBytes(out);
out.flush();
} finally {
if (out != null) {
try { out.close(); } catch (IOException ioe) {}
}
}
} }
@Override @Override
@ -377,7 +390,8 @@ public class PrivateKeyFile {
} }
} }
} }
} catch (Exception ioe) { } catch (DataFormatException dfe) {
} catch (IOException ioe) {
} }
// not found, continue to the next file // not found, continue to the next file
} }

View File

@ -76,10 +76,11 @@ public class DestReplyMessage extends I2CPMessageImpl {
} }
protected byte[] doWriteMessage() throws I2CPMessageException, IOException { protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
if (_dest == null && _hash == null) if (_dest == null) {
return new byte[0]; // null response allowed if (_hash == null)
if (_dest == null && _hash != null) return new byte[0]; // null response allowed
return _hash.getData(); return _hash.getData();
}
ByteArrayOutputStream os = new ByteArrayOutputStream(_dest.size()); ByteArrayOutputStream os = new ByteArrayOutputStream(_dest.size());
try { try {
_dest.writeBytes(os); _dest.writeBytes(os);

View File

@ -89,7 +89,7 @@ public class FrequencyStat {
/** @since 0.8.2 */ /** @since 0.8.2 */
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ((obj == null) || (obj.getClass() != FrequencyStat.class)) return false; if ((obj == null) || !(obj instanceof FrequencyStat)) return false;
return _statName.equals(((FrequencyStat)obj)._statName); return _statName.equals(((FrequencyStat)obj)._statName);
} }

View File

@ -473,7 +473,7 @@ public class Rate {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ((obj == null) || (obj.getClass() != Rate.class)) return false; if ((obj == null) || !(obj instanceof Rate)) return false;
if (obj == this) return true; if (obj == this) return true;
Rate r = (Rate) obj; Rate r = (Rate) obj;
return _period == r.getPeriod() && _creationDate == r.getCreationDate() && return _period == r.getPeriod() && _creationDate == r.getCreationDate() &&

View File

@ -108,7 +108,7 @@ public class RateStat {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ((obj == null) || (obj.getClass() != RateStat.class)) return false; if ((obj == null) || !(obj instanceof RateStat)) return false;
RateStat rs = (RateStat) obj; RateStat rs = (RateStat) obj;
if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription()) if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription())
&& DataHelper.eq(getName(), rs.getName())) { && DataHelper.eq(getName(), rs.getName())) {

View File

@ -116,7 +116,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
if (!eq) if (!eq)
throw new I2NPMessageException("Hash does not match for " + getClass().getName()); throw new I2NPMessageException("Hash does not match for " + getClass().getName());
long start = _context.clock().now(); //long start = _context.clock().now();
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration); _log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration);
readMessage(buffer, 0, size, type); readMessage(buffer, 0, size, type);
@ -159,7 +159,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
if (!eq) if (!eq)
throw new I2NPMessageException("Hash does not match for " + getClass().getName()); throw new I2NPMessageException("Hash does not match for " + getClass().getName());
long start = _context.clock().now(); //long start = _context.clock().now();
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration); _log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration);
readMessage(data, cur, size, type); readMessage(data, cur, size, type);
@ -215,7 +215,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
} }
public int toByteArray(byte buffer[]) { public int toByteArray(byte buffer[]) {
long start = _context.clock().now(); //long start = _context.clock().now();
int prefixLen = 1 // type int prefixLen = 1 // type
+ 4 // uniqueId + 4 // uniqueId

View File

@ -160,7 +160,7 @@ public class TunnelDataMessage extends I2NPMessageImpl {
/** write the message body to the output array, starting at the given index */ /** write the message body to the output array, starting at the given index */
protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException { protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException {
if ( (_tunnelId <= 0) || (_data == null) ) if ( (_tunnelId <= 0) || (_data == null) )
throw new I2NPMessageException("Not enough data to write out (id=" + _tunnelId + " data=" + _data + ")"); throw new I2NPMessageException("Not enough data to write out (id=" + _tunnelId + ")");
if (_data.length <= 0) if (_data.length <= 0)
throw new I2NPMessageException("Not enough data to write out (data.length=" + _data.length + ")"); throw new I2NPMessageException("Not enough data to write out (data.length=" + _data.length + ")");