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


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


推回用于输入流,以允许读取字节然后将其返回(即“pushed back”)到流。 PushbackInputStream 类实现了这个想法。它提供了一种机制“peek”来处理来自输入流的内容,而不会中断它。
它扩展了 FilterInputStream。
领域:

  • 受保护字节[] buf:这是推回缓冲区。
  • 受保护的 int 位置- 这是推回缓冲区内将从中读取下一个字节的位置。
  • 受保护的InputStream- 这是要过滤的输入流。

构造函数:

  • PushbackInputStream(输入流中):这将创建一个流对象,允许将一个字节返回到输入流。
  • PushbackInputStream(输入流,int numBytes):这将创建一个具有 numBytes 长的推回缓冲区的流。这允许将多个字节返回到输入流。

方法:

  • int available():返回可以从此输入流读取(或跳过)的字节数的估计值,而不会被该输入流的方法的下一次调用所阻塞。下一次调用可能是同一个线程或另一个线程。单次读取或跳过这么多字节不会阻塞,但可能会读取或跳过更少的字节。
Syntax: public int available()
Returns: the number of bytes that can be read
 (or skipped over) from the input stream without blocking.
Exception: IOException - if this input stream has 
been closed by invoking its close() method, or an I/O error occurs.
  • 无效close():关闭此输入流并释放与该流关联的所有系统资源。流关闭后,进一步的 read()、unread()、available()、reset() 或 skip() 调用将抛出 IOException。关闭之前关闭的流没有任何效果。
Syntax: public void close()
Returns: NA
Exception: IOException - if an I/O error occurs.
  • 布尔值markSupported():测试此输入流是否支持标记和重置方法,但它不支持。
Syntax: public boolean markSupported()
Returns: false, since this class does not support the mark and reset methods.
Exception: NA

Java


// java code illustrating unread() method
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo 
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
      // unread method
        push.unread('A');
        b[1] = (byte)push.read();
        pw.println((char)b[1]);
    }
}

输出:

available bytes: 10
mark supported? :false
  • int read():从此输入流读取下一个数据字节。值字节以 int 形式返回,范围为 0 到 255。如果由于已到达流末尾而没有可用字节,则返回值 -1。此方法会阻塞,直到输入数据可用、检测到流结束或引发异常为止。
Syntax: public int read()
Returns: the next byte of data, or -1 if 
the end of the stream has been reached.
Exception: IOException - if this input stream 
has been closed by invoking its close() method, or an I/O error occurs.
  • int 读取(字节[] b,int 关闭,int len):从此输入流中读取最多 len 个字节的数据到字节数组中。该方法首先读取任意pushed-back字节;之后,如果读取的字节数少于 len,则从底层输入流中读取。如果 len 不为零,则该方法将阻塞,直到至少有 1 个字节的输入可用;否则,不会读取任何字节并返回 0。
Syntax: public int read(byte[] b, int off, int len).
Returns: the total number of bytes read into 
the buffer, or -1 if there is no more data because the end of 
the stream has been reached.
Exception:  NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or 
len is greater than b.length - off
IOException - if this input stream has been closed by invoking its 
close() method, or an I/O error occurs.

Java

输出:

GeeksforGeeks a computer science portal 
GeeksforGeeks
  • 无效标记(int readlimit):标记此输入流中的当前位置。
    PushbackInputStream 的标记方法不执行任何操作。
Syntax: public void mark(int readlimit)
Returns: NA
Exception: NA
  • 无效reset():将此流重新定位到上次在此输入流上调用 mark 方法时的位置。
    类 PushbackInputStream 的重置方法除了抛出 IOException 之外什么也不做。
Syntax: public void reset()
Returns: NA
Exception: IOException - if this method is invoked.

Java


// Java code illustrating mark() and reset() method
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo 
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
        int c;
        while((c=push.read())!=-1)
        {
            pw.print((char)c);
        }
        pw.println();
         
        // marking the position 
        push.mark(5);
         
        // resetting is not supported throw exception
        push.reset();
         
        pw.close();
    }
}

输出:

GeeksforGeeks a computer science portal 
Exception in thread "main" java.io.IOException: mark/reset not supported
    at java.io.PushbackInputStream.reset(PushbackInputStream.java:364)
    at PushbackInputStreamDemo.main(PushbackInputStreamDemo.java:29)
  • 无效未读(字节[] b):通过将字节数组复制到推回缓冲区的前面来推回字节数组。该方法返回后,要读取的下一个字节的值为 b[0],之后的字节的值为 b[1],依此类推。
Syntax: public void unread(byte[] b)
returns: NA
Exception: IOException - If there is not enough room in 
the pushback buffer for the specified number of bytes, or this input 
stream has been closed by invoking its close() method.
  • void unread(byte[] b,int off,int len):通过将字节数组复制到推回缓冲区的前面来推回字节数组。该方法返回后,要读取的下一个字节的值为 b[0],之后的字节的值为 b[1],依此类推。
Syntax: public void unread(byte[] b,int off,int len)
Returns: NA
Exception: IOException - If there is not enough room 
in the pushback buffer for the specified number of bytes, or this input 
stream has been closed by invoking its close() method.

Java


// Java code illustrating unread() method
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo 
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
        int c;
        while((c=push.read())!=-1)
        {
            pw.print((char)c);
        }
        pw.println();
      // unread method
        push.unread(b);
        push.unread(b, 0, 6);
        while((c=push.read())!=-1)
        {
            pw.print((char)c);
        }
        pw.println();
        pw.close();
    }
}

输出:

GeeksforGeeks a computer science portal
orGeeks a computer science portal
  • 无效未读(int b):通过将字节复制到推回缓冲区的前面来推回一个字节。该方法返回后,下一个要读取的字节将具有值(byte)b。
Syntax: public void unread(int b)
Returns: NA
Exception: IOException - If there is not enough 
room in the pushback buffer for the byte, or this input stream 
has been closed by invoking its close() method.

Java


// java code illustrating unread() method
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo 
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
      // unread method
        push.unread('A');
        b[1] = (byte)push.read();
        pw.println((char)b[1]);
    }
}

输出:

A


相关用法


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