1 介紹
/**
* The {@link RecvByteBufAllocator} that automatically increases and
* decreases the predicted buffer size on feed back.
* <p>
* It gradually increases the expected number of readable bytes if the previous
* read fully filled the allocated buffer. It gradually decreases the expected
* number of readable bytes if the read operation was not able to fill a certain
* amount of the allocated buffer two times consecutively. Otherwise, it keeps
* returning the same prediction.
*/
用來自動增加或減少可預(yù)測的buffer的大小。
當(dāng)上一次read操作填滿了所有的可分配的buffer空間,就會按照期望的增加可讀字節(jié)數(shù);當(dāng)之前連續(xù)的兩次read操作都不能填充一點數(shù)量的內(nèi)容,就會對減少可讀的字節(jié)數(shù)目。
2 屬性說明
//最小值
static final int DEFAULT_MINIMUM = 64;
//默認(rèn)值
static final int DEFAULT_INITIAL = 1024;
//最大值
static final int DEFAULT_MAXIMUM = 65536;
//數(shù)組索引增加的步長
private static final int INDEX_INCREMENT = 4;
//數(shù)組索引減少的步長
private static final int INDEX_DECREMENT = 1;
//存放所有大小值的數(shù)組
private static final int[] SIZE_TABLE;
3 初始化SIZE_TABLE數(shù)組
static {
List<Integer> sizeTable = new ArrayList<Integer>();
for (int i = 16; i < 512; i += 16) {
sizeTable.add(i);
}
for (int i = 512; i > 0; i <<= 1) {
sizeTable.add(i);
}
SIZE_TABLE = new int[sizeTable.size()];
for (int i = 0; i < SIZE_TABLE.length; i ++) {
SIZE_TABLE[i] = sizeTable.get(i);
}
}
SIZE_TABLE可以分為兩部分值,前31個元素從16開始,以16的步長遞增;其余的元素從512開始每次擴大一倍,知道Intger的最大值。
4 getSizeTableIndex方法
private static int getSizeTableIndex(final int size) {
for (int low = 0, high = SIZE_TABLE.length - 1;;) {
if (high < low) {
return low;
}
if (high == low) {
return high;
}
int mid = low + high >>> 1;
int a = SIZE_TABLE[mid];
int b = SIZE_TABLE[mid + 1];
if (size > b) {
low = mid + 1;
} else if (size < a) {
high = mid - 1;
} else if (size == a) {
return mid;
} else {
return mid + 1;
}
}
}