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
9
votes
1 answer

Why are absolute reads from a ByteBuffer not considered thread-safe?

My use case requires a directly allocated ByteBuffer that is written to once and thereafter read by many concurrent threads. All reads are absolute and so I'm never concerned with the buffer's state (position, limit, mark). This article on byte…
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
9
votes
3 answers

ByteBuffer, CharBuffer, String and Charset

I'm trying to sort out characters, their representation in byte sequences according to character sets, and how to convert from one character set to another in Java. I've some difficulties. For instance, ByteBuffer bybf =…
mins
  • 6,478
  • 12
  • 56
  • 75
9
votes
2 answers

A simple rule of when I should use direct buffers with Java NIO for network I/O?

Can someone with the natural gift to explain complex things in an easy and straightforward way address this question? To acquire the best performance when should I use direct ByteBuffers versus regular ByteBuffers when doing network I/O with Java…
JohnPristine
  • 3,485
  • 5
  • 30
  • 49
8
votes
2 answers

ByteBuffer getInt() question

We are using Java ByteBuffer for socket communication with a C++ server. We know Java is Big-endian and Socket communication is also Big-endian. So whenever the byte stream received and put into a ByteBuffer by Java, we call getInt() to get the…
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
8
votes
6 answers

Send data in multiple ways depending on how you want to send it

I have bunch of keys and values that I want to send to our messaging queue by packing them in one byte array. I will make one byte array of all the keys and values which should always be less than 50K and then send to our messaging queue. Packet…
8
votes
2 answers

How to convert ByteBuffer into image in Android

I am receiving jpg image through socket and it is sent as ByteBuffer what I am doing is: ByteBuffer receivedData ; // Image bytes byte[] imageBytes = new byte[0]; // fill in received data buffer with data …
zelf
  • 93
  • 1
  • 1
  • 7
8
votes
4 answers

Convert ByteBuffer to String in Java

I have a byte[] bytes from a ByteBuffer which contains a packet. I want to put the packet in a String. Currently I have the following byte[] bytes = packet.array(); System.out.println("Packet String:" + new String(bytes)); But then I end up…
Liam de Haas
  • 1,258
  • 3
  • 18
  • 39
8
votes
2 answers

Is there a Java ByteBuffer implementation that combines multiple backing ByteBuffers under the hood?

I have one or more ByteBuffers containing parts of a single message. Now I want to read this message but I do not want to copy N ByteBuffer into a single one. My parser expects a single ByteBuffer with the complete message, but my message is divided…
Michelle Queen
  • 249
  • 3
  • 10
8
votes
1 answer

Is there a way to create a direct ByteBuffer from a pointer solely in Java?

Or do I have to have a JNI helper function that calls env->NewDirectByteBuffer(buffer, size)?
Trevor Bernard
  • 992
  • 10
  • 13
8
votes
1 answer

Determining Appropriate Buffer Size

I am using ByteBuffer.allocateDirect() to allocate some buffer memory for reading a file into memory and then eventually hashing that files bytes and getting a file hash (SHA) out of it. The input files range greatly in size, anywhere from a few…
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
8
votes
2 answers

How write big endian ByteBuffer to little endian in Java

I currently have a Java ByteBuffer that already has the data in Big Endian format. I then want to write to a binary file as Little Endian. Here's the code which just writes the file still in Big Endian: public void writeBinFile(String fileName,…
user1575243
  • 165
  • 2
  • 2
  • 9
8
votes
3 answers

byte collection based similar with ByteBuffer from java

I need a C# implementation of something similar with ByteBuffer from Java. Methods of interest - .remaining() - returns the number of elements between the current position and the limit. - .array() - .clear() - .put(byte[], int, int) I started…
pulancheck1988
  • 2,024
  • 3
  • 28
  • 46
7
votes
1 answer

Java/Android - Fast ByteBuffer Parsing

i'm writing a webserver for mobile android based devices in java. This webserver is single-threaded and follow the idea behind nginx, node.js and similar: don't spawn multiple threads just use async operations in an event loop. While using a…
Daniele Salvatore Albano
  • 1,263
  • 2
  • 13
  • 29
7
votes
3 answers

ByteBuffer and Byte Array

Problem I need to convert two ints and a string of variable length to bytes. What I did I converted each data type into a byte array and then added them into a byte buffer. Of which right after that I will copy that buffer to one byte array, as…
darksky
  • 20,411
  • 61
  • 165
  • 254
7
votes
1 answer

When to use Uint8Array, Uint16Array, Uint32Array

I have an application that loads user text through a XMLHttpRequest and returns it in a binary format. I understand that the main difference between 8, 16, and 32 is the amount of bytes per element but I don't know when to use each. For example, for…
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95