Java中ByteBuffer的使用粗解

1个月前 152次点击 来自 其他

标签: Java

Java中提供了内置数据类型byte,其主要有以下特点:

  • byte 数据类型是8位、有符号的,以二进制补码表示的整数;
  • 最小值是 -128(-2^7);最大值是 127(2^7-1);默认值是 0;
  • byte 类型用在大型数组中节约空间,主要代替整数,因为 byte 变量占用的空间只有 int 类型的四分之一;
  • 例子:byte a = 100,byte b = -50。

ByteBuffer是一个字节缓冲区,ByteBuffer分为读与写模式,其主要有五个属性:mark,position,limit,capacity和array。

这五个属性的作用如下:

  • mark:记录了当前所标记的索引下标;
  • position:对于写入模式,表示当前可写入数据的下标,对于读取模式,表示接下来可以读取的数据的下标;
  • limit:对于写入模式,表示当前可以写入的数组大小,默认为数组的最大长度,对于读取模式,表示当前最多可以读取的数据的位置下标;
  • capacity:表示当前数组的容量大小;
  • array:保存了当前写入的数据。

Examples 1:

验证读写模式切换后,position,limit和capacity三个属性值的变化。

public class ByteBufferApp {
  @Test
  public void testBuffer() {
    // 初始化一个大小为6的ByteBuffer
    ByteBuffer buffer = ByteBuffer.allocate(6);
    print(buffer);  // 初始状态:position: 0, limit: 6, capacity: 6

    // 往buffer中写入3个字节的数据
    buffer.put((byte) 1);
    buffer.put((byte) 2);
    buffer.put((byte) 3);
    print(buffer);  // 写入之后的状态:position: 3, limit: 6, capacity: 6

    System.out.println("************** after flip **************");
    buffer.flip();
    print(buffer);  // 切换为读取模式之后的状态:position: 0, limit: 3, capacity: 6

    buffer.get();
    buffer.get();
    print(buffer);  // 读取两个数据之后的状态:position: 2, limit: 3, capacity: 6
  }

  private void print(ByteBuffer buffer) {
    System.out.printf("position: %d, limit: %d, capacity: %d\n",
      buffer.position(), buffer.limit(), buffer.capacity());
  }
}

Examples 2:

// Java program to demonstrate
// get() method

import java.nio.*;
import java.util.*;

public class GFG {

	public static void main(String[] args)
	{

		// Declaring the capacity of the ByteBuffer
		int capacity = 5;

		// Creating the ByteBuffer
		try {

			// creating object of ByteBuffer
			// and allocating size capacity
			ByteBuffer bb = ByteBuffer.allocate(capacity);

			// putting the int to byte value in ByteBuffer
			bb.put((byte)20);
			bb.put((byte)30);
			bb.put((byte)40);
			bb.rewind();

			// print the ByteBuffer
			System.out.println("Original ByteBuffer: "
							+ Arrays.toString(bb.array()));

			// Reads the byte at this buffer's current position
			// using get() method
			byte value = bb.get();

			// print the byte value
			System.out.println("\nByte Value: " + value);

			// Reads the Byte at this buffer's next position
			// using get() method
			byte value1 = bb.get();

			// print the Float value
			System.out.print("\nNext Byte Value: " + value1);
		}

		catch (IllegalArgumentException e) {

			System.out.println("\nException Thrown : " + e);
		}

		catch (ReadOnlyBufferException e) {

			System.out.println("\nException Thrown : " + e);
		}

		catch (BufferUnderflowException e) {

			System.out.println("\nException Thrown : " + e);
		}
	}
}

Output:

Original ByteBuffer:  [20, 30, 40, 0, 0]

Byte Value: 20

Next Byte Value: 30

Examples 3:

// Java program to demonstrate
// get() method

import java.nio.*;
import java.util.*;

public class GFG {

	public static void main(String[] args)
	{

		// Declaring the capacity of the ByteBuffer
		int capacity = 3;

		// Creating the ByteBuffer
		try {

			// creating object of ByteBuffer
			// and allocating size capacity
			ByteBuffer bb = ByteBuffer.allocate(capacity);

			// putting the int to byte typecast value in ByteBuffer
			bb.put((byte)20);
			bb.put((byte)30);

			// print the ByteBuffer
			System.out.println("Original ByteBuffer: "
							+ Arrays.toString(bb.array()));

			// Reads the byte at this buffer's current position
			// using get() method
			byte value = bb.get();

			// print the byte value
			System.out.println("\nByte Value: " + value);

			// Reads the Byte at this buffer's next position
			// using get() method
			System.out.print("\nsince the buffer current position is incremented");
			System.out.print(" to greater than its limit ");

			byte value1 = bb.get();

			// print the Byte value
			System.out.print("\nNext Byte Value: " + value1);
		}

		catch (IllegalArgumentException e) {

			System.out.println("\nIllegalArgumentException catched");
		}

		catch (ReadOnlyBufferException e) {

			System.out.println("\nReadOnlyBufferException catched");
		}

		catch (BufferUnderflowException e) {

			System.out.println("\nException throws : " + e);
		}
	}
}

Output:

Original ByteBuffer:  [20, 30, 0]

Byte Value: 0

since the buffer current position is incremented to greater than its limit 
Exception throws : java.nio.BufferUnderflowException
Card image cap
开发者雷

尘世间一个小小的开发者,每天增加一些无聊的知识,就不会无聊了

要加油~~~

技术文档 >> 系列应用 >>
热推应用
Let'sLearnSwift
学习Swift的入门教程
PyPie
Python is as good as Pie
标签