Showing posts with label ByteBuffer class. Show all posts
Showing posts with label ByteBuffer class. Show all posts

Character.UnicodeBlock.of() char in Java

Character.UnicodeBlock.of(): This method is available in java.lang.Character.UnicodeBock of Java.

Syntax:

UnicodeBlock java.lang.Character.UnicodeBlock.of(char c)

This method takes one argument of type char as its parameter. This method returns the object representing the Unicode block containing the given character, or null if the character is not a member of a defined block.

Parameters: One parameter is required for this method.

c: The character in question.

Returns: The UnicodeBlock instance representing the Unicode block of which this character is a member or null if the character is not a member of any Unicode block.

Exceptions: NA

Approach

Java

public class CharacterUnicodeBlockcharof {
    public static void main(String[] args) {

        char c = 'a';
        System.out.println(Character.UnicodeBlock.of(c));

    }
}

Output:

BASIC_LATIN


ByteBuffer class Methods in Java Part- III

java.nio.ByteBuffer

A byte buffer.

This class defines six categories of operations upon byte buffers:

1. Absolute and relative get and put methods that read and write single bytes.

2. Absolute and relative bulk get methods that transfer contiguous sequences of bytes from this buffer into an array.

3. Absolute and Relative bulk put methods that transfer contiguous sequences of bytes from a byte array or some other bytebuffer into this buffer.

4. Absolute and relative get and put methods that read and write values of other primitive types, translating them to and from sequences of bytes in particular byte order.

5.  Methods for creating view buffers, which allow a byte buffer to be viewed as a buffer containing values of some other primitive type.

6. A method for compacting a byte buffer.


Some methods of ByteBuffer class


order()The byte order is used when reading or writing multibyte values, and when creating buffers that are views of this byte buffer. 


position()It returns this buffer's position or This method sets this buffer's position.


put(byte)It writes the given byte into this buffer at the current position and then increments the position.


put(byte[])It transfers the entire content of the given source byte array into this buffer.


put(ByteBuffer): It transfers the bytes remaining in the given source buffer into this buffer.


put(int, byte): It writes the given byte into this buffer at the given index.


put(int , byte[]) It copies bytes into this buffer from the given source array. 


put(byte[], int, int)It transfers bytes into this buffer from the given source array.


put(int, byte[], int, int)It transfers length bytes from the given array, starting at the given offset in the array and at the given index in this buffer.


putChar() It 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.


putDouble()It 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.


putFloat() It 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.


putInt(). It 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.


putLong()It 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.


putShort() It 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.


remaining()It returns the number of elements between the current position and the limit.


reset()It resets this buffer's position to the previously marked position.


rewind()It rewinds this buffer. The position is set to zero and the mark is discarded.


slice()It creates a new byte buffer whose content is a shared subsequence of this buffer's content.


toString()It returns a string summarizing the state of this buffer.


ByteBuffer.wrap()It wraps a byte array into a buffer.







ByteBuffer.wrap() in Java

ByteBuffer.wrap(): This method is available in java.nio.ByteBuffer class of Java.

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.wrap(byte[] array)

This method takes one argument of type byte array as it parameter. This method wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero, its mark will be undefined, and its byte order will be BIG_ENDIAN. Its backing array will be the given array, and its array offset will be zero.

Parameters: One parameter is required for this method.

array: The array that will back this buffer.

Returns: The new byte buffer.

Exceptions: NA

Java

import java.nio.ByteBuffer;

public class ByteBufferwrap {
    public static void main(String[] args) {

        byte array[] = { 12347 };

        System.out.println(ByteBuffer.wrap(array));

    }
}

Output:

java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]


Approach 2: When the method takes three arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.wrap(byte[] array, int offset, int length)

This method takes three arguments one of type byte array and the rest two are of type int as its parameter. This method wraps a byte array into a buffer. The new buffer's capacity will be array.length, its position will be offset, its limit will be offset + length, its mark will be undefined, and its byte order will be BIG_ENDIAN. Its backing array will be the given array, and its array offset will be zero.

Parameters: Three parameters are required for this method.

