bytebuffer

How to avoid OutOfMemoryError when using Bytebuffers and NIO?

I'm using ByteBuffers and FileChannels to write binary data to a file. When doing that for big files or successively for multiple files, I get a OutOfMemoryError exception. I've read elsewhere that using Bytebuffers with NIO is broken and should be avoided. Does any of you already faced this kind of problem and found a solution to effici...

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); ...

Java bytebuffer to C

Hello, From a C program on Windows we need to read and write like a Java bytebuffer which stores binary in BIG_ENDIAN The algorithm is described at : http://mindprod.com/jgloss/binaryformats.html Need to read and write int and float. Does anyone have example c or C++ code that does this or a reference ? TIA, Bert ...

JAVA NIO ByteBuffer allocatation to fit largest dataset?

I'm working on an online game and I've hit a little snag while working on the server side of things. When using nonblocking sockets in Java, what is the best course of action to handle complete packet data sets that cannot be processed until all the data is available? For example, sending a large 2D tiled map over a socket. I can think...

How comes .array() doesn't work on ByteBuffers returned from map'ed FileChannels?

I'm doing memory-mapped IO in Java. The FileChannel class allows you to map a ByteBuffer to a particular part of a file. I'm doing that with a file opened read only. The problem I am having is that I'm getting an exception when I attempt to call the .array() method on the resulting ByteBuffer. Perhaps that's because the .array() return...

Difference between ByteBuffer.allocateDirect() and MappedByteBuffer.load()

Hi , I was trying to implement a sort of shared cache between two or more JVMs by memory mapping a particular file using MappedByteBuffer. From the specifications I see that when we use MappedByteBuffer.load() it should load the data into a direct buffer. I have a couple of questions on this. My code snippet:: RandomAccessFile file =...

Equivalent of Java's "ByteBuffer.putType()" in C#

Hello :) I am trying to format a byte array in C#, by porting a code from Java. In Java, the methods "buf.putInt(value);", buf.putShort, buf.putDouble, (and so forth) are used. However I don't know how to port this to C#. I have tried the MemoryStream class, but there is no method to put a specific type at the end of the byte array. Qu...

What is the equivalent of "ByteBuffer.flip" & "ByteBuffer.slice" in .NET?

Hello :) I need to port code from Java to C#. In the Java code, the methods "ByteBuffer.flip()" and "ByteBuffer.slice" is used, and I don't know how to translate this. I've read this question (http://stackoverflow.com/questions/607587/an-equivalent-of-javax-nio-buffer-flip-in-c), but although an answer is given, I cannot figure how to ...

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 starting file format can be any size, but I need to write the data in the TIFF to li...

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 able to: build a buffer from a byte array/point and get primitives from the buffer, e.g. g...

Java - ByteBuffer.remaining() problems..

Summary: I have a ByteBuffer in which I am pumping some data. After that, I want to send this data over a Socket. So, I wrote code like this: private static void serialize(ByteBuffer buffer, EmployeeData emp, CharsetEncoder encoder) { // id buffer.putInt(emp.getId()); CharBuffer nameBuffer = CharBuffer.wrap(emp.getFirstName().t...

What is the Java equivalent of .NET BitConverter ?

I was writing an article in which I wanted to show how to send objects across the network, from Java to CLR/.Net and Back. http://ferozedaud.blogspot.com/2009/11/howto-serialize-data-from-object-from.html While doing research for this, I could not find a Java equivalent for BitConverter class that exists in .NET. Due to this, I had to ...

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: 1) I don't know how much space I need ahead of time. 2) I'd rather not do a new ByteBuffer.allocate() then a bulk put() every time I run out of space. I co...

ByteBuffer, what is a clean way to detect if it needs to be flipped.

Is there a clean sure fire way to detect if a ByteBuffer needs flipping? I have a ByteBuffer that is used for packing and unpacking a data stucture and also for storage of bytes to be packed / unpacked. However, if a write operation has just been performed or a read operation has been performed or if a ByteBuffer was passed to my code...

Java: Using type punning on primitive arrays?

Hello, everyone! I need to be able to convert byte arrays to/from other primitive type arrays, but instead of casting, I need type punning. Correct term for raw copy without casting? I thought it would be possible to do the following: // idea: byte[12] -> int[3], and int[3] -> byte[12] int[] ints; ByteBuffer bb = ByteBuffer.wrap( ...

ByteBuffer recycling class

Hi everyone, I'm wonder how I'd code up a ByteBuffer recycling class that can get me a ByteBuffer which is at least as big as the specified length, and which can lock up ByteBuffer objects in use to prevent their use while they are being used by my code. This would prevent re-construction of DirectByteBuffers and such over and over, inst...

Which JVMs do not support direct java.nio.ByteBuffer?

The release notes for Java NIO (in Java 1.4+) state that support for direct ByteBuffers is an optional feature. I am curious which JVM vendors/flavors do not support it? Should a JNI library always code for managed ByteBuffers and relegate direct ByteBuffers as an optimization? Thanks ...

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 a data array from jobject (that is a ByteBuffer instance) ? ...

Cocoa - does CGDataProviderCopyData() actually copy the bytes? Or just the pointer?

I'm running that method in quick succession as fast as I can, and the faster the better, so obviously if CGDataProviderCopyData() is actually copying the data byte-for-byte, then I think there must be a faster way to directly access that data...it's just bytes in memory. Anyone know for sure if CGDataProviderCopyData() actually copies t...

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 times and fill it every time with different junk data, that's why I am looking for a fas...