Java ByteArrayInputStream

WBOY
リリース: 2024-08-30 16:09:13
オリジナル
660 人が閲覧しました

ByteArrayInputStream クラスは、バイト配列と入力ストリーム用の 2 つのフェーズで構成されます。バイト配列は、入力ストリームに関して重要なデータと必要なバイトを保持する上で重要な役割を果たします。 Java ByteArrayInputStream クラスは内部バッファで構成され、バイト配列をストリームとして読み取る役割を果たします。バイト配列は、入力ストリームとして供給されるデータを渡します。このデータがバッファ内に存在すると、増加します。このクラスを使用すると、データの走査と取得が柔軟かつシームレスになります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

public class ByteArrayInputStream extends InputStream
ログイン後にコピー

Java ByteArrayInputStream の一部としての実行フローは次のとおりです。

ByteArrayInputStream のパブリック クラスが宣言されています。これは、InputStream で配置されるストリームと対話するための媒体としてインターフェイスを拡張します。

ByteArrayInputStream クラスは Java でどのように機能しますか?

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 クラスの一部としてのこのコンストラクターは、ストリームから 3 つの引数をパラメーターとして渡します。これには、バイト配列、整数オフ、定義された整数の長さが考慮されます。また、フローは、これは、最初にバイト配列 a[]、次に 2 つの整数値を意味します。ここで、off は読み取られる最初のバイトで、その後に読み取られるバイト数の長さが続きます。

Java ByteArrayInputStream のメソッド

以下は Java ByteArrayInputStream のメソッドです:

  1. public int read()
  2. public int read(byte[] r, int off, int len)
  3. public int available()
  4. public void mark(int read)
  5. パブリックロングスキップ(long n)
  6. public boolean markSupported()
  7. パブリック void リセット()
  8. public void close()

1. public int read()

このメソッドは ByteArrayInputStream クラスの一部であり、すでに流れている入力ストリームで利用可能な次のバイトを読み取るために使用され、範囲 0 ~ 255 の int 型を返します。入力ストリームとしてバッファーにバイトが存在しない場合、ストリームの最後に戻り、値として -1 を返します。

2. public int read(byte[] r, int off, int len)

このメソッドは、入力ストリームから off から始まるバイト数の長さまでのバイトを配列に読み取り、最後のバイト -1 が返されるまでの合計バイト数を返します。

3. public int available()

このメソッドは、ByteArrayInputStream クラスの一部として、入力ストリームから読み取ることができる合計バイト数を読み取り、確認するために使用されます。

4. public void mark(int read)

このメソッドは ByteArrayInputStream クラスの一部として、入力ストリームの現在位置をマークおよび設定するために使用されます。基本的に、マークされた制限セットが無効になる前に読み取ることができる最大バイト数を取得するための読み取り制限を設定します。

5.パブリックロングスキップ(long n)

このメソッドは、ByteArrayInputStream クラスの一部として、メソッドへの引数として入力ストリーム内のバイト数をスキップするために使用されます。

6. public boolean markSupported()

このメソッドは、入力ストリームがマークされた制限をサポートしているか、制限が存在しなくても機能しているかどうかをテストするために使用されます。これには、このマークがサポートされている場合は必ずメソッドとして使用されるという特別な機能があります。常に true の値を返します。

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:

Java 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:

Java 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:

Java 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:

Java 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:

Java 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>
ログイン後にコピー

Java 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.

以上がJava ByteArrayInputStreamの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!