Open In App

CharBuffer append() methods in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

append(char c)

The append(char c) method of java.nio.CharBuffer Class is used to append the specified char to this buffer (optional operation). 
An invocation of this method of the form dst.append(c) behaves in exactly the same way as the invocation 
dst.put(c) 
Syntax : 

public CharBuffer append(char c)

Parameters: This method takes the 16-bit char to append.
Return Value: This method returns this buffer, in which the char value is inserted.
Exception: This method throws the following exceptions: 

  • BufferOverflowException- If there is insufficient space in this buffer
  • ReadOnlyBufferException- If this buffer is read-only

Below are the examples to illustrate the append(char c) method:
Example 1:

Java




// Java program to demonstrate
// append() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
 
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c')
                .rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

Original CharBuffer:  [a, b, c]

 

Example 2: To demonstrate BufferOverflowException.

Java




// Java program to demonstrate
// append() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
 
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c');
 
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
 
            // again appending the value in CharBuffer
            // using append() method
            System.out.println("\nBuffer position : "
                               + charbuffer.position());
            charbuffer.append('x');
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("buffer's current position "
                               + "is not smaller than its limit");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

Original CharBuffer:  [a, b, c]

Buffer position : 3
buffer's current position is not smaller than its limit
Exception throws : java.nio.BufferOverflowException

 

Examples 3: To demonstrate ReadOnlyBufferException.

Java




// Java program to demonstrate
// append() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
 
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c');
 
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
 
            // Creating a read-only copy of CharBufferBuffer
            // using asReadOnlyBuffer() method
            CharBuffer chb = charbuffer.asReadOnlyBuffer();
 
            System.out.println("\nTrying to append the char value"
                               + " in read-only buffer");
 
            // putting the value in readonly CharBuffer
            // using append() method
            chb.append('d');
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("buffer's current position "
                               + "is not smaller than its limit");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

Original CharBuffer:  [a, b, c]

Trying to append the char value in read-only buffer
Exception throws : java.nio.ReadOnlyBufferException

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#append-char-
 

append(CharSequence csq)

The append(CharSequence csq) method of java.nio.CharBuffer Class is used to Appends the specified character sequence to this buffer (optional operation). 
Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer’s position and limit.
Syntax: 
 

public CharBuffer append(CharSequence csq)

Parameters: This method takes the character sequence to append. If csq is null, then the four characters “null” are appended to this character buffer.
Return Value: This method returns the this buffer.
Exception: This method throws the following exception: 
 

  • BufferOverflowException- If there is insufficient space in this buffer
  • ReadOnlyBufferException- If this buffer is read-only

Below are the examples to illustrate the append(CharSequence csq) method:
Example 1:
 

Java




// Java program to demonstrate
// append() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Declaring and initializing CharSequence
        CharSequence cha = "cow";
 
        // Creating the CharBuffer
        try {
 
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
 
            // appending the CharSequence in CharBuffer
            // using append() method
            charbuffer.append(cha)
                .rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

Original CharBuffer:  

 

Example 2: To demonstrate BufferOverflowException.
 

Java




// Java program to demonstrate
// append() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 2;
 
        // Declaring and initializing CharSequence
        CharSequence cha = "cow";
 
        // Creating the CharBuffer
        try {
 
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
 
            // appending the CharSequence in CharBuffer
            // using append() method
            charbuffer.append(cha)
                .rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
        }
 
        catch (BufferOverflowException e) {
            System.out.println("CharSequence length is greater"
                               + " than the length of charbuffer");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

CharSequence length is greater than the length of charbuffer
Exception throws : java.nio.BufferOverflowException

 

Example 3: To demonstrate ReadOnlyBufferException.
 

Java




// Java program to demonstrate
// append() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 2;
 
        // Declaring and initializing CharSequence
        CharSequence cha = "cow";
 
        // Creating the CharBuffer
        try {
 
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer charbuffer1
                = charbuffer.asReadOnlyBuffer();
 
            // appending the CharSequence in CharBuffer
            // using append() method
            charbuffer1.append(cha)
                .rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer1.array()));
        }
 
        catch (BufferOverflowException e) {
            System.out.println("CharSequence length is greater"
                               + " than the length of charbuffer");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
            System.out.println("Trying to append CharSequence "
                               + "in read-only charbuffer");
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

Trying to append CharSequence in read-only charbuffer
Exception throws : java.nio.ReadOnlyBufferException

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#append-java.lang.CharSequence-



Last Updated : 07 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads