当前位置: 移动技术网 > IT编程>开发语言>Java > ByteArrayInputStream简介和使用_动力节点Java学院整理

ByteArrayInputStream简介和使用_动力节点Java学院整理

2019年07月22日  | 移动技术网IT编程  | 我要评论

bytearrayinputstream 介绍

bytearrayinputstream 是字节数组输入流。它继承于inputstream。

它包含一个内部缓冲区,该缓冲区包含从流中读取的字节;通俗点说,它的内部缓冲区就是一个字节数组,而bytearrayinputstream本质就是通过字节数组来实现的。

我们都知道,inputstream通过read()向外提供接口,供它们来读取字节数据;而bytearrayinputstream 的内部额外的定义了一个计数器,它被用来跟踪 read() 方法要读取的下一个字节。

inputstream 函数列表

// 构造函数
inputstream()

       int   available()
       void  close()
       void  mark(int readlimit)
       boolean marksupported()
       int   read(byte[] buffer)
abstract   int   read()
       int   read(byte[] buffer, int offset, int length)
synchronized void  reset()
       long  skip(long bytecount)

bytearrayinputstream 函数列表

// 构造函数
bytearrayinputstream(byte[] buf)
bytearrayinputstream(byte[] buf, int offset, int length)
synchronized int     available()
       void    close()
synchronized void    mark(int readlimit)
       boolean   marksupported()
synchronized int     read()
synchronized int     read(byte[] buffer, int offset, int length)
synchronized void    reset()
synchronized long    skip(long bytecount)

inputstream和bytearrayinputstream源码分析

inputstream是bytearrayinputstream的父类,我们先看看inputstream的源码,然后再学bytearrayinputstream的源码。

1. inputstream.java源码分析(基于jdk1.7.40)

package java.io;
 public abstract class inputstream implements closeable {
   // 能skip的大小
   private static final int max_skip_buffer_size = ;
   // 从输入流中读取数据的下一个字节。
   public abstract int read() throws ioexception;
   // 将数据从输入流读入 byte 数组。
   public int read(byte b[]) throws ioexception {
     return read(b, 0, b.length);
   } 
   // 将最多 len 个数据字节从此输入流读入 byte 数组。
   public int read(byte b[], int off, int len) throws ioexception {
     if (b == null) {
       throw new nullpointerexception();
     } else if (off < 0 || len < 0 || len > b.length - off) {
      throw new indexoutofboundsexception();
     } else if (len == 0) {
       return 0;
     } 
     int c = read();
     if (c == -1) {
       return -1;
    }
    b[off] = (byte)c; 
     int i = 1;
     try {
       for (; i < len ; i++) {
         c = read();
         if (c == -) {
           break;
         }
         b[off + i] = (byte)c;
       }
     } catch (ioexception ee) {
     }
     return i;
   }
   // 跳过输入流中的n个字节
   public long skip(long n) throws ioexception {
     long remaining = n;
     int nr; 
     if (n <= 0) {
       return 0;
     } 
     int size = (int)math.min(max_skip_buffer_size, remaining);
     byte[] skipbuffer = new byte[size];
     while (remaining > 0) {
       nr = read(skipbuffer, 0, (int)math.min(size, remaining));
      if (nr < 0) {
         break;
       }
       remaining -= nr;
     }
     return n - remaining;
   }
   public int available() throws ioexception {
    return 0;
   }
   public void close() throws ioexception {}
   public synchronized void mark(int readlimit) {}
   public synchronized void reset() throws ioexception {
     throw new ioexception("mark/reset not supported");
   }
   public boolean marksupported() {
     return false;
   }
 }

2. bytearrayinputstream.java源码分析(基于jdk1.7.40) 

package java.io;
  public class bytearrayinputstream extends inputstream {
    // 保存字节输入流数据的字节数组
    protected byte buf[];
    // 下一个会被读取的字节的索引
    protected int pos;
   // 标记的索引
   protected int mark = 0;
   // 字节流的长度
   protected int count;
   // 构造函数:创建一个内容为buf的字节流
   public bytearrayinputstream(byte buf[]) {
     // 初始化“字节流对应的字节数组为buf”
     this.buf = buf;
     // 初始化“下一个要被读取的字节索引号为”
     this.pos = ;
     // 初始化“字节流的长度为buf的长度”
     this.count = buf.length;
   }
   // 构造函数:创建一个内容为buf的字节流,并且是从offset开始读取数据,读取的长度为length
   public bytearrayinputstream(byte buf[], int offset, int length) {
     // 初始化“字节流对应的字节数组为buf”
     this.buf = buf;
     // 初始化“下一个要被读取的字节索引号为offset”
     this.pos = offset;
     // 初始化“字节流的长度”
     this.count = math.min(offset + length, buf.length);
     // 初始化“标记的字节流读取位置”
     this.mark = offset;
   }
   // 读取下一个字节
   public synchronized int read() {
     return (pos < count) ? (buf[pos++] & 0xff) : -1;
   }
   // 将“字节流的数据写入到字节数组b中”
   // off是“字节数组b的偏移地址”,表示从数组b的off开始写入数据
   // len是“写入的字节长度”
   public synchronized int read(byte b[], int off, int len) {
     if (b == null) {
       throw new nullpointerexception();
     } else if (off < 0 || len < 0 || len > b.length - off) {
       throw new indexoutofboundsexception();
     }
     if (pos >= count) {
      return -1;
     }
     int avail = count - pos;
     if (len > avail) {
       len = avail;
     }
     if (len <= 0) {
      return 0;
     }
     system.arraycopy(buf, pos, b, off, len);
     pos += len;
     return len;
   }
   // 跳过“字节流”中的n个字节。
   public synchronized long skip(long n) {
     long k = count - pos;
     if (n < k) {
       k = n < 0 ? 0 : n;
     }
     pos += k;
     return k;
   }
   // “能否读取字节流的下一个字节”
   public synchronized int available() {
     return count - pos;
   }
   // 是否支持“标签”
   public boolean marksupported() {
     return true;
   }
   // 保存当前位置。readaheadlimit在此处没有任何实际意义
   public void mark(int readaheadlimit) {
     mark = pos;
   }
   // 重置“字节流的读取索引”为“mark所标记的位置”
   public synchronized void reset() {
     pos = mark;
   }
   public void close() throws ioexception {
   }
 }

说明:

bytearrayinputstream实际上是通过“字节数组”去保存数据。

(01) 通过bytearrayinputstream(byte buf[]) 或 bytearrayinputstream(byte buf[], int offset, int length) ,我们可以根据buf数组来创建字节流对象。

(02) read()的作用是从字节流中“读取下一个字节”。

(03) read(byte[] buffer, int offset, int length)的作用是从字节流读取字节数据,并写入到字节数组buffer中。offset是将字节写入到buffer的起始位置,length是写入的字节的长度。

(04) marksupported()是判断字节流是否支持“标记功能”。它一直返回true。

(05) mark(int readlimit)的作用是记录标记位置。记录标记位置之后,某一时刻调用reset()则将“字节流下一个被读取的位置”重置到“mark(int readlimit)所标记的位置”;也就是说,reset()之后再读取字节流时,是从mark(int readlimit)所标记的位置开始读取。

示例代码

关于bytearrayinputstream中api的详细用法,参考示例代码(bytearrayinputstreamtest.java):

import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
/**
 * bytearrayinputstream 测试程序
 *
 */
public class bytearrayinputstreamtest {
  private static final int len = 5;
  // 对应英文字母“abcddefghijklmnopqrsttuvwxyz”
  private static final byte[] arrayletters = {
    0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a
  };
  public static void main(string[] args) {
    string tmp = new string(arrayletters);
    system.out.println("arrayletters="+tmp);
    tesbytearrayinputstream() ;
  }
  /**
   * bytearrayinputstream的api测试函数
   */
  private static void tesbytearrayinputstream() {
    // 创建bytearrayinputstream字节流,内容是arrayletters数组
    bytearrayinputstream bais = new bytearrayinputstream(arrayletters);
    // 从字节流中读取5个字节
    for (int i=0; i<len; i++) {
      // 若能继续读取下一个字节,则读取下一个字节
      if (bais.available() >= 0) {
        // 读取“字节流的下一个字节”
        int tmp = bais.read();
        system.out.printf("%d : 0x%s\n", i, integer.tohexstring(tmp));
      }
    }
    // 若“该字节流”不支持标记功能,则直接退出
    if (!bais.marksupported()) {
      system.out.println("make not supported!");
      return ;
    }
    // 标记“字节流中下一个被读取的位置”。即--标记“0x66”,因为因为前面已经读取了5个字节,所以下一个被读取的位置是第6个字节”
    // (01), bytearrayinputstream类的mark(0)函数中的“参数0”是没有实际意义的。
    // (02), mark()与reset()是配套的,reset()会将“字节流中下一个被读取的位置”重置为“mark()中所保存的位置”
    bais.mark(0);
    // 跳过5个字节。跳过5个字节后,字节流中下一个被读取的值应该是“0x6b”。
    bais.skip(5);
    // 从字节流中读取5个数据。即读取“0x6b, 0x6c, 0x6d, 0x6e, 0x6f”
    byte[] buf = new byte[len];
    bais.read(buf, 0, len);
    // 将buf转换为string字符串。“0x6b, 0x6c, 0x6d, 0x6e, 0x6f”对应字符是“klmno”
    string str1 = new string(buf);
    system.out.printf("str1=%s\n", str1);
    // 重置“字节流”:即,将“字节流中下一个被读取的位置”重置到“mark()所标记的位置”,即0x66。
    bais.reset();
    // 从“重置后的字节流”中读取5个字节到buf中。即读取“0x66, 0x67, 0x68, 0x69, 0x6a”
    bais.read(buf, 0, len);
    // 将buf转换为string字符串。“0x66, 0x67, 0x68, 0x69, 0x6a”对应字符是“fghij”
    string str2 = new string(buf);
    system.out.printf("str2=%s\n", str2);
  }
}

运行结果:

arrayletters=abcdefghijklmnopqrstuvwxyz
0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
str1=klmno
str2=fghij

结果说明:

(01) arrayletters 是字节数组。0x61对应的ascii码值是a,0x62对应的ascii码值是b,依次类推...

(02) bytearrayinputstream bais = new bytearrayinputstream(arrayletters); 这句话是创建“字节流bais”,它的内容就是arrayletters。

(03) for (int i=0; i<len; i++) ; 这个for循环的作用就是从字节流中读取5个字节。每次调用bais.read()就从字节流中读取一个字节。

(04) bais.mark(0); 这句话就是“设置字节流的标记”,此时标记的位置对应的值是0x66。

(05) bais.skip(5); 这句话是跳过5个字节。跳过5个字节后,对应的字节流中下一个被读取的字节的值是0x6b。

(06) bais.read(buf, 0, len); 这句话是“从字节流中读取len个数据写入到buf中,0表示从buf的第0个位置开始写入”。

(07) bais.reset(); 这句话是将“字节流中下一个被读取的位置”重置到“mark()所标记的位置”,即0x66。

学完了bytearrayinputstream输入流。下面,我们学习与之对应的输出流bytearrayoutputstream。

以上所述是小编给大家介绍的bytearrayinputstream简介和使用,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网