• 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 > Java 中的 DataInputStream 介绍_动力节点Java学院整理

Java 中的 DataInputStream 介绍_动力节点Java学院整理

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

通过本文主要向大家介绍了java datainputstream,datainputstream,datainputstream用法,datainputstream read,datainputstream类等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

DataInputStream 介绍

DataInputStream 是数据输入流。它继承于FilterInputStream。

DataInputStream 是用来装饰其它输入流,它“允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型”。应用程序可以使用DataOutputStream(数据输出流)写入由DataInputStream(数据输入流)读取的数据。

DataInputStream 函数列表 

DataInputStream(InputStream in)
final int  read(byte[] buffer, int offset, int length)
final int  read(byte[] buffer)
final boolean  readBoolean()
final byte  readByte()
final char  readChar()
final double  readDouble()
final float  readFloat()
final void  readFully(byte[] dst)
final void  readFully(byte[] dst, int offset, int byteCount)
final int  readInt()
final String  readLine()
final long  readLong()
final short  readShort()
final static String  readUTF(DataInput in)
final String  readUTF()
final int  readUnsignedByte()
final int  readUnsignedShort()
final int  skipBytes(int count)
</div>

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

package java.io;
 public class DataInputStream extends FilterInputStream implements DataInput {
  // 构造函数。
  public DataInputStream(InputStream in) {
   super(in);
  }
  private byte bytearr[] = new byte[80];
  private char chararr[] = new char[80];
  // 从“数据输入流”中读取一个字节
  public final int read(byte b[]) throws IOException {
  return in.read(b, 0, b.length);
  }
  // 从“数据输入流”中读取数据并存储到字节数组b中。
  // off是字节数组b中开始存储元素的起始位置。
  // len是读取字节的个数。
  public final int read(byte b[], int off, int len) throws IOException {
   return in.read(b, off, len);
  }
  // 从“数据输入流”中读取数据并填满字节数组b中;没有填满数组b则一直读取,直到填满位置。
  // 从字节数组b的位置0开始存储,并且读取的字节个数等于b的长度。
  public final void readFully(byte b[]) throws IOException {
   readFully(b, 0, b.length);
  }
  // 从“数据输入流”中读取数据并存储到字节数组b中;若没读取len个字节,直到一直读取直到读取完len个字节为止。
  public final void readFully(byte b[], int off, int len) throws IOException {
   if (len < 0)
    throw new IndexOutOfBoundsException();
   int n = 0;
   while (n < len) {
    int count = in.read(b, off + n, len - n);
   if (count < 0)
     throw new EOFException();
    n += count;
   }
  }
  // 跳过n个字节
  public final int skipBytes(int n) throws IOException {
   int total = 0;
  int cur = 0;
  while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
    total += cur;
   }
   return total;
  }
  // 从“数据输入流”中读取boolean类型的值
  public final boolean readBoolean() throws IOException {
   int ch = in.read();
  if (ch < 0)
    throw new EOFException();
   return (ch != 0);
  }
  // 从“数据输入流”中读取Byte类型的值
  public final byte readByte() throws IOException {
   int ch = in.read();
   if (ch < 0)
    throw new EOFException();
   return (byte)(ch);
  }
  // 从“数据输入流”中读取“无符号的Byte类型”的值,即读取值为正数的byte值
  public final int readUnsignedByte() throws IOException {
   int ch = in.read();
   if (ch < 0)
    throw new EOFException();
   return ch;
  }
  // 从“数据输入流”中读取“short类型”的值
  public final short readShort() throws IOException {
   int ch = in.read();
   int ch = in.read();
  if ((ch1 | ch2) < 0)
    throw new EOFException();
  return (short)((ch1 << 8) + (ch2 << 0));
  }
  // 从“数据输入流”中读取“无符号的short类型”的值
  public final int readUnsignedShort() throws IOException {
   int ch1 = in.read();
   int ch2 = in.read();
   if ((ch1 | ch2) < 0)
    throw new EOFException();
  return (ch1 << 8) + (ch2 << 0);
  }
  // 从“数据输入流”中读取“char类型”的值
  public final char readChar() throws IOException {
  int ch1 = in.read();
  int ch2 = in.read();
   if ((ch1 | ch2) < 0)
   throw new EOFException();
  return (char)((ch1 << 8) + (ch2 << 0));
  }
  // 从“数据输入流”中读取“int类型”的值
 public final int readInt() throws IOException {
  int ch1 = in.read();
   int ch2 = in.read();
  int ch3 = in.read();
  int ch4 = in.read();
  if ((ch1 | ch2 | ch3 | ch4) < 0)
    throw new EOFException();
  return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  }
 private byte readBuffer[] = new byte[8];
  // 从“数据输入流”中读取“long类型”的值
  public final long readLong() throws IOException {
  readFully(readBuffer, 0, 8);
   return (((long)readBuffer[0] << 56) +
    ((long)(readBuffer[1] & 255) << 48) +
    ((long)(readBuffer[2] & 255) << 40) +
    ((long)(readBuffer[3] & 255) << 32) +
     ((long)(readBuffer[4] & 255) << 24) +
    ((readBuffer[5] & 255) << 16) +
     ((readBuffer[6] & 255) << 8) +
    ((readBuffer[7] & 255) << 0));
  }
  // 从“数据输入流”中读取“float类型”的值
  public final float readFloat() throws IOException {
   return Float.intBitsToFloat(readInt());
  }
  // 从“数据输入流”中读取“double类型”的值
  public final double readDouble() throws IOException {
   return Double.longBitsToDouble(readLong());
  }
  private char lineBuffer[];
  @Deprecated
  public final String readLine() throws IOException {
   char buf[] = lineBuffer;
   if (buf == null) {
    buf = lineBuffer = new char[];
   }
   int room = buf.length;
  int offset = 0;
   int c;
 loop: while (true) {
    switch (c = in.read()) {
    case -1:
    case '\n':
     break loop;
    case '\r':
     int c2 = in.read();
     if ((c2 != '\n') && (c2 != -1)) {
      if (!(in instanceof PushbackInputStream)) {
       this.in = new PushbackInputStream(in);
      }
      ((PushbackInputStream)in).unread(c2);
     }
     break loop;
    default:
    if (--room < 0) {
     buf = new char[offset + 128];
      room = buf.length - offset - 1;
     System.arraycopy(lineBuffer, 0, buf, 0, offset);
      lineBuffer = buf;
     }
     buf[offset++] = (char) c;
     break;
    }
   }
   if ((c == -1) && (offset == 0)) {
    return null;
   }
   return String.copyValueOf(buf, , offset);
  }
  // 从“数据输入流”中读取“UTF类型”的值
  public final String readUTF() throws IOException {
   return readUTF(this);
  }
  public final static String readUTF(DataInput in) throws IOException {
   // 从“数据输入流”中读取“无符号的short类型”的值:
   // 注意:UTF-8输入流的前2个字节是数据的长度
   int utflen = in.readUnsignedShort();
   byte[] bytearr = null;
   char[] chararr = null;
   // 如果in本身是“数据输入流”,
   // 则,设置字节数组bytearr = "数据输入流"的成员bytearr
   //  设置字符数组chararr = "数据输入流"的成员chararr
   // 否则的话,新建数组bytearr和chararr
   if (in instanceof DataInputStream) {
    DataInputStream dis = (DataInputStream)in;
    if (dis.bytearr.length < utflen){
     dis.bytearr = new byte[utflen*2];
     dis.chararr = new char[utflen*2];
    }
    chararr = dis.chararr;
    bytearr = dis.bytearr;
   } else {
    bytearr = new byte[utflen];
    chararr = new char[utflen];
   }
   int c, char2, char3;
   int count = 0;
   int chararr_count=0;
   // 从“数据输入流”中读取数据并存储到字节数组bytearr中;从bytearr的位置0开始存储,存储长度为utflen。
   // 注意,这里是存储到字节数组!而且读取的是全部的数据。
   in.readFully(bytearr, 0, utflen);
   // 将“字节数组bytearr”中的数据 拷贝到 “字符数组chararr”中
   // 注意:这里相当于“预处理的输入流中单字节的符号”,因为UTF-8是1-4个字节可变的。
   while (count < utflen) {
    // 将每个字节转换成int值
    c = (int) bytearr[count] & xff;
    // UTF-8的单字节数据的值都不会超过127;所以,超



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

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

  • Java 中的 DataInputStream 介绍_动力节点Java学院整理
  • Java 中的 DataInputStream 介绍_动力节点Java学院整理

相关文章

  • 2017-05-28Java获取UTC时间的方法详解
  • 2017-05-28Java通过PropertyDescriptor反射调用set和get方法
  • 2017-05-28浅谈SpringMVC中的session用法及细节记录
  • 2017-05-28值得Java程序猿阅读的书籍
  • 2017-05-28Java数据结构之数组(动力节点之Java学院整理)
  • 2017-05-28spring boot配合前端实现跨域请求访问
  • 2017-05-28Spring事务Transaction配置的五种注入方式详解
  • 2017-05-28Swing图形界面实现可动态刷新的验证码
  • 2017-05-28Java定时任务详解
  • 2017-05-28Spring Boot的filter(过滤器)简单使用实例详解

文章分类

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

最近更新的内容

    • Java 网络编程socket编程等详解
    • springboot整合 beatlsql的实例代码
    • Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例
    • springmvc实现简单的拦截器
    • java实现短信通信的完整教程
    • Java环境配置与编译运行详解
    • spring boot整合CAS配置详解
    • Java Socket编程(五) 简单的WEB服务器
    • java 中链表的定义与使用方法
    • java随机验证码生成实现实例代码

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

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