2.4 ByteBuffer
在这一小节中,我们将更详细的讨论ByteBuffer。除了boolean其他所有的基本数据类型都相应的Buffer类型,但是ByteBuffer类型具有很多其他Buffer类型不具备的特性。字节是操作系统及其IO设备使用的基本数据类型。当在操作系统和虚拟机JVM中传递数据时,经常需要将其他的数据类型转换为组成他们的基本字节数据。就像我们下面将要看到的,面向字节级别的系统IO经常在设计Buffer和他们打交道的服务中被深入的考虑。
下面是ByteBuffer详细的API清单,以供参看。下面有一些方法已经在以前的小节中被介绍过了,那些新出现的方法将在这个小节或者后面的小节中介绍。
package java.nio;
public abstract class ByteBuffer
extends Buffer implements Comparable{
public static ByteBuffer allocate(int capacity);
public static ByteBuffer allocateDirect(int capacity);
public abstract boolean isDirect();
public static ByteBuffer wrap(byte[] array,int offset,int length);
public static ByteBuffer wrap(byte[] array);
public abstract ByteBuffer duplicate();
public abstract ByteBuffer asReadOnlyBuffer();
public abstract ByteBuffer slice();
public final boolean hasArray();
public final byte[] array();
public final int arrayOffset();
public abstract byte get();
public abstract byte get(int index);
public ByteBuffer get(byte[] dst);
public ByteBuffer get(byte[] dst,int offset,int length);
public abstract ByteBuffer put(byte b);
public abstract ByteBuffer put(int index,byte b);
public ByteBuffer put(ByteBuffer src);
public ByteBuffer put(byte[] src,int offset,int length);
public final ByteBuffer put(byte[] src);
public final ByteOrder order();
public final ByteBuffer order(ByteOrder bo);
public abstract CharBuffer asCharBuffer();
public abstract ShortBuffer asShortBuffer();
public abstract IntBuffer asIntBuffer();
public abstract LongBuffer asLongBuffer();
public abstract FloatBuffer asFloatBuffer();
public abstract DoubleBuffer asDoubleBuffer();
public abstract char getChar();
public abstract char getChar(int index);
public abstract ByteBuffer putChar(char value);
public abstract ByteBuffer putChar(int index,char value);
public abstract short getShort();
public abstract short getShort(int index);
public abstract ByteBuffer putShort(short value);
public abstract ByteBuffer putShort(int index,short value);
public abstract int getInt();
public abstract int getInt(int index);
public abstract ByteBuffer putInt(int value);
public abstract ByteBuffer putInt(int index,int value);
public abstract long getLong();
public abstract long getLong(int index);
public abstract ByteBuffer putLong(long value);
public abstract ByteBuffer putLong(int index,long value);
public abstract float getFloat();
public abstract float getFloat(int index);
public abstract ByteBuffer putFloat(float value);
public abstract ByteBuffer putFloat(int index,float value);
public abstract double getDouble();
public abstract double getDouble(int index);
public abstract ByteBuffer putDouble(double value);
public abstract ByteBuffer putDouble(int index,double value);
public abstract ByteBuffer compact();
public boolean equals(Object ob);
public int compareTo(Object ob);
public String toString();
public int hashcode();
}
注意: 字节通常都是八位吗? 现在,字节被通常认为是八个比特位的。但是以前并不总是这样。在过去的前几十年,字节可以是3到12或者更多个比特位,通常是6到9个比特位。八比特位的字节来至于实际和商业的推动。它的实用价值表现在以下几方面:首先8个比特足够表达英文的字符集,其次8是2的幂次,对硬件来说更容易设计,然后它能刚好表达两个十六进制数字,最后8个倍数很容易由多个字节结合起来存储有用的数值。推动8位字节的商业力量是IBM。在1960年首先推出的IBM360大型机,就是使用的8位字节。现在我几乎把这个问题解释清楚了。如果你还想知道更多的细节,你去咨询IBM的Bob Bemer本人吧,他的网站是:http://www.bobbemer.com/BYTE.HTM。 |