热门标签 | 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();

}



推荐阅读
  • 本文旨在介绍在iOS平台进行直播技术开发前的准备工作,重点讲解AVFoundation框架的基本概念和使用方法。通过对AVFoundation的深入理解,开发者能够更好地掌握直播应用中的音视频处理技巧。 ... [详细]
  • 本文介绍了两种使用Java发送短信的方法:利用第三方平台的HTTP请求和通过硬件设备短信猫。重点讲解了如何通过Java代码配置和使用短信猫发送短信的过程,包括必要的编码转换、串口操作及短信发送的核心逻辑。 ... [详细]
  • 本文介绍了两种在Android设备上获取MAC地址的有效方法,包括通过Wi-Fi连接和使用移动数据流量的情况。第一种方法依赖于Wi-Fi连接来获取MAC地址,而第二种方法则无需Wi-Fi,直接通过网络接口获取。 ... [详细]
  • 本文探讨了在JavaScript中执行字符串形式代码的多种方法,包括使用eval()函数以及跨页面调用的方法。同时,文章详细介绍了JavaScript中字符串的各种常用方法及其应用场景。 ... [详细]
  • Eclipse 下 JavaFX 程序开发指南
    本文介绍了 JavaFX,这是一个用于创建富客户端应用程序的 Java 图形和媒体工具包,并详细说明了如何在 Eclipse 环境中配置和开发 JavaFX 应用。 ... [详细]
  • 题意题目大意很简单,很容易找出对应字母的ASCII码值的关系,但是有一点需要注意,请看代码:读字符串必须要用getline ... [详细]
  • 解决VSCode中文乱码问题的综合方案
    在使用VSCode进行开发时,尤其是涉及Python编程,可能会遇到中文乱码的问题。本文总结了多种有效的解决方案,帮助开发者快速解决这一常见问题。 ... [详细]
  • 本文探讨了STL迭代器的最佳实践,包括iterator与const_iterator、reverse_iterator及其const版本之间的关系,以及如何高效地转换和使用这些迭代器类型。 ... [详细]
  • 本文总结了WebSphere应用服务器出现宕机问题的解决方法,重点讨论了关键参数的调整,包括数据源连接池、线程池设置以及JVM堆大小等,旨在提升系统的稳定性和性能。 ... [详细]
  • addcslashes—以C语言风格使用反斜线转义字符串中的字符addslashes—使用反斜线引用字符串bin2hex—函数把包含数据的二进制字符串转换为十六进制值chop—rt ... [详细]
  • 本文整理了一系列Java面试问题,涵盖Java开发环境的分类、Java语言的核心特性、Linux环境下Java SE的安装步骤、常用的Java开发工具介绍,以及类与对象的基本概念等。 ... [详细]
  • 前文|功能型_品读鸿蒙HDF架构
    前文|功能型_品读鸿蒙HDF架构 ... [详细]
  • 本教程将深入探讨C#编程语言中的条件控制结构,包括if语句和switch语句的使用方法。通过本课的学习,您将掌握如何利用这些控制结构来实现程序的条件分支逻辑。 ... [详细]
  • 本文介绍了一个简单的智能指针类的设计与实现方法,通过模板结构体实现资源管理,确保对象在不再需要时能够自动释放内存。 ... [详细]
  • 本文详细介绍了 Java 中 freemarker.ext.dom.NodeModel 类的 removeComments 方法,并提供了多个实际使用的代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
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社区 版权所有