java NIO 学习笔记2 Byte Buffers

学习资料来源:http://www.exampledepot.com/egs/java.nio/pkg.html

 

1. Converting Between a ByteBuffer an a Byte Array

ByteBuffer与Byte数组的相互转化。

 

import java.nio.ByteBuffer;

public class ConvertByteBufferByteArray {
	public static void main(String[] args) {
		//由Byte数组创建ByteBuffer
		byte[] bytes=new byte[10];
		ByteBuffer buf=ByteBuffer.wrap(bytes);
		
		//remaining()方法
		//return the number of elements between the current position and the limit.
		System.out.println(buf.remaining());
		
		// 检索/取回 bytes between the position and limit。
		bytes=new byte[buf.remaining()];
		buf.get(bytes, 0, bytes.length);
		//debug模式下可以看到,经过上面的buf.get(bytes, 0, bytes.length);
		//buf的position变为10。所以后面需要buf.clear()方法。
		
		// 检索/取回 all bytes in the buffer
		buf.clear();
		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数组创建ByteBuffer
		byte[] bytes=new byte[10];
		ByteBuffer buf=ByteBuffer.wrap(bytes);
		
		//Create a non-direct ByteBuffer with a 10 byte capacity 
		//底层存储的是一个字节数组 
		buf=ByteBuffer.allocate(10);
		
		// 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 
		System.out.println(buf.remaining());
		
		// Create a character ByteBuffer 
		CharBuffer cbuf = buf.asCharBuffer(); // remaining = 7 
		System.out.println(cbuf.remaining());
		
		// Create a short ByteBuffer 
		ShortBuffer sbuf = buf.asShortBuffer(); // remaining = 7 
		System.out.println(sbuf.remaining());
		
		// Create an integer ByteBuffer 
		IntBuffer ibuf = buf.asIntBuffer(); // remaining = 3 
		System.out.println(ibuf.remaining());
		
		// Create a long ByteBuffer 
		LongBuffer lbuf = buf.asLongBuffer(); // remaining = 1 
		System.out.println(lbuf.remaining());
		
		// Create a float ByteBuffer 
		FloatBuffer fbuf = buf.asFloatBuffer(); // remaining = 3 
		System.out.println(fbuf.remaining());
		
		// Create a double ByteBuffer 
		DoubleBuffer dbuf = buf.asDoubleBuffer(); // remaining = 1 
		System.out.println(dbuf.remaining());
	}
}

 

 

 

 

 

 

 

发布了56 篇原创文章 · 获赞 3 · 访问量 3612
展开阅读全文

没有更多推荐了,返回首页

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

分享到微信朋友圈

×

扫一扫,手机浏览