bytebuffer

java: converting part of a ByteBuffer to a string

I have a ByteBuffer containing bytes that were derived by String.getBytes(charsetName), where "containing" means that the string comprises the entire sequence of bytes between the ByteBuffer's position() and limit(). What's the best way for me to get the string back? (assuming I know the encoding charset) Is there anything better than ...

Using Java's ByteBuffer to read millions of messages...

Here's my problem: one big gzipped file; millions of messages. Each message consists of: ***************** *************** ****************** * 2-byte LENGTH * * 1-byte TYPE * * N-byte PAYLOAD * , where N = (LENGTH-1). ***************** *************** ****************** Depending on the TYPE, I need to read a few bytes from an off...

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? ...

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 micro-benchmarks on ByteBuffer.allocate() vs. ByteBuffer.allocateDirect() performance. There ...

Fastest and most efficient conversion of a byte array to a 29 bit integer in Java

Since 29 bit integers are popular in AMF, I would like to incorporate the fastest / best routine known. Two routines currently exist in our library and can be tested live on ideone http://ideone.com/KNmYT Here is the source for quick reference public static int readMediumInt(ByteBuffer in) { ByteBuffer buf = ByteBuffer.allocate(4)...

ReadableByteChannel.read(ByteBuffer dest) reads capped at 8KB. Why?

I've got some code that: reads from a ReadableByteChannel into a ByteBuffer, takes note of the bytes transfered, pauses a few tens to hundreds of miliseconds, passes the ByteBuffer onto a WritableByteChannel. Some details: Both Channels are TCP/IP sockets. The total connection read size is in the tens of megabytes. The sourc...

compare ByteBuffer contents?

What's the easiest way in Java to compare the contents of two ByteBuffers to check for equality? ...

How to serialize ByteBuffer

Hi everyone, I wish to send a java.nio.ByteBuffer accross a network using RMI, however ByteBuffer isn't serializable. I've tried the following custom class to no avail: public class NetByteBuffer implements java.io.Serializable { ByteBuffer buffer; public NetByteBuffer(ByteBuffer buffer) { this.buffer = buffer; } public ByteBuff...

How can I decode ogg vorbis data from a ByteBuffer ?

The libraries I founded so far only have methods to decode from a file or InputStream, I have a ByteBuffer with ogg vorbis data and I need it decoded to PCM without having to write it to a file first. ...

Are the ByteBuffer/IntBuffer/ShortBuffer Java classes fast??

I'm working on an Android application (in Java, obviously) and I recently updated my UDP reader code. In both versions, I set up some buffers and receive a UDP packet: byte[] buf = new byte[10000]; short[] soundData = new short[1000]; DatagramPacket packet = new DatagramPacket (buf, buf.length); socket.receive (packet); In the initia...

Android download file out of memory woes

I am trying to download a zip file that is just less than 22 mb on start. I changed the default BufferedInputStream after these exceptions, but still get an out of memory error. public void downloadFromUrl(String fileName) { //avoid unknown host exception try { InetAddress i = InetAddress ...

Using Tritonus AudioSystem: Convert PCM byte[] array to mp3 byte[] array

One of the libraries in my audio project continually outputs a byte[] array stream of PCM audio (along with a length and offset integer). I'm learning to use the Tritonus implementation of the Java Sound API to encode this PCM stream into mp3. The Java Sound API provides an converted/encoded byte[] array via an AudioInputStream (create...