总结:我有一个ByteBuffer,我在其中提取一些数据。之后,我想通过Socket发送这些数据。
所以,我写了这样的代码:
private static void serialize(ByteBuffer buffer, EmployeeData emp, CharsetEncoder encoder)
{
// id
buffer.putInt(emp.getId());
CharBuffer nameBuffer = CharBuffer.wrap(emp.getFirstName().toCharArray());
ByteBuffer nbBuffer = null;
// length of first name
try
{
nbBuffer = encoder.encode(nameBuffer);
}
catch(CharacterCodingException e)
{
throw new ArithmeticException();
}
System.out.println(String.format("String [%1$s] #bytes = %2$s", emp.getFirstName(), nbBuffer.limit()));
buffer.putInt(nbBuffer.limit());
buffer.put(nbBuffer);
// put lastname
nameBuffer = CharBuffer.wrap(emp.getLastName().toCharArray());
nbBuffer = null;
// length of first name
try
{
nbBuffer = encoder.encode(nameBuffer);
}
catch(CharacterCodingException e)
{
throw new ArithmeticException();
}
System.out.println(String.format("String [%1$s] #bytes = %2$s", emp.getLastName(), nbBuffer.limit()));
buffer.putInt(nbBuffer.limit());
buffer.put(nbBuffer);
// salary
buffer.putInt(emp.getSalary());
}
在调用代码中,我执行以下操作。我首先获得序列化的ByteBuffer,然后将其写入套接字......
Socket client = new Socket("localhost", 8080);
OutputStream oStream = client.getOutputStream();
serialize(buffer, emp, encoder);
buffer.rewind();
while(buffer.hasRemaining())
{
byte temp = buffer.get();
++ written;
}
buffer.rewind();
System.out.println("#Bytes in output buffer: " + written + " limit = " + buffer.limit() + " pos = " + buffer.position() + " remaining = " + buffer.remaining());
int remaining = buffer.remaining();
while(remaining > 0)
{
oStream.write(buffer.get());
-- remaining;
}
我期望buffer.remaining()应该完全等于我输入缓冲区的#bytes。但是我发现它不是,它总是等于1024,这是缓冲区底层数组的大小。
这是我创建缓冲区的方法......
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
byte [] underlyingBuffer = new byte[1024];
ByteBuffer buffer = ByteBuffer.wrap(underlyingBuffer);
buffer.order(ByteOrder.LITTLE_ENDIAN);
这是我得到的打印声明的输出......
String [John] #bytes = 4 String [Smith] #bytes = 5
输出缓冲区中的#Bytes:1024 limit = 1024 pos = 0 remaining = 1024
如何获得放入缓冲区的确切#bytes?
谢谢!
答案 0 :(得分:11)
当你完成将字节放入缓冲区时,你应该flip
它。您可以代替首次拨打rewind
来执行此操作。
java.nio.Buffer
区分capacity
和limit
。如果不翻转它,则limit和capacity与初始化它的数组的长度相同。
通过翻转,限制将设置为您编码的数据的末尾,容量仍为1024. ByteBuffer#remaining
查看位置和限制之间的差值。