El método getLong() de la clase java.nio.ByteBuffer se usa para leer los siguientes ocho bytes en la posición actual de este búfer, componiéndolos en un valor largo de acuerdo con el orden de bytes actual y luego incrementa la posición en ocho.
Sintaxis:
public abstract long getLong()
Valor devuelto: este método devuelve el valor largo en la posición actual del búfer.
Lanza: este método lanza BufferUnderflowException : si quedan menos de cuatro bytes en este búfer.
A continuación se muestran los ejemplos para ilustrar el método getLong():
Ejemplos 1:
// Java program to demonstrate // getLong() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the long value in the bytebuffer bb.asLongBuffer() .put(1233003) .put(2292292); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getLong() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getLong() method long value = bb.getLong(); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the long at this buffer's next position // using getLong() method long value1 = bb.getLong(); // print the long value System.out.print("\nNext Byte Value: " + value1); } catch (BufferUnderflowException e) { System.out.println("\nException Thrown : " + e); } } }
Original ByteBuffer: 1233003 2292292 Byte Value: 1233003 Next Byte Value: 2292292
Ejemplos 2:
// Java program to demonstrate // getLong() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the long value in the bytebuffer bb.asLongBuffer() .put(1233003) .put(2292292); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getLong() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getLong() method long value = bb.getLong(); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the long at this buffer's next position // using getLong() method long value1 = bb.getLong(); // print the long value System.out.print("\nNext Byte Value: " + value1); // Reads the long at this buffer's next position // using getLong() method long value2 = bb.getLong(); } catch (BufferUnderflowException e) { System.out.println("\nthere are fewer than " + "eight bytes remaining in this buffer"); System.out.println("Exception Thrown : " + e); } } }
Original ByteBuffer: 1233003 2292292 Byte Value: 1233003 Next Byte Value: 2292292 there are fewer than eight bytes remaining in this buffer Exception Thrown : java.nio.BufferUnderflowException
Referencia: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getLong–
El método getLong(int index) de ByteBuffer se usa para leer cuatro bytes en el índice dado, componiéndolos en un valor flotante de acuerdo con el orden de bytes actual.
Sintaxis:
public abstract long getLong(int index)
Parámetros: este método toma el índice (el índice del que se leerá el byte) como parámetro.
Valor devuelto: este método devuelve el valor largo en el índice dado.
Excepción: este método genera una excepción IndexOutOfBoundsException . Si el índice es negativo o no menor que el límite del búfer, se lanza esta excepción.
A continuación se muestran los ejemplos para ilustrar el método getLong(int index) :
Ejemplos 1:
// Java program to demonstrate // getLong() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the long value in the bytebuffer bb.asLongBuffer() .put(1233003) .put(2292292); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getLong() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getLong() method long value = bb.getLong(0); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the long at this buffer's next position // using getLong() method long value1 = bb.getLong(8); // print the long value System.out.print("\nNext Byte Value: " + value1); } catch (IndexOutOfBoundsException e) { System.out.println("\nindex is negative or " + "smaller than the buffer's limit, " + "minus seven"); System.out.println("Exception Thrown : " + e); } } }
Original ByteBuffer: 1233003 2292292 Byte Value: 1233003 Next Byte Value: 2292292
Ejemplos 2:
// Java program to demonstrate // getLong() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the long value in the bytebuffer bb.asLongBuffer() .put(1233003) .put(2292292); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getLong() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getLong() method long value = bb.getLong(0); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the long at this buffer's next position // using getLong() method long value1 = bb.getLong(11); // print the long value System.out.print("\nNext Byte Value: " + value1); } catch (IndexOutOfBoundsException e) { System.out.println("\nindex is negative or" + " smaller than the buffer's limit, " + "minus seven"); System.out.println("Exception Thrown : " + e); } } }
Original ByteBuffer: 1233003 2292292 Byte Value: 1233003 index is negative or smaller than the buffer's limit, minus seven Exception Thrown : java.lang.IndexOutOfBoundsException
Referencia: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getLong-int-
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA