Don't fill the array with zeroes when we reinitialize the Throttle with the same number of operations as we already had, as that allows a potentially dangerous burst of operations.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2471 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
par.winzell
2008-11-05 17:16:59 +00:00
parent 0415bf7502
commit 405fa50a48
+5 -2
View File
@@ -73,14 +73,17 @@ public class Throttle
*/ */
public void reinit (int operations, long period) public void reinit (int operations, long period)
{ {
_period = period;
if (operations != _ops.length) {
long[] ops = new long[operations]; long[] ops = new long[operations];
if (operations > _ops.length) { if (operations > _ops.length) {
// copy to a larger buffer, leaving zeroes at the beginning // copy to a larger buffer, leaving zeroes at the beginning
int lastOp = _lastOp + operations - _ops.length; int lastOp = _lastOp + operations - _ops.length;
System.arraycopy(_ops, 0, ops, 0, _lastOp); System.arraycopy(_ops, 0, ops, 0, _lastOp);
System.arraycopy(_ops, _lastOp, ops, lastOp, _ops.length - _lastOp); System.arraycopy(_ops, _lastOp, ops, lastOp, _ops.length - _lastOp);
} else if (operations < _ops.length) { } else {
// if we're truncating, copy the first (oldest) stamps into ops[0..] // if we're truncating, copy the first (oldest) stamps into ops[0..]
int endCount = Math.min(operations, _ops.length - _lastOp); int endCount = Math.min(operations, _ops.length - _lastOp);
System.arraycopy(_ops, _lastOp, ops, 0, endCount); System.arraycopy(_ops, _lastOp, ops, 0, endCount);
@@ -88,7 +91,7 @@ public class Throttle
_lastOp = 0; _lastOp = 0;
} }
_ops = ops; _ops = ops;
_period = period; }
} }
/** /**