• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > ByteArrayInputStream简介和使用_动力节点Java学院整理

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了bytearrayinputstream,inputstream,fileinputstream,inputstreamreader,bufferedinputstream等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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)
</div>

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)
</div>

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;
   }
 }
</div>

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 {
   }
 }
</div>

说明:

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



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28Java自定义异常_动力节点Java学院整理
  • 2017-05-28Spring基于注解整合Redis完整实例
  • 2017-05-28Spring Boot多数据源及其事务管理配置方法
  • 2017-05-28Java System类详解_动力节点Java学院整理
  • 2017-05-28Spring Boot 集成Dubbo框架实例
  • 2017-05-28详解Spring Boot 添加JSP支持
  • 2017-05-28java实现word文件转html文件
  • 2017-05-28Spring Boot + Jpa(Hibernate) 架构基本配置详解
  • 2017-05-28SWT JFace Bookmark 制作
  • 2017-05-28java设计模式—静态代理模式(聚合与继承方式对比)

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Java字符编码原理(动力节点Java学院整理)
    • 解决Java原生压缩组件不支持中文文件名乱码的问题
    • 详解SpringBoot中实现依赖注入功能
    • 命令提示符编译java的方法(必看篇)
    • SpringBoot整合JPA的实例代码
    • Java四种线程池的使用
    • Java正则匹配中文的方法实例分析
    • Java IO流 File类的常用API实例
    • Spring依赖注入的两种方式(根据实例详解)
    • 分隔List集合,按指定大小,将集合分成多个的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有