ByteBuffer buffer = ByteBuffer.allocate(1024); 时 buffer 为读模式还是写模式
时间: 2023-06-10 07:08:05 浏览: 104
在调用 `ByteBuffer.allocate(1024)` 方法时,返回的 `ByteBuffer` 实例默认是写模式。你可以通过调用 `buffer.flip()` 方法将其切换为读模式。在读模式下,可以从 `ByteBuffer` 中读取数据。在写模式下,可以将数据写入到 `ByteBuffer` 中。
相关问题
import java.nio.ByteBuffer; public class ByteBufferTest { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.allocate(6); print(buffer); buffer.put((byte) 1); buffer.put((byte) 2); buffer.put((byte) 3); print(buffer); buffer.flip(); print(buffer); buffer.get(); buffer.get(); print(buffer); } public static void print(ByteBuffer buffer) { System.out.printf("position: %d, limit: %d, capacity: %d\n", buffer.position(), buffer.limit(), buffer.capacity()); } }
以上代码的输出结果是:
```
position: 0, limit: 6, capacity: 6
position: 3, limit: 6, capacity: 6
position: 0, limit: 3, capacity: 6
position: 2, limit: 3, capacity: 3
```
这段代码使用 ByteBuffer 类测试了缓冲区的写和读操作。首先,创建一个容量为 6 的 ByteBuffer 对象,打印出它的位置、限制和容量信息,输出结果为 `position: 0, limit: 6, capacity: 6`。然后,向缓冲区中写入 3 个字节的数据,打印出缓冲区的位置、限制和容量信息,输出结果为 `position: 3, limit: 6, capacity: 6`。接着,调用 `flip()` 方法,将缓冲区的位置设置为 0,限制设置为 3,打印出缓冲区的位置、限制和容量信息,输出结果为 `position: 0, limit: 3, capacity: 6`。接下来,调用 `get()` 方法读取 2 个字节的数据,打印出缓冲区的位置、限制和容量信息,输出结果为 `position: 2, limit: 3, capacity: 3`。注意,此时缓冲区的容量已经变为 3,因为调用 `flip()` 方法后,缓冲区的限制被设置为了 3,而调用 `get()` 方法会将缓冲区的位置向前移动。
public class BERCodec { public static byte[] encodeInteger(int value) { ByteBuffer buffer = ByteBuffer.allocate(4); buffer.putInt(value); byte[] bytes = buffer.array(); int i = 0; while(i < bytes.length && bytes[i]==0){ i++; } byte[] result = new byte[bytes.length-i+2]; result[0] = 0x02; result[1] = (byte)(bytes.length-i); System.arraycopy(bytes, i, result,2 ,bytes.length-i); return result; } public static int decodeInteger(byte[] data) { int i = data[1]; byte[] s = new byte[4]; System.arraycopy(data,2,s,4-i,i); ByteBuffer wp = ByteBuffer.wrap(s); return wp.getInt(); } public static byte[] encodeOctetString(String value) { byte[] bt = value.getBytes(); byte[] bt1 = new byte[bt.length+2]; bt1[0] = 0x04; bt1[1] = (byte) bt.length; System.arraycopy(bt,0,bt1,2,bt.length); return bt1; } public static String decodeOctetString(byte[] data) { int length = decodeLength(data[1]); return new String(data, 2, length, StandardCharsets.UTF_8); } private static int decodeLength(byte lengthByte) { if ((lengthByte & 0x80) == 0) { return lengthByte; } else { int numBytes = lengthByte & 0x7F; int length = 0; for (int i = 1; i <= numBytes; i++) { length <<= 8; length |= lengthByte & 0xFF; } return length; } } }把编码和解码分开
好的,我来帮你把编码和解码分开:
编码部分:
```
public static byte[] encodeInteger(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(value);
byte[] bytes = buffer.array();
int i = 0;
while(i < bytes.length && bytes[i] == 0) {
i++;
}
byte[] result = new byte[bytes.length - i + 2];
result[0] = 0x02;
result[1] = (byte)(bytes.length - i);
System.arraycopy(bytes, i, result, 2, bytes.length - i);
return result;
}
public static byte[] encodeOctetString(String value) {
byte[] bt = value.getBytes();
byte[] bt1 = new byte[bt.length + 2];
bt1[0] = 0x04;
bt1[1] = (byte) bt.length;
System.arraycopy(bt, 0, bt1, 2, bt.length);
return bt1;
}
```
解码部分:
```
public static int decodeInteger(byte[] data) {
int i = data[1];
byte[] s = new byte[4];
System.arraycopy(data, 2, s, 4 - i, i);
ByteBuffer wp = ByteBuffer.wrap(s);
return wp.getInt();
}
public static String decodeOctetString(byte[] data) {
int length = decodeLength(data[1]);
return new String(data, 2, length, StandardCharsets.UTF_8);
}
private static int decodeLength(byte lengthByte) {
if ((lengthByte & 0x80) == 0) {
return lengthByte;
} else {
int numBytes = lengthByte & 0x7F;
int length = 0;
for (int i = 1; i <= numBytes; i++) {
length <<= 8;
length |= lengthByte & 0xFF;
}
return length;
}
}
```
阅读全文