> Java > java지도 시간 > 본문

자바 ByteArrayInputStream

WBOY
풀어 주다: 2024-08-30 16:09:13
원래의
659명이 탐색했습니다.

ByteArrayInputStream 클래스는 바이트 배열과 입력 스트림의 두 단계로 구성됩니다. 바이트 배열은 입력 스트림과 관련하여 중요한 데이터와 필요한 바이트를 보관하는 데 중추적인 역할을 합니다. Java ByteArrayInputStream 클래스는 바이트 배열을 스트림으로 읽는 역할을 하는 내부 버퍼로 구성됩니다. 바이트 배열은 입력 스트림으로 공급할 데이터를 전달합니다. 이 데이터가 버퍼에 있으면 증가합니다. 이 클래스를 사용하면 데이터 탐색 및 검색이 유연하고 원활해집니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

public class ByteArrayInputStream extends InputStream
로그인 후 복사

Java ByteArrayInputStream의 일부로 실행 흐름은 다음과 같습니다.

InputStream과 함께 입력되는 스트림과 상호 작용하기 위한 매체로 인터페이스를 확장하는 ByteArrayInputStream의 공개 클래스가 선언됩니다.

Java에서 ByteArrayInputStream 클래스는 어떻게 작동하나요?

ByteArrayInputStream의 작동은 매우 간단하며 주요 원칙은 바이트 배열을 입력 스트림으로 원활하게 변환하는 것입니다. 이 클래스의 추가 이점으로, 버퍼는 API로 구성된 java.io.ByteArrayInput 패키지를 통해 바이트 배열의 데이터를 바이트 배열의 스트림으로 읽을 수 있도록 하며, 그런 다음 입력 스트림으로 변환됩니다. 버퍼에 공급됩니다. ByteArrayInputstream은 InputStream 클래스의 하위 클래스입니다. 따라서 ByteArrayInputStream을 InputStream으로 사용할 수 있습니다. 데이터가 입력 스트림으로 배열에 배열되어 저장되면 이를 바이트 배열로 래핑할 수 있으며 쉽게 스트림으로 변환할 수 있습니다. ByteArrayInputStream 객체가 준비되면 스트림에서 작업을 읽고 수행하는 데 도우미 메서드 목록을 사용할 수 있습니다.

Java ByteArrayInputStream 생성자

다음 생성자는 java의 ByteArrayInputStream 클래스를 지원하는 데 사용됩니다.

ByteArrayInputStream[byte a]
로그인 후 복사

Java ByteArrayInputStream의 일부인 이 생성자는 준비된 바이트 배열 세트를 수용하는 데 사용되는 방식으로 작동하며, 특히 패키지 및 클래스의 일부로 존재하는 내부 버퍼 메모리의 매개변수로 작동합니다.

ByteArrayInputStream(byte [] a, int off, int len)
로그인 후 복사

Java ByteArrayInputStream 클래스의 일부인 이 생성자는 바이트 배열, 정수 오프 및 정의된 정수의 길이를 고려하여 스트림의 매개변수로 세 개의 인수를 전달하며 흐름은 또한 버퍼 메모리에 입력합니다. 즉, 먼저 바이트 배열 a[], 그 다음 두 개의 정수 값을 의미합니다. 여기서 off는 읽을 첫 번째 바이트이고 그 뒤에 읽을 바이트 수의 길이가 옵니다.

Java ByteArrayInputStream 메서드

다음은 Java ByteArrayInputStream의 메소드입니다.

  1. public int read()
  2. public int read(byte[] r, int off, int len)
  3. 공개 정수 사용 가능()
  4. 공개 무효 표시(읽지 않음)
  5. 공개 긴 건너뛰기(long n)
  6. 공개 부울 표시 지원()
  7. 공개 무효 재설정()
  8. 공개 무효 닫기()

1. 공개 int 읽기()

이 메서드는 이미 흐르는 입력 스트림에서 사용 가능한 다음 바이트를 읽는 데 사용되는 ByteArrayInputStream 클래스의 일부이며 범위 0-255의 int 유형을 반환합니다. 입력 스트림으로 버퍼에 바이트가 없으면 스트림의 끝으로 반환되어 값으로 -1을 반환합니다.

2. 공개 int 읽기(byte[] r, int off, int len)

이 메서드는 입력 스트림에서 off부터 시작하여 바이트 수 길이만큼 바이트를 배열로 읽어서 마지막 바이트 -1이 반환될 때까지의 전체 바이트 수를 반환합니다.

3. 공개 정수 사용 가능()

ByteArrayInputStream 클래스의 일부인 이 메소드는 입력 스트림에서 읽을 수 있는 총 바이트 수를 읽고 확인하는 데 사용됩니다.

4. 공개 무효 표시(읽지 않음)

