KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > nio > ByteBuffer

java.nio
Class ByteBuffer

java.lang.Object
  extended by java.nio.Buffer
      extended by java.nio.ByteBuffer
All Implemented Interfaces:
Comparable<ByteBuffer>
Direct Known Subclasses:
MappedByteBuffer
See Also:
Top Examples, Source Code, FloatBuffer, asFloatBuffer, putFloat(int index, float f), putFloat(float f), getFloat(int index), getFloat(), BIG_ENDIAN, order, isDirect, mapping, allocateDirect, wrapping, allocation, slicing, duplicating, compacting, put, get, bulk put, bulk get, put, get

public static ByteBuffer allocate(int capacity)
See Also:
IllegalArgumentException, array offset, backing array
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[239]Uses memory mapping
By Anonymous on 2003/04/25 14:47:04  Rate
//Here is code that uses memory mapping: 
  
  
 import java.io.*; // For FileInputStream 
 import java.nio.*; // For ByteBuffer 
 import java.nio.channels.*; // For FileChannel 
  
  
 // Create a FileChannel, get the file size and map the file to a ByteBuffer 
 FileChannel in = new FileInputStream ( "test.data" ) .getChannel (  ) ; 
 int filesize =  ( int ) in.size (  ) ; 
 ByteBuffer mappedfile = in.map ( FileChannel.MapMode.READ_ONLY, 0, filesize ) ; 
 // Assume the file contains fixed-size records of binary data. 
 static final int RECORDSIZE = 80; // The size of each record 
 int recordNumber = 1; // This is the record we want to read 
 byte [  ]  recorddata = new byte [ RECORDSIZE ] ; // An array to hold record data 
 mappedfile.position ( recordNumber*RECORDSIZE ) ; // Start of desired record 
 mappedfile.get ( recorddata ) ; // Fill the array from the buffer 
  
  
 //allocate


public static ByteBuffer allocateDirect(int capacity)
See Also:
IllegalArgumentException, backing array
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1083]Read direct buffer
By Anonymous on 2004/10/29 23:55:31  Rate
public class ReadDirectBuff  {  
   public static void main ( String args [  ]  )   {  
      if  ( args.length != 0 )   {  
       String arg = args [ 0 ] ; 
       int size = arg.length (  ) ; 
       ByteBuffer byteBuffer = ByteBuffer.allocateDirect ( size*2 ) ; 
       CharBuffer buff = byteBuffer.asCharBuffer (  ) ; 
       buff.put ( arg ) ; 
       buff.rewind (  ) ; 
       for  ( int i=0, n=buff.length (  ) ; i < n; i++ )   {  
         System.out.println ( i + " : " + buff.get (  )  ) ; 
        }  
      }  
    }  
  }  
 


public final byte[] array()
See Also:
UnsupportedOperationException, ReadOnlyBufferException, hasArray
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int arrayOffset()
See Also:
UnsupportedOperationException, ReadOnlyBufferException, hasArray
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract CharBuffer asCharBuffer()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract DoubleBuffer asDoubleBuffer()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract FloatBuffer asFloatBuffer()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract IntBuffer asIntBuffer()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract LongBuffer asLongBuffer()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer asReadOnlyBuffer()
See Also:
duplicate
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ShortBuffer asShortBuffer()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer compact()
See Also:
ReadOnlyBufferException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int compareTo(ByteBuffer that)
See Also:
Comparable, compareTo
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer duplicate()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean equals(Object ob)
See Also:
Hashtable, Object.hashCode()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract byte get()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ByteBuffer get(byte[] dst)
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ByteBuffer get(byte[] dst,
                      int offset,
                      int length)
See Also:
IndexOutOfBoundsException, BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract byte get(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract char getChar()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract char getChar(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract double getDouble()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract double getDouble(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract float getFloat()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract float getFloat(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int getInt()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int getInt(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long getLong()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long getLong(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract short getShort()
See Also:
BufferUnderflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract short getShort(int index)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean hasArray()
See Also:
arrayOffset, array
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int hashCode()
See Also:
Hashtable, Object.equals(java.lang.Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract boolean isDirect()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final ByteOrder order()
See Also:
BIG_ENDIAN
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final ByteBuffer order(ByteOrder bo)
See Also:
LITTLE_ENDIAN, BIG_ENDIAN
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer put(byte b)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final ByteBuffer put(byte[] src)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ByteBuffer put(byte[] src,
                      int offset,
                      int length)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer put(int index,
                               byte b)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ByteBuffer put(ByteBuffer src)
See Also:
ReadOnlyBufferException, IllegalArgumentException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putChar(char value)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putChar(int index,
                                   char value)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putDouble(double value)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putDouble(int index,
                                     double value)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putFloat(float value)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putFloat(int index,
                                    float value)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putInt(int value)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putInt(int index,
                                  int value)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putLong(int index,
                                   long value)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putLong(long value)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putShort(int index,
                                    short value)
See Also:
ReadOnlyBufferException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer putShort(short value)
See Also:
ReadOnlyBufferException, BufferOverflowException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract ByteBuffer slice()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static ByteBuffer wrap(byte[] array)
See Also:
array offset, backing array
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static ByteBuffer wrap(byte[] array,
                              int offset,
                              int length)
See Also:
IndexOutOfBoundsException, array offset, backing array
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags