com.sun.enterprise.mgmt.transport.grizzly.grizzly1_9
Class ByteBuffersBuffer

java.lang.Object
  extended by com.sun.enterprise.mgmt.transport.grizzly.grizzly1_9.ByteBuffersBuffer
All Implemented Interfaces:
Buffer, Comparable<Buffer>

public final class ByteBuffersBuffer
extends Object
implements Buffer

Author:
Alexey Stashok

Constructor Summary
ByteBuffersBuffer()
          Construct ByteBuffersBuffer.
ByteBuffersBuffer(ByteBuffer[] buffers)
           
ByteBuffersBuffer(ByteBuffersBuffer that)
           
 
Method Summary
 ByteBuffersBuffer append(ByteBuffer buffer)
           
 void calcCapacity()
           
 int capacity()
          Returns this buffer's capacity.
 ByteBuffersBuffer clear()
          Clears this buffer.
 int compareTo(Buffer that)
           
 void dispose()
          Notify the allocator that the space for this Buffer is no longer needed.
 Buffer duplicate()
          Creates a new Buffer that shares this buffer's content.
 boolean equals(Object obj)
           
 ByteBuffersBuffer flip()
          Flips this buffer.
 byte get()
          Relative get method.
 ByteBuffersBuffer get(byte[] dst)
          Relative bulk get method.
 ByteBuffersBuffer get(byte[] dst, int offset, int length)
          Relative bulk get method.
 byte get(int index)
          Absolute get method.
 char getChar()
          Relative get method for reading a char value.
 char getChar(int index)
          Absolute get method for reading a char value.
 double getDouble()
          Relative get method for reading a double value.
 double getDouble(int index)
          Absolute get method for reading a double value.
 float getFloat()
          Relative get method for reading a float value.
 float getFloat(int index)
          Absolute get method for reading a float value.
 int getInt()
          Relative get method for reading an int value.
 int getInt(int index)
          Absolute get method for reading an int value.
 long getLong()
          Relative get method for reading a long value.
 long getLong(int index)
          Absolute get method for reading a long value.
 short getShort()
          Relative get method for reading a short value.
 short getShort(int index)
          Absolute get method for reading a short value.
 int hashCode()
          Returns the current hash code of this buffer.
 boolean hasRemaining()
          Tells whether there are any elements between the current position and the limit.
 int limit()
          Returns this buffer's limit.
 ByteBuffersBuffer limit(int newLimit)
          Sets this buffer's limit.
 ByteBuffersBuffer mark()
          Sets this buffer's mark at its position.
 int position()
          Returns this buffer's position.
 ByteBuffersBuffer position(int newPosition)
          Sets this buffer's position.
 ByteBuffersBuffer prepend(ByteBuffer buffer)
           
 ByteBuffersBuffer put(byte b)
          Relative put method  (optional operation).
 ByteBuffersBuffer put(byte[] src)
          Relative bulk put method  (optional operation).
 ByteBuffersBuffer put(byte[] src, int offset, int length)
          Relative bulk put method  (optional operation).
 ByteBuffersBuffer put(int index, byte b)
          Absolute put method  (optional operation).
 ByteBuffersBuffer putChar(char value)
          Relative put method for writing a char value  (optional operation).
 ByteBuffersBuffer putChar(int index, char value)
          Absolute put method for writing a char value  (optional operation).
 ByteBuffersBuffer putDouble(double value)
          Relative put method for writing a double value  (optional operation).
 ByteBuffersBuffer putDouble(int index, double value)
          Absolute put method for writing a double value  (optional operation).
 ByteBuffersBuffer putFloat(float value)
          Relative put method for writing a float value  (optional operation).
 ByteBuffersBuffer putFloat(int index, float value)
          Absolute put method for writing a float value  (optional operation).
 ByteBuffersBuffer putInt(int value)
          Relative put method for writing an int value  (optional operation).
 ByteBuffersBuffer putInt(int index, int value)
          Absolute put method for writing an int value  (optional operation).
 ByteBuffersBuffer putLong(int index, long value)
          Absolute put method for writing a long value  (optional operation).
 ByteBuffersBuffer putLong(long value)
          Relative put method for writing a long value  (optional operation).
 ByteBuffersBuffer putShort(int index, short value)
          Absolute put method for writing a short value  (optional operation).
 ByteBuffersBuffer putShort(short value)
          Relative put method for writing a short value  (optional operation).
 int remaining()
          Returns the number of elements between the current position and the limit.
 ByteBuffersBuffer reset()
          Resets this buffer's position to the previously-marked position.
 ByteBuffersBuffer rewind()
          Rewinds this buffer.
 void shrink()
          Disposes the buffer part, outside [position, limit] interval if possible.
 String toString()
           
 String toStringContent()
          Returns Buffer content as String, using default Charset
 String toStringContent(Charset charset)
          Returns Buffer content as String
 String toStringContent(Charset charset, int position, int limit)
          Returns Buffer's chunk content as String
 ByteBuffer[] underlying()
          Return the underlying buffer
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteBuffersBuffer

