IO源码笔记

在这里插入图片描述

InputStream

在这里插入图片描述

Class Name @since Field Method Comment
InputStream 1.0 MAX_SKIP_BUFFER_SIZE = 2048 int read()
int read(byte b[])
int read(byte b[], int off, int len)
long skip(long n)
int available()
void close()
synchronized void mark(int readlimit)
synchronized void reset()
boolean markSupported()
FilterInputStream 1.0 volatile InputStream in 所有方法都转发给了被装饰字段。
这个类有点鸡肋
ByteArrayInputStream 1.0 byte buf[]
int pos
int mark = 0
int count
字节数组作为数据来源,支持标记、重置等方法
FileInputStream 1.0 final FileDescriptor fd;
final String path;
FileChannel channel = null;
final Object closeLock = new Object();
volatile boolean closed = false;
final FileDescriptor getFD()
FileChannel getChannel()
基本都是native方法
PipedInputStream 1.0 boolean closedByWriter = false;
volatile boolean closedByReader = false;
boolean connected = false;
Thread readSide;
Thread writeSide;
static final int DEFAULT_PIPE_SIZE = 1024;
static final int PIPE_SIZE = DEFAULT_PIPE_SIZE;
byte buffer[];
int in = -1;
int out = 0;
void connect(PipedOutputStream src)
synchronized void receive(int b)
synchronized void receive(byte b[], int off, int len)
void checkStateForReceive()
void awaitSpace()
void receivedLast()
PipedInputStream和PipedOutputStream详解
PipedInputStream与PipedOutputStream组合使用,PipedOutputStream接收外部输入的数据,存储到PipedInputStream的字节数组中,消费程序从PipedInputStream数组中获取数据
BufferedInputStream 1.0 static int DEFAULT_BUFFER_SIZE = 8192;
static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
volatile byte buf[];
static final AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater =
AtomicReferenceFieldUpdater.newUpdater
(BufferedInputStream.class, byte[].class, “buf”);
int count;
int pos;
int markpos = -1;
int marklimit;
void fill() 内部通过字节数组作为缓冲区。缓冲区大小取决于磁盘和网络
PushbackInputStream 1.0 byte[] buf;
int pos;
void unread(int b)
void unread(byte[] b, int off, int len)
void unread(byte[] b)
将读取的字节回退到buf数组中,供下次继续读取。
pos初始位置是buf.length,从后向前存储数据(一般是存储字节数组,字节数组顺序不变)。
读取数据是从前向后读取
DataInputStream 1.0 byte bytearr[] = new byte[80];
char chararr[] = new char[80];
byte readBuffer[] = new byte[8];
char lineBuffer[];
void readFully(byte b[])
void readFully(byte b[], int off, int len)
final int skipBytes(int n)
final boolean readBoolean
final byte readByte()
final int readUnsignedByte()
final short readShort()
final int readUnsignedShort()
final char readChar()
final int readInt()
final long readLong()
final float readFloat()
final double readDouble()
final String readLine()
final String readUTF()
final static String readUTF(DataInput in)
读取java类型值(一次读取多个字节)
结束标志建议通过捕获EOFException异常(有可能传入了一个-1整数)
ObjectInputStream 1.1 final Object readObject()
字段方法较多,后续再说。
比较有用的是readObject()方法,与ObjectOutputStream实现序列化与反序列化
SequenceInputStream 1.0 Enumeration<? extends InputStream> e;
InputStream in;
final void nextStream() 将多个InputStream合并为一个,按顺序读取

FileDescriptor

认知IO流之 — FileDescriptor

OutputStream

在这里插入图片描述

Class Name @since Field Method Comment
OutputStream 1.0 void write(int b)
void write(byte b[])
void write(byte b[], int off, int len)
void flush()
void close()
FilterOutputStream 1.0 protected OutputStream out;
ByteArrayOutputStream 1.0 protected byte buf[];
protected int count;
synchronized byte toByteArray()[]
synchronized void reset()
synchronized int size()
默认数组大小32,最大Integer.MAX_VALUE
可通过toByteArray()方法获取输出数组
FileOutputStream 1.0 final FileDescriptor fd;
final boolean append;
FileChannel channel;
final String path;
final Object closeLock = new Object();
volatile boolean closed = false;
native void write(int b, boolean append)
native void writeBytes(byte b[], int off, int len, boolean append)
final FileDescriptor getFD()
FileChannel getChannel()
默认覆盖原文件
PipedOutputStream 1.0 private PipedInputStream sink; synchronized void connect(PipedInputStream snk) PipedOutputStream中持有对应的PipedInputStream对象
write()方法实际调用PipedInputStream的receive()方法存储到PipedInputStream中
BufferingOutputStream final OutputStream other;
final byte[] buf;
int bufOff;
缓冲区默认大小4096,
DataOutputStream 1.0 int written;
byte[] bytearr = null;
byte writeBuffer[] = new byte[8];
private void incCount(int value)
final void writeBoolean(boolean v)
final void writeByte(int v)
final void writeShort(int v)
final void writeChar(int v)
final void writeInt(int v)
final void writeFloat(float v)
final void writeDouble(double v)
final void writeBytes(String s)
final void writeChars(String s)
final void writeUTF(String str)
static int writeUTF(String str, DataOutput out)
final int size()
将java类型转换为字节输出
PrintStream 1.0 final boolean autoFlush;
boolean trouble = false;
Formatter formatter;
BufferedWriter textOut;
OutputStreamWriter charOut;
boolean closing = false;
PrintStream append
PrintStream format
PrintStream printf
void println
void print
void write(byte buf[], int off, int len)
void write(int b)
System.out和System.err的类型。
能够将原始值转换为文本、格式化
ObjectOutputStream 1.1 final void writeObject(Object obj)

