Trong hướng dẫn này, chúng ta sẽ tìm hiểu về Java ByteArrayInputStream và các phương thức của nó với sự trợ giúp của các ví dụ.
Các ByteArrayInputStream
lớp của java.io
gói có thể được sử dụng để đọc một loạt các dữ liệu đầu vào (theo byte).
Nó mở rộng InputStream
lớp trừu tượng.
Lưu ý : Trong ByteArrayInputStream
, luồng đầu vào được tạo bằng cách sử dụng mảng byte. Nó bao gồm một mảng bên trong để lưu trữ dữ liệu của mảng byte cụ thể đó.
Tạo ByteArrayInputStream
Để tạo luồng đầu vào mảng byte, java.io.ByteArrayInputStream
trước tiên chúng ta phải nhập gói. Sau khi chúng tôi nhập gói, đây là cách chúng tôi có thể tạo luồng đầu vào.
// Creates a ByteArrayInputStream that reads entire array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr);
Ở đây, chúng tôi đã tạo một luồng đầu vào đọc toàn bộ dữ liệu từ arr
mảng. Tuy nhiên, chúng ta cũng có thể tạo luồng đầu vào chỉ đọc một số dữ liệu từ mảng.
// Creates a ByteArrayInputStream that reads a portion of array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr, int start, int length);
Ở đây luồng đầu vào đọc số byte bằng chiều dài từ mảng bắt đầu từ vị trí bắt đầu .
Các phương thức của ByteArrayInputStream
Các ByteArrayInputStream
lớp học cung cấp triển khai cho các phương pháp khác nhau trình bày trong InputStream
lớp.
phương thức read ()
read()
– reads the single byte from the array present in the input streamread(byte[] array)
– reads bytes from the input stream and stores in the specified arrayread(byte[] array, int start, int length)
– reads the number of bytes equal to length from the stream and stores in the specified array starting from the position start
Ví dụ: ByteArrayInputStream để đọc dữ liệu
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String[] args) {
// Creates an array of byte
byte[] array = {1, 2, 3, 4};
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
System.out.print("The bytes read from the input stream: ");
for(int i= 0; i < array.length; i++) {
// Reads the bytes
int data = input.read();
System.out.print(data + ", ");
}
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Đầu ra
The bytes read from the input stream: 1, 2, 3, 4,
Trong ví dụ trên, chúng ta đã tạo một luồng đầu vào mảng byte có tên input
.
ByteArrayInputStream input = new ByteArrayInputStream(array);
Ở đây, luồng đầu vào bao gồm tất cả dữ liệu từ mảng được chỉ định. Để đọc dữ liệu từ luồng đầu vào, chúng tôi đã sử dụng read()
phương pháp.
phương thức sẵn có ()
Để lấy số byte có sẵn trong luồng đầu vào, chúng ta có thể sử dụng available()
phương pháp. Ví dụ,
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String args[]) {
// Creates an array of bytes
byte[] array = { 1, 2, 3, 4 };
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
// Returns the available number of bytes
System.out.println("Available bytes at the beginning: " + input.available());
// Reads 2 bytes from the input stream
input.read();
input.read();
// Returns the available number of bytes
System.out.println("Available bytes at the end: " + input.available());
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Đầu ra
Available bytes at the beginning: 4 Available bytes at the end: 2
Trong ví dụ trên,
- Chúng tôi đã sử dụng
available()
phương pháp để kiểm tra số lượng byte có sẵn trong luồng đầu vào. - Sau đó, chúng tôi đã sử dụng
read()
phương pháp 2 lần để đọc 2 byte từ luồng đầu vào. - Bây giờ, sau khi đọc 2 byte, chúng tôi đã kiểm tra các byte có sẵn. Lần này số byte có sẵn giảm đi 2.
phương thức bỏ qua ()
Để loại bỏ và bỏ qua số byte được chỉ định, chúng ta có thể sử dụng skip()
phương pháp này. Ví dụ,
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String args[]) {
// Create an array of bytes
byte[] array = { 1, 2, 3, 4 };
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
// Using the skip() method
input.skip(2);
System.out.print("Input stream after skipping 2 bytes: ");
int data = input.read();
while (data != -1) {
System.out.print(data + ", ");
data = input.read();
}
// close() method
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Đầu ra
Input stream after skipping 2 bytes: 3, 4,
Trong ví dụ trên, chúng ta đã sử dụng phương thức bỏ qua () để bỏ qua 2 byte dữ liệu từ luồng đầu vào. Do đó 1 và 2 không được đọc từ luồng đầu vào.
phương thức close ()
Để đóng luồng đầu vào, chúng ta có thể sử dụng close()
phương thức.
Tuy nhiên, close()
phương thức không có hiệu lực trong ByteArrayInputStream
lớp. Chúng ta có thể sử dụng các phương thức của lớp này ngay cả sau khi close()
phương thức được gọi.
Các phương pháp khác của ByteArrayInputStream
Methods | Decfscriptions |
---|---|
finalize() | ensures that the close() method is called |
mark() | marks the position in input stream up to which data has been read |
reset() | returns the control to the point in the input stream where the mark was set |
markSupported() | checks if the input stream supports mark() and reset() |
Để tìm hiểu thêm, hãy truy cập Java ByteArrayInputStream (tài liệu Java chính thức) .