欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java IO学习之缓冲输入流(BufferedInputStream)

程序员文章站 2024-03-07 16:33:09
java io  bufferedinputstream 概要: bufferedinputstream是缓冲输入流,继承于filterinputstream...

java io  bufferedinputstream

概要:

bufferedinputstream是缓冲输入流,继承于filterinputstream,作用是为另一个输入流添加一些功能,本质上是通过一个内部缓冲数组实现的。例如,在新建某输入流对应的bufferedinputstream后,当通过read()读取数据时,bufferedinputstream会将输入流的数据分批的填入到缓冲区中,每当缓冲区的数据读完之后,输入流会再次填充数据缓冲区,直到读完数据。

bufferedinputstream主要的函数列表:

bufferedinputstream(inputstream in) 
bufferedinputstream(inputstream in, int size) 
synchronized int available() 
void close() 
synchronized void mark(int readlimit) 
boolean marksupported() 
synchronized int read() 
synchronized int read(byte[] buffer, int offset, int bytecount) 
synchronized void reset() 
synchronized long skip(long bytecount) 

示例代码:

public class bufferedinputstreamtest { 
 
  private static final int len = 5; 
 
  public static void main(string[] args) { 
    testbufferedinputstream() ; 
  } 
  private static void testbufferedinputstream() { 
 
    // 创建bufferedinputstream字节流,内容是arrayletters数组 
    try { 
      file file = new file("file.txt"); 
      inputstream in =new bufferedinputstream(new fileinputstream(file), 512); 
 
      // 从字节流中读取5个字节。“abcde”,a对应0x61,b对应0x62,依次类推... 
      for (int i=0; i<len; i++) { 
      // 若能继续读取下一个字节,则读取下一个字节 
        if (in.available() >= 0) { 
        // 读取“字节流的下一个字节” 
        int tmp = in.read(); 
        system.out.printf("%d : 0x%s\n", i, integer.tohexstring(tmp)); 
        } 
      } 
 
      // 若“该字节流”不支持标记功能,则直接退出 
      if (!in.marksupported()) { 
        system.out.println("make not supported!"); 
        return ; 
      } 
 
      // 标记“当前索引位置”,即标记第6个位置的元素--“f” 
      // 1024对应marklimit 
      in.mark(1024); 
 
      // 跳过22个字节。 
      in.skip(22); 
 
      // 读取5个字节 
      byte[] buf = new byte[len]; 
      in.read(buf, 0, len); 
      // 将buf转换为string字符串。 
      string str1 = new string(buf); 
      system.out.printf("str1=%s\n", str1); 
 
      // 重置“输入流的索引”为mark()所标记的位置,即重置到“f”处。 
      in.reset(); 
      // 从“重置后的字节流”中读取5个字节到buf中。即读取“fghij” 
      in.read(buf, 0, len); 
      // 将buf转换为string字符串。 
      string str2 = new string(buf); 
      system.out.printf("str2=%s\n", str2); 
 
      in.close(); 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch (securityexception e) { 
      e.printstacktrace(); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

运行结果:

0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
str1=12345
str2=fghij

基于jdk8的bufferinputstream代码:

public class bufferedinputstream extends filterinputstream { 
 
  private static int default_buffer_size = 8192;//默认缓冲区大小为8x1024 
  private static int max_buffer_size = integer.max_value - 8; 
  protected volatile byte buf[]; //缓冲数组 
 
  /** 
   * atomic updater to provide compareandset for buf. this is 
   * necessary because closes can be asynchronous. we use nullness 
   * of buf[] as primary indicator that this stream is closed. (the 
   * "in" field is also nulled out on close.) 
   */ 
  private static final atomicreferencefieldupdater<bufferedinputstream, byte[]> bufupdater = 
      atomicreferencefieldupdater.newupdater(bufferedinputstream.class, byte[].class, "buf"); 
  //值在0到buf.len之间 
  protected int count; 
  //在buffer的当前位置,下一个字符被读取 
  protected int pos; 
  //值为mark函数最近被调用的时候,值为-1到pos 
  protected int markpos = -1; 
 
  /** 
   * the maximum read ahead allowed after a call to the 
   * <code>mark</code> method before subsequent calls to the 
   * <code>reset</code> method fail. 
   * whenever the difference between <code>pos</code> 
   * and <code>markpos</code> exceeds <code>marklimit</code>, 
   * then the mark may be dropped by setting 
   * <code>markpos</code> to <code>-1</code>. 
   * 
   * @see   java.io.bufferedinputstream#mark(int) 
   * @see   java.io.bufferedinputstream#reset() 
   */ 
 
  protected int marklimit; 
 
  /** 
   * check to make sure that underlying input stream has not been 
   * nulled out due to close; if not return it; 
   */ 
  // 
  private inputstream getinifopen() throws ioexception { 
    inputstream input = in; 
    if (input == null) 
      throw new ioexception("stream closed"); 
    return input; 
  } 
 
  /** 
   * check to make sure that buffer has not been nulled out due to 
   * close; if not return it; 
   */ 
  private byte[] getbufifopen() throws ioexception { 
    byte[] buffer = buf; 
    if (buffer == null) 
      throw new ioexception("stream closed"); 
    return buffer; 
  } 
 
  /** 
   * creates a <code>bufferedinputstream</code> 
   * and saves its argument, the input stream 
   * <code>in</code>, for later use. an internal 
   * buffer array is created and stored in <code>buf</code>. 
   * 
   * @param  in  the underlying input stream. 
   */ 
  //带inputstream的构造函数 
  public bufferedinputstream(inputstream in) { 
      this(in, default_buffer_size); 
  } 
 
  /** 
   * creates a <code>bufferedinputstream</code> 
   * with the specified buffer size, 
   * and saves its argument, the input stream 
   * <code>in</code>, for later use. an internal 
   * buffer array of length <code>size</code> 
   * is created and stored in <code>buf</code>. 
   * 
   * @param  in   the underlying input stream. 
   * @param  size  the buffer size. 
   * @exception illegalargumentexception if {@code size <= 0}. 
   */ 
  //带inputstream和大小的构造函数 
  public bufferedinputstream(inputstream in, int size) { 
    super(in); 
    if (size <= 0) { 
        throw new illegalargumentexception("buffer size <= 0"); 
    } 
    buf = new byte[size]; 
  } 
 
  /** 
   * fills the buffer with more data, taking into account 
   * shuffling and other tricks for dealing with marks. 
   * assumes that it is being called by a synchronized method. 
   * this method also assumes that all data has already been read in, 
   * hence pos > count. 
   */ 
  // 
  private void fill() throws ioexception { 
    byte[] buffer = getbufifopen(); 
    if (markpos < 0) 
      pos = 0;      /* no mark: throw away the buffer */ 
    else if (pos >= buffer.length) /* no room left in buffer */ 
      if (markpos > 0) { /* can throw away early part of the buffer */ 
        int sz = pos - markpos; 
        system.arraycopy(buffer, markpos, buffer, 0, sz); 
        pos = sz; 
        markpos = 0; 
      } else if (buffer.length >= marklimit) { 
         markpos = -1;  /* buffer got too big, invalidate mark */ 
         pos = 0;    /* drop buffer contents */ 
      } else if (buffer.length >= max_buffer_size) { 
        throw new outofmemoryerror("required array size too large"); 
      } else {      /* grow buffer */ 
        int nsz = (pos <= max_buffer_size - pos) ? 
        pos * 2 : max_buffer_size; 
        if (nsz > marklimit) 
          nsz = marklimit; 
        byte nbuf[] = new byte[nsz]; 
        system.arraycopy(buffer, 0, nbuf, 0, pos); 
        if (!bufupdater.compareandset(this, buffer, nbuf)) { 
          // can't replace buf if there was an async close. 
          // note: this would need to be changed if fill() 
          // is ever made accessible to multiple threads. 
          // but for now, the only way cas can fail is via close. 
          // assert buf == null; 
          throw new ioexception("stream closed"); 
        } 
        buffer = nbuf; 
      } 
    count = pos; 
    int n = getinifopen().read(buffer, pos, buffer.length - pos); 
    if (n > 0) 
      count = n + pos; 
    } 
 
  /** 
   * see 
   * the general contract of the <code>read</code> 
   * method of <code>inputstream</code>. 
   * 
   * @return   the next byte of data, or <code>-1</code> if the end of the 
   *       stream is reached. 
   * @exception ioexception if this input stream has been closed by 
   *             invoking its {@link #close()} method, 
   *             or an i/o error occurs. 
   * @see    java.io.filterinputstream#in 
   */ 
  //读下一个字节,没有数据返回-1 
  public synchronized int read() throws ioexception { 
    if (pos >= count) { 
      fill(); 
      if (pos >= count) 
        return -1; 
    } 
    return getbufifopen()[pos++] & 0xff; 
  } 
 
  /** 
   * read characters into a portion of an array, reading from the underlying 
   * stream at most once if necessary. 
   */ 
  private int read1(byte[] b, int off, int len) throws ioexception { 
    int avail = count - pos; 
    if (avail <= 0) { 
      /* if the requested length is at least as large as the buffer, and 
        if there is no mark/reset activity, do not bother to copy the 
        bytes into the local buffer. in this way buffered streams will 
        cascade harmlessly. */ 
      if (len >= getbufifopen().length && markpos < 0) { 
        return getinifopen().read(b, off, len); 
      } 
      fill(); 
      avail = count - pos; 
      if (avail <= 0) return -1; 
    } 
    int cnt = (avail < len) ? avail : len; 
    system.arraycopy(getbufifopen(), pos, b, off, cnt); 
    pos += cnt; 
    return cnt; 
  } 
 
  /** 
   * reads bytes from this byte-input stream into the specified byte array, 
   * starting at the given offset. 
   * 
   * <p> this method implements the general contract of the corresponding 
   * <code>{@link inputstream#read(byte[], int, int) read}</code> method of 
   * the <code>{@link inputstream}</code> class. as an additional 
   * convenience, it attempts to read as many bytes as possible by repeatedly 
   * invoking the <code>read</code> method of the underlying stream. this 
   * iterated <code>read</code> continues until one of the following 
   * conditions becomes true: <ul> 
   * 
   *  <li> the specified number of bytes have been read, 
   * 
   *  <li> the <code>read</code> method of the underlying stream returns 
   *  <code>-1</code>, indicating end-of-file, or 
   * 
   *  <li> the <code>available</code> method of the underlying stream 
   *  returns zero, indicating that further input requests would block. 
   * 
   * </ul> if the first <code>read</code> on the underlying stream returns 
   * <code>-1</code> to indicate end-of-file then this method returns 
   * <code>-1</code>. otherwise this method returns the number of bytes 
   * actually read. 
   * 
   * <p> subclasses of this class are encouraged, but not required, to 
   * attempt to read as many bytes as possible in the same fashion. 
   * 
   * @param   b   destination buffer. 
   * @param   off  offset at which to start storing bytes. 
   * @param   len  maximum number of bytes to read. 
   * @return   the number of bytes read, or <code>-1</code> if the end of 
   *       the stream has been reached. 
   * @exception ioexception if this input stream has been closed by 
   *             invoking its {@link #close()} method, 
   *             or an i/o error occurs. 
   */ 
  // 
  public synchronized int read(byte b[], int off, int len)throws ioexception 
  { 
    getbufifopen(); // check for closed stream 
    if ((off | len | (off + len) | (b.length - (off + len))) < 0) { 
      throw new indexoutofboundsexception(); 
    } else if (len == 0) { 
      return 0; 
    } 
 
    int n = 0; 
    for (;;) { 
      int nread = read1(b, off + n, len - n); 
      if (nread <= 0) 
        return (n == 0) ? nread : n; 
      n += nread; 
      if (n >= len) 
        return n; 
      // if not closed but no bytes available, return 
      inputstream input = in; 
      if (input != null && input.available() <= 0) 
        return n; 
    } 
  } 
 
  /** 
   * see the general contract of the <code>skip</code> 
   * method of <code>inputstream</code>. 
   * 
   * @exception ioexception if the stream does not support seek, 
   *             or if this input stream has been closed by 
   *             invoking its {@link #close()} method, or an 
   *             i/o error occurs. 
   */ 
  //跳过n长的数据 
  public synchronized long skip(long n) throws ioexception { 
    getbufifopen(); // check for closed stream 
    if (n <= 0) { 
      return 0; 
    } 
    long avail = count - pos; 
 
    if (avail <= 0) { 
      // if no mark position set then don't keep in buffer 
      if (markpos <0) 
        return getinifopen().skip(n); 
 
      // fill in buffer to save bytes for reset 
      fill(); 
      avail = count - pos; 
      if (avail <= 0) 
        return 0; 
    } 
 
    long skipped = (avail < n) ? avail : n; 
    pos += skipped; 
    return skipped; 
  } 
 
  /** 
   * returns an estimate of the number of bytes that can be read (or 
   * skipped over) from this input stream without blocking by the next 
   * invocation of a method for this input stream. the next invocation might be 
   * the same thread or another thread. a single read or skip of this 
   * many bytes will not block, but may read or skip fewer bytes. 
   * <p> 
   * this method returns the sum of the number of bytes remaining to be read in 
   * the buffer (<code>count - pos</code>) and the result of calling the 
   * {@link java.io.filterinputstream#in in}.available(). 
   * 
   * @return   an estimate of the number of bytes that can be read (or skipped 
   *       over) from this input stream without blocking. 
   * @exception ioexception if this input stream has been closed by 
   *             invoking its {@link #close()} method, 
   *             or an i/o error occurs. 
   */ 
  //返回还有多少数据可以读 
  public synchronized int available() throws ioexception { 
    int n = count - pos; 
    int avail = getinifopen().available(); 
    return n > (integer.max_value - avail)? integer.max_value: n + avail; 
  } 
 
  /** 
   * see the general contract of the <code>mark</code> 
   * method of <code>inputstream</code>. 
   * 
   * @param  readlimit  the maximum limit of bytes that can be read before 
   *           the mark position becomes invalid. 
   * @see   java.io.bufferedinputstream#reset() 
   */ 
  public synchronized void mark(int readlimit) { 
    marklimit = readlimit; 
    markpos = pos; 
  } 
 
  /** 
   * see the general contract of the <code>reset</code> 
   * method of <code>inputstream</code>. 
   * <p> 
   * if <code>markpos</code> is <code>-1</code> 
   * (no mark has been set or the mark has been 
   * invalidated), an <code>ioexception</code> 
   * is thrown. otherwise, <code>pos</code> is 
   * set equal to <code>markpos</code>. 
   * 
   * @exception ioexception if this stream has not been marked or, 
   *         if the mark has been invalidated, or the stream 
   *         has been closed by invoking its {@link #close()} 
   *         method, or an i/o error occurs. 
   * @see    java.io.bufferedinputstream#mark(int) 
   */ 
  public synchronized void reset() throws ioexception { 
    getbufifopen(); // cause exception if closed 
    if (markpos < 0) 
      throw new ioexception("resetting to invalid mark"); 
    pos = markpos; 
  } 
 
  /** 
   * tests if this input stream supports the <code>mark</code> 
   * and <code>reset</code> methods. the <code>marksupported</code> 
   * method of <code>bufferedinputstream</code> returns 
   * <code>true</code>. 
   * 
   * @return a <code>boolean</code> indicating if this stream type supports 
   *     the <code>mark</code> and <code>reset</code> methods. 
   * @see   java.io.inputstream#mark(int) 
   * @see   java.io.inputstream#reset() 
   */ 
  //是否支持标记 
  public boolean marksupported() { 
    return true; 
  } 
 
  /** 
   * closes this input stream and releases any system resources 
   * associated with the stream. 
   * once the stream has been closed, further read(), available(), reset(), 
   * or skip() invocations will throw an ioexception. 
   * closing a previously closed stream has no effect. 
   * 
   * @exception ioexception if an i/o error occurs. 
   */ 
  //关闭资源  
  public void close() throws ioexception { 
    byte[] buffer; 
    while ( (buffer = buf) != null) { 
      if (bufupdater.compareandset(this, buffer, null)) { 
        inputstream input = in; 
        in = null; 
        if (input != null) 
          input.close(); 
        return; 
      } 
      // else retry in case a new buf was cased in fill() 
    } 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!