An utility class for java.nio.Buffers : Buffer « File « Android






An utility class for java.nio.Buffers

 

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

/**
 * An utility class for java.nio.Buffers.
 * @author ke Svedin <ake.svedin@gmail.com>
 * @version $Revision: 24 $
 * @lastmodified $Date: 2009-08-17 08:32:24 -0700 (Mon, 17 Aug 2009) $
 */
public abstract class BufferUtils {
  
  /**
   * @param size number of bytes the buffer should hold
   * @return the newly allocated byte buffer
   */
  public static ByteBuffer createByteBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(size);
    bb.order(ByteOrder.nativeOrder());
    return bb;
  }
  
  /**
   * @param size number of chars the buffer should hold
   * @return the newly allocated char buffer
   */
  public static CharBuffer createCharBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(2*size);
    bb.order(ByteOrder.nativeOrder());
    return bb.asCharBuffer();
  }
  
  /**
   * @param size number of floats the buffer should hold
   * @return the newly allocated float buffer
   */
  public static FloatBuffer createFloatBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(4*size);
    bb.order(ByteOrder.nativeOrder());
    return bb.asFloatBuffer();
  }
  
  /**
   * @param size number of integers the buffer should hold
   * @return the newly allocated integer buffer
   */
  private static IntBuffer createIntBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(4*size);
    bb.order(ByteOrder.nativeOrder());
    return bb.asIntBuffer();
  }

  /**
   * Converts a float buffer to a fixed point int buffer
   * @param floatBuffer a FloatBuffer
   * @return an IntBuffer containing fixed points
   */
  public static IntBuffer toFixedBuffer(FloatBuffer floatBuffer) {
    int len = floatBuffer.capacity();
    IntBuffer fixedBuffer = createIntBuffer(len);
    floatBuffer.clear();
    fixedBuffer.clear();
    for(int i = 0; i < len; i++) {
      fixedBuffer.put((int)(floatBuffer.get(i)*65536));
    }
    fixedBuffer.clear();
    return null;
  }
  
}

   
  








Related examples in the same category

1.Fast Float Buffer
2.Demonstrate the Frame Buffer Object OpenGL ES extension.
3.Loads a file to a ByteBuffer.
4.Make a direct NIO FloatBuffer from an array of floats
5.Make a direct NIO ByteBuffer from an array of bytes
6.Make Float Buffer From Array
7.Creates a floatbuffer of the given size.
8.Make Float Buffer with ByteBuffer.allocateDirect
9.allocate Float Buffer
10.to Short Buffer
11.to Float Buffer Position Zero
12.allocate Short Buffer
13.allocate Int Buffer
14.Read InputStream with BufferedReader
15.To convert the InputStream to String we use the BufferedReader.readLine() method.
16.make Float Buffer
17.get Buffer From Array
18.Your own float buffer
19.Direct Buffer
20.create Buffer
21.Write String to File with BufferWriter
22.Byte Buffer Stack
23.Compute the SHA-1 hash of the bytes in the given buffer