美文网首页
ByteBuffer源码阅读笔记

ByteBuffer源码阅读笔记

作者: Jaesoon | 来源:发表于2018-05-01 20:25 被阅读9次

    A byte buffer is either direct or non-direct.

    字符缓冲区可以是直接的或非直接的

    Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it.

    给定一个字符缓冲区,Java虚拟机将会尽最大的努力直接在它上面执行本地I/O操作。

    That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.

    也就是说,它将会试图避免任一一个底层操作系统I/O操作的每一次调用之前复制这个缓冲区的内容去一个中间缓冲区(或从一个中间缓冲区复制到这个缓冲区)(翻译君:感觉是这个意思:它尽量避免任一一个底层操作系统I/O每次调用之前,发生对这个缓冲区的操作。换句话:尽量在操作系统本地I/O读取之后再操作

    A direct byte buffer may be created by invoking the allocateDirect factory method of this class.

    直接缓冲区可以通过调用这个类的工厂方法allocateDirect创建

    The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers.

    这个方法返回的缓冲区和不直接缓冲区相比,通常在分配和释放上的成本有点高。

    The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint of an application might not be obvious.

    直接缓冲区的内容可能会在普通的垃圾回收堆之外,因此他们对应用程序内存占用的影响可能不明显。

    It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers that are subject to the underlying system's native I/O operations.

    因此,建议将直接缓冲区主要分配给受底层操作系统本地I/O操作管制的大型、长寿命缓冲区

    In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.

    总的来说,最好只有当它在程序的性能中产生可观增益时,再分配直接缓冲区。

    A direct byte buffer may also be created by mapping a region of a file directly into memory.

    直接字符缓冲区也可通过将一个文件的区域直接映射到内存中来创建。

    An implementation of the Java platform may optionally support the creation of direct byte buffers from native code via JNI.

    Java平台的一个实现可以支持直接字符缓冲区从原生代码中通过JNI创建。

    If an instance of one of these kinds of buffers refers to an inaccessible region of memory then an attempt to access that region will not change the buffer's content and will cause an unspecified exception to be thrown either at the time of the access or at some later time.

    如果其中一种缓冲区的实例指向一个不可访问的内存区域,那么访问该区域的尝试将不会改变缓冲区的内容,并将导致在访问时或稍后的时间抛出未指定的异常。

    Whether a byte buffer is direct or non-direct may be determined by invoking its isDirect method. This method is provided so that explicit buffer management can be done in performance-critical code.

    一个字符缓冲区是否为直接或非直接的可以通过调用它的isDirect方法来确定。提供这种方法,以便在性能关键代码中实现显式缓冲区管理。

    Invocation chaining

    链式调用

    Methods in this class that do not otherwise have a value to return are specified to return the buffer upon which they are invoked.

    这个类中没有返回值的方法被指定为返回它们被调用的缓冲区。

    This allows method invocations to be chained.

    这允许链式的调用方法。

    The sequence of statements

    bb.putInt(0xCAFEBABE);
     bb.putShort(3);
     bb.putShort(45);
    

    can, for example, be replaced by the single statement

     bb.putInt(0xCAFEBABE).putShort(3).putShort(45);
    

    举个例子,这些序列的语句,可以通过单个语句来替换。

    相关文章

      网友评论

          本文标题:ByteBuffer源码阅读笔记

          本文链接:https://www.haomeiwen.com/subject/zsnwhftx.html