Java中的ByteBuffer asLongBuffer()方法
可以使用asLongBuffer()
类java.nio.ByteBuffer中的方法将ByteBuffer的视图创建为LongBuffer。此方法不需要任何参数,并且根据需要返回一个长缓冲区。该缓冲区反映了对原始缓冲区所做的更改,反之亦然。
演示此的程序如下所示-
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 50; try { ByteBuffer bufferB = ByteBuffer.allocate(n); LongBuffer bufferL = bufferB.asLongBuffer(); bufferL.put(5000); bufferL.put(9000); bufferL.put(2000); bufferL.put(8000); bufferL.put(3000); bufferL.rewind(); long l; System.out.print("The LongBuffer is: "); while ((l = bufferL.get()) != 0) { System.out.print(l + " "); } } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
输出结果
The LongBuffer is: 5000 9000 2000 8000 3000