public ByteBuffersBuffer()
Construct ByteBuffersBuffer.


ByteBuffersBuffer

public ByteBuffersBuffer(ByteBuffer[] buffers)

ByteBuffersBuffer

public ByteBuffersBuffer(ByteBuffersBuffer that)
Method Detail

duplicate

public Buffer duplicate()
Description copied from interface: Buffer
Creates a new Buffer that shares this buffer's content.

The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffer's position, limit, and mark values will be independent.

The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
duplicate in interface Buffer
Returns:
The new Buffer

dispose

public void dispose()
Description copied from interface: Buffer
Notify the allocator that the space for this Buffer is no longer needed. All calls to methods on a Buffer will fail after a call to dispose().

Specified by:
dispose in interface Buffer

append

public ByteBuffersBuffer append(ByteBuffer buffer)

prepend

public ByteBuffersBuffer prepend(ByteBuffer buffer)

underlying

public ByteBuffer[] underlying()
Description copied from interface: Buffer
Return the underlying buffer

Specified by:
underlying in interface Buffer
Returns:
the underlying buffer

position

public int position()
Description copied from interface: Buffer
Returns this buffer's position.

Specified by:
position in interface Buffer
Returns:
The position of this buffer

position

public ByteBuffersBuffer position(int newPosition)
Description copied from interface: Buffer
Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded.

Specified by:
position in interface Buffer
Parameters:
newPosition - The new position value; must be non-negative and no larger than the current limit
Returns:
This buffer

limit

public int limit()
Description copied from interface: Buffer
Returns this buffer's limit.

Specified by:
limit in interface Buffer
Returns:
The limit of this buffer

limit

public ByteBuffersBuffer limit(int newLimit)
Description copied from interface: Buffer
Sets this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.

Specified by:
limit in interface Buffer
Parameters:
newLimit - The new limit value; must be non-negative and no larger than this buffer's capacity
Returns:
This buffer

capacity

public int capacity()
Description copied from interface: Buffer
Returns this buffer's capacity.

Specified by:
capacity in interface Buffer
Returns:
The capacity of this buffer

mark

public ByteBuffersBuffer mark()
Description copied from interface: Buffer
Sets this buffer's mark at its position.

Specified by:
mark in interface Buffer
Returns:
This buffer

reset

public ByteBuffersBuffer reset()
Description copied from interface: Buffer
Resets this buffer's position to the previously-marked position.

Invoking this method neither changes nor discards the mark's value.

Specified by:
reset in interface Buffer
Returns:
This buffer

clear

public ByteBuffersBuffer clear()
Description copied from interface: Buffer
Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.

Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:

 buf.clear();     // Prepare buffer for reading
 in.read(buf);    // Read data

This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.

Specified by:
clear in interface Buffer
Returns:
This buffer

flip

public ByteBuffersBuffer flip()
Description copied from interface: Buffer
Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.

After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:

 buf.put(magic);    // Prepend header
 in.read(buf);      // Read data into rest of buffer
 buf.flip();        // Flip buffer
 out.write(buf);    // Write header + data to channel

This method is often used in conjunction with the com.sun.enterprise.mgmt.transport.Buffer#trimLeft() method when transferring data from one place to another.

Specified by:
flip in interface Buffer
Returns:
This buffer

rewind

public ByteBuffersBuffer rewind()
Description copied from interface: Buffer
Rewinds this buffer. The position is set to zero and the mark is discarded.

Invoke this method before a sequence of channel-write or get operations, assuming that the limit has already been set appropriately. For example:

 out.write(buf);    // Write remaining data
 buf.rewind();      // Rewind buffer
 buf.get(array);    // Copy data into array

