Questions tagged [bytebuffer]

A binary buffer with no specific encoding. Use this tag only if you're having specific problems in relation with byte buffers

A binary buffer with no specific encoding.

Examples include byte[] in Java, char[] or uint8_t[] in C++. Objects like std::wstring_t that have a specific encoding (like UTF-8) assigned to them are no byte buffers.

Use this tag only if you're having specific problems in relation with byte buffers. Do not use this tag if there is no indication that the byte buffer you are using has any relation to the problem you're asking about.

919 questions
11
votes
3 answers

Java - optimize writing values as bits to bytebuffer

I am currently working on some networking code (this is my first server) and had a quick question about optimizing a specific function which writes values as bits and then packs them into a byte. The reason for optimizing this function is because it…
Eladian
  • 958
  • 10
  • 29
11
votes
2 answers

Could ByteBuffer implement DataOutput/DataInput?

Is there some subtle reason why java.nio.ByteBuffer does not implement java.io.DataOutput or java.io.DataInput, or did the authors just not choose to do this? It would seem straightforward to map the calls (e.g. putInt() -> writeInt()). The basic…
Justin
  • 4,437
  • 6
  • 32
  • 52
11
votes
1 answer

Java - Heap vs Direct memory access

I recenty came across sun.misc.Unsafe class, allowing user to allocate,deallocate and in general access memory in a similar fashion like in C. I read in a couple of blogs that tackle this issue e.g. Which is faster - heap or direct memory - test…
Bober02
  • 15,034
  • 31
  • 92
  • 178
11
votes
3 answers

Efficient way to convert io.netty.buffer.ByteBuf to java.nio.ByteBuffer

I came across this query: Create a ByteBuf in Netty 4.0 about conversion from byte[] to ByteBuf and ByteBuffer to ByteBuf. I was curious to know about the conversion the other way: io.netty.buffer.ByteBuf to java.nio.ByteBuffer and how to do it…
Corehacker
  • 267
  • 1
  • 2
  • 7
11
votes
1 answer

Using sun.misc.Unsafe, what is the fastest way to scan bytes from a Direct ByteBuffer?

BACKGROUND Assume I have a direct ByteBuffer: ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); and assume I am passing the buffer to an AsynchronousSocketChannel to read chunks of data off that socket up to X bytes at a time (1024 in the…
Riyad Kalla
  • 10,604
  • 7
  • 53
  • 56
11
votes
2 answers

How can I get short[] from a ByteBuffer

I am using JNI code in an Android project in which the JNI native function requires a short[] argument. However, the original data is stored as a ByteBuffer. I'm trying the convert the data format as follows. ByteBuffer rgbBuf =…
bei
  • 221
  • 4
  • 7
10
votes
2 answers

Get the pointer of a Java ByteBuffer though JNI

How can I get a pointer to the inner array of a Java ByteBuffer? JNIEXPORT void JNICALL test(JNIEnv *env, jobject thiso) { jclass cls = env->FindClass("java/nio/ByteBuffer"); jmethodID aloc = env->GetStaticMethodID(cls, "allocateDirect",…
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
10
votes
3 answers

Getting string from netty ByteBuf

How can I get the string from a netty ByteBuf? As of now I am able to get it character by character. Is there a way to get the string object directly? // message is of type ByteBuf for (int i = 0; i < message.capacity(); i++) { byte b =…
aries
  • 849
  • 3
  • 11
  • 24
10
votes
1 answer

"Mechanically generated" java source files in the Java source code

As I was looking through the Java source code, I found some unusual files, mostly related to ByteBuffers in the java.nio package which had a very messy source code and were labelled This file was mechanically generated: Do not edit!. These files…
user1779715
10
votes
5 answers

Concat two ByteBuffers in Java

How can I concat two ByteBuffers to one ByteBuffer? The following doesn't work: ByteBuffer bb = ByteBuffer.allocate(100); ByteBuffer bb2 = ByteBuffer.allocate(200); bb.allocate(200).put(bb2); …
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
10
votes
2 answers

byte[] to ushort[]

Here is my question. Bear with me giving a little explanation: I am reading tiff image into buffer; Each pixel of my tiff is represented by a ushort (16 bits data, non-negtive). My image size is 64*64 = 4096. When my tiff is loaded into buffer,…
Nick Tsui
  • 524
  • 2
  • 9
  • 29
10
votes
1 answer

Am I doing this correctly?

Before I ask for help, let me tell you what I did: Assuming I have a sampling rate of 8000Hz and sample size of 16 bits (2 bytes), at the end of the second I need 16000 byte or 8000 short. Now I have a 10fps recording speed then for each fps I…
An SO User
  • 24,612
  • 35
  • 133
  • 221
9
votes
2 answers

Direct java.nio.ByteBuffer vs Java Array Performance Test

I wanted to compare performance of a direct byte buffer (java.nio.ByteBuffer, off-heap) and a heap buffer (achieved via array) for both read and writes. My understanding was, ByteBuffer being off-heap gets at least two benefits over a heap buffer.…
Abidi
  • 7,846
  • 14
  • 43
  • 65
9
votes
4 answers

Java Circular Byte Buffer that Extends java.nio.ByteBuffer

Every Java circular byte buffer implementation I have seen referenced on SO and elsewhere does not extend java.nio.ByteBuffer, which for me is necessary for use with a SocketChannel. Does anyone know of an open source implementation that extends…
LINEMAN78
  • 2,562
  • 16
  • 19
9
votes
1 answer

Java - When does direct buffer released?

Since it's out of jvm heap & gc, when does it released? Or, it remain until process termination? I already checked: how to garbage collect a direct buffer java Deallocating Direct Buffer Native Memory in Java for JOGL ByteBuffer.allocate() vs.…
Eric
  • 22,183
  • 20
  • 145
  • 196