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 &#61; read(); if (c &#61;&#61; -1) { break; } b[off &#43; i] &#61; (byte)c; } } catch (IOException ee) { } return i;//返回成功读取的字节数 } //将一个输入流转为输出流&#xff0c;返回为已转的字节数 public long transferTo(OutputStream out) throws IOException {//3 Objects.requireNonNull(out, "out"); long transferred &#61; 0; byte[] buffer &#61; new byte[DEFAULT_BUFFER_SIZE]; int read; while ((read &#61; this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >&#61; 0) { out.write(buffer, 0, read); transferred &#43;&#61; read; } return transferred; } 1.2.1 OutputStream public abstract class OutputStream implements Closeable, Flushable public abstract void write(int b) /** 只写入低8位&#xff1f; * Writes the specified byte to this output stream. The general * contract for {&#64;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 {&#64;code b}. The 24 * high-order bits of {&#64;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. * * &#64;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 &#61;&#61; 0 condition implicitly handled by loop bounds for (int i &#61; 0 ; i write(b[off &#43; i]); } } 2.1.1 DataOutput /** 将Java基础数据类型写入字节流中 * The {&#64;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&#xff0c;false写入0 void writeByte(int v) //和write方法一样&#xff0c;不过这个显著提醒了只写入一个字节 void writeShort(int v) //写入2个字节 void writeChar(int v)//写入单字符 /* Writes a {&#64;code char} value, which 如十进制的65&#xff0c;对应于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 {&#64;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是表示的是字符&#xff0c;定义的时候用单引号&#xff0c;只能存储一个字符。例如; char&#61;&#39;d&#39; 而String表示的是字符串&#xff0c;定义的时候用双引号&#xff0c;可以存储一个或者多个字符。例如&#xff1a;String&#61;“we are neuer”。 char是基本数据类型&#xff0c;而String是个类&#xff0c;属于引用数据类型。String类可以调用方法&#xff0c;具有面向对象的特征。 String类是final类&#xff0c;也即意味着String类不能被继承&#xff0c;并且它的成员方法都默认为final方法。在Java中&#xff0c;被final修饰的类是不允许被继承的&#xff0c;并且该类中的成员方法都默认为final方法 3.1.1 File 文件继承序列化接口,可通过IO stream 读取或写入 public class File implements Serializable, Comparable /** * The FileSystem object representing the platform&#39;s local file system. */ private static final FileSystem fs &#61; DefaultFileSystem.getFileSystem(); public static final char separatorChar &#61; fs.getSeparator(); 3.1.2 /* On UNIX systems the value of this field is &#39;/&#39;; on Microsoft Windows systems it is &#39;\\&#39;. */ public static final String separator &#61; "" &#43; 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 &#61;&#61; 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". * * &#64;param obj object to be written to the underlying stream * &#64;throws IOException if there are I/O errors while writing to the * underlying stream * &#64;see #ObjectOutputStream() * &#64;see #writeObject(Object) * &#64;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 {&#64;code PipedInputStream} * object by one thread and data is written * to the corresponding {&#64;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. * &#64;since 1.1 */ protected byte buffer[];//环形字节缓冲区 protected int in &#61; -1;//写入指针 protected int out &#61; 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 &#61; Thread.currentThread(); if (in &#61;&#61; 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(); }