java NIO 学习笔记2 Byte Buffers



1. Converting Between a ByteBuffer an a Byte Array



import java.nio.ByteBuffer;

public class ConvertByteBufferByteArray {
	public static void main(String[] args) {
		byte[] bytes=new byte[10];
		ByteBuffer buf=ByteBuffer.wrap(bytes);
		//return the number of elements between the current position and the limit.
		// 检索/取回 bytes between the position and limit。
		bytes=new byte[buf.remaining()];
		buf.get(bytes, 0, bytes.length);
		//debug模式下可以看到,经过上面的buf.get(bytes, 0, bytes.length);
		// 检索/取回 all bytes in the buffer
		bytes=new byte[buf.capacity()];
		buf.get(bytes, 0, bytes.length); 


2. Creating a ByteBuffer

A ByteBuffer is a fixed-capacity buffer that holds byte values


import java.nio.ByteBuffer;

public class CreateByteBuffer {
	public static void main(String[] args) {
		byte[] bytes=new byte[10];
		ByteBuffer buf=ByteBuffer.wrap(bytes);
		//Create a non-direct ByteBuffer with a 10 byte capacity 
		// Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity. 
		buf = ByteBuffer.allocateDirect(10); 
		// To create a ByteBuffer for a memory-mapped file

>Non-direct ByteBuffer

    (1) HeapByteBuffer, 标准的java类。

    (2) 维护一份byte[]在JVM堆上。

    (3) 创建开销小

>Direct ByteBuffer

    (1) 底层存储在非JVM堆上,通过native代码操作。

    (2) -XX:MaxDirectMemorySize=<size>

    (3) 创建开销大


3. Creating a Non-Byte Java Type Buffer on a ByteBuffer

You can create views on a ByteBuffer to support buffers of other Java primitive types. For example, by creating a character view on a ByteBuffer , you treat the ByteBuffer like a buffer of characters. The character buffer supports strings directly. Also, hasRemaining() properly works with characters rather than with bytes.

When you create a typed view, it is important to be aware that it is created on top of the bytes between position and limit. That is, the capacity of the new view is (limit – position). The limit of the new view may be reduced so that the capacity is an integral value based on the size of the type. Finally, the view shares the same storage as the underlying ByteBuffer , so any changes to the byte buffer will be seen by the view and visa versa. However, changes to a view's position or limit do not affect the ByteBuffer 's properties and visa versa.


import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class CreateNonByteJavaTypeBuffer {
	public static void main(String[] args) {
		// Obtain a ByteBuffer; see also Creating a ByteBuffer 
		ByteBuffer buf = ByteBuffer.allocate(15); // remaining = 15 
		// Create a character ByteBuffer 
		CharBuffer cbuf = buf.asCharBuffer(); // remaining = 7 
		// Create a short ByteBuffer 
		ShortBuffer sbuf = buf.asShortBuffer(); // remaining = 7 
		// Create an integer ByteBuffer 
		IntBuffer ibuf = buf.asIntBuffer(); // remaining = 3 
		// Create a long ByteBuffer 
		LongBuffer lbuf = buf.asLongBuffer(); // remaining = 1 
		// Create a float ByteBuffer 
		FloatBuffer fbuf = buf.asFloatBuffer(); // remaining = 3 
		// Create a double ByteBuffer 
		DoubleBuffer dbuf = buf.asDoubleBuffer(); // remaining = 1 








发布了56 篇原创文章 · 获赞 3 · 访问量 3612


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客