Java IO 之 FileInputStream & FileOutputStream源碼分析

Writer      :BYSocket(泥沙磚瓦漿木匠) java

微         博:BYSocket ios

豆         瓣:BYSocket git

FaceBook:BYSocket github

Twitter    :BYSocket express

1、引子

文件,做爲常見的數據源。關於操做文件的字節流就是 — FileInputStream & FileOutputStream。它們是Basic IO字節流中重要的實現類。 apache

2、FileInputStream源碼分析

FileInputStream源碼以下: 數組

/**
 * FileInputStream 從文件系統的文件中獲取輸入字節流。文件取決於主機系統。
 *  好比讀取圖片等的原始字節流。若是讀取字符流,考慮使用 FiLeReader。
 */
publicclassSFileInputStream extendsInputStream
{
    /* 文件描述符類---此處用於打開文件的句柄 */
    private final FileDescriptor fd;
 
    /* 引用文件的路徑 */
    private final String path;
 
    /* 文件通道,NIO部分 */
    private FileChannel channel = null;
 
    private final Object closeLock = new Object();
    private volatile boolean closed = false;
 
    private static final ThreadLocal<Boolean> runningFinalize =
        new ThreadLocal<>();
 
    private static boolean isRunningFinalize() {
        Boolean val;
        if ((val = runningFinalize.get()) != null)
            return val.booleanValue();
        return false;
    }
 
    /* 經過文件路徑名來建立FileInputStream */
    public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }
 
    /* 經過文件來建立FileInputStream */
    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.incrementAndGetUseCount();
        this.path = name;
        open(name);
    }
 
    /* 經過文件描述符類來建立FileInputStream */
    public FileInputStream(FileDescriptor fdObj) {
        SecurityManager security = System.getSecurityManager();
        if (fdObj == null) {
            throw new NullPointerException();
        }
        if (security != null) {
            security.checkRead(fdObj);
        }
        fd = fdObj;
        path = null;
        fd.incrementAndGetUseCount();
    }
 
    /* 打開文件,爲了下一步讀取文件內容。native方法 */
    private native void open(String name) throws FileNotFoundException;
 
    /* 今後輸入流中讀取一個數據字節 */
    public int read() throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int b = 0;
        try {
            b = read0();
        } finally {
            IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);
        }
        return b;
    }
 
    /* 今後輸入流中讀取一個數據字節。native方法 */
    private native int read0() throws IOException;
 
    /* 今後輸入流中讀取多個字節到byte數組中。native方法 */
    private native int readBytes(byte b[], int off, int len) throws IOException;
 
    /* 今後輸入流中讀取多個字節到byte數組中。 */
    public int read(byte b[]) throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int bytesRead = 0;
        try {
            bytesRead = readBytes(b, 0, b.length);
        } finally {
            IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
        }
        return bytesRead;
    }
 
    /* 今後輸入流中讀取最多len個字節到byte數組中。 */
    public int read(byte b[], int off, int len) throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int bytesRead = 0;
        try {
            bytesRead = readBytes(b, off, len);
        } finally {
            IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
        }
        return bytesRead;
    }
 
     
    public native long skip(long n) throws IOException;
 
    /* 返回下一次對此輸入流調用的方法能夠不受阻塞地今後輸入流讀取(或跳過)的估計剩餘字節數。 */
    public native int available() throws IOException;
 
    /* 關閉此文件輸入流並釋放與此流有關的全部系統資源。 */
    public void close() throws IOException {
        synchronized (closeLock) {
            if (closed) {
                return;
            }
            closed = true;
        }
        if (channel != null) {
           fd.decrementAndGetUseCount();
           channel.close();
        }
 
        int useCount = fd.decrementAndGetUseCount();
 
        if ((useCount <= 0) || !isRunningFinalize()) {
            close0();
        }
    }
 
    public final FileDescriptor getFD() throws IOException {
        if (fd != null) return fd;
        throw new IOException();
    }
 
    /* 獲取此文件輸入流的惟一FileChannel對象 */
    publicFileChannel getChannel() {
        synchronized(this) {
            if(channel == null) {
                channel = FileChannelImpl.open(fd, path, true, false, this);
                fd.incrementAndGetUseCount();
            }
            returnchannel;
        }
    }
 
    privatestaticnativevoidinitIDs();
 
    privatenativevoidclose0() throwsIOException;
 
    static{
        initIDs();
    }
 
    protected void finalize() throws IOException {
        if((fd != null) &&  (fd != FileDescriptor.in)) {
            runningFinalize.set(Boolean.TRUE);
            try{
                close();
            } finally{
                runningFinalize.set(Boolean.FALSE);
            }
        }
    }
}

1. 三個核心方法 app

三個核心方法,也就是Override(重寫)了抽象類InputStreamread方法。 less

int read() 方法,即 socket

public int read() throws IOException

代碼實現中很簡單,一個try中調用本地nativeread0()方法,直接從文件輸入流中讀取一個字節。IoTrace.fileReadEnd(),字面意思是防止文件沒有關閉讀的通道,致使讀文件失敗,一直開着讀的通道,會形成內存泄露。


int read(byte b[]) 方法,即

public int read(byte b[]) throws IOException

代碼實現也是比較簡單的,也是一個try中調用本地nativereadBytes()方法,直接從文件輸入流中讀取最多b.length個字節到byte數組b中。

int read(byte b[], int off, int len) 方法,即

public int read(byte b[], int off, int len) throws IOException

代碼實現和 int read(byte b[])方法 同樣,直接從文件輸入流中讀取最多len個字節到byte數組b中。

但是這裏有個問:

Q: 爲何 int read(byte b[]) 方法須要本身獨立實現呢? 直接調用 int read(byte b[], int off, int len) 方法,即read(b , 0 , b.length),等價於read(b)?

A:待完善,但願路過大神回。。。。向下兼容?? Finally??

2. 值得一提的native方法

上面核心方法中爲何實現簡單,由於工做量都在native方法裏面,即JVM裏面實現了。native卻是很多一一列舉吧:

native void open(String name) // 打開文件,爲了下一步讀取文件內容

native int read0() // 從文件輸入流中讀取一個字節

native int readBytes(byte b[], int off, int len) // 從文件輸入流中讀取,從off句柄開始的len個字節,並存儲至b字節數組內。

native void close0() // 關閉該文件輸入流及涉及的資源,好比說若是該文件輸入流的FileChannel對被獲取後,須要對FileChannel進行close。

其餘還有值得一提的就是,在jdk1.4中,新增了NIO包,優化了一些IO處理的速度,因此在FileInputStream和FileOutputStream中新增了FileChannel getChannel()的方法。即獲取與該文件輸入流相關的 java.nio.channels.FileChannel對象。


3、FileOutputStream 源碼分析

FileOutputStream 源碼以下:


/**
 * 文件輸入流是用於將數據寫入文件或者文件描述符類
 *  好比寫入圖片等的原始字節流。若是寫入字符流,考慮使用 FiLeWriter。
 */
