diff --git a/core/java/src/net/i2p/I2PAppContext.java b/core/java/src/net/i2p/I2PAppContext.java index e235511aa0ad3135b66a24998886723fc57d5d5d..e5ec21c753c50471a3a269771355f045a14aea90 100644 --- a/core/java/src/net/i2p/I2PAppContext.java +++ b/core/java/src/net/i2p/I2PAppContext.java @@ -63,7 +63,7 @@ import net.i2p.util.SecureDirectory; */ public class I2PAppContext { /** the context that components without explicit root are bound */ - protected static I2PAppContext _globalAppContext; + protected static volatile I2PAppContext _globalAppContext; private Properties _overrideProps; @@ -117,7 +117,8 @@ public class I2PAppContext { * */ 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; if (rv != null) return rv; @@ -474,6 +475,9 @@ public class I2PAppContext { * provided during the context construction, as well as the ones included in * 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 */ public Set getPropertyNames() { diff --git a/core/java/src/net/i2p/client/I2PSessionImpl.java b/core/java/src/net/i2p/client/I2PSessionImpl.java index 10e7695c04e5560e4a9b40ba8ea77e6e80e881b4..b904b121d805925e985c782d4076d8f6e7b07a77 100644 --- a/core/java/src/net/i2p/client/I2PSessionImpl.java +++ b/core/java/src/net/i2p/client/I2PSessionImpl.java @@ -421,7 +421,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa * */ public byte[] receiveMessage(int msgId) throws I2PSessionException { - MessagePayloadMessage msg = _availableMessages.remove(new Long(msgId)); + MessagePayloadMessage msg = _availableMessages.remove(Long.valueOf(msgId)); if (msg == null) { _log.error("Receive message " + msgId + " had no matches"); return null; @@ -468,7 +468,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa * Recieve a payload message and let the app know its available */ public void addNewMessage(MessagePayloadMessage msg) { - Long mid = new Long(msg.getMessageId()); + Long mid = Long.valueOf(msg.getMessageId()); _availableMessages.put(mid, msg); long id = msg.getMessageId(); byte data[] = msg.getPayload().getUnencryptedData(); @@ -518,7 +518,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa public void available(long msgId, int size) { synchronized (AvailabilityNotifier.this) { - _pendingIds.add(new Long(msgId)); + _pendingIds.add(Long.valueOf(msgId)); _pendingSizes.add(Integer.valueOf(size)); AvailabilityNotifier.this.notifyAll(); } diff --git a/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java b/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java index 837bc2dbc2de07b7e8567a80a429a14244cc5413..2f12f96f64fa99018de6f4775a2e17d72a48cd2c 100644 --- a/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java +++ b/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java @@ -191,7 +191,7 @@ class I2PSessionMuxedImpl extends I2PSessionImpl2 implements I2PSession { */ @Override public void addNewMessage(MessagePayloadMessage msg) { - Long mid = new Long(msg.getMessageId()); + Long mid = Long.valueOf(msg.getMessageId()); _availableMessages.put(mid, msg); long id = msg.getMessageId(); byte data[] = msg.getPayload().getUnencryptedData(); diff --git a/core/java/src/net/i2p/client/I2PSimpleSession.java b/core/java/src/net/i2p/client/I2PSimpleSession.java index 1c564f0db4654b69ab35ab70a4384a5633ad0ab2..e108fc008ba0e577d142f40fd0ca127adec1360d 100644 --- a/core/java/src/net/i2p/client/I2PSimpleSession.java +++ b/core/java/src/net/i2p/client/I2PSimpleSession.java @@ -108,7 +108,7 @@ class I2PSimpleSession extends I2PSessionImpl2 { /** * Only map message handlers that we will use */ - class SimpleMessageHandlerMap extends I2PClientMessageHandlerMap { + private static class SimpleMessageHandlerMap extends I2PClientMessageHandlerMap { public SimpleMessageHandlerMap(I2PAppContext context) { int highest = Math.max(DestReplyMessage.MESSAGE_TYPE, BandwidthLimitsMessage.MESSAGE_TYPE); _handlers = new I2CPMessageHandler[highest+1]; diff --git a/core/java/src/net/i2p/crypto/CryptixAESEngine.java b/core/java/src/net/i2p/crypto/CryptixAESEngine.java index ea2338003ce4c9f80209a7f642f54405e440466c..350821e35558fe6324db6cbc538ce02ad53f6174 100644 --- a/core/java/src/net/i2p/crypto/CryptixAESEngine.java +++ b/core/java/src/net/i2p/crypto/CryptixAESEngine.java @@ -149,7 +149,7 @@ public class CryptixAESEngine extends AESEngine { @Override public final void decryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte rv[], int outIndex) { 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) throw new IllegalArgumentException("bad block args [payload.len=" + payload.length + " inIndex=" + inIndex + " rv.len=" + rv.length diff --git a/core/java/src/net/i2p/data/Base32.java b/core/java/src/net/i2p/data/Base32.java index 3071e0bc013ca784b8264c37589fa04809a38c3e..cbcb93515f9f8dcb90e3eaa73fdf1da862c706f5 100644 --- a/core/java/src/net/i2p/data/Base32.java +++ b/core/java/src/net/i2p/data/Base32.java @@ -72,13 +72,13 @@ public class Base32 { } 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 { - 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) { out = new FileOutputStream(args[2]); } @@ -95,6 +95,9 @@ public class Base32 { } } catch (IOException ioe) { ioe.printStackTrace(System.err); + } finally { + try { in.close(); } catch (IOException e) {} + try { out.close(); } catch (IOException e) {} } } diff --git a/core/java/src/net/i2p/data/Base64.java b/core/java/src/net/i2p/data/Base64.java index fe730af6b4a0349e527f7347d32e094c16aeaa6c..52d59002d8ef654bff0b9314b9d48b348895e0b7 100644 --- a/core/java/src/net/i2p/data/Base64.java +++ b/core/java/src/net/i2p/data/Base64.java @@ -178,13 +178,13 @@ public class Base64 { } 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 { - 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) { out = new FileOutputStream(args[2]); } @@ -201,6 +201,9 @@ public class Base64 { } } catch (IOException ioe) { ioe.printStackTrace(System.err); + } finally { + try { in.close(); } catch (IOException e) {} + try { out.close(); } catch (IOException e) {} } } diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index bd6bf260d9c5734186e6ae50f91b15274d52ed3a..33264edf2bec16fdb63108b77ce7a9200d3bf795 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -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) { 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) throw new IllegalArgumentException("Left hand side is too short"); if (rhs.length < startRight + len) diff --git a/core/java/src/net/i2p/data/PrivateKeyFile.java b/core/java/src/net/i2p/data/PrivateKeyFile.java index 0c752f9d6a22c0eed86d5aeb97d99081997f8eb1..4f4af3cf9ae51a582bce40f0a7a0cb6c7a71c390 100644 --- a/core/java/src/net/i2p/data/PrivateKeyFile.java +++ b/core/java/src/net/i2p/data/PrivateKeyFile.java @@ -133,9 +133,15 @@ public class PrivateKeyFile { */ public Destination createIfAbsent() throws I2PException, IOException, DataFormatException { if(!this.file.exists()) { - FileOutputStream out = new FileOutputStream(this.file); - this.client.createDestination(out); - out.close(); + FileOutputStream out = null; + try { + out = new FileOutputStream(this.file); + this.client.createDestination(out); + } finally { + if (out != null) { + try { out.close(); } catch (IOException ioe) {} + } + } } return getDestination(); } @@ -243,29 +249,36 @@ public class PrivateKeyFile { public I2PSession open() throws I2PSessionException, IOException { return this.open(new Properties()); } + public I2PSession open(Properties opts) throws I2PSessionException, IOException { - // open input file - FileInputStream in = new FileInputStream(this.file); - - // create sesssion - I2PSession s = this.client.createSession(in, opts); - - // close file - in.close(); - - return s; + FileInputStream in = null; + try { + in = new FileInputStream(this.file); + I2PSession s = this.client.createSession(in, opts); + return s; + } finally { + if (in != null) { + try { in.close(); } catch (IOException ioe) {} + } + } } /** * Copied from I2PClientImpl.createDestination() */ public void write() throws IOException, DataFormatException { - FileOutputStream out = new FileOutputStream(this.file); - this.dest.writeBytes(out); - this.privKey.writeBytes(out); - this.signingPrivKey.writeBytes(out); - out.flush(); - out.close(); + FileOutputStream out = null; + try { + out = new FileOutputStream(this.file); + this.dest.writeBytes(out); + this.privKey.writeBytes(out); + this.signingPrivKey.writeBytes(out); + out.flush(); + } finally { + if (out != null) { + try { out.close(); } catch (IOException ioe) {} + } + } } @Override @@ -377,7 +390,8 @@ public class PrivateKeyFile { } } } - } catch (Exception ioe) { + } catch (DataFormatException dfe) { + } catch (IOException ioe) { } // not found, continue to the next file } diff --git a/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java b/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java index 7aaba9c8923a83e70f33e71b4e91073bf4ae48eb..67e1351059e9570958bd5c04a17f4339a36f8984 100644 --- a/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java +++ b/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java @@ -76,10 +76,11 @@ public class DestReplyMessage extends I2CPMessageImpl { } protected byte[] doWriteMessage() throws I2CPMessageException, IOException { - if (_dest == null && _hash == null) - return new byte[0]; // null response allowed - if (_dest == null && _hash != null) + if (_dest == null) { + if (_hash == null) + return new byte[0]; // null response allowed return _hash.getData(); + } ByteArrayOutputStream os = new ByteArrayOutputStream(_dest.size()); try { _dest.writeBytes(os); diff --git a/core/java/src/net/i2p/stat/FrequencyStat.java b/core/java/src/net/i2p/stat/FrequencyStat.java index dfa7bcfe675298c564d2169f4d8d95372c19e4ad..d18a469dbd3c921a640cf7e06d3141db968747be 100644 --- a/core/java/src/net/i2p/stat/FrequencyStat.java +++ b/core/java/src/net/i2p/stat/FrequencyStat.java @@ -89,7 +89,7 @@ public class FrequencyStat { /** @since 0.8.2 */ @Override 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); } diff --git a/core/java/src/net/i2p/stat/Rate.java b/core/java/src/net/i2p/stat/Rate.java index fd0fae49f00d197c575c866c87cc21aca98bfdd2..8473d58ebbbfdcf2a2ca68e67980036a5db28a4f 100644 --- a/core/java/src/net/i2p/stat/Rate.java +++ b/core/java/src/net/i2p/stat/Rate.java @@ -473,7 +473,7 @@ public class Rate { @Override 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; Rate r = (Rate) obj; return _period == r.getPeriod() && _creationDate == r.getCreationDate() && diff --git a/core/java/src/net/i2p/stat/RateStat.java b/core/java/src/net/i2p/stat/RateStat.java index 79ddec51980efdcf689f9f95b4cade1f25223d18..fded9b019f4665dd15dc15214f75d67bdf8c0618 100644 --- a/core/java/src/net/i2p/stat/RateStat.java +++ b/core/java/src/net/i2p/stat/RateStat.java @@ -108,7 +108,7 @@ public class RateStat { @Override 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; if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription()) && DataHelper.eq(getName(), rs.getName())) { diff --git a/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java b/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java index 6b4ab71f4048065751cea8174b3d6d164b25ceee..021bbf6c17ceaa4ce50c7abcd40ee583a73f0b31 100644 --- a/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java +++ b/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java @@ -116,7 +116,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM if (!eq) 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)) _log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration); readMessage(buffer, 0, size, type); @@ -159,7 +159,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM if (!eq) 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)) _log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration); readMessage(data, cur, size, type); @@ -215,7 +215,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM } public int toByteArray(byte buffer[]) { - long start = _context.clock().now(); + //long start = _context.clock().now(); int prefixLen = 1 // type + 4 // uniqueId diff --git a/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java b/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java index 1ac01d0a6c87772a14604d72716473522ef07d2e..ef4930594f0f0491d17e18341dfb6803370b8d72 100644 --- a/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java +++ b/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java @@ -160,7 +160,7 @@ public class TunnelDataMessage extends I2NPMessageImpl { /** write the message body to the output array, starting at the given index */ protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException { 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) throw new I2NPMessageException("Not enough data to write out (data.length=" + _data.length + ")");