Home Tutorial Java Corejava Nio ShortBuffer in java, Use of slice method of ShortBuffer class.

 
 

ShortBuffer in java, Use of slice method of ShortBuffer class.
Posted on: August 13, 2010 at 12:00 AM
In this tutorial, you will see the use of slice method of ShortBuffer class.

ShortBuffer in java, Use of slice method of ShortBuffer class.

In this tutorial, we will see the use of slice method of ShortBuffer class in java.

ShortBuffer API:

The java.nio.ShortBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ShortBuffer wrap(short [] array)  The wrap(....) method wrapping an existing short array into short buffer.
abstract ShortBuffer slice() The slice() method returns slice buffer that share the remaining content of associated short buffer.

Code

import java.nio.*;
import java.nio.ShortBuffer;

public class SliceShortBuffer {
  public static void main(String[] arg) {
    short[] array = new short[] { 789734};
    ShortBuffer shortBuf = ShortBuffer.wrap(array);
    System.out.println("Elements in short buffer.");
    while (shortBuf.hasRemaining()) {
      System.out.print(shortBuf.get() " ");
    }
    shortBuf.flip();
    System.out.println();
  System.out.println("Remaining element in buffer : "
        + shortBuf.remaining());
    ShortBuffer shortBuf1 = shortBuf.slice();
System.out.println("Elements in slice short buffer.");
    while (shortBuf1.hasRemaining()) {
      System.out.print(shortBuf1.get() " ");
    }
  }
}

Output

C:\>java SliceShortBuffer
Elements in short buffer.
78 97 34 5
Remaining element in buffer : 4
Elements in slice short buffer.
78 97 34 5

Download this code

Related Tags for ShortBuffer in java, Use of slice method of ShortBuffer class.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.