`
suiyuan0808
  • 浏览: 152541 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java nio重温

    博客分类:
  • Java
阅读更多

Java NIO是一种new IO操作API,比原来的IO更高效执行IO操作,比如在磁盘文件读写,TCP/IP网络通讯,内存文件读写等,都是十分高效的。在网上找了一些国人些的资料都是比较简单的例子,都没有比较深入详细介绍其内部机制。没有办法只能自己动手,丰衣足食,看看外国佬写的书<JAVA NIO Programming>。下面几段是比较详细NIO Buffer内部机制:

Buffer Basics
Conceptually, a buffer is an array of primitive data elements wrapped inside an object.The advantage of a Buffer class over a simple array is that it encapsulates data content and information about the data into a single object. The Buffer class and its specialized subclasses define a API for processing data buffers.

 

NIO Buffer基础

从理论上讲,buffer(缓存数组)就是一个在其内部封装基本数据的数组。buffer比一个简单数组高级就是它封装了数据内容和信息进入一个简单的对象。Buffer类以及他的子类都定义了一组接口来处理数据缓存。
2.1.1 Attributes
There are four attributes all buffers possess that provide information about the contained data elements. These are:

 

2 1.1属性

所有的buffer都有四个提供关于被其包装数据元素信息的属性。它们是:

 

Capacity

The maximum number of data elements the buffer can hold. The capacity is set when
the buffer is created and can never be changed.

 

容量

一个buffer所能持有数据的最大数目。当一个buffer对象被创建时候其容量属性就被创建。而且将不再变化。
Limit
The first element of the buffer that should not be read or written. In other words,the count of live elements in the buffer.

 

最大限制

buffer的第一个应该被读写的第一个元素。也就是,在buffer内部活对象的数目。
Position
The index of the next element to be read or written. The position is updated
automatically by relative get( ) and put( ) methods.

 

位置

buffer下一个将被读写的位置索引。位置将相对函数get()和put函数自动更新


Mark
A remembered position. Calling mark( ) sets mark = position. Calling reset( ) sets position = mark. The mark is undefined until set.The following relationship between these four attributes always holds:
0 <= mark <= position <= limit <= capacity
Let's look at some examples of these attributes in action. Figure 2-2 shows a logical view of a newly created ByteBuffer with a capacity of 10.

 

标记位置

一个被buffer推荐的位置(既默认位置)。调用mark函数使得mak=position.调用reset函数使得position=mak。mark直到被设置之后才有值的。在这些四个属性中保持以下关系:

0 <= mark <= position <= limit <= capacity

让我们实战看看这4个属性的例子吧。图 2-2展示了一个包含10个元素新创建的ByteBuffer 逻辑视图


Figure 2-2.

 

 

 A newly created ByteBuffer The position is set to 0, and the capacity and limit are set to 10, just past the last byte the buffer can hold. The mark is initially undefined. The capacity is fixed, but the other three attributes can change as the buffer is used.

 

对于一个新创建的ByteBuffer ,position 被设置为0,capacity 和limit属性被设置为10既最后一个可以缓存的字节。mark 在吃石化时候是没有定义的。容量capacity 都是固定不变的,但是其他四个属性将被改变随着buffer被使用过程中

 

2.1.2 Buffer API
Let's take a look now at how we can use a buffer. These are the method signatures for
the Buffer class:

 

Buffer接口

现在让我们看一下我们是如何使用一个buffer对象。一个Buffer 类有一个签名函数:
package java.nio;
public abstract class Buffer {
public final int capacity( )
public final int position( )
public final Buffer position (int newPosition)
public final int limit( )
public final Buffer limit (int newLimit)
public final Buffer mark( )
public final Buffer reset( )
public final Buffer clear( )
public final Buffer flip( )
public final Buffer rewind( )
public final int remaining( )
public final boolean hasRemaining( )
public abstract boolean isReadOnly( );
}
One thing to notice about this API is that methods you would normally expect to return void,
such as clear( ), instead return a Buffer reference. These methods return a reference to
the object they were invoked upon (this). This is a class design technique that allows for
invocation chaining. Chaining invocations allows code like this:

 

在这些接口中值得我们关注的是那些在通常情况应该没有返回值确返回Buffer对象引用本身的函数,例如clear()函数。这些将返回一个buffer引用给那些调用以上函数的对象使得链式调用时这个类的设计技巧。链式调用代码如下:


buffer.mark( );
buffer.position(5);
buffer.reset( );
to be written like this:  以上调用可以如下链式调用
buffer.mark().position(5).reset( );


The classes in java.nio were designed with invocation chaining in mind. You may have seen invocation chaining used with the StringBuffer class.When used wisely, invocation chaining can produce concise, elegant,and easy-to-read code. When abused, it yields a cryptic tangle of muddled gibberish. Use invocation chaining when it improves readability and makes your intentions clearer. If clarity of purpose suffers when using invocation chaining, don't use it. Always make your code easy for others to read.

 

java.nio中的此类在设计时考虑到链接调用。你也许看见过链式调用在使用StringBuffer类时候。很明智,链式调用可以生成简介美观而且容易阅读的代码。当链式调用被滥用则产生一种糊涂迷糊的感觉。当提供链式调用使得代码更具有可阅读性,你的目的意图将更清晰。如果在使用链式调用时候没有明确的目的,就不要使用它了。记得总是保持你的代码让别人更容易读懂


Another thing to note about this API is the isReadOnly( ) method. All buffers are readable,but not all are writable. Each concrete buffer class implements isReadOnly( ) to indicate whether it will allow the buffer content to be modified. Some types of buffers may not have their data elements stored in an array. The content of MappedByteBuffer, for example, may actually be a read-only file. You can also explicitly create read-only view buffers to protect the content from accidental modification. Attempting to modify a read-only buffer will cause a ReadOnlyBufferException to be thrown. But we're getting ahead of ourselves.

 

另外一个值得注意关于这些接口是isReadOnly函数。所有的buffer类都是可读的,但并不是所有都可写。每一个具体的buffer类实现isReadOnly函数来表明其是否允许buffer中的内容被修改。一些类型的buffer病没有保存他们数据元素的数组。例如MappedByteBuffer,其实就是一个只读的文件。你可以明确地创建一个只读视图buffer来保护其内容被意外修改。试图去修改一个只读视图buffer将抛出ReadOnlyBufferException 异常产生。但是目前我们已经超前。

  • 大小: 12.5 KB
0
0
分享到:
评论
1 楼 baikkp 2013-03-16  
Limit应该是"第一个不应该被读或写的位置"吧

相关推荐

    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 nio 包读取超大数据文件

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

    Java NIO英文高清原版

    Java NIO英文高清原版

    java NIO 中文版

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

    java NIO.zip

    java NIO.zip

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

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

    java NIO 视频教程

    Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式。 Java NIO: Channels and Buffers(通道和缓冲区) 标准的IO基于字节流和字符流进行操作的,...

    java nio 实现socket

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

    java nio中文版

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

    Java Nio selector例程

    java侧起server(NioUdpServer1.java),基于Java Nio的selector 阻塞等候,一个android app(NioUdpClient1文件夹)和一个java程序(UI.java)作为两个client分别向该server发数据,server收到后分别打印收到的消息...

    java NIO技巧及原理

    java NIO技巧及原理解析,java IO原理,NIO框架分析,性能比较

    基于Java NIO实现五子棋游戏.zip

    基于Java NIO实现五子棋游戏.zip基于Java NIO实现五子棋游戏.zip 基于Java NIO实现五子棋游戏.zip基于Java NIO实现五子棋游戏.zip 基于Java NIO实现五子棋游戏.zip基于Java NIO实现五子棋游戏.zip 基于Java NIO实现...

    java基于NIO实现Reactor模型源码.zip

    java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现...

    java nio 读文件

    java nio 读文件,java nio 读文件

    JAVA NIO 学习资料

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

    Java NIO测试示例

    Java NIO测试示例

    java nio入门学习,两个pdf

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

    Java NIO.pdf

    java nio编程 非阻塞模式的通信 电子书 带目录标签

    Java NIO实战开发多人聊天室

    01-Java NIO-课程简介.mp4 05-Java NIO-Channel-FileChannel详解(一).mp4 06-Java NIO-Channel-FileChannel详解(二).mp4 08-Java NIO-Channel-ServerSocketChannel.mp4 09-Java NIO-Channel-SocketChannel.mp4 ...

Global site tag (gtag.js) - Google Analytics