Buffer的API文档讲解

1.put()与get()

/*
    ByteBuffer类型化的 get() 与 put()
    byteBuffer可以存不同的类型,但是取的时候得按顺序取(字节码会自动识别),但如果循环取,会取错值
 */
public static void main(String[] args) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(64);

    buffer.putInt(15);
    buffer.putLong(60000000L);
    buffer.putDouble(15.666);
    buffer.putChar('学');
    buffer.putShort((short) 2);
    buffer.putChar('习');

    buffer.flip();

    System.out.println(buffer.getInt());
    System.out.println(buffer.getLong());
    System.out.println(buffer.getDouble());
    System.out.println(buffer.getChar());
    System.out.println(buffer.getShort());
    System.out.println(buffer.getChar());
}

2.slice()

/**
     * Creates a new byte buffer whose content is a shared subsequence of
     * this buffer's content.
     *
     * <p> The content of the new buffer will start at this buffer's current
     * position.  Changes to this buffer's content will be visible in the new
     * buffer, and vice versa(反之亦然); the two buffers' position, limit, and mark
     * values will be independent(独立的).
     *
     * <p> The new buffer's position will be zero, its capacity and its limit
     * will be the number of bytes remaining in this buffer, and its mark
     * will be undefined.  The new buffer will be direct if, and only if, this
     * buffer is direct, and it will be read-only if, and only if, this buffer
     * is read-only.  </p>
     *
     * @return  The new byte buffer
     */
public abstract ByteBuffer slice();

public class NioTest {
    /**
     *   Slice Buffer,与原有 buffer 共享相同的数组
     */
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte)i);
        }

        buffer.position(2);
        buffer.limit(6);

        ByteBuffer slice = buffer.slice();
        System.out.println(slice);//java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]

        for (int i = 0; i < slice.capacity(); i++) {
            byte b = slice.get(i);
            b += 100;
            slice.put(i, b);
        }

        buffer.position(0);
        buffer.limit(buffer.capacity());

        while (buffer.hasRemaining()) {
            System.out.print(buffer.get() + ", ");
        }

    }

}

输出:
java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
0, 1, 102, 103, 104, 105, 6, 7, 8, 9, 

3.asReadOnlyBuffer()

/**
 * Creates a new, read-only byte buffer that shares this buffer's
 * content.
 *
 * <p> The content of the new buffer will be that of this buffer.  Changes
 * to this buffer's content will be visible in the new buffer(改变原来的buffer影响新Buffer); 
 * the new buffer itself, however, will be read-only and will not allow the shared
 * content to be modified(新buffer只可以读,不可以修改).  The two buffers' position, limit, 
 * and mark values will be independent(独立的).
 *
 * <p> The new buffer's capacity, limit, position, and mark values will be
 * identical to those of this buffer(初始值是和原来的buffer相同的).
 *
 * <p> If this buffer is itself read-only then this method behaves in
 * exactly the same way as the {@link #duplicate duplicate} method.  </p>
 *
 * @return  The new, read-only byte buffer
 */
public abstract ByteBuffer asReadOnlyBuffer();
/**
 *   只读Buffer,我们随时可以将一个普通buffer调用asReadOnlyBuffer()返回一个只读Buffer
 *   但不能将一个只读Buffer转换为读写Buffer
 *   只读缓存底层是:HeapByteBufferR,而普通缓冲底层是:HeapByteBuffer。
 */
public static void main(String[] args) {
    ByteBuffer buffer = ByteBuffer.allocate(10);

    System.out.println(buffer.getClass());

    for (int i = 0; i < buffer.capacity(); i++) {
        buffer.put((byte)i);
    }

    ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
    System.out.println(readOnlyBuffer.getClass());
    System.out.println(readOnlyBuffer);

    readOnlyBuffer.position(0);

    //readOnlyBuffer.put((byte) 3);

}

class java.nio.HeapByteBuffer
class java.nio.HeapByteBufferR
java.nio.HeapByteBufferR[pos=10 lim=10 cap=10]//初始值为原来buffer的值

是怎么实现只读的呢?查看源码便知:

public ByteBuffer put(byte x) {
     throw new ReadOnlyBufferException();
}

只是在写操作的方法里面,直接抛异常即可

4.wrap()

/**
 * Wraps a byte array into a buffer.
 *
 * <p> The new buffer will be backed by the given byte array;
 * that is, modifications to the buffer will cause the array to be modified
 * and vice versa.  The new buffer's capacity and limit will be
 * <tt>array.length</tt>, its position will be zero, and its mark will be
 * undefined.  Its {@link #array backing array} will be the
 * given array, and its {@link #arrayOffset array offset>} will
 * be zero.  </p>
 *
 * @param  array
 *         The array that will back this buffer
 *
 * @return  The new byte buffer
 */
public static ByteBuffer wrap(byte[] array) {
    return wrap(array, 0, array.length);
}

返回以传入的数组为底层数组的buffer,修改buffer将会导致数组中的数据也会修改,反之亦然。

但是需要注意这样一点:

​ 你既可以通过buffer来修改底层的字节数组,也可以直接操纵字节数组,这么做是比较危险的操作,除非

你要保证不会对已有的字节数组改动,都通过ByteByffer来进行操作,就不会有问题。

5.allocateDirect()

/**
 * Allocates a new direct byte buffer.
 *
 * <p> The new buffer's position will be zero, its limit will be its
 * capacity, its mark will be undefined, and each of its elements will be
 * initialized to zero.  Whether or not it has a {@link #hasArray backing array} 
 * is unspecified.
 */
public static ByteBuffer allocateDirect(int capacity) {
    return new DirectByteBuffer(capacity);
}
DirectByteBuffer和HeapByteBuffer有什么区别呢?
HeapByteBuffer :
    在进行IO操作的时候多了一个拷贝到过程,会把Java堆内存中的数组拷贝到Java内存模型之外的操作系统的某块内存区域中,然后这块内存区域会直接的与IO设备进行交互。
DirectByteBuffer:
    直接将数据存储在堆外内存,就减少了数组从java堆内向外拷贝的这一过程,简称零拷贝。
    并且在DirectByteBuffer对象中,会有一个 long 类型的 adress 来存储数据所在的地址。但是为了访问方便,这个变量定义在了Buffer中。
    // Used only by direct buffers
    // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
    long address;

好处:
    1)零拷贝
    2)在gc的时候,如果是标记整理算法,那么在移动byte[]数组时,就会涉及copy,如果选择堆外内存就不会因为垃圾回收而复制。
堆外内存怎么释放呢?
    在DirectByteBuffer被回收掉的时候
1604825361351.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271