publicclassSFileOutputStream extendsOutputStream
{
    /* 文件描述符類---此處用於打開文件的句柄 */
    private final FileDescriptor fd;
 
    /* 引用文件的路徑 */
    private final String path;
 
    /* 若是爲 true,則將字節寫入文件末尾處,而不是寫入文件開始處 */
    private final boolean append;
 
    /* 關聯的FileChannel類,懶加載 */
    private FileChannel channel;
 
    private final Object closeLock = new Object();
    private volatile boolean closed = false;
    private static final ThreadLocal<Boolean> runningFinalize =
        new ThreadLocal<>();
 
    private static boolean isRunningFinalize() {
        Boolean val;
        if ((val = runningFinalize.get()) != null)
            return val.booleanValue();
        return false;
    }
 
    /* 經過文件名建立文件輸入流 */
    public FileOutputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null, false);
    }
 
    /* 經過文件名建立文件輸入流,並肯定文件寫入起始處模式 */
    public FileOutputStream(String name, boolean append)
        throws FileNotFoundException
    {
        this(name != null ? new File(name) : null, append);
    }
 
    /* 經過文件建立文件輸入流,默認寫入文件的開始處 */
    public FileOutputStream(File file) throws FileNotFoundException {
        this(file, false);
    }
 
    /* 經過文件建立文件輸入流,並肯定文件寫入起始處  */
    public FileOutputStream(File file, boolean append)
        throws FileNotFoundException
    {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        this.fd = new FileDescriptor();
        this.append = append;
        this.path = name;
        fd.incrementAndGetUseCount();
        open(name, append);
    }
 
    /* 經過文件描述符類建立文件輸入流 */
    public FileOutputStream(FileDescriptor fdObj) {
        SecurityManager security = System.getSecurityManager();
        if (fdObj == null) {
            throw new NullPointerException();
        }
        if (security != null) {
            security.checkWrite(fdObj);
        }
        this.fd = fdObj;
        this.path = null;
        this.append = false;
 
        fd.incrementAndGetUseCount();
    }
 
    /* 打開文件,並肯定文件寫入起始處模式 */
    private native void open(String name, boolean append)
        throws FileNotFoundException;
 
    /* 將指定的字節b寫入到該文件輸入流,並指定文件寫入起始處模式 */
    private native void write(int b, boolean append) throws IOException;
 
    /* 將指定的字節b寫入到該文件輸入流 */
    public void write(int b) throws IOException {
        Object traceContext = IoTrace.fileWriteBegin(path);
        int bytesWritten = 0;
        try {
            write(b, append);
            bytesWritten = 1;
        } finally {
            IoTrace.fileWriteEnd(traceContext, bytesWritten);
        }
    }
 
    /* 將指定的字節數組寫入該文件輸入流,並指定文件寫入起始處模式 */
    private native void writeBytes(byte b[], int off, int len, boolean append)
        throws IOException;
 
    /* 將指定的字節數組b寫入該文件輸入流 */
    public void write(byte b[]) throws IOException {
        Object traceContext = IoTrace.fileWriteBegin(path);
        int bytesWritten = 0;
        try {
            writeBytes(b, 0, b.length, append);
            bytesWritten = b.length;
        } finally {
            IoTrace.fileWriteEnd(traceContext, bytesWritten);
        }
    }
 
    /* 將指定len長度的字節數組b寫入該文件輸入流 */
    public void write(byte b[], int off, int len) throws IOException {
        Object traceContext = IoTrace.fileWriteBegin(path);
        int bytesWritten = 0;
        try {
            writeBytes(b, off, len, append);
            bytesWritten = len;
        } finally {
            IoTrace.fileWriteEnd(traceContext, bytesWritten);
        }
    }
 
    /* 關閉此文件輸出流並釋放與此流有關的全部系統資源 */
    publicvoidclose() throwsIOException {
        synchronized(closeLock) {
            if(closed) {
                return;
            }
            closed = true;
        }
 
        if(channel != null) {
            fd.decrementAndGetUseCount();
            channel.close();
        }
 
        intuseCount = fd.decrementAndGetUseCount();
 
        if((useCount <= 0) || !isRunningFinalize()) {
            close0();
        }
    }
 
     public final FileDescriptor getFD()  throws IOException {
        if(fd != null) returnfd;
        thrownewIOException();
     }
 
    publicFile Channel getChannel() {
        synchronized(this) {
            if(channel == null) {
                channel = FileChannelImpl.open(fd, path, false, true, append, this);
 
                fd.incrementAndGetUseCount();
            }
            returnchannel;
        }
    }
 
    protected void finalize() throws IOException {
        if(fd != null) {
            if(fd == FileDescriptor.out || fd == FileDescriptor.err) {
                flush();
            } else{
 
                runningFinalize.set(Boolean.TRUE);
                try{
                    close();
                } finally{
                    runningFinalize.set(Boolean.FALSE);
                }
            }
        }
    }
 
    private native void close0() throws IOException;
 
    private static native void initIDs();
 
    static{
        initIDs();
    }
 
}



1. 三個核心方法

三個核心方法,也就是Override(重寫)了抽象類OutputStreamwrite方法。

void write(int b) 方法,即

public void write(intb) throws IOException