Specified by:
rewind in interface Buffer
Returns:
This buffer

remaining

public int remaining()
Description copied from interface: Buffer
Returns the number of elements between the current position and the limit.

Specified by:
remaining in interface Buffer
Returns:
The number of elements remaining in this buffer

hasRemaining

public boolean hasRemaining()
Description copied from interface: Buffer
Tells whether there are any elements between the current position and the limit.

Specified by:
hasRemaining in interface Buffer
Returns:
true if, and only if, there is at least one element remaining in this buffer

shrink

public void shrink()
Description copied from interface: Buffer
Disposes the buffer part, outside [position, limit] interval if possible. May return without changing capacity. After shrink is called, position/limit/capacity values may have different values, than before, but still point to the same Buffer elements.

Specified by:
shrink in interface Buffer

get

public byte get()
Description copied from interface: Buffer
Relative get method. Reads the byte at this buffer's current position, and then increments the position.

Specified by:
get in interface Buffer
Returns:
The byte at the buffer's current position

put

public ByteBuffersBuffer put(byte b)
Description copied from interface: Buffer
Relative put method  (optional operation).

Writes the given byte into this buffer at the current position, and then increments the position.

Specified by:
put in interface Buffer
Parameters:
b - The byte to be written
Returns:
This buffer

get

public byte get(int index)
Description copied from interface: Buffer
Absolute get method. Reads the byte at the given index.

Specified by:
get in interface Buffer
Parameters:
index - The index from which the byte will be read
Returns:
The byte at the given index

put

public ByteBuffersBuffer put(int index,
                             byte b)
Description copied from interface: Buffer
Absolute put method  (optional operation).

Writes the given byte into this buffer at the given index.

Specified by:
put in interface Buffer
Parameters:
index - The index at which the byte will be written
b - The byte value to be written
Returns:
This buffer

get

public ByteBuffersBuffer get(byte[] dst)
Description copied from interface: Buffer
Relative bulk get method.

This method transfers bytes from this buffer into the given destination array. An invocation of this method of the form src.get(a) behaves in exactly the same way as the invocation

     src.get(a, 0, a.length) 

Specified by:
get in interface Buffer
Returns:
This buffer

get

public ByteBuffersBuffer get(byte[] dst,
                             int offset,
                             int length)
Description copied from interface: Buffer
Relative bulk get method.

This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if length > remaining(), then no bytes are transferred and a BufferUnderflowException is thrown.

Otherwise, this method copies length bytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented by length.

In other words, an invocation of this method of the form src.get(dst, off, len) has exactly the same effect as the loop

     for (int i = off; i < off + len; i++)
         dst[i] = src.get(); 
except that it first checks that there are sufficient bytes in this buffer and it is potentially much more efficient.

Specified by:
get in interface Buffer
Parameters:
dst - The array into which bytes are to be written
offset - The offset within the array of the first byte to be written; must be non-negative and no larger than dst.length
length - The maximum number of bytes to be written to the given array; must be non-negative and no larger than dst.length - offset
Returns:
This buffer

put

public ByteBuffersBuffer put(byte[] src)
Description copied from interface: Buffer
Relative bulk put method  (optional operation).

This method transfers the entire content of the given source byte array into this buffer. An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation

     dst.put(a, 0, a.length) 

Specified by:
put in interface Buffer
Returns:
This buffer

put

public ByteBuffersBuffer put(byte[] src,
                             int offset,
                             int length)
Description copied from interface: Buffer
Relative bulk put method  (optional operation).

This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than remain in this buffer, that is, if length > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.

Otherwise, this method copies length bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.

In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop

     for (int i = off; i < off + len; i++)
         dst.put(a[i]); 
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Specified by:
put in interface Buffer
Parameters:
src - The array from which bytes are to be read
offset - The offset within the array of the first byte to be read; must be non-negative and no larger than array.length
length - The number of bytes to be read from the given array; must be non-negative and no larger than array.length - offset
Returns:
This buffer

getChar

public char getChar()
Description copied from interface: Buffer
Relative get method for reading a char value.

Reads the next two bytes at this buffer's current position, composing them into a char value according to the current byte order, and then increments the position by two.

Specified by:
getChar in interface Buffer
Returns:
The char value at the buffer's current position

putChar

