Java Java类
Pushback 用于输入流以允许读取一个字节,然后将其返回(即“推回”)到流中。 PushbackInputStream 类实现了这个想法。它提供了一种机制“窥视”来自输入流的内容,而不会破坏它。
它扩展了 FilterInputStream。
领域:
- protected byte[] buf :这是推回缓冲区。
- protected int pos – 这是下一个字节将被读取的推回缓冲区中的位置。
- protected InputStream in – 这是要过滤的输入流。
构造函数:
- PushbackInputStream(InputStream in):这将创建一个流对象,允许将一个字节返回到输入流。
- PushbackInputStream(InputStream in, 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.
- void close():关闭此输入流并释放与该流关联的所有系统资源。关闭流后,进一步的 read()、unread()、available()、reset() 或 skip() 调用将引发 IOException。关闭以前关闭的流没有效果。
Syntax: public void close()
Returns: NA
Exception: IOException - if an I/O error occurs.
- boolean markSupported():测试此输入流是否支持 mark 和 reset 方法,但它不支持。
Syntax: public boolean markSupported()
Returns: false, since this class does not support the mark and reset methods.
Exception: NA
Java
// Java code illustrating available(), close()
// and markSupported() methods
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 IOException
{
PrintWriter pw = new PrintWriter(System.out, true);
String str = "Hey geeks ";
byte b[] = str.getBytes();
ByteArrayInputStream bout = new ByteArrayInputStream(b);
PushbackInputStream push = new PushbackInputStream(bout);
// checking no. of bytes available
pw.println("available bytes: " + push.available());
// checking if mark is supported
pw.println("mark supported? :" + push.markSupported());
pw.close();
}
}
Java
// Java code illustrating read() 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 IOException
{
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();
push.read(b, 0, 13);
for(int i=0; i<13; i++)
{
pw.print((char)b[i]);
}
pw.println();
pw.close();
}
}
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();
}
}
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();
}
}
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 read(byte[] b, int off, int len):从此输入流中读取最多 len 个字节的数据到一个字节数组中。此方法首先读取任何推回的字节;之后,如果读取的字节数少于 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
// Java code illustrating read() 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 IOException
{
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();
push.read(b, 0, 13);
for(int i=0; i<13; i++)
{
pw.print((char)b[i]);
}
pw.println();
pw.close();
}
}
输出:
GeeksforGeeks a computer science portal
GeeksforGeeks
- void mark(int readlimit):标记此输入流中的当前位置。
PushbackInputStream 的 mark 方法什么都不做。
Syntax: public void mark(int readlimit)
Returns: NA
Exception: NA
- void reset():将此流重新定位到最后一次在此输入流上调用标记方法时的位置。
PushbackInputStream 类的方法 reset 除了抛出一个 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)
- void unread(byte[] 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
- void unread(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