java io源码解读_# Java-IO 源码解析

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 < len ; 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 < len ; 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 = 0;

out = 0;

}

buffer[in++] = (byte)(b & 0xFF);//写入一个字节

//0xFF 8位二进制 1111 1111

if (in >= buffer.length) {

in = 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 == out)

return buffer.length;

else if (in > out)

return in - out;

else

return in + 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 == null) {

throw new NullPointerException();

} else if (sink != null || snk.connected) {

throw new IOException("Already connected");

}

sink = snk;

snk.in = -1;

snk.out = 0;

snk.connected = true;

}

write && flush

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

if (sink == null) {

throw new IOException("Pipe not connected");

} else if (b == null) {

throw new NullPointerException();

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

((off + len) > b.length) || ((off + len) < 0)) {

throw new IndexOutOfBoundsException();

} else if (len == 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.

*

* @throws IOException if an I/O error occurs.

*/

public synchronized void flush() throws IOException {

if (sink != null) {

synchronized (sink) {

sink.notifyAll();

}

}

}

5) FilterInputStream

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

public class FilterInputStream extends InputStream

/**

* The input stream to be filtered.

*/

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

/**

* Creates a {@code FilterInputStream}

* by assigning the argument {@code in}

* to the field {@code this.in} so as

* to remember it for later use.

*

* @param in the underlying input stream, or {@code null} if

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

*/

protected FilterInputStream(InputStream in) {

this.in = 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 = out;

}

write && flush

@Override

public void write(int b) throws IOException {

out.write(b);

}

@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.

*

* @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 = 0;//流中当前标记的位置

protected int count;

//初始化

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

this.buf = buf;

this.pos = offset;

this.count = Math.min(offset + length, buf.length);

this.mark = offset;

}

read && transferTo

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

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

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;

}

public synchronized long transferTo(OutputStream out) throws IOException {

int len = count - pos;

out.write(buf, pos, len);

pos = count;

return len;

}

mark && reset

public void mark(int readAheadLimit) {

mark = 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 = 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 {@code len} bytes from the specified byte array

* starting at offset {@code off} to this {@code ByteArrayOutputStream}.*/

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

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

ensureCapacity(count + len);

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

count += len;

}

/**

* Writes the complete contents of this {@code ByteArrayOutputStream} to

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

* stream's write method using {@code out.write(buf, 0, count)}.*/

public synchronized void writeTo(OutputStream out) throws IOException {

out.write(buf, 0, count);

}

toString

/*** Converts the buffer'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 {@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 是一样的 */

@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();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值