public ByteBuffersBuffer putChar(char value)
Description copied from interface: Buffer
Relative put method for writing a char value  (optional operation).

Writes two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two.

Specified by:
putChar in interface Buffer
Parameters:
value - The char value to be written
Returns:
This buffer

getChar

public char getChar(int index)
Description copied from interface: Buffer
Absolute get method for reading a char value.

Reads two bytes at the given index, composing them into a char value according to the current byte order.

Specified by:
getChar in interface Buffer
Parameters:
index - The index from which the bytes will be read
Returns:
The char value at the given index

putChar

public ByteBuffersBuffer putChar(int index,
                                 char value)
Description copied from interface: Buffer
Absolute put method for writing a char value  (optional operation).

Writes two bytes containing the given char value, in the current byte order, into this buffer at the given index.

Specified by:
putChar in interface Buffer
Parameters:
index - The index at which the bytes will be written
value - The char value to be written
Returns:
This buffer

getShort

public short getShort()
Description copied from interface: Buffer
Relative get method for reading a short value.

Reads the next two bytes at this buffer's current position, composing them into a short value according to the current byte order, and then increments the position by two.

Specified by:
getShort in interface Buffer
Returns:
The short value at the buffer's current position

putShort

public ByteBuffersBuffer putShort(short value)
Description copied from interface: Buffer
Relative put method for writing a short value  (optional operation).

Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two.

Specified by:
putShort in interface Buffer
Parameters:
value - The short value to be written
Returns:
This buffer

getShort

public short getShort(int index)
Description copied from interface: Buffer
Absolute get method for reading a short value.

Reads two bytes at the given index, composing them into a short value according to the current byte order.

Specified by:
getShort in interface Buffer
Parameters:
index - The index from which the bytes will be read
Returns:
The short value at the given index

putShort

public ByteBuffersBuffer putShort(int index,
                                  short value)
Description copied from interface: Buffer
Absolute put method for writing a short value  (optional operation).

Writes two bytes containing the given short value, in the current byte order, into this buffer at the given index.

Specified by:
putShort in interface Buffer
Parameters:
index - The index at which the bytes will be written
value - The short value to be written
Returns:
This buffer

getInt

public int getInt()
Description copied from interface: Buffer
Relative get method for reading an int value.

Reads the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four.

Specified by:
getInt in interface Buffer
Returns:
The int value at the buffer's current position

putInt

public ByteBuffersBuffer putInt(int value)
Description copied from interface: Buffer
Relative put method for writing an int value  (optional operation).

Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.

Specified by:
putInt in interface Buffer
Parameters:
value - The int value to be written
Returns:
This buffer

getInt

public int getInt(int index)
Description copied from interface: Buffer
Absolute get method for reading an int value.

Reads four bytes at the given index, composing them into a int value according to the current byte order.

Specified by:
getInt in interface Buffer
Parameters:
index - The index from which the bytes will be read
Returns:
The int value at the given index

putInt

public ByteBuffersBuffer putInt(int index,
                                int value)
Description copied from interface: Buffer
Absolute put method for writing an int value  (optional operation).

Writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.

Specified by:
putInt in interface Buffer
Parameters:
index - The index at which the bytes will be written
value - The int value to be written
Returns:
This buffer

getLong

public long getLong()
Description copied from interface: Buffer
Relative get method for reading a long value.

Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight.

Specified by:
getLong in interface Buffer
Returns:
The long value at the buffer's current position

putLong

public ByteBuffersBuffer putLong(long value)
Description copied from interface: Buffer
Relative put method for writing a long value  (optional operation).

Writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight.

Specified by:
putLong in interface Buffer
Parameters:
value - The long value to be written
Returns:
This buffer

getLong

public long getLong(int index)
Description copied from interface: Buffer
Absolute get method for reading a long value.

Reads eight bytes at the given index, composing them into a long value according to the current byte order.

Specified by:
getLong in interface Buffer
Parameters:
index - The index from which the bytes will be read
Returns:
The long value at the given index

putLong

public ByteBuffersBuffer putLong(int index,
                                 long value)
Description copied from interface: Buffer
Absolute put method for writing a long value  (optional operation).

Writes eight bytes containing the given long value, in the current byte order, into this buffer at the given index.

Specified by:
putLong in interface Buffer
Parameters:
index - The index at which the bytes will be written
value - The long value to be written
Returns:
This buffer

