JAVA伊奥。Java中的PushbackInputStream类

Pushback用于输入流,允许读取一个字节,然后将其返回(即“Pushback”)到流中。PushbackInputStream类实现了这个想法。它提供了一种机制,在不中断输入流的情况下“窥视”来自输入流的内容。 它扩展了过滤流。

null

领域:

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

    施工人员:

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

    方法:

    1. 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.
      
    2. void close(): 关闭此输入流并释放与该流关联的所有系统资源。一旦关闭流,进一步的read()、unread()、available()、reset()或skip()调用将抛出IOException。关闭以前关闭的流没有效果。
      Syntax: public void close()
      Returns: NA
      Exception: IOException - if an I/O error occurs.
      
    3. 支持布尔标记() 测试此输入流是否支持标记和重置方法,但它不支持。
      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();
      }
      }

      
      

      输出:

      available bytes: 10
      mark supported? :false
      
    4. int read(): 从这个输入流中读取下一个字节的数据。字节值以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.
      
    5. int read(字节[]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
      
    6. 无效标记(int readlimit): 标记此输入流中的当前位置。 PushbackInputStream的标记方法不起任何作用。
      Syntax: public void mark(int readlimit)
      Returns: NA
      Exception: NA
      
    7. void reset(): 将此流重新定位到上次在此输入流上调用mark方法时的位置。 类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 );
      // reseting 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)
      
    8. 无效未读(字节[]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.
      
    9. 无效未读(字节[]b,整数关闭,整数长度): 通过将字节数组复制到回推缓冲区的前端来回推字节数组。此方法返回后,下一个要读取的字节将具有值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
      
    10. 无效未读(int b): 通过将字节复制到回推缓冲区的前端来回推字节。此方法返回后,下一个要读取的字节将具有值(字节)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
      

    本文由 阿布舍克·维马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞9 分享