我想通过以下方式使用 java nio ByteBuffer 的 put 方法:
ByteBuffer small = ByteBuffer.allocate(SMALL_AMOUNT_OF_BYTES);
ByteBuffer big = getAllData();
while (big.hasRemaining()){
small.put(big);
send(small);
}
put 方法会抛出缓冲区溢出异常,所以我修复它的方法是:
ByteBuffer small = ByteBuffer.allocate(SMALL_AMOUNT_OF_BYTES);
ByteBuffer big = getAllData();
while (big.hasRemaining()){
while (small.hasRemaining() && big.hasRemaining()){
small.put(big.get());
}
send(small);
}
我的问题是 - 有没有更好的方法来做到这一点,或者至少有一种有效的方法来做我想做的事?