Buffer

本文详细介绍了Java NIO中Buffer的基本概念、操作方法、注意事项等内容。包括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.
 *

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍基于Matlab代码实现的四轴飞行器动力学建模与仿真方法。研究构建了考虑非线性特性的飞行器数学模型,涵盖姿态动力学与运动学方程,实现了三自由度(滚转、俯仰、偏航)的精确模拟。文中详细阐述了系统建模过程、控制算法设计思路及仿真结果分析,帮助读者深入理解四轴飞行器的飞行动力学特性与控制机制;同时,该模拟器可用于算法验证、控制器设计与教学实验。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及无人机相关领域的工程技术人员,尤其适合从事飞行器建模、控制算法开发的研究生和初级研究人员。; 使用场景及目标:①用于四轴飞行器非线性动力学特性的学习与仿真验证;②作为控制器(如PID、LQR、MPC等)设计与测试的仿真平台;③支持无人机控制系统教学与科研项目开发,提升对姿态控制与系统仿真的理解。; 阅读建议:建议读者结合Matlab代码逐模块分析,重点关注动力学方程的推导与实现方式,动手运行并调试仿真程序,以加深对飞行器姿态控制过程的理解。同时可扩展为六自由度模型或加入外部干扰以增强仿真真实性。
基于分布式模型预测控制DMPC的多智能体点对点过渡轨迹生成研究(Matlab代码实现)内容概要:本文围绕“基于分布式模型预测控制(DMPC)的多智能体点对点过渡轨迹生成研究”展开,重点介绍如何利用DMPC方法实现多智能体系统在复杂环境下的协同轨迹规划与控制。文中结合Matlab代码实现,详细阐述了DMPC的基本原理、数学建模过程以及在多智能体系统中的具体应用,涵盖点对点转移、避障处理、状态约束与通信拓扑等关键技术环节。研究强调算法的分布式特性,提升系统的可扩展性与鲁棒性,适用于多无人机、无人车编队等场景。同时,文档列举了大量相关科研方向与代码资源,展示了DMPC在路径规划、协同控制、电力系统、信号处理等多领域的广泛应用。; 适合人群:具备一定自动化、控制理论或机器人学基础的研究生、科研人员及从事智能系统开发的工程技术人员;熟悉Matlab/Simulink仿真环境,对多智能体协同控制、优化算法有一定兴趣或研究需求的人员。; 使用场景及目标:①用于多智能体系统的轨迹生成与协同控制研究,如无人机集群、无人驾驶车队等;②作为DMPC算法学习与仿真实践的参考资料,帮助理解分布式优化与模型预测控制的结合机制;③支撑科研论文复现、毕业设计或项目开发中的算法验证与性能对比。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注DMPC的优化建模、约束处理与信息交互机制;按文档结构逐步学习,同时参考文中提及的路径规划、协同控制等相关案例,加深对分布式控制系统的整体理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值