Found 34483 Articles for Programming

ByteBuffer asFloatBuffer() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

107 Views

A view of the ByteBuffer can be created as a FloatBuffer using the asFloatBuffer() method in the class java.nio.ByteBuffer. This method requires no parameters and it returns a float buffer as required. This buffer reflects the changes made to the original buffer and vice versa.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 50;       try {          ByteBuffer bufferB = ByteBuffer.allocate(n);          FloatBuffer bufferF = bufferB.asFloatBuffer();       ... Read More

ByteBuffer allocate() method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

350 Views

A new ByteBuffer can be allocated using the method allocate() in the class java.nio.ByteBuffer. This method requires a single parameter i.e. the capacity of the buffer. It returns the new ByteBuffer that is allocated. If the capacity provided is negative, then the IllegalArgumentException is thrown.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          ByteBuffer buffer = ByteBuffer.allocate(n);          buffer.put((byte)1);          buffer.put((byte)2);   ... Read More

ByteBuffer array() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

450 Views

A byte array for the buffer can be obtained using the method array() in the class java.nio.ByteBuffer. If the returned array is modified, then the contents of the buffer are also similarly modified and vice versa. If the buffer is read-only, then the ReadOnlyBufferException is thrown.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          ByteBuffer buffer = ByteBuffer.allocate(n);          buffer.put((byte)1);          buffer.put((byte)2); ... Read More

ByteBuffer asCharBuffer() method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

86 Views

A view of the ByteBuffer can be created as a CharBuffer using the asCharBuffer() method in the class java.nio.ByteBuffer. This method requires no parameters and it returns a char buffer as required. This buffer reflects the changes made to the original buffer and vice versa.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 50;       try {     ByteBuffer bufferB = ByteBuffer.allocate(n); ... Read More

ByteBuffer allocateDirect() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

244 Views

A direct byte buffer can be allocated using the method allocateDirect() in the class java.nio.ByteBuffer. This method requires a single parameter i.e. the capacity in bytes and it returns the direct byte buffer. If the capacity provided is negative, then the IllegalArgumentException is thrown.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          ByteBuffer buffer = ByteBuffer.allocateDirect(n);          byte[] byteValues = { 7, 1, 6, 3, ... Read More

ByteBuffer asDoubleBuffer() method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

84 Views

A view of the ByteBuffer can be created as a DoubleBuffer using the asDoubleBuffer() method in the class java.nio.ByteBuffer. This method requires no parameters and it returns a double buffer as required. This buffer reflects the changes made to the original buffer and vice versa.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 50;       try {          ByteBuffer bufferB = ByteBuffer.allocate(n);          DoubleBuffer bufferD = bufferB.asDoubleBuffer();       ... Read More

ByteBuffer asReadOnlyBuffer() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

345 Views

A read-only byte buffer can be created using the contents of a buffer with the method asReadOnlyBuffer() in the class java.nio.ByteBuffer. The new buffer cannot have any modifications as it is a read-only buffer. However, the capacity, positions, limits etc. of the new buffer are the same as the previous buffer.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {     int n = 5;     try {       ByteBuffer buffer = ByteBuffer.allocate(5);   ... Read More

ByteBuffer asLongBuffer() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

63 Views

A view of the ByteBuffer can be created as a LongBuffer using the asLongBuffer() method in the class java.nio.ByteBuffer. This method requires no parameters and it returns a long buffer as required. This buffer reflects the changes made to the original buffer and vice versa.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 50;       try {          ByteBuffer bufferB = ByteBuffer.allocate(n);          LongBuffer bufferL = bufferB.asLongBuffer();       ... Read More

ByteBuffer compareTo() method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

97 Views

A buffer can be compared with another buffer using the method compareTo() in the class java.nio.ByteBuffer. This method returns a negative integer if the buffer is less than the given buffer, zero if the buffer is equal to the given buffer and a positive integer if the buffer is greater than the given buffer.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          ByteBuffer buffer1 = ByteBuffer.allocate(n);     ... Read More

ByteBuffer get() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

363 Views

The value at the current position of the buffer is read and then incremented using the method get() in the class java.nio.ByteBuffer. This method returns the value that is at the current buffer position. Also, the BufferUnderflowException is thrown if underflow situation occurs.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { ByteBuffer buffer = ByteBuffer.allocate(n); ... Read More

Advertisements