SeekableByteChannel接口定义

ByteChannel,分散聚集通道接口的定义(SocketChannel):[url]http://donald-draper.iteye.com/blog/2371065[/url]
package java.nio.channels;

import java.nio.ByteBuffer;
import java.io.IOException;

/**
* A byte channel that maintains a current <i>position</i> and allows the
* position to be changed.
*SeekableByteChannel维护是一个当前position,运行修改position。
* <p> A seekable byte channel is connected to an entity, typically a file,
* that contains a variable-length sequence of bytes that can be read and
* written. The current position can be {@link #position() <i>queried</i>} and
* {@link #position(long) <i>modified</i>}. The channel also provides access to
* the current <i>size</i> of the entity to which the channel is connected. The
* size increases when bytes are written beyond its current size; the size
* decreases when it is {@link #truncate <i>truncated</i>}.
*SeekableByteChannel连接一个实体,典型为一个包含可变长度的字节序列文件,可以读取
和写。可通过#position方法获取当前位置,通过#position(long)修改当前position。通道
提供了访问通道连接实体的当前size。当字节序列被写到实体时,实体size增加;当压缩时,
实体size减少。
* <p> The {@link #position(long) position} and {@link #truncate truncate} methods
* which do not otherwise have a value to return are specified to return the
* channel upon which they are invoked. This allows method invocations to be
* chained. Implementations of this interface should specialize the return type
* so that method invocations on the implementation class can be chained.
*#position(long)和#truncate方法,在调用时,返回的值是不确定。这些方法可以链式调用。
接口的实现,将该使这些方法返回一个精确的子,以便链式调用。
* @since 1.7
* @see java.nio.file.Files#newByteChannel
*/

public interface SeekableByteChannel
extends ByteChannel
{
/**
* Reads a sequence of bytes from this channel into the given buffer.
*从通道读取字节序列,写到buffer中
* <p> Bytes are read starting at this channel's current position, and
* then the position is updated with the number of bytes actually read.
* Otherwise this method behaves exactly as specified in the {@link
* ReadableByteChannel} interface.
从当前通道的postion位置开始读取字节,当实际字节被读取时,则更细positon。其他
的和ReadableByteChannel接口相同
*/
@Override
int read(ByteBuffer dst) throws IOException;

/**
* Writes a sequence of bytes to this channel from the given buffer.
*从给定的buffer,读取字节序列写到通道中
* <p> Bytes are written starting at this channel's current position, unless
* the channel is connected to an entity such as a file that is opened with
* the {@link java.nio.file.StandardOpenOption#APPEND APPEND} option, in
* which case the position is first advanced to the end. The entity to which
* the channel is connected is grown, if necessary, to accommodate the
* written bytes, and then the position is updated with the number of bytes
* actually written. Otherwise this method behaves exactly as specified by
* the {@link WritableByteChannel} interface.
从通道当前的位置写字节序列,除非通道连接一个实体,比如以StandardOpenOption#APPEND
配置选项打开的文件,在这种情况下,position首先定位到文件的尾部。通道连接的实际将会
增加,如果需要,计算写的字节数,更新position。其他方法与WritableByteChannel相同。
*/
@Override
int write(ByteBuffer src) throws IOException;

/**
* Returns this channel's position.
*返回当前通道的位置
* @return This channel's position,
* a non-negative integer counting the number of bytes
* from the beginning of the entity to the current position
*
* @throws ClosedChannelException
* If this channel is closed
* @throws IOException
* If some other I/O error occurs
*/
long position() throws IOException;

/**
* Sets this channel's position.
*设置通道的位置
* <p> Setting the position to a value that is greater than the current size
* is legal but does not change the size of the entity. A later attempt to
* read bytes at such a position will immediately return an end-of-file
* indication. A later attempt to write bytes at such a position will cause
* the entity to grow to accommodate the new bytes; the values of any bytes
* between the previous end-of-file and the newly-written bytes are
* unspecified.
*如果设置position大于当前实体size,是合法的,不会改变实体的size。尝试从设置后的
position位置读取字节,position将会立即定位到文件的末端。此时尝试写字节序列,
将会引起实体的增加。
* <p> Setting the channel's position is not recommended when connected to
* an entity, typically a file, that is opened with the {@link
* java.nio.file.StandardOpenOption#APPEND APPEND} option. When opened for
* append, the position is first advanced to the end before writing.
*当通道连接一个实体时,不建议设置通道的position,特别是一个以StandardOpenOption#APPEND
配置选项打开的文件,在这种情况下,在写之前,首先定位position的位置到文件的末尾。
* @param newPosition
* The new position, a non-negative integer counting
* the number of bytes from the beginning of the entity
*
* @return This channel
*
* @throws ClosedChannelException
* If this channel is closed
* @throws IllegalArgumentException
* If the new position is negative
* @throws IOException
* If some other I/O error occurs
*/
SeekableByteChannel position(long newPosition) throws IOException;

/**
* Returns the current size of entity to which this channel is connected.
*返回通道连接实体的size
* @return The current size, measured in bytes
*
* @throws ClosedChannelException
* If this channel is closed
* @throws IOException
* If some other I/O error occurs
*/
long size() throws IOException;

/**
* Truncates the entity, to which this channel is connected, to the given
* size.
*压缩通道连接的实体到指定的size
* <p> If the given size is less than the current size then the entity is
* truncated, discarding any bytes beyond the new end. If the given size is
* greater than or equal to the current size then the entity is not modified.
* In either case, if the current position is greater than the given size
* then it is set to that size.
*如果给定的size小于实体当前的size,将会压缩实体,丢弃超过新末端的字节。如果
给定的size大于或等于当前size,实际将不会修改。在其他一些情况下,如果当前的position
大于给定的size,那么将会设置position。
* <p> An implementation of this interface may prohibit truncation when
* connected to an entity, typically a file, opened with the {@link
* java.nio.file.StandardOpenOption#APPEND APPEND} option.
*当连接一个实体时,具体接口的实现也许会禁止压缩实体,比如以StandardOpenOption#APPEND
配置选项打开的文件。
* @param size
* The new size, a non-negative byte count
*
* @return This channel
*
* @throws NonWritableChannelException
* If this channel was not opened for writing
* @throws ClosedChannelException
* If this channel is closed
* @throws IllegalArgumentException
* If the new size is negative
* @throws IOException
* If some other I/O error occurs
*/
SeekableByteChannel truncate(long size) throws IOException;
}
LK驱动开发全攻略:从零开始为高通/MTK平台编写Bootloader驱动 本文深入解析了为高通和MTK平台开发LK(Little Kernel)Bootloader驱动的完整流程。内容涵盖开发环境搭建、核心驱动模型剖析、显示与存储等关键外设驱动开发,并针对高通与MTK平台的差异提供了实战指南与调试技巧,是嵌入式系统工程师掌握设备底层启动命脉的必备攻略。 阅读详情

