JDK源码系列

JDK源码-InputStream系列

2017-08-22  本文已影响21人  薛云龙

byte

了解InputStream前,我们先了解下byte.

byte的包装类就是Byte,列举一下常见方法

InputStream

抽象架构图

InputSteam是所有字节输入流的实现类的基类.它封装了流处理类的基本操作.

缓存区字节数组最大值
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
从输入流中读取数据的下一个字节,返回int
public abstract int read() throws IOException;
从输入流中读取b.length大小的字节,并存储到字节数组b.
public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

该方法调用了抽象方法:read(),通过该方法获取流中下一个元素.

public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException e) {
        }
        return i;
    }

原始流处理器

接收一个Byte数组对象,或者一个FileDiscriptor对象,一个String对象,或者其他类型的流源对象.
主要一下四种:

ByteArrayInputStream
/**
     * An array of bytes that was provided
     * by the creator of the stream. Elements <code>buf[0]</code>
     * through <code>buf[count-1]</code> are the
     * only bytes that can ever be read from the
     * stream;  element <code>buf[pos]</code> is
     * the next byte to be read.
     */
    protected byte buf[];
/**
     * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
     * this class can be called after the stream has been closed without
     * generating an <tt>IOException</tt>.
     */
    public void close() throws IOException {
    }
FileInputStream

先了解下一些相关类的实现:JDK源码-File系列

public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }

public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name);
    }
public FileInputStream(FileDescriptor fdObj) {
        SecurityManager security = System.getSecurityManager();
        if (fdObj == null) {
            throw new NullPointerException();
        }
        if (security != null) {
            security.checkRead(fdObj);
        }
        fd = fdObj;
        path = null;

        /*
         * FileDescriptor is being shared by streams.
         * Register this stream with FileDescriptor tracker.
         */
        fd.attach(this);
    }
 /**
     * Opens the specified file for reading.
     * @param name the name of the file
     */
    private native void open0(String name) throws FileNotFoundException;

    // wrap native call to allow instrumentation
    /**
     * Opens the specified file for reading.
     * @param name the name of the file
     */
    private void open(String name) throws FileNotFoundException {
        open0(name);
    }

    /**
     * Reads a byte of data from this input stream. This method blocks
     * if no input is yet available.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             file is reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public int read() throws IOException {
        return read0();
    }

    private native int read0() throws IOException;
PipedInputStream&PipedOutputStream

PipedInputStream与PipedOutputStream的设计,主要为了实现线程之间可以通过字节流来传输数据,来达到通信.

参考文章:Java流编程实例及代码

StringBufferInputStream

StringBufferInputStream:将一个字符串缓冲区转换为一个输入流。接收一个String对象作为流的源。(JDK帮助文档上说明:已过时。此类未能正确地将字符转换为字节。从JDK1.1开始,从字符串创建流的首选方法是通过StringReader类进行创建。只有字符串中每个字符的低八位可以由此类使用。)

ObjectInputStream/ObjectOutputStream

详见JDK源码-InputStream系列之ObjectOutputStream/ObjectInputStream

链接流处理器

FilterInputStream
BufferedInputStream
上一篇 下一篇

猜你喜欢

热点阅读