热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javaio源码解读_#JavaIO源码解析

1.1.1InputStreampublicabstractclassInputStreamimplementsCloseableInputStreamm继承接口Closeable

1.1.1 InputStream

public abstract class InputStream implements Closeable

//InputStreamm 继承接口 Closeable,流可关闭

主要方法:

public abstract int read()//1

public int read(byte b[], int off, int len)//2

public int readNBytes(byte[] b, int off, int len)

public long skip(long n)

public void skipNBytes(long n)

public int available()

public void close()

public synchronized void mark(int readlimit)

public synchronized void reset()

public long transferTo(OutputStream out)//3

1.1.2 Closeable 接口

public interface Closeable extends AutoCloseable{

/**

* Closes this stream and releases any system resources associated

* with it. If the stream is already closed then invoking this

* method has no effect.*/

public void close() throws IOException;

}

1.1.3 read && transferTo

/**

* Constructor for subclasses to call.

*/

public InputStream() {}//所有子类的默认构造方法

/*The stream is closed by calling the * {@code close()} method. Subsequent calls to {@code close()} have no

* effect.*/

public abstract int read() throws IOException;//1

/*基本方法,读一个字节。成功,返回读取的字节数,失败,返回-1

*/

//从数组中index为off处,开始读取len长度的字节。成功,返回,已读取的字节数

public int read(byte b[], int off, int len) throws IOException {//2

Objects.checkFromIndexSize(off, len, b.length);

if (len == 0) {

return 0;

}

int c = read();//读取一个字节

if (c == -1) {

return -1;

}

b[off] = (byte)c;//强制类型转换,int转为byte

int i = 1;

try {

for (; i

c = read();

if (c == -1) {

break;

}

b[off + i] = (byte)c;

}

} catch (IOException ee) {

}

return i;//返回成功读取的字节数

}

//将一个输入流转为输出流,返回为已转的字节数

public long transferTo(OutputStream out) throws IOException {//3

Objects.requireNonNull(out, "out");

long transferred = 0;

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

int read;

while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {

out.write(buffer, 0, read);

transferred += read;

}

return transferred;

}

1.2.1 OutputStream

public abstract class OutputStream implements Closeable, Flushable

public abstract void write(int b)

/** 只写入低8位?

* Writes the specified byte to this output stream. The general

* contract for {@code write} is that one byte is written

* to the output stream. The byte to be written is the eight

* low-order bits of the argument {@code b}. The 24

* high-order bits of {@code b} are ignored.*/

public void write(byte b[])

public void write(byte b[], int off, int len)//1

public void flush()

/** 将输出缓冲区的字节全部flush

* Flushes this output stream and forces any buffered output bytes

* to be written out. */

public void close()

1.2.2 Flushable

public interface Flushable {

/**

* Flushes this stream by writing any buffered output to the underlying

* stream.

*

* @throws IOException If an I/O error occurs

*/

void flush() throws IOException;

}

1.2.3 write

public void write(byte b[], int off, int len) throws IOException {//1

Objects.checkFromIndexSize(off, len, b.length);

// len == 0 condition implicitly handled by loop bounds

for (int i = 0 ; i

write(b[off + i]);

}

}

2.1.1 DataOutput

/** 将Java基础数据类型写入字节流中

* The {@code DataOutput} interface provides

* for converting data from any of the Java

* primitive types to a series of bytes and

* writing these bytes to a binary stream.*/