相关推荐

13、Java文件监控与随机访问:Watch Service API与SeekableByteChannel的应用

本文深入探讨了Java中利用Watch Service API实现文件监控和使用SeekableByteChannel及FileChannel进行随机访问文件处理的技术。通过具体代码示例,展示了如何应用这些技术实现视频监控、打印机托盘管理、内存映射、文件锁定等场景,并对不同文件复制方式进行了性能比较。适合希望提升Java文件处理能力的开发者参考。

xgboost6farmer的博客 61

Java SeekableByteChannel接口

总结 SeekableByteChannel接口是一种维持了当前位置并允许改变位置的字节通道 SeekableByteChannel接口它在文件中维持一个当前位置position,可以修改当前位置position在任意位置读取(读取越界会报错)和写入数据(写入越界会增加文件长度),当写入的字节超过其当前大小时,文件的大小会增加, 当调用truncate方法时,文件大小减小 position(long)和 truncate(long size)方法返回调用者其本身,所以能够支持链式调用 SeekableB

weixin_45754452的博客 1074

power bi 演唱会-佐罗

我是业务分析人员,注重使用性,因此,课程不到 30% 的内容就可以解决目前为止我碰到的所有业务问题,剩下的 70% 内容是帮助我提升,优化,升华,精益求精用的,比如第三方工具,比如代码运行速度优化,比如查询逻辑优化等等。系统性上来说,结构明晰,安排上非常有条理,层层递进;《BI 真经》,中有真经二字,略显夸大,而当我第一次证明了这点的时候,我也非常惊讶,这就是我做以下所有事情的一切动力。《BI 真经》综合考量思想性,系统性,本质性,创新性,通用性,技巧性,启发性,对比其他课程有压倒性的显著优势。

v_1090213742的博客 911

Java7 NIO2 高级教程(三)

协议:CC BY-NC-SA 4.0 七、随机访问文件 在前面的章节中,我们已经按顺序探索了文件。可以顺序浏览的文件称为顺序文件。在本章中,你将看到使用非顺序(随机)访问文件内容的优点。允许随机访问其内容的文件被称为随机访问文件(RAFs )。顺序文件更常用,因为它们易于创建,但是 RAF 更灵活,并且可以更快地找到它们的数据。 使用 RAF,您可以打开文件,查找特定位置,并读取或写入该文件。在你打开 RAF 之后,你可以通过使用一个记录号以随机的方式读取或写入它,或者你可以添加到文件的开头或结尾,因为

龙哥盟 1220

