java ByteBuffer
来源:互联网 发布:信捷plc编程手册 编辑:程序博客网 时间:2024/05/20 04:28
import java.nio.ByteBuffer; import java.nio.ByteOrder; public class bytebuffertest { public static void main(String[] args) { // Create a ByteBuffer using a byte array byte[] bytes = new byte[10]; ByteBuffer buf = ByteBuffer.wrap(bytes); // Create a non-direct ByteBuffer with a 10 byte capacity // The underlying storage is a byte array. buf = ByteBuffer.allocate(10); // Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity. buf = ByteBuffer.allocateDirect(10); // Create an empty ByteBuffer with a 10 byte capacity ByteBuffer bbuf = ByteBuffer.allocate(10); // Get the ByteBuffer's capacity int capacity = bbuf.capacity(); // 10 System.out.println(capacity); // Use the absolute get(). // This method does not affect the position. byte b = bbuf.get(5); // position=0 // Set the position bbuf.position(5); // Use the relative get() b = bbuf.get(); // Get the new position int pos = bbuf.position(); // 6 // Get remaining byte count int rem = bbuf.remaining(); // 4 // Set the limit bbuf.limit(7); // remaining=1 // This convenience method sets the position to 0 bbuf.rewind(); // remaining=7 //Converting Between a ByteBuffer an a Byte Array // Create a ByteBuffer from a byte array byte[] bytes1 = new byte[10]; ByteBuffer buf1 = ByteBuffer.wrap(bytes); // Retrieve bytes between the position and limit // (see Putting Bytes into a ByteBuffer) bytes1 = new byte[buf.remaining()]; buf1.get(bytes1, 0, bytes1.length); // Retrieve all bytes in the buffer buf1.clear(); bytes1 = new byte[buf1.capacity()]; buf1.get(bytes1, 0, bytes1.length); // Use the absolute put(). // This method does not affect the position. bbuf.put((byte)0xFF); // position=0 // Set the position bbuf.position(5); // Use the relative put() bbuf.put((byte)0xFF); // Get the new position int pos1 = bbuf.position(); // 6 //Setting the Byte Ordering for a ByteBuffer // Get default byte ordering ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN // Put a multibyte value buf.putShort(0, (short)123); buf.get(0); // 0 buf.get(1); // 123 // Set to little endian buf.order(ByteOrder.LITTLE_ENDIAN); // Put a multibyte value buf.putShort(0, (short)123); buf.get(0); // 123 buf.get(1); // 0 } }这是一篇转载的博客我还没有来得及进行验证,放在这里只是为了以后自己方便查看,如有错误的地方还请以评论的方式告知,我会进行改正。
0 0
- java bytebuffer
- java ByteBuffer
- java ByteBuffer
- java byteBuffer 的学习
- java.nio.ByteBuffer 类
- java.nio.ByteBuffer 详解
- java.nio.ByteBuffer类
- Java ByteBuffer用法总结
- java.nio.ByteBuffer 详解 .
- Java ByteBuffer用法
- java bytebuffer 测试
- Java nio ByteBuffer用法
- Java ByteBuffer用法总结
- java中的ByteBuffer
- Java ByteBuffer 使用实例
- Java NIO ByteBuffer图解
- Java 学习系列:ByteBuffer
- Java之ByteBuffer详解
- 自行编译CodeBlocks 13.12(比12.11方便了太多了)
- Mongodb启动命令mongod参数说明
- UVa:10127 Ones
- LeetCode 之 Reorder List
- Linux后台进程管理
- java ByteBuffer
- 【Hadoop技术博客推荐】站在风口的Hadoop技术研究者
- linux普通用户获得root权限:xxx is not in the sudoers file. This incident will be reported.
- 2014趋势预测,大数据将成主流
- 黑马程序员_javascript学习心得
- Effective Objective-C 2.0: Item 47: Familiarize Yourself with the System Frameworks
- 其它内置变量(4)
- 入门HTML之有序列表
- openoffice.org3.4 源代码 编译