探究java IO之PushbackInputStream类

1 篇文章 0 订阅

缓存的新应用之一就是回推pushback)的实现。回推用于输入流,以允许读取字节,然后再将它们返回(回推)到流中。PushbackInputStream类实现了这一思想,提供了一种机制,可以“偷窥”来自输入流的内容而不对它们进行破坏。

PushbackInputStream类具有以下构造函数:

?
1
2
PushbackInputStream(InputStream inputStream)
PushbackInputStream(InputStream inputStream, int  numBytes)

第一种形式创建的流对象允许将一个字节返回到输入流; 第二种形式创建的流对象具有一个长度为numBytes的回推缓存,从而允许将多个字节回推到输入流中。

除了熟悉的来自InputStream的方法外,PushbackInputStream类还提供了unread()方法,如下所示:

?
1
2
3
void  unread( int  b)
void  unread( byte [] buffer)
void  unread( byte [] buffer, int  offset, int  numBytes)

第一种形式回推b的低字节,这会使得后续的read()调用会把这个字节再次读取出来。第二种形式回推buffer中的字节。第三种形式回推buffer中从offset开始的numBytes个字节。当回推缓存已满时,如果试图回推字节,就会抛出IOException异常。

用几个示例测试一下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  io;
 
import  java.io.ByteArrayInputStream;
import  java.io.PushbackInputStream;
 
public  class  PushbackInputStreamDemo1 {
     public  static  void  main(String[] args)  throws  Exception {
         String s =  "abcdefg" ;
         /*
          * PushbackInputStream pbin = new PushbackInputStream(in)
          * 这个构造函数创建的对象一次只能回推一个字节
          */
         try  (ByteArrayInputStream in =  new  ByteArrayInputStream(s.getBytes());
                         PushbackInputStream pbin =  new  PushbackInputStream(in)) {
             int  n;
             while  ((n = pbin.read()) != - 1 ) {
                 System.out.println(( char ) n);
                 if ( 'b'  == n) pbin.unread( 'U' );
             }
         }
     }
}

示例2:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package  io;
 
import  java.io.ByteArrayInputStream;
import  java.io.PushbackInputStream;
 
public  class  PushbackInputStreamDemo1 {
     public  static  void  main(String[] args)  throws  Exception {
         String s =  "abcdefg" ;
         /*
          * PushbackInputStream pbin = new PushbackInputStream(in,3)
          * 这个构造函数创建的对象一次可以回推一个缓存
          */
         try  (ByteArrayInputStream in =  new  ByteArrayInputStream(s.getBytes());
                         PushbackInputStream pbin =  new  PushbackInputStream(in,  3 )) {
             int  n;
             byte [] buffer =  new  byte [ 3 ];
             while  ((n = pbin.read(buffer)) != - 1 ) {
                 System.out.println( new  String(buffer));
                 if ( new  String(buffer).equals( "abc" ))pbin.unread( new  byte []{ 'M' , 'N' , 'O' });
                 buffer =  new  byte [ 3 ];
             }
         }
     }
}

示例3:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package  io;
 
import  java.io.ByteArrayInputStream;
import  java.io.PushbackInputStream;
 
public  class  PushbackInputStreamDemo1 {
     public  static  void  main(String[] args)  throws  Exception {
         String s =  "abcdefg" ;
         /*
          * PushbackInputStream pbin = new PushbackInputStream(in,4)
          * 这个构造函数创建的对象一次可以回推一个缓存
          */
         try  (ByteArrayInputStream in =  new  ByteArrayInputStream(s.getBytes());
                         PushbackInputStream pbin =  new  PushbackInputStream(in,  4 )) {
             int  n;
             byte [] buffer =  new  byte [ 4 ];
             while  ((n = pbin.read(buffer)) != - 1 ) {
                 System.out.println( new  String(buffer));
                 //取回推缓存中的一部分数据 
                 if ( new  String(buffer).equals( "abcd" ))pbin.unread(buffer, 2 , 2 );
                 buffer =  new  byte [ 4 ];
             }
         }
     }
}

注:PushbackInputStream对象会使得InputStream对象(用于创建PushbackInputStream对象)的mark()或reset()方法无效。对于准备使用mark()或reset()方法的任何流来说,都应当使用markSupported()方法进行检查。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值