CentOS 7 离线升级 OpenSSH 9.9p2 保姆级教程(含依赖包下载与常见报错解决)

本文提供了一份详尽的CentOS 7系统离线升级OpenSSH至9.9p2版本的完整教程。针对无外网环境,重点讲解了依赖包(尤其是OpenSSL 3.x)的离线下载与编译部署,并深入解决了编译过程中常见的“libcrypto.so缺失”等动态库链接错误。教程涵盖从环境准备、源码编译、服务配置到系统集成的全流程,最后还提供了关键的回滚方案与应急预案,是运维人员在内网环境中进行安全加固的实用指南。

weixin_29248313的博客 324

变邻域搜索(VNS)原理梳理和应用细节-附求解VRPTW问题C++代码

文章目录1、简介-introduction2、VNS的基本机制2.1 变邻域下降方法-Variable Neighborhood Descent2.2基本变邻域搜索方法-Basic Variable Neighborhood Search2.3 终止准则3、VNS的拓展和混杂算法3.1 VNS的拓展3.2 变邻域分解搜索-Variable Neighborhood Decomposition Search3.3 偏变邻域搜索-Skewed VNS3.4 并行变邻域搜索-Parallel VNS4、VNS和其

dragon Fly的博客 6826

java seek_Java 7新I/O特性解析

【IT168 分析】Java 7提供了一个新API访问文件系统,但除此之外,JSR 203(NIO.2)还包含其它很多新特性,这个新版本的确新增了很多改善I/O编程的类,本文将会介绍下面的新特性:· SeekableByteChannel:随机访问通道;· MulticastChannel:允许IP多播的通道;· NetworkChannel:新的网络通道超级接口;· 异步I/O API:新的AP...

weixin_39862985的博客 191

ubuntu18.04 显卡驱动问题 造成屏幕分辨变大 NVIDIA-SMI has failed because it couldn't communicate with ....等问题

问题1:装好了显卡驱动,cudnn,tensorflow等没问题,一直可以用。但是突然有一天不行了,终端输入:nvidia-smi 显示:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed...

梦萦飞--CV/DL/ML 2248

java nio SeekableByteChannel源码分析

