getInt()
java.nio.ByteBuffer類的getInt()方法用於讀取此緩衝區當前位置的下四個字節,並根據當前字節順序將它們組成一個int值,然後將該位置加4。
用法:
public abstract int getInt()
返回值:此方法返回緩衝區當前位置的int值
拋出:此方法引發BufferUnderflowException-如果此緩衝區中剩餘的字節數少於四個。
下麵是說明getInt()方法的示例:
範例1:
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 12;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20)
.put(30);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt();
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt();
// print the int value
System.out.println("Next Byte Value: " + value1);
}
catch (BufferUnderflowException e) {
System.out.println("\nException Thrown : " + e);
}
}
}
輸出:
Original ByteBuffer: 10 20 30 Byte Value: 10 Next Byte Value: 20
範例2:
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 8;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt();
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt();
// print the int value
System.out.println("Next Byte Value: " + value1);
// Reads the int at this buffer's next position
// using getInt() method
int value2 = bb.getInt();
}
catch (BufferUnderflowException e) {
System.out.println("\nthere are fewer than "
+ "four bytes remaining in this buffer");
System.out.println("Exception Thrown : " + e);
}
}
}
輸出:
Original ByteBuffer: 10 20 Byte Value: 10 Next Byte Value: 20 there are fewer than four bytes remaining in this buffer Exception Thrown : java.nio.BufferUnderflowException
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getInt–
getInt(int索引)
ByteBuffer的getInt(int index)方法用於讀取給定索引處的四個字節,並根據當前字節順序將它們組成一個int值。
用法:
public abstract int getInt(int index)
參數:此方法將索引(將從中讀取Byte的索引)作為參數。
返回值:此方法返回給定索引處的int值。
異常:此方法引發IndexOutOfBoundsException。如果index為負或不小於緩衝區的限製,則拋出此異常。
以下示例說明了getInt(int index)方法:
範例1:
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 8;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt(0);
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt(4);
// print the int value
System.out.println("Next 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: 10 20 Byte Value: 10 Next Byte Value: 20
範例2:
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 8;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt(0);
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt(7);
// print the int value
System.out.println("Next 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: 10 20 Byte Value: 10 index is negative or smaller than the buffer's limit, minus seven Exception Thrown : java.lang.IndexOutOfBoundsException
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getInt-int-
相關用法
- Java Field getInt()用法及代碼示例
- Java ByteBuffer get()用法及代碼示例
- Java ByteBuffer compareTo()用法及代碼示例
- Java ByteBuffer getShort()用法及代碼示例
- Java ByteBuffer getDouble()用法及代碼示例
- Java ByteBuffer getFloat()用法及代碼示例
- Java ByteBuffer allocate()用法及代碼示例
- Java ByteBuffer order()用法及代碼示例
- Java ByteBuffer slice()用法及代碼示例
- Java ByteBuffer toString()用法及代碼示例
- Java ByteBuffer wrap()用法及代碼示例
- Java ByteBuffer getLong()用法及代碼示例
- Java ByteBuffer hasArray()用法及代碼示例
- Java ByteBuffer hashCode()用法及代碼示例
- Java ByteBuffer asCharBuffer()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer getInt() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。