码迷,mamicode.com
首页 > 编程语言 > 详细

Java IO之PushbackInputStream类

时间:2015-06-14 09:38:55      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:pushback   java回推   java-io回推   

  PushbackInputStream存在的意义就是允许我试探性的读取数据流,如果不是我们想要的则返还回去,之所以能够这样,因为其内部维护了一个pushback buffer缓冲区。构造函数可以指定返回的字节个数。
  缓存的新应用之一就是回推(pushback)的实现。回推用于输入流,以允许读取字节,然后再将它们返回(回推)到流中。PushbackInputStream类实现了这一思想,提供了一种机制,可以“偷窥”来自输入流的内容而不对它们进行破坏。

  PushbackInputStream类具有以下构造函数:

PushbackInputStream(InputStream inputStream)
PushbackInputStream(InputStream inputStream,int numBytes)

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

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

void unread(int b)
void unread(byte[] buffer)
void unread(byte[] buffer,int offset,int numBytes)

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

  用几个示例测试一下:
  

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;

public class test {
    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‘);
            }
        }
    }
}

a
b
U
c
d
e
f
g 

  示例2:
  

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;

public class test {
    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];
            }
        }
    }
}

abc
def
g

  示例3:
  

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;

public class test {
    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];
            }
        }
    }
}

abcd
cdef
g 

  示例4:
  

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;

public class test {
    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,3);
                buffer = new byte[4];
            }
        }
    }
}

abcd
Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException at
java.lang.System.arraycopy(Native Method) at
java.io.PushbackInputStream.unread(Unknown Source) at
test.main(test.java:18)

  因为abcd从下标2的c开始,数3个字符,越界了!!!所以异常! 


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

Java IO之PushbackInputStream类

标签:pushback   java回推   java-io回推   

原文地址:http://blog.csdn.net/wtyvhreal/article/details/46484025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!