Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

How to use the ShortBuffer put() method in Java

Ayesha Naeem

In Java, the ShortBuffer put() method writes a new value to the buffer at a given index.

The program needs to include the following module to use the ShortBuffer limit() method:

 java.nio.ShortBuffer

Syntax

public abstract ShortBuffer put(int index, short s)

Parameters and return value

The FloatBuffer put() method accepts the following parameters:

  • index: Indicates where to write the new value. It is of type int.
  • s: The value to be written. It is of type short.

The ShortBuffer put() method throws the following exceptions:

  • IndexOutOfBoundsException: The function throws this exception if the index is negative or greater than/equal to the buffer’s limit.
  • ReadOnlyBufferException: The function throws this exception if the buffer is read-only.

Examples

The following examples demonstrate how to use the ShortBuffer put() method in Java.

Example 1

Example 1 allocates a capacity of 4 to a buffer and populates it with short values through the ShortBuffer put() method. The program then displays the buffer:

import java.nio.*;
import java.util.*;

public class main {
	public static void main(String[] args)
	{
    try{
    System.out.println( "-----Example 1----");
    // Example 1
		//create a ShortBuffer
		ShortBuffer buf	= ShortBuffer.allocate(4);

		//populate using short values
		buf.put(0, (short)50);
		buf.put(1, (short)20);
		buf.put(2, (short)88);

		// print the short buffer
		System.out.println(	"ShortBuffer "+ Arrays.toString(buf.array()));
		System.out.println( "\nPosition: "+ buf.position());
		System.out.println( "\nLimit: "	+ buf.limit());

    
    }
    catch (IndexOutOfBoundsException e) {
  
            System.out.println("Exception throws: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws: " + e);
        }
	}
}

Example 2

Example 2 allocates a capacity of 4 to a buffer and populates it with short values through the ShortBuffer put() method. However, a value is put at index 4 that is out of bounds. So, an exception is thrown, as illustrated in the output below:

import java.nio.*;
import java.util.*;

public class main {
	public static void main(String[] args)
	{
    try{
   
    //Example 2
    System.out.println( "-----Example 2----");
    ShortBuffer buf1	= ShortBuffer.allocate(4);

		//populate using short values
		buf1.put(0, (short)50);
		buf1.put(1, (short)20);
		buf1.put(4, (short)88);

		// print the short buffer
		System.out.println(	"ShortBuffer before "+ "setting buffer's limit: "+ Arrays.toString(buf1.array()));
		System.out.println( "\nPosition: "+ buf1.position());
		System.out.println( "\nLimit: "	+ buf1.limit());

    


    }
    catch (IndexOutOfBoundsException e) {
  
            System.out.println("Exception throws: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws: " + e);
        }
	}
}

Example 3

Example 3 allocates a capacity of 4 to a buffer. The program creates a read-only copy of this buffer through the asReadOnlyBuffer() method and populates it with short values through the ShortBuffer put() method. However, a value cannot be written to the read-only buffer; so an exception is thrown, as illustrated in the output:

import java.nio.*;
import java.util.*;

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

     System.out.println( "-----Example 3----");
    ShortBuffer buf2	= ShortBuffer.allocate(4);
    //create a readonly buffer
    ShortBuffer buf3 = buf2.asReadOnlyBuffer();

		//populate using short values
		buf3.put(0, (short)50);
		buf3.put(1, (short)20);
		buf3.put(2, (short)88);

		// print the short buffer
		System.out.println(	"ShortBuffer before "+ "setting buffer's limit: "+ Arrays.toString(buf3.array()));
		System.out.println( "\nPosition: "+ buf3.position());
		System.out.println( "\nLimit: "	+ buf3.limit());



    }
    catch (IndexOutOfBoundsException e) {
  
            System.out.println("Exception throws: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws: " + e);
        }
	}
}

RELATED TAGS

java

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring