java io系列14之 DataInputStream(数据输入流)的认知、源码和示例 您所在的位置:网站首页 socket获取inputstream长度 java io系列14之 DataInputStream(数据输入流)的认知、源码和示例

java io系列14之 DataInputStream(数据输入流)的认知、源码和示例

2024-05-21 03:01| 来源: 网络整理| 查看: 265

本章介绍DataInputStream。我们先对DataInputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解。

转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_14.html

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)

 

DataInputStream.java源码分析(基于jdk1.7.40) 1 package java.io; 2 3 public class DataInputStream extends FilterInputStream implements DataInput { 4 5 // 构造函数。 6 public DataInputStream(InputStream in) { 7 super(in); 8 } 9 10 private byte bytearr[] = new byte[80]; 11 private char chararr[] = new char[80]; 12 13 // 从“数据输入流”中读取一个字节 14 public final int read(byte b[]) throws IOException { 15 return in.read(b, 0, b.length); 16 } 17 18 // 从“数据输入流”中读取数据并存储到字节数组b中。 19 // off是字节数组b中开始存储元素的起始位置。 20 // len是读取字节的个数。 21 public final int read(byte b[], int off, int len) throws IOException { 22 return in.read(b, off, len); 23 } 24 25 // 从“数据输入流”中读取数据并填满字节数组b中;没有填满数组b则一直读取,直到填满位置。 26 // 从字节数组b的位置0开始存储,并且读取的字节个数等于b的长度。 27 public final void readFully(byte b[]) throws IOException { 28 readFully(b, 0, b.length); 29 } 30 31 // 从“数据输入流”中读取数据并存储到字节数组b中;若没读取len个字节,直到一直读取直到读取完len个字节为止。 32 public final void readFully(byte b[], int off, int len) throws IOException { 33 if (len < 0) 34 throw new IndexOutOfBoundsException(); 35 int n = 0; 36 while (n


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有