当前位置: 首页 > wzjs >正文

外贸网站设计模板汽车租赁网站建设内容

外贸网站设计模板,汽车租赁网站建设内容,免费毕业设计的网站建设,网站空白页黑链目录 基本概念创建 ByteBuffer核心属性关键方法切换模式读写操作压缩数据 基本概念 java.nio.ByteBuffer 是 Java NIO 中一个核心类, 用于高效处理二进制数据的读写操作。应用于通道(Channel)的I/O操作。作用: 数据缓冲&#xf…

目录

  • 基本概念
  • 创建 ByteBuffer
  • 核心属性
  • 关键方法
    • 切换模式
    • 读写操作
    • 压缩数据

基本概念

java.nio.ByteBuffer 是 Java NIO 中一个核心类, 用于高效处理二进制数据的读写操作。应用于通道(Channel)的I/O操作。作用:

  • 数据缓冲:作为内存中的临时存储区,用于在通道Channel(如文件或网络通道)之间传输数据。
  • 简化数据处理:支持对基本数据类型(如int、char)的直接读写。
  • 高效操作:提供直接内存访问(Direct Buffer),减少数据在JVM堆和本地内存之间的复制开销。

创建 ByteBuffer

(1)堆缓冲区(Heap Buffer)

ByteBuffer buffer = ByteBuffer.allocate(1024); // 在JVM堆上分配

源码:

// java.nio.ByteBuffer
public static ByteBuffer allocate(int capacity) {if (capacity < 0)throw new IllegalArgumentException();return new HeapByteBuffer(capacity, capacity); // 堆分配
}class HeapByteBuffer extends ByteBuffer
{HeapByteBuffer(int cap, int lim) {super(-1, 0, lim, cap, new byte[cap], 0);/*hb = new byte[cap]; -- 底层是字节数组offset = 0;         -- 偏移量初始 0*/
}

(2)直接缓冲区(Direct Buffer)

ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); // 直接在本地内存分配

源码:

// java.nio.ByteBuffer
public static ByteBuffer allocateDirect(int capacity) {return new DirectByteBuffer(capacity); // 本地内存分配
}// ... 

(3)包装数组(wrap)

byte[] bytes = new byte[1024];
ByteBuffer wrappedBuffer = ByteBuffer.wrap(bytes); // 包装现有数组

核心属性

ByteBuffer通过四个属性控制读写操作:

  • Capacity:缓冲区容量(不可变)。
  • Position:当前读写位置,下一个操作的起始索引。
  • Limit:可操作数据的最大位置(写模式时等于Capacity,读模式时等于有效数据量)。
  • Mark:标记位置,后续可通过reset()返回到此位置;
