`

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());
	}
}

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    javaNIO学习笔记

    java NIO的基本知识点学习笔记,不包含具体代码

    javaNIO学习笔记(csdn)————程序.pdf

    javaNIO学习笔记(csdn)————程序

    JAVA NIO 学习资料

    JAVA NIO学习资料JAVA NIO学习资料

    java NIO和java并发编程的书籍

    java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java...

    JavaNIO chm帮助文档

    Java NIO系列教程(一) Java NIO 概述 Java NIO系列教程(二) Channel Java NIO系列教程(三) Buffer Java NIO系列教程(四) Scatter/Gather Java NIO系列教程(五) 通道之间的数据传输 Java NIO系列教程(六)...

    java学习笔记1(java io/nio)

    java学习笔记1(java io/nio)设计模式

    Java NIO学习笔记——ByteBuffer用法

    NULL 博文链接:https://zheng12tian.iteye.com/blog/1094811

    java NIO 视频教程

    Java NIO: Channels and Buffers(通道和缓冲区) 标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(Buffer)进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。 ...

    JAVA NIO学习笔记.docx

    IO 是主存和外部设备 ( 硬盘、终端和网络等 ) 拷贝数据的过程。 IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成。 所有语言运行时系统提供执行 I/O 较高级别的... (c 的 printf scanf,java 的面向对象封装 )

    JAVA NIO 按行读取大文件支持 GB级别-修正版

    设计思想: 每次通过nio读取字节到 fbb中 然后对fbb自己中的内容进行行判断即 10 回车 13 行号 0 文件结束 这样字节的判断,然后 返回行 如果 到达 fbb的结尾 还没有结束,就再通过nio读取一段字节,继续处理。 ...

    java nio 包读取超大数据文件

    Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据...

    Java NIO英文高清原版

    Java NIO英文高清原版

    Java NIO学习资料+代码.zip

    Java NIO学习资料+代码.zip

    Nio学习笔记(java)

    Nio学习笔记

    Java NIO 中文 Java NIO 中文 Java NIO 中文文档

    Java NIO 深入探讨了 1.4 版的 I/O 新特性,并告诉您如何使用这些特性来极大地提升您所写的 Java 代码的执行效率。这本小册子就程序员所面临的有代表性的 I/O 问题作了详尽阐述,并讲解了 如何才能充分利用新的 I/O ...

    java NIO 中文版

    讲解了 JavaIO 与 JAVA NIO区别,JAVA NIO设计理念,以及JDK中java NIO中语法的使用

    java_nio学习文档

    java_nio学习文档

    java nio 实现socket

    java nio 实现socketjava nio 实现socketjava nio 实现socketjava nio 实现socketjava nio 实现socket

    java nio入门学习,两个pdf

    java nio入门学习,两个pdfjava nio入门学习,两个pdf

    java nio中文版

    java NIO是 java New IO 的简称,在 jdk1.4 里提供的新 api 。 Sun 官方标榜的特性如下: – 为所有的原始类型提供 (Buffer) 缓存支持。 – 字符集编码解码解决方案。 – Channel :一个新的原始 I/O 抽象。 – 支持...

Global site tag (gtag.js) - Google Analytics