我正在使用ByteBuffers并将FileChannels二进制数据写入文件.当为大文件或连续多个文件执行此操作时,我得到一个OutOfMemoryError例外.我在其他地方读过,Bytebuffers与NIO一起使用已被打破,应该避免.你们中是否有人遇到过这种问题,并找到了一个有效地在java文件中保存大量二进制数据的解决方案?
jvm选项是否可行-XX:MaxDirectMemorySize?
有没有办法用BufferedReader读取ByteBuffer而不必先将它变成String?我想通过相当大的ByteBuffer读取文本行,出于性能原因,我想避免将其写入磁盘.在ByteBuffer上调用toString不起作用,因为生成的String太大(它会抛出java.lang.OutOfMemoryError:Java堆空间).我原以为API中会有一些东西将ByteBuffer包装在合适的阅读器中,但我似乎找不到合适的东西.
这是一个缩写代码示例,说明了我在做什么):
// input stream is from Process getInputStream()
public String read(InputStream istream)
{
ReadableByteChannel source = Channels.newChannel(istream);
ByteArrayOutputStream ostream = new ByteArrayOutputStream(bufferSize);
WritableByteChannel destination = Channels.newChannel(ostream);
ByteBuffer buffer = ByteBuffer.allocateDirect(writeBufferSize);
while (source.read(buffer) != -1)
{
buffer.flip();
while (buffer.hasRemaining())
{
destination.write(buffer);
}
buffer.clear();
}
// this data can be up to 150 MB.. won't fit in a String.
result = ostream.toString();
source.close();
destination.close();
return result;
}
// after the process is run, we call this method with the String
public …Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序,我正在压缩大量信息并将其以字节存储在缓冲区中.我不能使用,ByteBuffer因为我不知道finall的大小.
实现这个更好的方法是什么?
我正在使用在netty顶部实现的一些框架.我正在使用以下两个选项从客户端向服务器发送消息.我想这两个片段应该将相同的字节写入套接字,它在服务器端的行为是不同的.它有什么不同?
选项1:好的
ChannelBuffer buf = ChannelBuffers.buffer(1);
buf.writeByte(0x1c);
e.getChannel().write(buf);
Run Code Online (Sandbox Code Playgroud)
选项2:失败
ByteBuffer buf = ByteBuffer.allocate(1);
buf.put(0x1c);
e.getChannel().write(ChannelBuffers.wrappedBuffer(buf));
Run Code Online (Sandbox Code Playgroud) 我不确定为什么以下示例给出了缓冲区溢出异常.希望有人可以解释原因,以及我如何正确地做到这一点.
这很简单:
ByteBuffer bf = ByteBuffer.allocate(4);
bf.order(ByteOrder.BIG_ENDIAN);
bf.putInt(8);
bf.putInt(7); // Throws exception
Run Code Online (Sandbox Code Playgroud)
目标:[0,0,8,7]
提前致谢!
在运行时,我从设备获取字节缓冲区数据,试图对数据进行解码以读取其内容。
当我使用字符串打印字节缓冲区时,显示如下,
java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下所有已知格式进行解码,
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(paramByteBuffer);
String text = charBuffer.toString();
System.out.println("UTF-8"+text);
charBuffer = StandardCharsets.UTF_16.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16"+text);
charBuffer = StandardCharsets.ISO_8859_1.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("ISO_8859_1"+text);
charBuffer = StandardCharsets.UTF_16BE.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16BE"+text);
charBuffer = StandardCharsets.UTF_16LE.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16LE"+text);
charBuffer = StandardCharsets.US_ASCII.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("US_ASCII"+text);
Run Code Online (Sandbox Code Playgroud)
一切都会返回空数据。
解码字节缓冲区数据的方法有哪些?
我今天刚开始学习D,我真的需要读取和写入这样的数据:
byte[] bytes = ...;
ByteBuffer buf = new ByteBuffer(bytes);
int a = buf.getInt();
byte b = buf.getByte();
short s = buf.getShort();
buf.putInt(200000);
Run Code Online (Sandbox Code Playgroud)
D中有什么内容可以实现这一点,还是我必须自己制作?
我试图转换int到byte[]并写了这以下内容:
byte[] bytes = new byte[4];
ByteBuffer buff = ByteBuffer.allocate(4);
buff.putInt(1222);
buff.get(bytes);
Run Code Online (Sandbox Code Playgroud)
但结果我得到了没有详细消息的java.nio.BufferOverflowException.
至于我,代码是有效的.怎么了?如何转换int为byte[]?
我的代码是:
if (frameRGBABuffer == null) {
frameRGBABuffer = ByteBuffer.allocateDirect(cameraHeight * cameraWidth * 4)
.order(ByteOrder.nativeOrder());
}
Log.d("tag",frameRGBABuffer.array().length)
Run Code Online (Sandbox Code Playgroud)
我的相机分辨率是1280×720,因此frameRGBABuffer应该分配3686400字节的空间.
但奇怪的是长度frameRGBABuffer.array()是3686407.为什么它有额外的7个字节空格?
顺便说一下,frameRGBABuffer.array()不会抛出异常并返回带有数据的byte []
似乎Android分配了7个额外的空间来处理对齐.源代码是:
MemoryRef(int capacity) {
VMRuntime runtime = VMRuntime.getRuntime();
buffer = (byte[]) runtime.newNonMovableArray(byte.class, capacity + 7);
allocatedAddress = runtime.addressOf(buffer);
// Offset is set to handle the alignment: http://b/16449607
offset = (int) (((allocatedAddress + 7) & ~(long) 7) - allocatedAddress);
isAccessible = true;
isFreed = false;
}
Run Code Online (Sandbox Code Playgroud) 我有一个从Cpp程序获取的字节数组。
arr[0..3] // a real32,
arr[4] // a uint8,
Run Code Online (Sandbox Code Playgroud)
我怎么解释arr[4]为int?
(uint)arr[4] // Err: can't implicitly convert string to int.
BitConverter.ToUint16(arr[4]) // Err: Invalid argument.
buff[0+4] as int // Err: must be reference or nullable type
Run Code Online (Sandbox Code Playgroud)
我是否必须将连续字节清零才能将其解释为UInt16?
好,这是混乱。最初,我定义了我的课程。
byte[] buff;
buff = getSerialBuffer();
public class Reading{
public string scale_id;
public string measure;
public int measure_revised;
public float wt;
}
rd = new Reading();
// !! here is the confusion... !!
// Err: Can't …Run Code Online (Sandbox Code Playgroud) bytebuffer ×10
java ×8
nio ×2
.net-3.5 ×1
android ×1
arrays ×1
asynchronous ×1
buffer ×1
bytearray ×1
c# ×1
d ×1
decode ×1
filechannel ×1
int ×1
netty ×1
read-write ×1
uint8t ×1