当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.ByteArrayInputStream用法及代码示例


java.io 包的 ByteArrayInputStream 类包含所有缓冲区,其中包含要从输入流读取的字节。 ByteArrayInputStream类方法不存在IO异常。即使关闭 Stream 后也可以调用该类的方法,对类方法没有影响。类视图如下:

--> java.io Package
    --> ByteArrayInputStream Class   

用法:

public class ByteArrayInputStream
extends InputStream

有一些特定的字段:

  • 受保护的字节[] buf:由流的创建者提供的字节数组。
  • 受保护的int计数:索引一大于输入流缓冲区中的最后一个有效字符。
  • 受保护的int标记:流中当前标记的位置。
  • 受保护的int位置:这是要从输入流缓冲区读取的下一个字符的索引。

Constructor of ByteArrayInputStream Class

构造函数 执行的操作
ByteArrayInputStream(字节[]缓冲区) 它创建ByteArrayInputStream以使用缓冲区数组-“buffer”。
ByteArrayInputStream(byte[] buf, int 偏移量, int 长度) 创建使用“buffer”的某些部分的ByteArrayInputStream,即缓冲区数组

Methods of ByteArrayInputStream class

方法 执行的操作
available() 它告诉要从输入流读取的字节总数。
close() 它关闭输入流并释放系统资源。
mark() 它标记输入流的当前位置,这意味着设置读取限制。
markSupported() 它测试该输入流是否支持标记和重置方法。
read() 它从输入流中读取下一个数据字节。
reset() 它将输入流重新定位到标记位置,称为mark()方法
skip() 跳过输入流中的“args”。

执行:

Java


// Java Program to Demonstrate ByteArrayInputStream Class
// Via mark(), read(), skip(), available(),
// markSupported(), close(), reset() Method
// Importing required classes
import java.io.*;
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
    {
        // Declaring and initializing byte array
        byte[] buffer = { 71, 69, 69, 75, 83 };
        ByteArrayInputStream geek = null;
        // Try block to check for exceptions
        try {
            geek = new ByteArrayInputStream(buffer);
            // Telling the no. of bytes to be read
            // using available() method
            int number = geek.available();
            System.out.println(
                "Use of available() method : " + number);
            // Reading and printing Characters one by one
            // using read() method
            System.out.println("\nChar : "
                               + (char)geek.read());
            System.out.println("Char : "
                               + (char)geek.read());
            System.out.println("Char : "
                               + (char)geek.read());
            // Usage of mark() method
            geek.mark(0);
            // Skipping 'k' from "GEEKS"
            // using skip() method
            geek.skip(1);
            System.out.println(
                "skip() method comes to play");
            System.out.println(
                "mark() method comes to play");
            System.out.println("Char : "
                               + (char)geek.read());
            // Usage of markSupported() method
            boolean check = geek.markSupported();
            System.out.println("\nmarkSupported() : "
                               + check);
            if (geek.markSupported()) {
                // Repositioning the stream to marked
                // positions using reset() method
                geek.reset();
                System.out.println("\nreset() invoked");
                System.out.println("Char : "
                                   + (char)geek.read());
                System.out.println("Char : "
                                   + (char)geek.read());
            }
            else {
                System.out.println(
                    "reset() method not supported.");
            }
            System.out.println(
                "geek.markSupported() supported reset() : "
                + check);
        }
        // Catch block to handle the exceptions
        catch (Exception except) {
            // Displaying the exception along with line
            // number using printStackTrace() method
            except.printStackTrace();
        }
        // finally block that execute for sure
        finally {
            // Releasing the resources back to GC when
            // closes
            if (geek != null) {
                // Closing the file and releasing resources
                // using close() method
                geek.close();
            }
        }
    }
}
输出
Use of available() method : 5

Char : G
Char : E
Char : E
skip() method comes to play
mark() method comes to play
Char : S

markSupported() : true

reset() invoked
Char : K
Char : S
geek.markSupported() supported reset() : true


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.ByteArrayInputStream class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。