- 浏览: 226077 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
forcer521:
非常好,收了!!
jquery控制表格奇偶行的颜色变化 及鼠标移动的高亮显示 -
fnet:
Ubuntu 真简单,但是我的服务器貌似装不上
ubuntu9.10 快速搭建简易的ftp服务器 -
rockman:
写的很详细。
ubuntu9.10 快速搭建简易的ftp服务器 -
明天的昨天:
dengli19881102 写道还有INDEX_CATEGO ...
TreePanel checkbox 联动 -
明天的昨天:
dengli19881102 写道if (node.paren ...
TreePanel checkbox 联动
学习资料来源: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()); } }
发表评论
-
java final域
2011-01-18 00:42 4039参考: http://liyuan2005.iteye.com ... -
java中 Class.getResource用法
2010-12-30 10:03 1143java中 Class.getResource用法 ... -
java NIO 学习笔记1
2010-12-15 15:52 1639jdon看到的一篇帖子。现 ... -
IO流读文件 中文乱码
2010-12-08 21:58 3502一段很简单读文件代码 public class Tes ... -
Common Errors in Setting Java Heap Size
2010-11-03 14:40 4416Two JVM options are often used ... -
java 正则表达式4
2010-10-15 14:18 816public static void main(String[ ... -
java 正则表达式 常见问题
2010-10-15 10:58 2180public static void main(String[ ... -
java 正则表达式3
2010-10-15 01:15 905问题: 匹配"联想 昭阳e280l"之中 ... -
java 正则表达式2
2010-10-15 00:15 782问题: public class Question { ... -
java 正则表达式1
2010-10-14 12:33 837public class Question { publi ... -
List.isEmpty
2010-09-21 10:17 3179jdk源码: /** * Returns ... -
getCanonicalName getSimpleName getName 区别与应用
2010-09-21 10:04 15676接口: package com.test; publi ... -
字节流转化为字符流
2010-09-03 16:32 1327字节流转化为字符流 FileInputStream f ... -
HttpClient4.0.1学习参考网页
2010-07-19 14:30 14921. HttpClient4.0.1应用指南 ... -
PrintWriter 中 write() 与 print() 的区别
2010-07-16 13:12 1483print方法提供了多种数据类型,而write都通过转换,大多 ... -
缺少jar包异常 汇总
2010-07-16 11:03 12721. java.lang.NoClassDefFou ... -
json-lib 实际使用中发生的错误
2010-07-11 14:19 31421. 使用json-lib时,完全相同的代码,在一个项目中可以 ... -
Calendar 确定日期是一周中的第几天
2010-06-13 07:59 3692public static void main(String[ ... -
JDK JRE Path CLASSPATH
2010-04-03 20:30 19301. JDK 就是J ...
相关推荐
java NIO的基本知识点学习笔记,不包含具体代码
javaNIO学习笔记(csdn)————程序
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...
Java NIO系列教程(一) Java NIO 概述 Java NIO系列教程(二) Channel Java NIO系列教程(三) Buffer Java NIO系列教程(四) Scatter/Gather Java NIO系列教程(五) 通道之间的数据传输 Java NIO系列教程(六)...
java学习笔记1(java io/nio)设计模式
NULL 博文链接:https://zheng12tian.iteye.com/blog/1094811
Java NIO: Channels and Buffers(通道和缓冲区) 标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(Buffer)进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。 ...
IO 是主存和外部设备 ( 硬盘、终端和网络等 ) 拷贝数据的过程。 IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成。 所有语言运行时系统提供执行 I/O 较高级别的... (c 的 printf scanf,java 的面向对象封装 )
设计思想: 每次通过nio读取字节到 fbb中 然后对fbb自己中的内容进行行判断即 10 回车 13 行号 0 文件结束 这样字节的判断,然后 返回行 如果 到达 fbb的结尾 还没有结束,就再通过nio读取一段字节,继续处理。 ...
Java NIO英文高清原版
Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据...
Java NIO学习资料+代码.zip
Nio学习笔记
Java NIO 深入探讨了 1.4 版的 I/O 新特性,并告诉您如何使用这些特性来极大地提升您所写的 Java 代码的执行效率。这本小册子就程序员所面临的有代表性的 I/O 问题作了详尽阐述,并讲解了 如何才能充分利用新的 I/O ...
java NIO.zip
讲解了 JavaIO 与 JAVA NIO区别,JAVA NIO设计理念,以及JDK中java NIO中语法的使用
java_nio学习文档
java nio 实现socketjava nio 实现socketjava nio 实现socketjava nio 实现socketjava nio 实现socket
java nio入门学习,两个pdfjava nio入门学习,两个pdf