public interface DataOutput {

2.1.2 主要方法

void write(int b)//将int b 的低8位写入流中

void write(byte b[])

void write(byte b[], int off, int len)

void writeBoolean(boolean v) //true写入1,false写入0

void writeByte(int v)

//和write方法一样,不过这个显著提醒了只写入一个字节

void writeShort(int v) //写入2个字节

void writeChar(int v)//写入单字符

/* Writes a {@code char} value, which

如十进制的65,对应于A (ACSII码)

* is comprised of two bytes*/

void writeInt(int v)//写入4个字节

void writeBytes(String s)

void writeChars(String s)

void writeUTF(String s)

2.2.1 DataInput

/**

* The {@code DataInput} interface provides

* for reading bytes from a binary stream and

* reconstructing from them data in any of

* the Java primitive types. */

public interface DataInput {

2.2.2 主要方法

/** Reads some bytes from an input stream and stores

them into the buffer array*/

void readFully(byte b[])

void readFully(byte b[], int off, int len)

int skipBytes(int n) //skip一定数量的字节

/**

* Reads the next line of text from the input stream.

* It reads successive bytes, converting

* each byte separately into a character*/

String readLine()//逐行读取

String readUTF()

byte, short, int, long, char, float, double, boolean

1 2 4 8 2 4 8

char是表示的是字符,定义的时候用单引号,只能存储一个字符。例如; char='d'

而String表示的是字符串,定义的时候用双引号,可以存储一个或者多个字符。例如:String=“we are neuer”。

char是基本数据类型,而String是个类,属于引用数据类型。String类可以调用方法,具有面向对象的特征。

String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。在Java中,被final修饰的类是不允许被继承的,并且该类中的成员方法都默认为final方法

3.1.1 File

文件继承序列化接口,可通过IO stream 读取或写入

public class File implements Serializable, Comparable

/**

* The FileSystem object representing the platform's local file system.

*/

private static final FileSystem fs = DefaultFileSystem.getFileSystem();

public static final char separatorChar = fs.getSeparator();

3.1.2

/* On UNIX systems the value of this field is '/';

on Microsoft Windows systems it is '\\'. */

public static final String separator = "" + separatorChar;

public File(URI uri)

public String getName()

public boolean isDirectory()

public long lastModified()

public long length()

public boolean createNewFile()

3.2 FileInputStream

public class FileInputStream extends InputStream

/* File Descriptor - handle to the open file */

private final FileDescriptor fd;

/**

* The path of the referenced file

* (null if the stream is created with a file descriptor)

*/

private final String path;

private volatile FileChannel channel;

public FileChannel getChannel()

3.3 FileOutputStream

public class FileOutputStream extends OutputStream

1) ObjectInputStream

/**

* An ObjectInputStream deserializes primitive data and objects previously

* written using an ObjectOutputStream.*/

//数据反序列化

public class ObjectInputStream extends InputStream

implements ObjectInput, ObjectStreamConstants

readObject && readString

public ObjectInputStream(InputStream in)//构造函数初始化

/**

* Internal method to read an object from the

ObjectInputStream of the expected type. */

private final Object readObject(Class> type)

//从另外一个readString方法了解如何使用

private String readString() throws IOException {

try {

return (String) readObject(String.class);//******

} catch (ClassNotFoundException cnf) {

throw new IllegalStateException(cnf);

}

}

2) ObjectOutputStream

public class ObjectOutputStream

extends OutputStream implements ObjectOutput, ObjectStreamConstants

writeObject

子类需要重写这个 writeObjectOverride 方法

public final void writeObject(Object obj) throws IOException {

if (enableOverride) {

writeObjectOverride(obj);

return;

}

try {

writeObject0(obj, false);

} catch (IOException ex) {

if (depth == 0) {

writeFatalException(ex);

}

throw ex;

}

}

/**

* Method used by subclasses to override the default writeObject method.

* This method is called by trusted subclasses of ObjectOutputStream that

* constructed ObjectOutputStream using the protected no-arg constructor.

* The subclass is expected to provide an override method with the modifier

* "final".

*

* @param obj object to be written to the underlying stream

* @throws IOException if there are I/O errors while writing to the

* underlying stream

* @see #ObjectOutputStream()

* @see #writeObject(Object)

* @since 1.2

*/

protected void writeObjectOverride(Object obj) throws IOException {

}

3) PipedInputStream

read在 一个线程, write在另一个线程

/**

* A piped input stream should be connected

* to a piped output stream; the piped input

* stream then provides whatever data bytes

* are written to the piped output stream.

* Typically, data is read from a {@code PipedInputStream}

* object by one thread and data is written

* to the corresponding {@code PipedOutputStream}

* by some other thread. */

public class PipedInputStream extends InputStream

boolean closedByWriter;

volatile boolean closedByReader;

boolean connected;//是否连接对方

/* REMIND: identification of the read and write sides needs to be

more sophisticated. Either using thread groups (but what about

pipes within a thread?) or using finalization (but it may be a

long time until the next GC). */

Thread readSide;//读取线程

Thread writeSide;//写入线程

/**

* The circular buffer into which incoming data is placed.

* @since 1.1

*/

protected byte buffer[];//环形字节缓冲区

protected int in = -1;//写入指针

protected int out = 0;//读取指针

receive && read

//构造函数,由输出流构造输入流,初始化,绑定

public PipedInputStream(PipedOutputStream src, int pipeSize)

throws IOException {

initPipe(pipeSize);//初始化缓冲区

connect(src);

}

//加了同步锁,写入一个字节 这个receive方法,由PipeOutputStream.write调用

protected synchronized void receive(int b) throws IOException {

checkStateForReceive();

writeSide = Thread.currentThread();

if (in == out)

awaitSpace();

if (in <0) {

in &#61; 0;

out &#61; 0;

}

buffer[in&#43;&#43;] &#61; (byte)(b & 0xFF);//写入一个字节

//0xFF 8位二进制 1111 1111

if (in >&#61; buffer.length) {

in &#61; 0;

}

}

synchronized void receive(byte b[], int off, int len)

public synchronized int read(byte b[], int off, int len)

//从输入流中读取字节数组

public synchronized int available() throws IOException {

if(in <0)

return 0;

else if(in &#61;&#61; out)

return buffer.length;

else if (in > out)

return in - out;

else

return in &#43; buffer.length - out;

}

4) PipedOutputStream

public class PipedOutputStream extends OutputStream

private PipedInputStream sink;//私有成员,PipeInputStream类的

public PipedOutputStream(PipedInputStream snk) throws IOException {

connect(snk);

}

connect

public synchronized void connect(PipedInputStream snk) throws IOException {

if (snk &#61;&#61; null) {

throw new NullPointerException();

} else if (sink !&#61; null || snk.connected) {

throw new IOException("Already connected");

}

sink &#61; snk;

snk.in &#61; -1;

snk.out &#61; 0;

snk.connected &#61; true;

}

write && flush

public void write(byte b[], int off, int len) throws IOException {

if (sink &#61;&#61; null) {

throw new IOException("Pipe not connected");

} else if (b &#61;&#61; null) {

throw new NullPointerException();

} else if ((off <0) || (off > b.length) || (len <0) ||

((off &#43; len) > b.length) || ((off &#43; len) <0)) {

throw new IndexOutOfBoundsException();

} else if (len &#61;&#61; 0) {

return;

}

sink.receive(b, off, len);//调用PipeinputStream的保护方法

}

/**

* Flushes this output stream and forces any buffered output bytes

* to be written out.

* This will notify any readers that bytes are waiting in the pipe.

*

* &#64;throws IOException if an I/O error occurs.

*/

public synchronized void flush() throws IOException {

if (sink !&#61; null) {

synchronized (sink) {

sink.notifyAll();

}

}

}

5) FilterInputStream

装饰者模式,就是将原有的基础流进行"装饰",那么装饰后的方法要与原先被装饰的基础类要保持一致,也可以在对基础流进行扩展.而继承是继承父类的属性和方法,通过重写父类里面的方法也可以起到"装饰"作用.比如强化或者优化父类里面的一些方法.两者的区别是装饰者模式可以动态地扩展一个对象.给对象添加额外的功能.而且装饰者和被装饰者之间不会产生耦合.

public class FilterInputStream extends InputStream

/**

* The input stream to be filtered.

*/

protected volatile InputStream in;//可装饰的输入流

/**

* Creates a {&#64;code FilterInputStream}

* by assigning the argument {&#64;code in}

* to the field {&#64;code this.in} so as

* to remember it for later use.

*

* &#64;param in the underlying input stream, or {&#64;code null} if

* this instance is to be created without an underlying stream.

*/

protected FilterInputStream(InputStream in) {

this.in &#61; in;//构造函数

}

read && skip && available

public int read() throws IOException {

return in.read();

}

public int read(byte b[], int off, int len) throws IOException {

return in.read(b, off, len);

}

public long skip(long n) throws IOException {

return in.skip(n);

}

public int available() throws IOException {

return in.available();

}

//...

6) FilterOutputStream

public class FilterOutputStream extends OutputStream

/**

* The underlying output stream to be filtered.

*/

protected OutputStream out;

/**

* Whether the stream is closed; implicitly initialized to false.

*/

private volatile boolean closed;

public FilterOutputStream(OutputStream out) {

this.out &#61; out;

}

write && flush

&#64;Override

public void write(int b) throws IOException {

out.write(b);

}

&#64;Override

public void flush() throws IOException {

out.flush();

}

7) DataInputStream

继承了FilterInputStream ,调用父类的构造函数,实现接口 DataInput的方法

public class DataInputStream extends FilterInputStream implements DataInput

/**

* Creates a DataInputStream that uses the specified

* underlying InputStream.

*

* &#64;param in the specified input stream

*/

public DataInputStream(InputStream in) {

super(in);

}

public final int read(byte b[]) throws IOException {

return in.read(b, 0, b.length);

}

public final int read(byte b[], int off, int len) throws IOException {

return in.read(b, off, len);

}

8) DataOutputStream

public class DataOutputStream extends FilterOutputStream implements DataOutput

public synchronized void write(int b) throws IOException {

out.write(b);

incCount(1);

}

public void flush() throws IOException {

out.flush();

}

9) ByteArrayInputStream

参考:

public class ByteArrayInputStream extends InputStream

protected byte buf[];

protected int pos;//要从输入流缓冲区中读取的下一个字节的索引

/*** The currently marked position in the stream.*/

protected int mark &#61; 0;//流中当前标记的位置

protected int count;

//初始化

public ByteArrayInputStream(byte buf[], int offset, int length) {

this.buf &#61; buf;

this.pos &#61; offset;

this.count &#61; Math.min(offset &#43; length, buf.length);

this.mark &#61; offset;

}

read && transferTo

public synchronized int read(byte b[], int off, int len) {

Objects.checkFromIndexSize(off, len, b.length);

if (pos >&#61; count) {

return -1;

}

int avail &#61; count - pos;//可读取的字节数

if (len > avail) {

len &#61; avail;

}

if (len <&#61; 0) {

return 0;

}

System.arraycopy(buf, pos, b, off, len);

pos &#43;&#61; len;

return len;

}

public synchronized long transferTo(OutputStream out) throws IOException {

int len &#61; count - pos;

out.write(buf, pos, len);

pos &#61; count;

return len;

}

mark && reset

public void mark(int readAheadLimit) {

mark &#61; pos;

}

/**

* Resets the buffer to the marked position. The marked position

* is 0 unless another position was marked or an offset was specified

* in the constructor.

*/

public synchronized void reset() {

pos &#61; mark;

}

10) ByteArrayOutputStream

public class ByteArrayOutputStream extends OutputStream

/**

* The buffer where data is stored.

*/

protected byte buf[];

/**

* The number of valid bytes in the buffer.

*/

protected int count;

write && writTo

/**

* Writes {&#64;code len} bytes from the specified byte array

* starting at offset {&#64;code off} to this {&#64;code ByteArrayOutputStream}.*/

public synchronized void write(byte b[], int off, int len) {

Objects.checkFromIndexSize(off, len, b.length);

ensureCapacity(count &#43; len);

System.arraycopy(b, off, buf, count, len);

count &#43;&#61; len;

}

/**

* Writes the complete contents of this {&#64;code ByteArrayOutputStream} to

* the specified output stream argument, as if by calling the output

* stream&#39;s write method using {&#64;code out.write(buf, 0, count)}.*/

public synchronized void writeTo(OutputStream out) throws IOException {

out.write(buf, 0, count);

}

toString

/*** Converts the buffer&#39;s contents into a string decoding bytes */

public synchronized String toString() {

return new String(buf, 0, count);

}

//java.nio.charset.Charset

public synchronized String toString(Charset charset) {

return new String(buf, 0, count, charset);

}

RandomAccessFile

随机可读可写文件

public class RandomAccessFile implements DataOutput, DataInput, Closeable {

private FileDescriptor fd;

private volatile FileChannel channel;

private boolean rw;

private final String path;

public RandomAccessFile(File file, String mode)

throws FileNotFoundException

{

this(file, mode, false);

}

public final FileChannel getChannel()

InputStreamReader

a bridge from byte streams to character streams

/**

* An InputStreamReader is a bridge from byte streams to character streams: It

* reads bytes and decodes them into characters using a specified {&#64;link

* java.nio.charset.Charset charset}. */

public class InputStreamReader extends Reader {

private final StreamDecoder sd;

public int read(char cbuf[], int offset, int length) throws IOException {

return sd.read(cbuf, offset, length);

}

OutputStreamWriter

a bridge from character streams to byte streams

/**

* An OutputStreamWriter is a bridge from character streams to byte streams*/

public class OutputStreamWriter extends Writer {

private final StreamEncoder se;

public void write(char cbuf[], int off, int len) throws IOException {

se.write(cbuf, off, len);

}

/* append和write 是一样的 */

&#64;Override

public Writer append(CharSequence csq) throws IOException {

if (csq instanceof CharBuffer) {

se.write((CharBuffer) csq);

} else {

se.write(String.valueOf(csq));

}

return this;

}

public void flush() throws IOException {

se.flush();

}

public void close() throws IOException {

se.close();

}



推荐阅读
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 在维护公司项目时,发现按下手机的某个物理按键后会激活相应的服务,并在屏幕上模拟点击特定坐标点。本文详细介绍了如何使用ADB Shell Input命令来模拟各种输入事件,包括滑动、按键和点击等。 ... [详细]
  • 本题探讨如何通过最大流算法解决农场排水系统的设计问题。题目要求计算从水源点到汇合点的最大水流速率,使用经典的EK(Edmonds-Karp)和Dinic算法进行求解。 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 本文探讨了Hive中内部表和外部表的区别及其在HDFS上的路径映射,详细解释了两者的创建、加载及删除操作,并提供了查看表详细信息的方法。通过对比这两种表类型,帮助读者理解如何更好地管理和保护数据。 ... [详细]
  • 在金融和会计领域,准确无误地填写票据和结算凭证至关重要。这些文件不仅是支付结算和现金收付的重要依据,还直接关系到交易的安全性和准确性。本文介绍了一种使用C语言实现小写金额转换为大写金额的方法,确保数据的标准化和规范化。 ... [详细]
author-avatar
俊铭士弘政翰
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有