代碼實現中很簡單,一個try中調用本地nativewrite()方法,直接將指定的字節b寫入文件輸出流。IoTrace.fileReadEnd()的意思和上面FileInputStream意思一致。


void write(byte b[]) 方法,即

public void write(byteb[]) throws IOException

代碼實現也是比較簡單的,也是一個try中調用本地nativewriteBytes()方法,直接將指定的字節數組寫入該文件輸入流。

void write(byte b[], int off, int len) 方法,即

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




代碼實現和 void write(byte b[]) 方法 同樣,直接將指定的字節數組寫入該文件輸入流。


2. 值得一提的native方法

上面核心方法中爲何實現簡單,由於工做量都在native方法裏面,即JVM裏面實現了。native卻是很多一一列舉吧:

native void open(String name) // 打開文件,爲了下一步讀取文件內容

native void write(int b, boolean append) // 直接將指定的字節b寫入文件輸出流

native native void writeBytes(byte b[], int off, int len, boolean append) // 直接將指定的字節數組寫入該文件輸入流。

native void close0() // 關閉該文件輸入流及涉及的資源,好比說若是該文件輸入流的FileChannel對被獲取後,須要對FileChannel進行close。


類似之處

其實到這裏,該想想。兩個源碼實現很類似,並且native方法也很類似。其實不能說「類似」,應該以「對應」來歸納它們。

它們是一組,是一根吸管的兩個孔的關係:「一個Input一個Output」。

休息一下吧~ 看看小廣告:

開源代碼都在個人gitHub上哦 — https://github.com/JeffLi1993 做者留言「請手賤,點項目star,支持支持拜託拜託」

4、使用案例

下面先看代碼:

packageorg.javacore.io;
 
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
 
/*
 * Copyright [2015] [Jeff Lee]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @author Jeff Lee
 * @since 2015-10-8 20:06:03
 * FileInputStream&FileOutputStream使用案例
 */
public class FileIOStreamT {
    private static final String thisFilePath =
            "src"+ File.separator +
            "org"+ File.separator +
            "javacore"+ File.separator +
            "io"+ File.separator +
            "FileIOStreamT.java";
    public static void main(String[] args) throws IOException {
        // 建立文件輸入流
        FileInputStream fileInputStream = new FileInputStream(thisFilePath);
        // 建立文件輸出流
        FileOutputStream fileOutputStream =  new FileOutputStream("data.txt");
         
        // 建立流的最大字節數組
        byte[] inOutBytes = new byte[fileInputStream.available()];
        // 將文件輸入流讀取,保存至inOutBytes數組
        fileInputStream.read(inOutBytes);
        // 將inOutBytes數組,寫出到data.txt文件中
        fileOutputStream.write(inOutBytes);
         
        fileOutputStream.close();
        fileInputStream.close();
    }
}

運行後,會發現根目錄中出現了一個「data.txt」文件,內容爲上面的代碼。

1. 簡單地分析下源碼:

一、建立了FileInputStream,讀取該代碼文件爲文件輸入流。

二、建立了FileOutputStream,做爲文件輸出流,輸出至data.txt文件。

三、針對流的字節數組,一個 read ,一個write,完成讀取和寫入。

四、關閉流

2. 代碼調用的流程如圖所示:

iostream

3. 代碼雖簡單,可是有點小問題

FileInputStream.available() 是返回流中的估計剩餘字節數。因此通常不會用此方法。

通常作法,好比建立一個 byte數組,大小1K。而後read至其返回值不爲-1,一直讀取便可。邊讀邊寫。

5、思考與小結

FileInputStream & FileOutputStream 是一對來自 InputStream和OutputStream的實現類。用於本地文件讀寫(二進制格式按順序讀寫)

本文小結:

一、FileInputStream 源碼分析

二、FileOutputStream 資源分析

三、FileInputStream & FileOutputStream 使用案例

四、其源碼調用過程

歡迎點擊個人博客及GitHub — 博客提供RSS訂閱哦!

———- http://www.bysocket.com/ ————- https://github.com/JeffLi1993 ———-

相關文章
相關標籤/搜索