JavaNioByteBuffer:放置和获取字符串
我有以下测试代码。我想知道如何使用JavaNioByteBuffer输入和获取字符串。 我在需要帮助的地方添加了两条评论JavaNioByteBuffer:放置和获取字符串,java,Java,我有以下测试代码。我想知道如何使用JavaNioByteBuffer输入和获取字符串。 我在需要帮助的地方添加了两条评论 package testPipe; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.
package testPipe;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class TestMemBuff {
static final String dataFile = "invoicedata";
static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
static final int[] units = { 12, 8, 13, 29, 50 };
static final String[] descs = { "Java T-shirt", "Java Mug",
"Duke Juggling Dolls", "Java Pin", "Java Key Chain" };
public static Charset charset = Charset.forName("UTF-8");
public static CharsetEncoder encoder = charset.newEncoder();
public static CharsetDecoder decoder = charset.newDecoder();
public static void main(String[] args) throws UnsupportedEncodingException {
double price;
int unit;
String desc;
double total = 0.0;
ByteBuffer buf = ByteBuffer.allocate(1024);
for (int i = 0; i < prices.length; i++) {
buf.putDouble(prices[i]);
buf.putInt(units[i]);
buf.asCharBuffer().put(descs[i]); // Is it correct?
}
buf.flip();
// INPUT
while (buf.hasRemaining()) {
price = buf.getDouble();
unit = buf.getInt();
desc = buf.asCharBuffer().toString(); //This must be wrong!
System.out.format("You ordered %d" + " units of %s at $%.2f%n",
unit, desc, price);
total += unit * price;
}
}
}
等等
感谢您的关注
ByteBuffer.asCharBuffer()
有自己的行为。它创建了一个新的缓冲区,与ByteBuffer共享内容,但有自己的独立位置和限制。
因此,在CharBuffer上放置()和get(),不要更改ByteBuffer上的位置
因此,我将完全避免使用ByteBuffer.asCharBuffer()
,只要这不是您所需要的
因此,解决方案是将字节写入并读取到原始ByteBuffer中:
buf.putInt(descs[i].length);
buf.put(descs[i].getBytes());`
要再次读取字符串,请读取长度并自行分配字节[]:
int stringLength = buf.getInt();
byte[] bytes = new byte[stringLength];
buf.get(bytes);
desc = new String(bytes);
写作部分:
for (int i = 0; i < prices.length; i++) {
buf.putDouble(prices[i]);
buf.putInt(units[i]);
byte[] descsBytes = descs[i].getBytes();
buf.putInt(descsBytes.length);
buf.put(descsBytes);
}
它真的打印
?
?toString
做什么?是的。编码/解码一定有问题。谢谢,但我还是有错误:你订购了12个单位的????线程“main”中的异常java.nio.BufferUnderflowException位于java.nio.CharBuffer.get(未知源代码)处的java.nio.CharBuffer.get(未知源代码)位于testPipe.TestMemBuff.main(TestMemBuff.java:48)处,正常工作!非常感谢你!您能告诉我如何正确使用ByteBuffer.asCharBuffer()吗?我的方法有什么问题?asCharBuffer
将只创建ByteBuffer
的视图,其位置值将独立于原始缓冲区。因此,在第二次迭代中写入缓冲区时,字符串字节被double
值覆盖,然后int
等等…请参考:顺便说一句,如果您确定descs[i]
的最大长度,您可以预分配字节[]字节
以避免读取循环中出现新的运算符。感谢您抽出时间。贪婪的回答:)
for (int i = 0; i < prices.length; i++) {
buf.putDouble(prices[i]);
buf.putInt(units[i]);
byte[] descsBytes = descs[i].getBytes();
buf.putInt(descsBytes.length);
buf.put(descsBytes);
}
while (buf.hasRemaining()) {
price = buf.getDouble();
unit = buf.getInt();
int len = buf.getInt();
byte[] bytes = new byte[len];
buf.get(bytes);
desc = new String(bytes);
System.out.format("You ordered %d" + " units of %s at $%.2f%n",
unit, desc, price);
total += unit * price;
}