This commit is contained in:
zzz
2010-10-02 14:02:41 +00:00
parent 2bffeea7eb
commit 171e3abe34

View File

@@ -11,7 +11,7 @@ import java.util.Map;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.data.ByteArray; import net.i2p.data.ByteArray;
import net.i2p.util.ByteCache; //import net.i2p.util.ByteCache;
import net.i2p.util.Log; import net.i2p.util.Log;
/** /**
@@ -20,8 +20,8 @@ import net.i2p.util.Log;
* *
*/ */
public class MessageInputStream extends InputStream { public class MessageInputStream extends InputStream {
private I2PAppContext _context; private final I2PAppContext _context;
private Log _log; private final Log _log;
/** /**
* List of ByteArray objects of data ready to be read, * List of ByteArray objects of data ready to be read,
* with the first ByteArray at index 0, and the next * with the first ByteArray at index 0, and the next
@@ -29,7 +29,7 @@ public class MessageInputStream extends InputStream {
* that array. * that array.
* *
*/ */
private List _readyDataBlocks; private final List<ByteArray> _readyDataBlocks;
private int _readyDataBlockIndex; private int _readyDataBlockIndex;
/** highest message ID used in the readyDataBlocks */ /** highest message ID used in the readyDataBlocks */
private volatile long _highestReadyBlockId; private volatile long _highestReadyBlockId;
@@ -40,7 +40,7 @@ public class MessageInputStream extends InputStream {
* out of order when there are lower IDs not yet * out of order when there are lower IDs not yet
* received * received
*/ */
private Map _notYetReadyBlocks; private final Map<Long, ByteArray> _notYetReadyBlocks;
/** /**
* if we have received a flag saying there won't be later messages, EOF * if we have received a flag saying there won't be later messages, EOF
* after we have cleared what we have received. * after we have cleared what we have received.
@@ -51,9 +51,9 @@ public class MessageInputStream extends InputStream {
private int _readTimeout; private int _readTimeout;
private IOException _streamError; private IOException _streamError;
private long _readTotal; private long _readTotal;
private ByteCache _cache; //private ByteCache _cache;
private byte[] _oneByte = new byte[1]; private final byte[] _oneByte = new byte[1];
private final Object _dataLock; private final Object _dataLock;
@@ -61,16 +61,12 @@ public class MessageInputStream extends InputStream {
_context = ctx; _context = ctx;
_log = ctx.logManager().getLog(MessageInputStream.class); _log = ctx.logManager().getLog(MessageInputStream.class);
_readyDataBlocks = new ArrayList(4); _readyDataBlocks = new ArrayList(4);
_readyDataBlockIndex = 0;
_highestReadyBlockId = -1; _highestReadyBlockId = -1;
_highestBlockId = -1; _highestBlockId = -1;
_readTimeout = -1; _readTimeout = -1;
_readTotal = 0;
_notYetReadyBlocks = new HashMap(4); _notYetReadyBlocks = new HashMap(4);
_dataLock = new Object(); _dataLock = new Object();
_closeReceived = false; //_cache = ByteCache.getInstance(128, Packet.MAX_PAYLOAD_SIZE);
_locallyClosed = false;
_cache = ByteCache.getInstance(128, Packet.MAX_PAYLOAD_SIZE);
} }
/** What is the highest block ID we've completely received through? /** What is the highest block ID we've completely received through?
@@ -140,10 +136,8 @@ public class MessageInputStream extends InputStream {
if (num <= 0) return null; if (num <= 0) return null;
blocks = new long[num]; blocks = new long[num];
int i = 0; int i = 0;
for (Iterator iter = _notYetReadyBlocks.keySet().iterator(); iter.hasNext(); ) { for (Long id : _notYetReadyBlocks.keySet()) {
Long id = (Long)iter.next(); blocks[i++] = id.longValue();
blocks[i] = id.longValue();
i++;
} }
} }
Arrays.sort(blocks); Arrays.sort(blocks);
@@ -178,16 +172,15 @@ public class MessageInputStream extends InputStream {
buf.append("Close received, ready bytes: "); buf.append("Close received, ready bytes: ");
long available = 0; long available = 0;
for (int i = 0; i < _readyDataBlocks.size(); i++) for (int i = 0; i < _readyDataBlocks.size(); i++)
available += ((ByteArray)_readyDataBlocks.get(i)).getValid(); available += _readyDataBlocks.get(i).getValid();
available -= _readyDataBlockIndex; available -= _readyDataBlockIndex;
buf.append(available); buf.append(available);
buf.append(" blocks: ").append(_readyDataBlocks.size()); buf.append(" blocks: ").append(_readyDataBlocks.size());
buf.append(" not ready blocks: "); buf.append(" not ready blocks: ");
long notAvailable = 0; long notAvailable = 0;
for (Iterator iter = _notYetReadyBlocks.keySet().iterator(); iter.hasNext(); ) { for (Long id : _notYetReadyBlocks.keySet()) {
Long id = (Long)iter.next(); ByteArray ba = _notYetReadyBlocks.get(id);
ByteArray ba = (ByteArray)_notYetReadyBlocks.get(id);
buf.append(id).append(" "); buf.append(id).append(" ");
if (ba != null) if (ba != null)
@@ -237,7 +230,7 @@ public class MessageInputStream extends InputStream {
long cur = _highestReadyBlockId + 1; long cur = _highestReadyBlockId + 1;
// now pull in any previously pending blocks // now pull in any previously pending blocks
while (_notYetReadyBlocks.containsKey(new Long(cur))) { while (_notYetReadyBlocks.containsKey(new Long(cur))) {
ByteArray ba = (ByteArray)_notYetReadyBlocks.remove(new Long(cur)); ByteArray ba = _notYetReadyBlocks.remove(new Long(cur));
if ( (ba != null) && (ba.getData() != null) && (ba.getValid() > 0) ) { if ( (ba != null) && (ba.getData() != null) && (ba.getValid() > 0) ) {
_readyDataBlocks.add(ba); _readyDataBlocks.add(ba);
} }
@@ -341,7 +334,7 @@ public class MessageInputStream extends InputStream {
return i; return i;
} else { } else {
// either was already ready, or we wait()ed and it arrived // either was already ready, or we wait()ed and it arrived
ByteArray cur = (ByteArray)_readyDataBlocks.get(0); ByteArray cur = _readyDataBlocks.get(0);
byte rv = cur.getData()[cur.getOffset()+_readyDataBlockIndex]; byte rv = cur.getData()[cur.getOffset()+_readyDataBlockIndex];
_readyDataBlockIndex++; _readyDataBlockIndex++;
boolean removed = false; boolean removed = false;
@@ -378,7 +371,7 @@ public class MessageInputStream extends InputStream {
int numBytes = 0; int numBytes = 0;
synchronized (_dataLock) { synchronized (_dataLock) {
for (int i = 0; i < _readyDataBlocks.size(); i++) { for (int i = 0; i < _readyDataBlocks.size(); i++) {
ByteArray cur = (ByteArray)_readyDataBlocks.get(i); ByteArray cur = _readyDataBlocks.get(i);
if (i == 0) if (i == 0)
numBytes += cur.getValid() - _readyDataBlockIndex; numBytes += cur.getValid() - _readyDataBlockIndex;
else else
@@ -402,14 +395,13 @@ public class MessageInputStream extends InputStream {
if (_locallyClosed) return 0; if (_locallyClosed) return 0;
int numBytes = 0; int numBytes = 0;
for (int i = 0; i < _readyDataBlocks.size(); i++) { for (int i = 0; i < _readyDataBlocks.size(); i++) {
ByteArray cur = (ByteArray)_readyDataBlocks.get(i); ByteArray cur = _readyDataBlocks.get(i);
if (i == 0) if (i == 0)
numBytes += cur.getValid() - _readyDataBlockIndex; numBytes += cur.getValid() - _readyDataBlockIndex;
else else
numBytes += cur.getValid(); numBytes += cur.getValid();
} }
for (Iterator iter = _notYetReadyBlocks.values().iterator(); iter.hasNext(); ) { for (ByteArray cur : _notYetReadyBlocks.values()) {
ByteArray cur = (ByteArray)iter.next();
numBytes += cur.getValid(); numBytes += cur.getValid();
} }
return numBytes; return numBytes;
@@ -421,7 +413,7 @@ public class MessageInputStream extends InputStream {
if (_locallyClosed) return 0; if (_locallyClosed) return 0;
int numBytes = 0; int numBytes = 0;
for (int i = 0; i < _readyDataBlocks.size(); i++) { for (int i = 0; i < _readyDataBlocks.size(); i++) {
ByteArray cur = (ByteArray)_readyDataBlocks.get(i); ByteArray cur = _readyDataBlocks.get(i);
if (i == 0) if (i == 0)
numBytes += cur.getValid() - _readyDataBlockIndex; numBytes += cur.getValid() - _readyDataBlockIndex;
else else
@@ -440,8 +432,7 @@ public class MessageInputStream extends InputStream {
// we don't need the data, but we do need to keep track of the messageIds // we don't need the data, but we do need to keep track of the messageIds
// received, so we can ACK accordingly // received, so we can ACK accordingly
for (Iterator iter = _notYetReadyBlocks.values().iterator(); iter.hasNext(); ) { for (ByteArray ba : _notYetReadyBlocks.values()) {
ByteArray ba = (ByteArray)iter.next();
ba.setData(null); ba.setData(null);
//_cache.release(ba); //_cache.release(ba);
} }