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
33
votes
4 answers

Why the odd performance curve differential between ByteBuffer.allocate() and ByteBuffer.allocateDirect()

I'm working on some SocketChannel-to-SocketChannel code which will do best with a direct byte buffer--long lived and large (tens to hundreds of megabytes per connection.) While hashing out the exact loop structure with FileChannels, I ran some…
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
33
votes
5 answers

How to create ByteArrayInputStream from a file in Java?

I have a file that can be any thing like ZIP, RAR, txt, CSV, doc etc. I would like to create a ByteArrayInputStream from it. I'm using it to upload a file to FTP through FTPClient from Apache Commons Net. Does anybody know how to do it? For…
itro
  • 7,006
  • 27
  • 78
  • 121
32
votes
7 answers

How to garbage collect a direct buffer in Java

I have a memory leak that I have isolated to incorrectly disposed direct byte buffers. ByteBuffer buff = ByteBuffer.allocateDirect(7777777); The GC collects the objects that harbor these buffers but does not dispose of the buffer itself. If I…
mglmnc
  • 1,400
  • 2
  • 14
  • 15
30
votes
6 answers

Deep copy duplicate() of Java's ByteBuffer

java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer?
Mr. Red
  • 301
  • 1
  • 3
  • 3
29
votes
6 answers

C++ equivalent of Java ByteBuffer?

I'm looking for a C++ "equivalent" of Java ByteBuffer. I'm probably missing the obvious or just need an isolated usage example to clarify. I've looked through the iostream family & it looks like it may provide a basis. Specifically, I want to be…
user172783
28
votes
2 answers

ByteBuffer Little Endian insert not working

I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the receiving part, however sending not works. Strange because for me it seems…
Sandor
  • 1,839
  • 2
  • 18
  • 22
26
votes
7 answers

Can multiple threads see writes on a direct mapped ByteBuffer in Java?

I'm working on something that uses ByteBuffers built from memory-mapped files (via FileChannel.map()) as well as in-memory direct ByteBuffers. I am trying to understand the concurrency and memory model constraints. I have read all of the relevant…
Alex Miller
  • 69,183
  • 25
  • 122
  • 167
24
votes
5 answers

How to fill byte array with junk?

I am using this: byte[] buffer = new byte[10240]; As I understand this initialize the buffer array of 10kb filled with 0s. Whats the fastest way to fill this array (or initialize it) with junk data every time? I need to use that array like >5000…
flyout
  • 497
  • 1
  • 9
  • 16
23
votes
7 answers

How to put data from an OutputStream into a ByteBuffer?

In Java I need to put content from an OutputStream (I fill data to that stream myself) into a ByteBuffer. How to do it in a simple way?
Rasto
  • 17,204
  • 47
  • 154
  • 245
21
votes
2 answers

Memory-Mapped MappedByteBuffer or Direct ByteBuffer for DB Implementation?

This looks like a long question because of all the context. There are 2 questions inside the novel below. Thank you for taking the time to read this and provide assistance. Situation I am working on a scalable datastore implementation that can…
Riyad Kalla
  • 10,604
  • 7
  • 53
  • 56
21
votes
1 answer

JNI - native method with ByteBuffer parameter

I've got a method: public native void doSomething(ByteBuffer in, ByteBuffer out); Generated by javah C/C++ header of this method is: JNIEXPORT void JNICALL Java__MyClass_doSomething (JNIEnv *, jobject, jobject, jobject, jint, jint); How can I get…
Arek
  • 3,106
  • 3
  • 23
  • 32
20
votes
7 answers

How to convert a double into a byte array in swift?

I know how to do it in java (see here), but I couldn't find a swift equivalent for java's ByteBuffer, and consequently its .putDouble(double value) method. Basically, I'm looking for a function like this: func doubleToByteArray(value: Double) ->…
Panini Raman
  • 491
  • 1
  • 5
  • 12
18
votes
4 answers

Java ByteBuffer performance issue

While processing multiple gigabyte files I noticed something odd: it seems that reading from a file using a filechannel into a re-used ByteBuffer object allocated with allocateDirect is much slower than reading from a MappedByteBuffer, in fact it is…
Folkert van Heusden
  • 433
  • 4
  • 17
  • 38
18
votes
7 answers

Extending ByteBuffer class

Is there any way to create class that extends ByteBuffer class? Some abstract methods from ByteBuffer are package private, and if I create package java.nio, security exception is thrown. I would want to do that for performance reasons - getInt for…
Sarmun
  • 2,378
  • 2
  • 22
  • 24
18
votes
3 answers

How to initialize a ByteBuffer if you don't know how many bytes to allocate beforehand?

Is this: ByteBuffer buf = ByteBuffer.allocate(1000); ...the only way to initialize a ByteBuffer? What if I have no idea how many bytes I need to allocate..? Edit: More details: I'm converting one image file format to a TIFF file. The problem is the…
Tony Stark
  • 24,588
  • 41
  • 96
  • 113
1
2
3
61 62