getFloat

public float getFloat()
Description copied from interface: Buffer
Relative get method for reading a float value.

Reads the next four bytes at this buffer's current position, composing them into a float value according to the current byte order, and then increments the position by four.

Specified by:
getFloat in interface Buffer
Returns:
The float value at the buffer's current position

putFloat

public ByteBuffersBuffer putFloat(float value)
Description copied from interface: Buffer
Relative put method for writing a float value  (optional operation).

Writes four bytes containing the given float value, in the current byte order, into this buffer at the current position, and then increments the position by four.

Specified by:
putFloat in interface Buffer
Parameters:
value - The float value to be written
Returns:
This buffer

getFloat

public float getFloat(int index)
Description copied from interface: Buffer
Absolute get method for reading a float value.

Reads four bytes at the given index, composing them into a float value according to the current byte order.

Specified by:
getFloat in interface Buffer
Parameters:
index - The index from which the bytes will be read
Returns:
The float value at the given index

putFloat

public ByteBuffersBuffer putFloat(int index,
                                  float value)
Description copied from interface: Buffer
Absolute put method for writing a float value  (optional operation).

Writes four bytes containing the given float value, in the current byte order, into this buffer at the given index.

Specified by:
putFloat in interface Buffer
Parameters:
index - The index at which the bytes will be written
value - The float value to be written
Returns:
This buffer

getDouble

public double getDouble()
Description copied from interface: Buffer
Relative get method for reading a double value.

Reads the next eight bytes at this buffer's current position, composing them into a double value according to the current byte order, and then increments the position by eight.

Specified by:
getDouble in interface Buffer
Returns:
The double value at the buffer's current position

putDouble

public ByteBuffersBuffer putDouble(double value)
Description copied from interface: Buffer
Relative put method for writing a double value  (optional operation).

Writes eight bytes containing the given double value, in the current byte order, into this buffer at the current position, and then increments the position by eight.

Specified by:
putDouble in interface Buffer
Parameters:
value - The double value to be written
Returns:
This buffer

getDouble

public double getDouble(int index)
Description copied from interface: Buffer
Absolute get method for reading a double value.

Reads eight bytes at the given index, composing them into a double value according to the current byte order.

Specified by:
getDouble in interface Buffer
Parameters:
index - The index from which the bytes will be read
Returns:
The double value at the given index

putDouble

public ByteBuffersBuffer putDouble(int index,
                                   double value)
Description copied from interface: Buffer
Absolute put method for writing a double value  (optional operation).

Writes eight bytes containing the given double value, in the current byte order, into this buffer at the given index.

Specified by:
putDouble in interface Buffer
Parameters:
index - The index at which the bytes will be written
value - The double value to be written
Returns:
This buffer

compareTo

public int compareTo(Buffer that)
Specified by:
compareTo in interface Comparable<Buffer>

toString

public String toString()
Overrides:
toString in class Object

toStringContent

public String toStringContent()
Description copied from interface: Buffer
Returns Buffer content as String, using default Charset

Specified by:
toStringContent in interface Buffer
Returns:
String representation of this Buffer content.

toStringContent

public String toStringContent(Charset charset)
Description copied from interface: Buffer
Returns Buffer content as String

Specified by:
toStringContent in interface Buffer
Parameters:
charset - the Charset, which will be use for byte[] -> String transformation.
Returns:
String representation of this Buffer content.

toStringContent

public String toStringContent(Charset charset,
                              int position,
                              int limit)
Description copied from interface: Buffer
Returns Buffer's chunk content as String

Specified by:
toStringContent in interface Buffer
Parameters:
charset - the Charset, which will be use for byte[] -> String transformation.
position - the first byte offset in the Buffer (inclusive)
limit - the last byte offset in the Buffer (exclusive)
Returns:
String representation of part of this Buffer.

calcCapacity

public void calcCapacity()

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Returns the current hash code of this buffer.

The hash code of a byte buffer depends only upon its remaining elements; that is, upon the elements from position() up to, and including, the element at limit() - 1.

Because buffer hash codes are content-dependent, it is inadvisable to use buffers as keys in hash maps or similar data structures unless it is known that their contents will not change.

Overrides:
hashCode in class Object
Returns:
The current hash code of this buffer


Copyright © 2012. All Rights Reserved.