---
title: "Buffer的几个属性和方法速记"
author: "lixjluck"
date: "2011-09-01T16:49:08Z"
tags: []
categories: ["编程语言"]
slug: "buffer"
draft: false
lastmod: "2011-09-02T10:49:43Z"
id: 1163490
url: "https://lixjluck.iteye.com/blog/1163490"
---

java.nio.Buffer 

 

属性

 

mark   position  
limit 一般情况下limit==catacity capacity 此属性一经设置，就不会改变 remaing limit-position address  

 

查询类方法

 

remaining  limit - position;  hasRemaining limit &lt; position;  

 

改变类方法

 

mark mark=position; reset position=mark; clear

position=0;

limit=capacity;

mark=-1;

flip

limit=position;

position=0;

mark=-1;

rewind

position=0;

mark=-1;

limit()

limit=newLimit;

 

if (position &gt; limit) position = limit;

if (mark &gt; limit) mark = -1;

 

position()

position = newPosition;

 

if (mark &gt; position) mark = -1;

 

System.arraycopy(hb, ix(position()), hb, ix(0), remaining());

 

position(remaining());

limit(capacity());

 

compact slice

new HeapIntBuffer(hb,

-1,

0,

this.remaining(),

this.remaining(),

this.position() + offset);

 

1. 共享底层存储byte\[]
2. mark=-1;
3. position=0;
4. limit=remaining;
5. capacity=remaing;
6. offset = offset+position;

 

 

 

duplicate

 new HeapByteBuffer(hb,

 

this.markValue(),

this.position(),

this.limit(),

this.capacity(),

offset);

 

1. 共享底层存储byte\[]
2. mark=mark;
3. position=position;
4. limit=limit;
5. capacity=capacity;
6. offset = offset;