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
321
votes
10 answers

converting Java bitmap to byte array

Bitmap bmp = intent.getExtras().get("data"); int size = bmp.getRowBytes() * bmp.getHeight(); ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); byte[] bytes = new byte[size]; try { b.get(bytes, 0,…
Tom Fobear
  • 6,729
  • 7
  • 42
  • 74
222
votes
5 answers

What is the use of ByteBuffer in Java?

What are example applications for a ByteBuffer in Java? Please list any example scenarios where this is used.
Aklin
  • 2,621
  • 2
  • 17
  • 17
165
votes
4 answers

ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()

To allocate() or to allocateDirect(), that is the question. For some years now I've just stuck to the thought that since DirectByteBuffers are a direct memory mapping at OS level, that it would perform quicker with get/put calls than…
user238033
133
votes
14 answers

Byte array to image conversion

I want to convert a byte array to an image. This is my database code from where I get the byte array: public void Get_Finger_print() { try { using (SqlConnection thisConnection = new SqlConnection(@"Data Source=" +…
Tanzeel ur Rahman
  • 1,431
  • 2
  • 9
  • 4
117
votes
4 answers

What is the purpose of ByteBuffer's flip method? (And why is it called "flip"?)

Why does ByteBuffer's flip() method called "flip"? What is "flipped" here? According to apidoc, two successive flips won't restore original state, and multiple flips will probably tend limit() to become zero. Can I "unflip" somehow to reuse bytes…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
115
votes
6 answers

Gets byte array from a ByteBuffer in java

Is this the recommended way to get the bytes from the ByteBuffer ByteBuffer bb =.. byte[] b = new byte[bb.remaining()] bb.get(b, 0, b.length);
kal
  • 28,545
  • 49
  • 129
  • 149
96
votes
3 answers

Java: Converting String to and from ByteBuffer and associated problems

I am using Java NIO for my socket connections, and my protocol is text based, so I need to be able to convert Strings to ByteBuffers before writing them to the SocketChannel, and convert the incoming ByteBuffers back to Strings. Currently, I am…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64
60
votes
2 answers

Convert ByteBuffer to byte array java

Does anyone know how to convert ByteBuffer to byte[] array? I need to get byte array from my ByteBuffer. When I run bytebuffer.hasArray() it returns no. Every question I looked so far is converting byte array to byteBuffer, but I need it other way…
Powisss
  • 1,072
  • 3
  • 9
  • 16
59
votes
7 answers

Wrapping a ByteBuffer with an InputStream

I have a method that takes an InputStream and reads data from it. I would like to use this method with a ByteBuffer also. Is there a way to wrap a ByteBuffer so it can be accessed as a stream?
Erik
  • 743
  • 1
  • 5
  • 11
59
votes
8 answers

C/C++ Why to use unsigned char for binary data?

Is it really necessary to use unsigned char to hold binary data as in some libraries which work on character encoding or binary buffers? To make sense of my question, have a look at the code below - char c[5], d[5]; c[0] = 0xF0; c[1] = 0xA4; c[2] =…
nightlytrails
  • 2,448
  • 3
  • 22
  • 33
54
votes
1 answer

Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

I have a method as of below which has been running properly for a long time: private String loadFromFile(){ RandomAccessFile inFile = null; FileChannel inChannel = null; StringBuilder sb = new StringBuilder(); try { inFile…
garykwwong
  • 2,327
  • 2
  • 16
  • 20
52
votes
11 answers

Growing ByteBuffer

Has anyone has ever seen an implementation of java.nio.ByteBuffer that will grow dynamically if a putX() call overruns the capacity? The reason I want to do it this way is twofold: I don't know how much space I need ahead of time. I'd rather not do…
Seth
  • 5,596
  • 8
  • 42
  • 56
41
votes
1 answer

How to put the content of a ByteBuffer into an OutputStream?

I need to put the contents of a java.nio.ByteBuffer into an java.io.OutputStream. (wish I had a Channel instead but I don't) What's the best way to do this? I can't use the ByteBuffer's array() method since it can be a read-only buffer. I also may…
Jason S
  • 184,598
  • 164
  • 608
  • 970
35
votes
3 answers

Java InputStream to ByteBuffer

I am reading dds textures, but since once built the jar I can't access those textures through url and file and have to use InputStream instead. So I would need to know how I can obtain a java.​nio.ByteBuffer from an java.io.InputStream. Ps: no…
elect
  • 6,765
  • 10
  • 53
  • 119
34
votes
2 answers

Determine number of Bytes in ByteBuffer

I have a ByteBuffer that can hold a maximum of (4 + size) bytes (that is, an integer followed by size characters). However, the number of characters written to the ByteBuffer , may be smaller than size. So I was wondering, is there anyway to…
user2268507
1
2 3
61 62