下面列出了com.google.common.primitives.Shorts#BYTES 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public AmplitudeData(byte[] rawData) {
if (rawData == null) {
amplitude = 0;
return;
}
int step = rawData.length / Shorts.BYTES / sampleCount;
int count = 0;
double sum = 0;
for (int i = 0;
i < rawData.length - Shorts.BYTES;
i += step) {
byte[] bytes = new byte[Shorts.BYTES];
for (int j = 0;
j < Shorts.BYTES;
j++) {
bytes[j] = rawData[i + j];
}
short s = Shorts.fromByteArray(bytes);
sum += s * s;
count++;
}
amplitude = (int) Math.sqrt(sum / count);
}
protected static int getValueSplit(MemcachedCacheConfig config, String keyS, int valueBLen) {
// the number 6 means the chunk number size never exceeds 6 bytes
final int valueSize = config.getMaxObjectSize() - Shorts.BYTES - Ints.BYTES
- keyS.getBytes(Charsets.UTF_8).length - 6;
final int maxValueSize = config.getMaxChunkSize() * valueSize;
Preconditions.checkArgument(valueBLen <= maxValueSize,
"the value bytes length [%d] exceeds maximum value size [%d]", valueBLen, maxValueSize);
return (valueBLen - 1) / valueSize + 1;
}
private static Object convertByteArrayValue(final Object value, final Class<?> convertType) {
byte[] bytesValue = (byte[]) value;
switch (bytesValue.length) {
case 1:
return convertNumberValue(bytesValue[0], convertType);
case Shorts.BYTES:
return convertNumberValue(Shorts.fromByteArray(bytesValue), convertType);
case Ints.BYTES:
return convertNumberValue(Ints.fromByteArray(bytesValue), convertType);
case Longs.BYTES:
return convertNumberValue(Longs.fromByteArray(bytesValue), convertType);
default:
return value;
}
}
public static byte[] writeShort(short num) {
byte[] output = new byte[Shorts.BYTES];
writeShort(num, output, 0, output.length);
return output;
}