`
Donald_Draper
  • 浏览: 979934 次
社区版块
存档分类
最新评论

SeekableByteChannel接口定义

    博客分类:
  • NIO
nio 
阅读更多
ByteChannel,分散聚集通道接口的定义(SocketChannel):http://donald-draper.iteye.com/blog/2371065
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;
}
分享到:
评论

相关推荐

    JDATREAM_java_

    6. ** SeekableByteChannel**: Java NIO(New IO)提供了一个SeekableByteChannel接口,它允许在通道中随机定位,读写文件。这对于大文件的处理非常有用,因为它允许我们跳过不需要的部分。 7. **多线程**: 对于...

    文件读写 文件读写文件读写 文件读写

    - Java的`SeekableByteChannel`接口提供了`position()`和`seek()`方法。 - Python的`seek()`和`tell()`方法同样用于文件定位。 6. 错误处理: - 程序应处理可能出现的异常,如文件不存在、权限不足等。 - 使用...

    JDK7 Reference Card 参考指南

    - **`SeekableByteChannel`**:新增的字节通道接口,支持随机读写操作。 - **`NetworkChannel`**:新的网络通道接口,提供了绑定通道套接字和设置套接字选项的能力。 - **`MulticastChannel`**:多播通道接口,支持 ...

    java大文件断点续传

    `Channels`和`SeekableByteChannel`接口可以用于定位读写位置,实现断点续传。 3. **HttpURLConnection**:Java内置的`HttpURLConnection`类可以设置请求头信息,实现断点续传请求。通过设置`setRequestProperty(...

    Generator:生成器:1. 代码生成器;2. 文件生成器

    在软件开发过程中,代码生成器是一种自动化工具,它可以自动生成特定类型的代码,如模板、接口实现、数据库访问层代码等,从而提高开发效率,减少人为错误。Java中,我们可以使用模板引擎(如FreeMarker或Thymeleaf...

    JavaIO流详细总结

    System.in、System.out和System.err是Java预定义的输入、输出和错误流,它们分别对应键盘输入、控制台输出和错误输出。 11. 终端流(PipedInputStream和PipedOutputStream): 这些流允许在多个线程间创建管道,...

    JDK1.7 之java.nio.file.Files 读取文件仅需一行代码实现

    3. 高效的读取方式:使用 SeekableByteChannel 和 InputStream 读取文件,提高了读取效率。 然而,需要注意的是,该方法不适合读取很大的文件,因为可能存在内存空间不足的问题。此外,开发者还应该明确规定文件的...

    memoryfilesystem:JSR-203文件系统的内存中实现

    SeekableByteChannel FileChannel AsynchronousFileChannel InputStream OutputStream BasicFileAttributeView , BasicFileAttributes DosFileAttributeView , DosFileAttributes PosixFileAttributeV

    多线程实现ftp断点续传

    4. **断点续传**:在数据传输线程中,使用`SeekableByteChannel`或`RandomAccessFile`来支持文件的随机访问,从而实现断点续传。每次读写时,都从上次保存的位置开始。 5. **错误处理和重试**:当网络中断或发生...

    OKHttp断点续传

    在`writeTo()`中,我们可以使用`SeekableByteChannel`从文件的当前位置开始写入数据。 3. **设置Range头**:在HTTP请求中,我们需要添加`Range`头部,指示服务器从哪个位置开始返回数据。例如,如果已下载了1MB,`...

    android word下载

    使用`java.nio`包中的`Channels`和`SeekableByteChannel`可以方便地实现这个功能。 五、错误处理与重试机制 在网络不稳定或服务器出现问题时,应有相应的错误处理和重试机制。例如,可以设置超时时间,当请求失败...

    Pro Java 7 NIO

    此外,书中还提到了SeekableByteChannel API,它是用于随机访问文件的,这对于需要频繁地读写文件中不同位置数据的应用程序来说,是一个非常便利的工具。 对于那些想要将现有的java.io.File代码进行重构,以使用更...

    Java〖NIO上篇〗看这一篇就够了 缓冲区 通道

    SeekableByteChannel channel = Files.newByteChannel(path, options); ``` 总结: Java NIO通过通道和缓冲区提供了更高效、灵活的I/O操作方式,适用于大数据处理和高并发场景。理解并掌握NIO,不仅有助于提升程序...

Global site tag (gtag.js) - Google Analytics