From 405fa50a4898123bfd51c1b8b1e0d94ee7fa51a5 Mon Sep 17 00:00:00 2001 From: "par.winzell" Date: Wed, 5 Nov 2008 17:16:59 +0000 Subject: [PATCH] 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 --- src/java/com/samskivert/util/Throttle.java | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/java/com/samskivert/util/Throttle.java b/src/java/com/samskivert/util/Throttle.java index 63feac70..e863cbfc 100644 --- a/src/java/com/samskivert/util/Throttle.java +++ b/src/java/com/samskivert/util/Throttle.java @@ -73,22 +73,25 @@ public class Throttle */ public void reinit (int operations, long period) { - long[] ops = new long[operations]; - if (operations > _ops.length) { - // copy to a larger buffer, leaving zeroes at the beginning - int lastOp = _lastOp + operations - _ops.length; - System.arraycopy(_ops, 0, ops, 0, _lastOp); - System.arraycopy(_ops, _lastOp, ops, lastOp, _ops.length - _lastOp); - - } else if (operations < _ops.length) { - // if we're truncating, copy the first (oldest) stamps into ops[0..] - int endCount = Math.min(operations, _ops.length - _lastOp); - System.arraycopy(_ops, _lastOp, ops, 0, endCount); - System.arraycopy(_ops, 0, ops, endCount, operations - endCount); - _lastOp = 0; - } - _ops = ops; _period = period; + if (operations != _ops.length) { + long[] ops = new long[operations]; + + if (operations > _ops.length) { + // copy to a larger buffer, leaving zeroes at the beginning + int lastOp = _lastOp + operations - _ops.length; + System.arraycopy(_ops, 0, ops, 0, _lastOp); + System.arraycopy(_ops, _lastOp, ops, lastOp, _ops.length - _lastOp); + + } else { + // if we're truncating, copy the first (oldest) stamps into ops[0..] + int endCount = Math.min(operations, _ops.length - _lastOp); + System.arraycopy(_ops, _lastOp, ops, 0, endCount); + System.arraycopy(_ops, 0, ops, endCount, operations - endCount); + _lastOp = 0; + } + _ops = ops; + } } /**