`
suiyuan0808
  • 浏览: 155741 次
  • 性别: 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环境jdk_1.6安装包

    7. **NIO.2文件系统API**:虽然完整的NIO.2是在Java 7中引入的,但Java 6开始奠定了基础,提供了一些预览功能,如异步文件通道。 8. **改进的垃圾回收**:JVM的垃圾回收机制在1.6中得到了优化,提高了内存管理和...

    Java5-Ejemplos.zip_java programming_java5

    Java 5,也被称为Java SE 5.0,是Java发展历程中的一个重要里程碑,它引入了许多新的特性和改进,显著提升了编程效率和代码的可读性。...对于想要熟悉或重温Java 5特性的开发者来说,这是一个宝贵的资源。

    java编程思想习题及答案

    《Java编程思想习题及答案》是一份针对Java编程学习者的宝贵资源,它包含了大量关于Java编程的习题和对应的解答。这份资料可能是从...对于有一定经验的开发者,重温这些习题也能帮助他们回顾和巩固知识。

    Java核心技术第10版卷1&卷2源代码

    一方面,它们可以作为重温和更新Java基础知识和高级特性的资料,另一方面,它们还能展示出如何运用Java的最佳实践和技巧,帮助开发者提升代码质量,优化性能。 总之,《Java核心技术第10版卷1&卷2源代码》是所有...

    Java项目实战营(第一期最新版).rar

    Java.IO和Java.NIO(非阻塞I/O)是另一个重要的学习领域,涵盖了输入输出流、文件操作、对象序列化等内容。学员将学会如何高效地处理数据读写,这对于开发涉及文件处理或网络通信的应用至关重要。 除此之外,课程还...

    java高编学习课件(包含练习项目)

    1. **基础回顾**:重温Java基本语法,如数据类型、运算符、流程控制语句等。 2. **面向对象编程**:深入讲解类、对象、封装、继承、多态等概念。 3. **集合框架**:介绍List、Set、Map接口及其实现类,如ArrayList、...

    java-chapter-6

    6. **IO流与NIO**:Java的IO流允许数据在不同设备间传输,而NIO(New Input/Output)提供了一种更高效的数据读写方式。本章会涵盖File类、InputStream/OutputStream家族,以及缓冲区、选择器和通道的概念。 7. **...

    BigJava:我读了Big Java书中的一些我认为很有趣的练习

    通过实际编写代码,作者得以重温Java语言的核心概念,并体验到解决编程问题的乐趣。 Java作为一种多用途、面向对象的编程语言,其主要特点包括平台独立性、垃圾回收机制、自动内存管理以及丰富的类库。以下是基于...

    java源码知乎-JavaCodeForBeginners:该存储库将包含面向初学者以及想要复兴其知识的开发人员的源代码。

    【Java源码知乎 - JavaCodeForBeginners】是面向初学者和希望重温基础知识的开发者的一个开源项目。这个项目旨在提供一系列的Java编程示例和源代码,帮助学习者理解和掌握Java语言的基础及其在实际开发中的应用。...

Global site tag (gtag.js) - Google Analytics