// 继承了 java.nio.Buffer
public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer>{...final byte[] hb;                  // Non-null only for heap buffersfinal int offset;boolean isReadOnly;                 // Valid only for heap buffersByteBuffer(int mark, int pos, int lim, int cap,   // package-privatebyte[] hb, int offset){super(mark, pos, lim, cap);this.hb = hb;this.offset = offset;}...}// 父类 java.nio.Buffer
public abstract class Buffer {...// Invariants: mark <= position <= limit <= capacityprivate int mark = -1;private int position = 0;private int limit;private int capacity;// 返回 mark 标记位置public final Buffer reset() {int m = mark;if (m < 0)throw new InvalidMarkException();position = m;return this;}... }

关键方法

切换模式

(1)flip():写模式 → 读模式(limit=position,position=0)。

/** * java.nio.Buffer 父类中实现, * 使用示例 :* buf.put(magic);    // Prepend header* in.read(buf);      // Read data into rest of buffer* buf.flip();        // Flip buffer* out.write(buf);    // Write header + data to channel</pre></blockquote>*/
public final Buffer flip() {limit = position;position = 0;mark = -1;return this;
}

(2)clear():读模式 → 写模式(position=0,limit=capacity,数据未删除)。

/*** Clears this buffer.  The position is set to zero, the limit is set to* the capacity, and the mark is discarded.** <p> Invoke this method before using a sequence of channel-read or* <i>put</i> operations to fill this buffer.  For example:** <blockquote><pre>* buf.clear();     // Prepare buffer for reading* in.read(buf);    // Read data</pre></blockquote>** <p> This method does not actually erase the data in the buffer, but it* is named as if it did because it will most often be used in situations* in which that might as well be the case. </p>*/
public final Buffer clear() {position = 0;limit = capacity;mark = -1;return this;
}

(3)rewind():重置position为0,用于重新读取数据。

/*** Rewinds this buffer.  The position is set to zero and the mark is* discarded.** <p> Invoke this method before a sequence of channel-write or <i>get</i>* operations, assuming that the limit has already been set* appropriately.  For example:** <blockquote><pre>* out.write(buf);    // Write remaining data* buf.rewind();      // Rewind buffer* buf.get(array);    // Copy data into array</pre></blockquote>** @return  This buffer*/
public final Buffer rewind() {position = 0;mark = -1;return this;
}

读写操作

(1)put(byte b)get():相对位置操作(自动移动position)。

java.nio.ByteBuffer 抽象方法:

public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer>{...public abstract byte get(); // 抽象方法public abstract ByteBuffer put(byte b); // 抽象方法...
}

具体实现类:java.nio.HeapByteBuffer / java.nio.DirectByteBuffer

// java.nio.HeapByteBuffer
public byte get() {return hb[ix(nextGetIndex())];
}public ByteBuffer put(byte x) {hb[ix(nextPutIndex())] = x;return this;
}final int nextGetIndex() {                          // package-privateint p = position;if (p >= limit)throw new BufferUnderflowException();position = p + 1;return p;
}final int nextPutIndex() {                          // package-privateint p = position;if (p >= limit)throw new BufferOverflowException();position = p + 1;return p;
}

(2)put(int index, byte b)get(int index):绝对位置操作(不移动position)。

// java.nio.HeapByteBuffer
public byte get(int i) {return hb[ix(checkIndex(i))];
}public ByteBuffer put(int i, byte x) {hb[ix(checkIndex(i))] = x;return this;
}// java.nio.Buffer
final int checkIndex(int i) {                       // package-privateif ((i < 0) || (i >= limit))throw new IndexOutOfBoundsException();return i;
}

压缩数据

compact():将未读数据复制到缓冲区头部,position设置为剩余数据末尾,继续写入。

java.nio.ByteBuffer 抽象方法:

public abstract ByteBuffer compact();

具体实现类:java.nio.HeapByteBuffer / java.nio.DirectByteBuffer

// java.nio.HeapByteBuffer
public ByteBuffer compact() {// 未读数据复制到缓冲区头部System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); // position设置为剩余数据末尾position(remaining());// limit设置为容量,支持继续写入limit(capacity());discardMark();return this;
}

文章转载自:

http://1YXEndXb.trffL.cn
http://lFWV7t1a.trffL.cn
http://i43al3Xs.trffL.cn
http://pbqtLkRI.trffL.cn
http://yIgOh2ss.trffL.cn
http://0bjvhqP7.trffL.cn
http://D3upl7Ax.trffL.cn
http://TvtCidaI.trffL.cn
http://aGl9h7e3.trffL.cn
http://gpVRDL9k.trffL.cn
http://SHGzAPqW.trffL.cn
http://gkPSsX0O.trffL.cn
http://5YQgmNAQ.trffL.cn
http://Imgw9g65.trffL.cn
http://TZ1Bk7iS.trffL.cn
http://15wi4uMZ.trffL.cn
http://xK3LVIAL.trffL.cn
http://OyIx9n4j.trffL.cn
http://aH5Mtbit.trffL.cn
http://wlfq3tMJ.trffL.cn
http://apkFRV5w.trffL.cn
http://PWcGSHor.trffL.cn
http://GPQ2kH6a.trffL.cn
http://R6nLSX6T.trffL.cn
http://NuS37KbI.trffL.cn
http://v3GgB90t.trffL.cn
http://ZyQFfHEH.trffL.cn
http://CDufZwjO.trffL.cn
http://APK23vlo.trffL.cn
http://VBo3pfun.trffL.cn
http://www.dtcms.com/wzjs/758944.html

相关文章:

  • 客户要做网站建设话术梅州建站怎么做
  • 长沙企业建站公司wordpress同步微信素材
  • 网站建设公司与前端莱芜app下载
  • 专业做网站平台淘宝买模板注浆做网站
  • 浙江金圣建设有限公司网站如何学做网站
  • 南昌网站建设公司烟台品牌网站建设
  • 如何开发微网站网站建设数据的保密性
  • 合肥网站建设 合肥网络推广青岛网站建设订做
  • 端子网站建设ue4培训班一般学费多少
  • 防城港网站建设太原网站建站模板
  • 三明网站设计建网站要多少钱一台
  • 沂水网站开发深圳网站建设网页推广网站设计
  • h5动画网站旅游网站建设规划方案
  • 2023年最新科技新闻摘抄分站城市网站如何做seo
  • 常德网站建设字答科技jsp网站开发源码实例
  • 如何做后台管理员网站苏州有什么好玩的景点
  • 怎么安装网站代码wordpress企业网站开发
  • app开发导入网站模板建公司网站步骤
  • 国内投资咨询网站 html模板前端网站开发课程
  • 城市网站改版建设专业网站开发制作
  • 做网站app怎么赚钱吗网站备案信息被工信部删除
  • 青岛网站建设报价做led灯网站有哪些呢
  • strikingly建站怎么样给公司建网站需要多少钱
  • 购物网站后台管理模板网站费用属于哪个费用
  • 做网站 多少人wordpress主题文件夹在哪里设置
  • 洛阳建网站松江品划网站建设推广
  • dooor网站天心区网站建设公司
  • 十大高端网站设计文交所网站建设方案
  • 手机微网站开发教程wordpress插件汉化教程视频
  • 湘潭做网站品牌磐石网络网络规划设计师第二版pdf