package java.nio.channels; import java.nio.ByteBuffer; import java.io.IOException; /** * 保持当前position并允许改变position的字节通道。 * * <p> 可查找字节通道连接到一个实体,通常是一个文件, * 该实体包含一个可读写的可变长度的字节序列。可以通过position()查询和通过position(long)修改当前位置。 * 通道还提供对通道所连接的实体的当前大小的访问..

今天吃了吗的博客 615

【技能】伪真openclaw主动式智能体—告别‘巨婴’AI工具人:体验主动智能体的魅力

摘要: 传统AI常因被动执行、缺乏闭环而令人抓狂。Proactivity主动执行能力将AI从"指令跟随者"升级为"主动合伙人",具备预判需求、自动推进、断聊恢复、问题自解等能力。其核心原理在于建立AI专属工作体系(持久化内存、动态任务层、临时缓存层、归档复盘层),通过7步闭环(感知触发→状态恢复→边界校验→方案探索→动作决策→执行落地→状态留存)确保安全高效。

海兰 209

TC3XX - MCAL知识点(三十五):Tasking编译器下,Tc3xx的MCAL代码如何使用多核,多核中断、多核任务、多核外设之间的MCAL配置与代码例程

文章用于记录在裸机情况下使用多核Tc387芯片,如何高效运行以及正确的代码、工程属性、链接文件配置等问题,相互结合、相互影响的地方,以示后续使用记录。

梦想技术家 963

SeekableByteChannel 和 FileChannel

Java7中引入了SeekableByteChannel接口,允许我们定位到文件的任意位置进行读写。注意这里的写,不是新增式的插入,而是覆盖,当然在文件末尾的写,是新增。 java.nio.channels.SeekableByteChannel A byte channel that maintains a current position and allows the posi...

weixin_34107739的博客 446

Android Framework 开发工程师职位深度解析与面试指南

摘要:博泰车联网科技(上海)股份有限公司招聘Android Framework开发工程师,负责车载系统的底层开发与优化。岗位要求5年以上经验,深入掌握Android Framework机制(WindowManager、PowerManager等模块),熟悉高通平台中间件架构及NDK/JNI开发,具备性能优化(启动时间、ANR处理)、OTA升级等能力。需熟练使用Android Studio、Git等工具,有车载开发经验者优先。工作内容包括Framework定制开发、问题排查及技术文档编写。

郑伟强dev的专栏 552

XXXbuffer与SeekableByteChannel的配合方法

import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; public class Easy { public.

jyheasy的博客 575

【YOLOv11工业级实战】27. TensorRT量化实战:FP16/INT8极致加速!边缘设备YOLOv11推理速度提升4倍,精度损失控制在1.5%内

摘要:本文针对智慧园区200路摄像头实时分析的工业痛点——RTX 3080单卡仅能处理12路视频流(YOLOv11-nano FP32精度45FPS,需≥120FPS且精度损失≤3%),提供TensorRT量化完整解决方案。从原理解析(FP16/INT8精度差异与量化逻辑)到实操落地(ONNX转换、FP16引擎构建、INT8精细校准),全程给出可复用代码。实测显示:FP16量化后速度达128FPS(×2.8),精度损失0.2%。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 1720

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

java-i o技术,Java 7新I/O特性解析

【IT168 分析】Java 7提供了一个新API访问文件系统,但除此之外,JSR 203(NIO.2)还包含其它很多新特性,这个新版本的确新增了很多改善I/O编程的类,本文将会介绍下面的新特性:· SeekableByteChannel:随机访问通道;· MulticastChannel:允许IP多播的通道;· NetworkChannel:新的网络通道超级接口;· 异步I/O API:新的AP...

weixin_29069131的博客 145

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

java 文本 特征分析,Java 7新I/O特性解析

Java 7提供了一个新API访问文件系统,但除此之外,JSR 203(NIO.2)还包含其它很多新特性,这个新版本的确新增了很多改善I/O编程的类,本文将会介绍下面的新特性:· SeekableByteChannel:随机访问通道;· MulticastChannel:允许IP多播的通道;· NetworkChannel:新的网络通道超级接口;· 异步I/O API:新的API使I/O操作可以异...

weixin_39980347的博客 139

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

java获取文件最后一个字符,Java如何从文件中读取最后n个字符?

在下面的文章中,您将学习如何从文件中读取最后n个字符。JDK 7引入了一个新SeekableByteChannel接口,该接口允许其实现更改字节通道的位置和大小。它的实现之一是FileChannel类(java.nio.channels.FileChannel)。该FileChannel班有可能得到保持,我们将要读或写一个文件的当前位置。下面的代码段显示了如何从日志文件中读取最后1000个字符。p...

weixin_39748838的博客 864

java channel 读写文件_Java 使用newByteChannel(Channel IO)读写文件方法及示例代码

1、ByteChannel、SeekableByteChannel和newByteChannelByteChannel接口提供了基本的read和write功能性。ASeekableByteChannel是ByteChannel具有维持频道中位置并更改该位置的能力。ASeekableByteChannel还支持截断与通道关联的文件并查询文件的大小。能够移动到文件中的不同点,然后从该位置读取或写入...

weixin_36340672的博客 524

java se7 if_Java SE 7新特性之文件操作(7) - 随机访问文件

随机访问文件允许我们不按照顺序的访问文件的内容,这里的访问包括读和写。要随机的访问文件,我们就要打开文件,定位到指定的位置,然后读或写文件内容。在Javs SE7中,SeekableByteChannel接口提供了这个功能。SeekableByteChannel提供了一些简单易用的方法。依靠这些方法,我们能够设置或查询当前的位置,然后从当前位置读或者往当前位置写。该接口提供的方法有:* posit...

weixin_30035343的博客 171

Java SE 7 新特性之文件操作(7) - 随机读写文件

转自 开发者的天空随机访问文件允许我们不按照顺序的访问文件的内容,这里的访问包括读和写。要随机的访问文件,我们就要打开文件,定位到指定的位置,然后读或写文件内容。在Javs SE 7中,SeekableByteChannel接口提供了这个功能。SeekableByteChannel提供了一些简单易用的方法。依靠这些方法,我们能够设置或查询当前的位置,然后从当前位

afrag的专栏 3994

java7 NIO2(7) random access file API

java7NIO2 随机文件访问API package com.mime; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel;

程序员之路 2714

Java 7新I/O特性解析

Java 7提供了一个新API访问文件系统,但除此之外,JSR 203(NIO.2)还包含其它很多新特性,这个新版本的确新增了很多改善I/O编程的类,本文将会介绍下面的新特性:· SeekableByteChannel:随机访问通道;· MulticastChannel:允许IP多播的通道;· NetworkChannel:新的网络通道超级接口;· 异步I/O API:新的AP

jianpingdu2009的专栏 675

Java NIO2 (Path、Paths 与 Files )

在面试前我整理归纳了一些面试学习资料,文中结合我的朋友同学面试美团滴滴这类大厂的资料及案例由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!大家看完有什么不懂的可以在下方留言讨论也可以关注。《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

2301_76668692的博客 838
上一篇: PipeImpl解析
下一篇: 下拉多选框bootstrap-multiselect教程
Donald_Draper
博客等级 码龄14年 9粉丝 106原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值