推回用於輸入流,以允許讀取字節然後將其返回(即“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 Java.io.PushbackInputStream.available()用法及代碼示例
- Java Java.io.PushbackInputStream.close()用法及代碼示例
- Java Java.io.PushbackInputStream.mark()用法及代碼示例
- Java Java.io.PushbackInputStream.read()用法及代碼示例
- Java Java.io.PushbackInputStream.reset()用法及代碼示例
- Java Java.io.PushbackInputStream.skip()用法及代碼示例
- Java Java.io.PushbackInputStream.unread()用法及代碼示例
- Java Java.io.PushbackInputStream markSupported()用法及代碼示例
- Java Java.io.PushbackReader.close()用法及代碼示例
- Java Java.io.PushbackReader.mark()用法及代碼示例
- Java Java.io.PushbackReader.markSupported()用法及代碼示例
- Java Java.io.PushbackReader.read()用法及代碼示例
- Java Java.io.PushbackReader.ready()用法及代碼示例
- Java Java.io.PushbackReader.reset()用法及代碼示例
- Java Java.io.PushbackReader.skip()用法及代碼示例
- Java Java.io.PushbackReader.unread()用法及代碼示例
- Java Java.io.PushbackReader用法及代碼示例
- Java Java.io.PipedInputStream.available()用法及代碼示例
- Java Java.io.PipedInputStream.close()用法及代碼示例
- Java Java.io.PipedInputStream.connect()用法及代碼示例
- Java Java.io.PipedInputStream.read()用法及代碼示例
- Java Java.io.PipedOutputStream.close()用法及代碼示例
- Java Java.io.PipedOutputStream.connect()用法及代碼示例
- Java Java.io.PipedOutputStream.flush()用法及代碼示例
- Java Java.io.PipedOutputStream.write()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.PushbackInputStream class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。