array: The array that will back the new buffer.

offset: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer's position will be set to this value.

length: The length of the subarray to be used; must be non-negative and no larger than the array.length - offset. The new buffer's limit will be set to offset + length.

Returns: The new byte buffer.

Throws:

1. IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold.

Java

import java.nio.ByteBuffer;

public class ByteBufferwrap2 {
    public static void main(String[] args) {

        byte array[] = { 123456 };
        int offset = 0, length = 3;
        ByteBuffer bb = ByteBuffer.wrap(array, offset, length);
        System.out.println(bb);

    }
}

Output:

java.nio.HeapByteBuffer[pos=0 lim=3 cap=6]


Approach 2.1: IndexOutOfBoundsException 

Java

import java.nio.ByteBuffer;

public class ByteBufferwrap2 {
    public static void main(String[] args) {

        byte array[] = { 123456 };
        int offset = 2, length = 5;
        ByteBuffer bb = ByteBuffer.wrap(array, offset, length);
        System.out.println(bb);

    }
}


Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.ByteBuffer.wrap(ByteBuffer.java:408)


ByteBuffer toString() in Java

toString(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

String java.nio.ByteBuffer.toString()

This method returns a string summarizing the state of this buffer.

Parameters: NA 

Returns: A summary string.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

public class ByteBuffertoString {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        System.out.println(bb.toString());

    }
}

Output:

java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]


ByteBuffer slice() in Java

slice(): This method is available in java.nio.ByteBuffer class of Java.

Approach 1: When the method does not take any argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.slice()

This method creates a new byte buffer whose content is a shared subsequence of this buffer's content.

Note:

1. The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer, its mark will be undefined, and its byte order will be BIG_ENDIAN.

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

Parameters: NA

Returns: The new byte buffer.

Exceptions: NA

Java

import java.nio.ByteBuffer;

public class ByteBufferslice {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        System.out.println("Before get method " + bb);
        bb.get();
        System.out.println("Before get method " + bb.slice());

    }
}


Output:

Before get method java.nio.HeapByteBuffer[pos=0 lim=4 cap=4] Before get method java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.slice(int index, int length)

This method creates a new byte buffer whose content is a shared subsequence of this buffer's content. The content of the new buffer will start at position index in this buffer and will contain length elements. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

Parameters: Two parameters are required for this method.

index: The position in this buffer at which the content of the new buffer will start; must be non-negative and no larger than limit().

length: The number of elements the new buffer will contain; must benon-negative and no larger than limit() - index.

Returns: The new buffer.

Throws:

1. IndexOutOfBoundsException - If index is negative or greater than limit(), length is negative, or length > limit() - index.

Java

import java.nio.ByteBuffer;

public class ByteBufferslice2 {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        int index = 1, length = 2;

        System.out.println("Before slice " + bb);
        ByteBuffer newBB = bb.slice(index, length);
        System.out.println("After slice " + newBB);

    }
}k


Output:

Before slice java.nio.HeapByteBuffer[pos=0 lim=4 cap=4] After slice java.nio.HeapByteBuffer[pos=0 lim=2 cap=2]


Approach 2.1: IndexOutOfBoundsException 

Java

import java.nio.ByteBuffer;

public class ByteBufferslice2 {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        int index = -1, length = 2;

        System.out.println("Before slice " + bb);
        ByteBuffer newBB = bb.slice(index, length);
        System.out.println("After slice " + newBB);

    }
}


Output:

Before slice java.nio.HeapByteBuffer[pos=0 lim=4 cap=4] Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 2) out of bounds for length 4 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapByteBuffer.slice(HeapByteBuffer.java:121)


ByteBuffer rewind() in Java

rewind(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

ByteBuffer java.nio.ByteBuffer.rewind()

This method 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.

Parameters: NA

Returns: This buffer.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

public class ByteBufferrewind {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        // set position
        bb.position(2);

        System.out.println(("Position before rewind is " +
 bb.position()));

        bb.rewind();

        System.out.println(("Position after rewind is " + 
bb.position()));

    }
}


Output:

Position before rewind is 2 Position after rewind is 0