ByteArrayInputStream 클래스의 일부인 이 메서드는 입력 스트림의 현재 위치를 표시하고 설정하는 데 사용됩니다. 기본적으로 표시된 제한 설정이 무효화되기 전에 읽을 수 있는 최대 바이트 수를 얻기 위해 읽기 제한을 설정합니다.

5. 공개 긴 건너뛰기(long n)

ByteArrayInputStream 클래스의 일부인 이 메서드는 메서드에 대한 인수로 입력 스트림의 바이트 수를 건너뛰는 데 사용됩니다.

6. 공개 부울 표시지원()

이 방법은 입력 스트림이 표시된 제한을 지원하는지 또는 제한 없이 작동하는지 테스트하는 데 사용됩니다. 이 마크가 지원될 때마다 메소드로 사용되는 특별한 기능이 있습니다. 항상 참인 값을 반환합니다.

7. public void reset()

This method is used to reset the position of the marker as it is provoked by the mark() method. The added advantage is to reposition and reset the marker for traversing.

8. public void close()

This method plays a crux to release all the resources once close. When It gets called, the input stream gets closed, and the stream gets associated with the garbage collector.

Examples to Implement of Java ByteArrayInputStream

Below are the examples of Java ByteArrayInputStream:

Example #1

This program is used to illustrate the read() method byte by byte in ByteArrayInputStream.

Code:

import java.io.*;
public class Input_Buffer_Stream1 {
public static void main(String[] args) throws Exception {
byte guava = 0;
byte pine = 0;
byte kiwi = 0;
byte orange = 0;
byte[] buffr = {guava, pine, kiwi,orange};
ByteArrayInputStream fruits = new ByteArrayInputStream(buffr);
int k = 0;
while((k=fruits.read())!=-1) {
System.out.println("These fruits are really tasty and relising to have & Its the time to have ad enjoy!");
}
}
}
로그인 후 복사

Output:

자바 ByteArrayInputStream

Example #2

This program illustrates the available method of ByteArrayInputStream.

Code :

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream2 {
public static void main(String[] args) {
byte[] buffr= {20,22};
ByteArrayInputStream bytes = new ByteArrayInputStream(buffr);
int bytes_Available = bytes.available();
System.out.println("no of bytes available:" + bytes_Available);
}
}
로그인 후 복사

Output:

자바 ByteArrayInputStream

Example #3

This program illustrates the mark method of the ByteArrayInputStream class.

Code:

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream3 {
public static void main(String[] args) {
byte[] buffr= {20,22,19,10};
ByteArrayInputStream bytes_arr = new ByteArrayInputStream(buffr);
bytes_arr.mark(0);
System.out.println("These are the marked bytes of the stream:" + bytes_arr);
}
}
로그인 후 복사

Output:

자바 ByteArrayInputStream

Example #4

This program illustrates the skip method of the ByteArrayInputStream class.

Code :

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream4 {
public static void main(String[] args) throws Exception {
byte[] buffr= {20,22,18,10};
ByteArrayInputStream learning = null;
learning = new ByteArrayInputStream(buffr);
long num = learning.skip(1);
System.out.println("Char : "+(char)learning.read());
}
}
로그인 후 복사

Output:

자바 ByteArrayInputStream

Example #5

This program illustrates the boolean mark supported method of the ByteArrayInputStream class.

Code :

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream_5 {
public static void main(String[] args) {
byte[] buff = {15, 18, 20, 40, 52};
ByteArrayInputStream educba = null;
educba = new ByteArrayInputStream(buff);
boolean checker = educba.markSupported();
System.out.println("\n mark is supported for : "+ checker );
}
}
로그인 후 복사

Output:

자바 ByteArrayInputStream

Example #6

This program illustrates the presence of boolean mark, reset, and close method of the ByteArrayInputStream class.

Code:

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Input_Buffer_Stream_5 {
public static void main(String[] args) {
byte[] buff = {15, 18, 20, 40, 52};
ByteArrayInputStream educba = null;
educba = new ByteArrayInputStream(buff);
boolean checker = educba.markSupported();
System.out.println("\n mark is supported for : "+ checker );
if(educba.markSupported())
{
educba.reset();
System.out.println("\n supports for the marking limit once reset");
System.out.println("Char : "+(char)educba.read());
}
else
{
System.out.println("It is not supporting the positioning using reset method");
}
System.out.println("educba.markSupported() supported reset() : "+checker);
if(educba!=null)
{
try {
educba.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
} <strong>Output:</strong>
로그인 후 복사

자바 ByteArrayInputStream

Conclusion

Java ByteArrayInputStream is a class that has a lot of capability and versatility to play around with the arrays in the internal buffer, which is the beauty of the class. It does not require any external class or plugin to support its base methods which work with a lot of functionality. ByteArrayInputStream together forms a perfect combination to feed the input and output stream related data.

위 내용은 자바 ByteArrayInputStream의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!