RandomAccessFile

Class Name @since Field Method Comment
RandomAccessFile 1.0 FileDescriptor fd;
FileChannel channel = null;
boolean rw;
final String path;
Object closeLock = new Object();
volatile boolean closed = false;
static final int O_RDONLY = 1;
static final int O_RDWR = 2;
static final int O_SYNC = 4;
static final int O_DSYNC = 8;
native long getFilePointer()
void seek(long pos)
native long length()
随机读写
r、rw、rws、rwd

Reader

在这里插入图片描述

Class Name @since Field Method Comment
Reader 1.1 Object lock;
static final int maxSkipBufferSize = 8192;
char skipBuffer[] = null;
int read(java.nio.CharBuffer target)
int read()
int read(char cbuf[])
int read(char cbuf[], int off, int len)
boolean ready()
boolean markSupported()
void mark(int readAheadLimit)
void reset()
void close()
InputStreamReader 1.1 final StreamDecoder sd; String getEncoding()
方法转发给了StreamDecoder,StreamDecoder属于nio包中内容,后续分析
FilterReader 1.1 Reader in;
CharArrayReader 1.1 char buf[];
int pos;
int markedPos = 0;
int count;
从buf中读取数据,支持标记、重置
FileReader 1.1 继承者InputStreamReader
如果需要使用不同的字符编码解决方案,请使用InputStreamReader
PipedReader 1.1 boolean closedByWriter = false;
boolean closedByReader = false;
boolean connected = false;
Thread readSide;
Thread writeSide;
static final int DEFAULT_PIPE_SIZE = 1024;
char buffer[];
int in = -1;
int out = 0;
void connect(PipedWriter src)
synchronized void receive(int c)
synchronized void receive(char c[], int off, int len)
synchronized void receivedLast()
synchronized int read()
synchronized int read(char cbuf[], int off, int len)
synchronized boolean ready()
read()方法会阻塞等待,read(buf[])不会(读取第一个字符可能会阻塞)
BufferedReader 1.1 Reader in;
char cb[];
int nChars, nextChar;
static final int INVALIDATED = -2;
static final int UNMARKED = -1;
int markedChar = UNMARKED;
int readAheadLimit = 0; /* Valid only when markedChar > 0 */
boolean skipLF = false;
boolean markedSkipLF = false;
static int defaultCharBufferSize = 8192;
static int defaultExpectedLineLength = 80;
String readLine()
Stream lines()
PushbackReader 1.1 char[] buf;
int pos;
void unread(int c)
void unread(char cbuf[], int off, int len)
void unread(char cbuf[])
如果需要提前阅读几个字符以了解即将发生的情况,然后才能确定如何解释当前字符。将读取的字符推回Reader,下次调用read()时读取
LineNumberReader 1.1 int lineNumber = 0;
int markedLineNumber; // Defaults to 0
boolean skipLF;
boolean markedSkipLF;
static final int maxSkipBufferSize = 8192;
char skipBuffer[] = null;
void setLineNumber(int lineNumber)
int getLineNumber()
String readLine()
StringReader 1.1 String str;
int length;
int next = 0;
int mark = 0;
mark()方法没有实际限制

Writer

在这里插入图片描述

Class Name @since Field Method Comment
Writer 1.1 char[] writeBuffer;
static final int WRITE_BUFFER_SIZE = 1024;
Object lock;
void write(int c)
void write(char cbuf[])
void write(char cbuf[], int off, int len)
write(String str)
write(String str, int off, int len)
Writer append(CharSequence csq)
Writer append(CharSequence csq, int start, int end)
Writer append(char c)
void flush()
void close()
OutputStreamWriter 1.1 final StreamEncoder se; 方法转发给了StreamEncoder,StreamEncoder属于nio包中内容,后续分析
CharArrayWriter 1.1 char buf[];
int count;
void writeTo(Writer out)
char toCharArray()[]
int size()
String toString()
数组默认大小32,
FileWriter 1.1 继承者OutputStreamWriter
如果需要使用不同的字符编码解决方案,请使
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值