ByteBuffer reset() in Java

reset(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

ByteBuffer java.nio.ByteBuffer.reset()

This method resets this buffer's position to the previously marked position.

Parameters: NA

Returns: This buffer.

Exceptions: InvalidMarkException

Approach 1: When no exceptions.

Java

import java.nio.ByteBuffer;

public class ByteBufferreset {
    public static void main(String[] args) {

        byte array[] = { 12345678 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        // set the position
        bb.position(3);

        bb.mark();

        // set new position
        bb.position(6);

        System.out.println("position before reset is " + bb.position());

        bb.reset();

        System.out.println("position after reset is " + bb.position());

    }
}

Output:

position before reset is 6

position after reset is 3


Approach 2: InvalidMarkException

Java

import java.nio.ByteBuffer;

public class ByteBufferreset {
    public static void main(String[] args) {

        byte array[] = { 12345678 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        // set the position
        bb.position(3);

        // set new position
        bb.position(6);

        System.out.println("position before reset is " + bb.position());

        bb.reset();

        System.out.println("position after reset is " + bb.position());

    }
}


Output:

position before reset is 6 Exception in thread "main" java.nio.InvalidMarkException at java.base/java.nio.Buffer.reset(Buffer.java:418) at java.base/java.nio.ByteBuffer.reset(ByteBuffer.java:1408)


ByteBuffer remaining() in Java

remaining(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

int java.nio.Buffer.remaining()

This method returns the number of elements between the current position and the limit.

Parameters: NA

Returns: The number of elements remaining in this buffer.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

public class ByteBufferremaining {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        System.out.println(bb.remaining());

    }
}

Output:

4

ByteBuffer putShort() in Java

putShort(): This method is available in java.nio.ByteBuffer class of Java.

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putShort(short value)

This method takes one argument of type short as its parameter. This method 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.

Parameters: One parameter is required for this method.

value: The short value to be written.

Returns: This buffer.

Throws:

1. BufferOverflowException - If there are fewer than two bytes remaining in this buffer.

2. ReadOnlyBufferException - If this buffer is read-only.

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteBufferputShort {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        short value = 12;
        ByteBuffer newBB = bb.putShort(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[0, 12, 3, 4]


Approach 1.1: BufferOverflowException

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteBufferputShort {
    public static void main(String[] args) {

        byte array[] = { 1 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        short value = 12;
        ByteBuffer newBB = bb.putShort(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.Buffer.nextPutIndex(Buffer.java:725) at java.base/java.nio.HeapByteBuffer.putShort(HeapByteBuffer.java:390)


Approach 1.2: ReadOnlyBufferException 

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteBufferputShort {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        short value = 12;
        ByteBuffer newBB = readOnly.putShort(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.putShort(HeapByteBufferR.java:393)


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putShort(int index, short value)

This method takes two arguments one of type int and another of type short. This method writes two bytes containing the given short value, in the current byte order, into this buffer at the given index.

Parameters: Two parameters are required for this method.

index: The index at which the bytes will be written.

value: The short value to be written.

Returns: This buffer.

Throws:

1. IndexOutOfBoundsException - If the index is negative or not smaller than the buffer's limit, minus one.

2. ReadOnlyBufferException - If this buffer is read-only

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteBufferputShort2 {
    public static void main(String[] args) {

        byte array[] = { 123467 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        int index = 2;
        short value = 12;
        ByteBuffer newBB = bb.putShort(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[1, 2, 0, 12, 6, 7]


Approach 2.1: IndexOutOfBoundsException

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteBufferputShort2 {
    public static void main(String[] args) {

        byte array[] = { 123467 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        int index = 6;
        short value = 12;
        ByteBuffer newBB = bb.putShort(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:744) at java.base/java.nio.HeapByteBuffer.putShort(HeapByteBuffer.java:400)


Approach 2.2: ReadOnlyBufferException 

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteBufferputShort2 {
    public static void main(String[] args) {

        byte array[] = { 123467 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        int index = 2;
        short value = 12;
        ByteBuffer newBB = readOnly.putShort(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.putShort(HeapByteBufferR.java:403)