`
eyesmore
  • 浏览: 364811 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Buffer

阅读更多

 

内容一、基本概念

java.io.Buffer的相关定义:A container for data of a specific primitive type.

 

(这点上很类似于DataInputStream,DataOutputStream,不同的是一个提供的是基于流的操作,具有单向,不可逆性;Buffer不再是流了。)

 

/**
 * A container for data of a specific primitive type.
 *
 * <p> A buffer is a linear, finite sequence of elements of a specific
 * primitive type.  Aside from its content, the essential properties of a
 * buffer are its capacity, limit, and position: </p>

 * (0<=position<=limit<=capacity        注:capacity一旦确定就不能更改)
 *
 * <blockquote>
 *
 *   <p> A buffer's <i>capacity</i> is the number of elements it contains.  The
 *   capacity of a buffer is never negative and never changes.  </p>
 *
 *   <p> A buffer's <i>limit</i> is the index of the first element that should
 *   not be read or written.  A buffer's limit is never negative and is never
 *   greater than its capacity.  </p>
 *
 *   <p> A buffer's <i>position</i> is the index of the next element to be
 *   read or written.  A buffer's position is never negative and is never
 *   greater than its limit.  </p>
 *
 * </blockquote>

内容二、基本操作

* <h4> Transferring data </h4>  存取数据
 *
 * <p> Each subclass of this class defines two categories(绝对和相对存取) of <i>get</i> and
 * <i>put</i> operations: </p>
 * (相对以前的IoStream变化体现在操作更为灵活,不仅局限于单向,不可以逆的流操作。)
 * <blockquote>
 *
 *   <p> <i>Relative</i> operations read or write one or more elements starting
 *   at the current position and then increment the position by the number of
 *   elements transferred.  If the requested transfer exceeds the limit then a
 *   relative <i>get</i> operation throws a {@link BufferUnderflowException}
 *   and a relative <i>put</i> operation throws a {@link
 *   BufferOverflowException}; in either case, no data is transferred.  </p>

      

      读写异常:

      get  (if: current+requested > limit)  throw:   BufferUnderflowException

      write (if:  current+requested > limit) throw:    BufferOverflowException
 *
 *   <p> <i>Absolute</i> operations take an explicit element index and do not
 *   affect the position.  Absolute <i>get</i> and <i>put</i> operations throw
 *   an {@link IndexOutOfBoundsException} if the index argument exceeds the
 *   limit.  </p>

 

内容三、标记

* <h4> Marking and resetting </h4>
 *在IoStream中也有类似的操作,只是为了这种流操作的单向性。
 * <p> A buffer's <i>mark</i> is the index to which its position will be reset
 * when the {@link #reset reset} method is invoked.  The mark is not always
 * defined, but when it is defined it is never negative and is never greater
 * than the position.  If the mark is defined then it is discarded when the
 * position or the limit is adjusted to a value smaller than the mark.(mark被隐含失效)
 

 

If the mark is not defined then invoking the {@link #reset reset} method causes an
 *
{@link InvalidMarkException} to be thrown.

 

 

内容四、控制

 

<h4> Clearing, flipping, and rewinding </h4>
 *
 * <p> In addition to methods for accessing the position, limit, and capacity
 * values and for marking and resetting,

     this class also defines the following operations upon buffers:
 *
 * <ul>
 *
 *   <li><p> {@link #clear} makes a buffer ready for a new sequence of
 *   channel-read or relative <i>put</i> operations: It sets the limit to the
 *   capacity and the position to zero.  </p></li>
 *
 *   <li><p> {@link #flip} makes a buffer ready for a new sequence of
 *   channel-write or relative <i>get</i> operations: It sets the limit to the
 *   current position and then sets the position to zero.  </p></li>
 *

       当我们创建完毕一个Buffer以后,我们应该养成这样的习惯:

      (1)放数据前,clear下,确保position从0开始写;(2)取数据前,flip下,确保我们只取有效数据【一般(limit,capacity]间的数据全部是0】。

      (3)如果我们要改变流操作的特性,需要对同一份数据进行多次读取的话,那么如果【全部复读】,则直接rewind下;如果【部分复读】则使用mark and reset。
 *   <li><p> {@link #rewind} makes a buffer ready for re-reading the data that
 *   it already contains: It leaves the limit unchanged and sets the position
 *   to zero.  </p></li>
 *
 * </ul>
 *

 

public class ByteBufferDemo {
 public static void main(String[] args) {
  ByteBuffer buf = ByteBuffer.allocateDirect(12);
  buf.clear();//put前准备操作
  buf.putInt(10);
  buf.putInt(20);
 //buf.putInt(30);//留4个字节没放东西,所以有效空间是[0,8)
  buf.flip();//get前准备操作
  System.out.println(buf.getInt());
  System.out.println(buf.getInt());
  System.out.println(buf.getInt());//读取时超过了有效范围
//  【全部复读】
  buf.rewind();
  System.out.println(buf.getInt());
  System.out.println(buf.getInt());
  
//  【部分复读】
  System.out.println("演示复读");
  buf.rewind();
  System.out.println(buf.getInt());
//  现在要对第2个int复读两次
  buf.mark();
  System.out.println(buf.getInt());//第一次读取
  buf.reset();//准备第二次读取
  System.out.println(buf.getInt());

  /* 10
   * 20
   * Exception in thread "main" java.nio.BufferUnderflowException
 at java.nio.Buffer.nextGetIndex(Buffer.java:404)
 at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:620)
 at com.umpay.construct.ByteBufferDemo.main(ByteBufferDemo.java:17)
   * */
 }
}
	 

 

内容五、注意事项

 

(1)非线程安全

* <h4> Thread safety </h4>
 *
 * <p> Buffers are not safe (非线程安全)for use by multiple concurrent threads.  If a
 * buffer is to be used by more than one thread then access to the buffer
 * should be controlled by appropriate synchronization.

 

(2)只读

 

* <h4> Read-only buffers </h4>
 *
 * <p> Every buffer is readable, but not every buffer is writable.  The
 * mutation(指:会改变对象状态的方法) methods of each buffer class are specified as <i>optional
 * operations</i> that will throw a {@link ReadOnlyBufferException} when
 * invoked upon a read-only bufferA read-only buffer does not allow its
 * content to be changed, but its mark, position, and limit values are mutable. (因为:这些变量都属于内部维护的变量InVar)
 * Whether or not a buffer is read-only may be determined by invoking its
 * {@link #isReadOnly isReadOnly} method.
 *

分享到:
评论

相关推荐

    netty-buffer-4.1.68.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.68.Final.jar; 赠送原API文档:netty-buffer-4.1.68.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.68.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.68.Final....

    netty-buffer-4.1.73.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.73.Final.jar; 赠送原API文档:netty-buffer-4.1.73.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.73.Final....

    环形buffer无锁一线程写一线程读

    如果你在找一个环形buffer这就是你想要的了。使用场景为一个线程写一个线程读完全不需要锁。可以设定buffer的初始块及数量,初始块是固定大小的,当需要扩环时会动态创建块即不像其它的库块满了就写失败了,当释放时...

    RingBuffer 循环缓存 亲测可用 V2 修改一处

    4.当写入数据的长度大于ringbuffer的可写入长度时,多余的数据将会丢弃。所以写入数据前,先判断ringbuffer的可写入长度。另外程序包含示例。 支持windows平台的vs与linux平台的clion,语言级别实现,与平台无关。 ...

    netty-buffer-4.1.11.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.11.Final.jar; 赠送原API文档:netty-buffer-4.1.11.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.11.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.11.Final....

    netty-buffer-4.1.23.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.23.Final.jar; 赠送原API文档:netty-buffer-4.1.23.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.23.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.23.Final....

    netty-buffer-4.1.27.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.27.Final.jar; 赠送原API文档:netty-buffer-4.1.27.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.27.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.27.Final....

    C语言头文件 BUFFER

    C语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC语言头文件 BUFFERC...

    buffer应用缓冲区

    buffer应用缓冲区 socket应用层

    netty-buffer-4.1.24.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.24.Final.jar; 赠送原API文档:netty-buffer-4.1.24.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.24.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.24.Final....

    Rt-thead studio软件下使用ringbuffer

    Rt-thead studio软件下使用ringbuffer

    netty-buffer-4.1.11.Final-API文档-中英对照版.zip

    赠送jar包:netty-buffer-4.1.11.Final.jar; 赠送原API文档:netty-buffer-4.1.11.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.11.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.11.Final....

    netty-buffer-4.1.65.Final-API文档-中文版.zip

    赠送jar包:netty-buffer-4.1.65.Final.jar; 赠送原API文档:netty-buffer-4.1.65.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.65.Final....

    Lab1-Buffer OverFlow

    Lab1-Buffer OverFlow,包含代码、实验说明和论文。

    ArcMap中Buffer的创建及使用

    介绍ArcMap中Buffer的创建及使用,并且附图说明。

    netty-buffer-4.1.73.Final-API文档-中英对照版.zip

    赠送jar包:netty-buffer-4.1.73.Final.jar; 赠送原API文档:netty-buffer-4.1.73.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.73.Final....

    一个c++buffer的设计和实现

    5、线程使用完一个group后可以在使用其他的group,且不限在相同的原子buffer。 可能的问题在于,多次申请的问题,经过测试基本可以用。 方法2: 1、使用默认缓存5M 2、默认线程使用的内存为128K,即最多默认...

    Z-Buffer消隐算法C#实现

    采用C#实现计算机图形学中Z-Buffer消隐算法

    Z-Buffer消隐算法的Matlab实现

    用Matlab实现的Z-Buffer算法,其中读取了一个wrl文件和obj文件

    VLC input buffer管理

    个人总结的VLC对于input buffer管理的分析,希望对大家有用,顺便赚点分数^_^

Global site tag (gtag.js) - Google Analytics