使用ByteBuffer buffer = ...
ShortBuffer shortBuffer = buffer.asShortBuffer();
short[] shortArray = new short[shortBuffer .remaining()];
shortBuffer.get(shortArray);
时,我的代码是这样的:
ByteBuf
现在使用netty 4,如何有效地从ByteBuf.nioBuffer()
获取短数组?
或者我只是使用ByteBuffer
先获得ByteBuf
?
而且,如何有效地将一个短数组放到Unpooled.buffer(...).nioBuffer().asShortBuffer().put(shortArray);
?我可以编写这样的代码:
private Animation animSlideUp;
animSlideUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
// set animation listener
animSlideUp.setAnimationListener(this);
animSlideUp.setDuration(500);
animSlideUp.setStartOffset(5000);
tickerView.startAnimation(animSlideUp);
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (animation == animSlideUp) {
ticker_counter++;
Log.e("onAnimationEnd=", "ticker_counter="+ticker_counter);
tickerView.startAnimation(animSlideUp);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0"/>
</set>
LOGCAT
11-19 17:06:54.375 E/onAnimationEnd=﹕ ticker_counter=1
11-19 17:06:54.392 E/onAnimationEnd=﹕ ticker_counter=2
11-19 17:06:59.912 E/onAnimationEnd=﹕ ticker_counter=3
11-19 17:06:59.928 E/onAnimationEnd=﹕ ticker_counter=4
11-19 17:07:05.453 E/onAnimationEnd=﹕ ticker_counter=5
11-19 17:07:05.470 E/onAnimationEnd=﹕ ticker_counter=6
11-19 17:07:10.991 E/onAnimationEnd=﹕ ticker_counter=7
11-19 17:07:11.008 E/onAnimationEnd=﹕ ticker_counter=8
答案 0 :(得分:2)
Netty没有很好的机制从Short[]
中提取ByteBuf
。您可以使用检测后端类型的复合解决方案,并使用不同的方法来处理此后端,然后再回到基础数组的简单复制。
NioBuffer
案例很简单,它有一个简单的get()
操作来读取生成的短数组。
直接和基于数组的情况更难,这些情况要求我们在循环中调用readShort()
,直到我们填充结果数组。
结果代码如下:
ByteBuf buf = ...;
short[] result;
if(buf.readableBytes() % 2 != 0) {
throw new IllegalArgumentException();
}
result = new short[buf.readableBytes() / 2];
if (buf.nioBufferCount() > 0 ){
buf.nioBuffer().asShortBuffer().get(result);
} else {
for(int i = 0; i < result.length; i++) {
result[i] = buf.readShort();
}
}