commons-fileupload框架源碼解析(八)--DeferredFileOutputStream

  1. commons-fileupload框架源碼解析(一)--實(shí)例
  2. commons-fileupload框架源碼解析(二)--HTTP
  3. commons-fileupload框架源碼解析(三)--ParseRequest
  4. commons-fileupload框架源碼解析(四)--FileItemIterator
  5. commons-fileupload框架源碼解析(五)--MultipartStream
  6. commons-fileupload框架源碼解析(六)--ParameterParser
  7. commons-fileupload框架源碼解析(七)--FileCleaningTracker
  8. commons-fileupload框架源碼解析(八)--DeferredFileOutputStream

簡(jiǎn)介

DeferredFileOutputStream是一個(gè)根據(jù)設(shè)定的最大內(nèi)存閥值去控制寫入到內(nèi)存中,還是寫入到硬盤中畴蹭。

ThresholdingOutputStream

DeferredFileOutputStream繼承ThresholdOutputStream,ThresholdOutputStream繼承OuptStream,重寫了write相關(guān)方法轧叽。

ThresholdOutputStream.write相關(guān)方法

 @Override
    public void write(final int b) throws IOException
    {
        checkThreshold(1);
        getStream().write(b);
        written++;
    }

    @Override
    public void write(final byte b[]) throws IOException
    {
        checkThreshold(b.length);
        getStream().write(b);
        written += b.length;
    }

  @Override
    public void write(final byte b[], final int off, final int len) throws IOException
    {
        checkThreshold(len);
        getStream().write(b, off, len);
        written += len;
    }

主要是加上了chekThreshold(len)方法罗洗,并調(diào)用getStream()交給另一個(gè)流
處理肛跌,并統(tǒng)計(jì)寫入了多少字節(jié)(written)艺配。

ThresholdOutputStream.checkThreshold(int)

protected void checkThreshold(final int count) throws IOException
    {
        if (!thresholdExceeded && written + count > threshold)
        {
            thresholdExceeded = true;
            thresholdReached();
        }
    }

檢查當(dāng)前的寫入到內(nèi)容流的字節(jié)數(shù)written是否大于閥值threshold,大于的時(shí)候會(huì)調(diào)用thresholdReached()

getStream & thresholdReached

/**
     * Returns the underlying output stream, to which the corresponding
     * <code>OutputStream</code> methods in this class will ultimately delegate.
     * 獲取輸出流
     * @return The underlying output stream.
     *
     * @throws IOException if an error occurs.
     */
    protected abstract OutputStream getStream() throws IOException;


    /**
     * Indicates that the configured threshold has been reached, and that a
     * subclass should take whatever action necessary on this event. This may
     * include changing the underlying output stream.
     * 當(dāng)前的寫入字節(jié)數(shù)written是否大于閥值threshold時(shí)觸發(fā)
     * @throws IOException if an error occurs.
     */
    protected abstract void thresholdReached() throws IOException;

這兩個(gè)是抽象方法衍慎,而DeferredFileOutputStream重寫了,先看getStream

DeferredFileOutputStream.getStream

  @Override
    protected OutputStream getStream() throws IOException
    {
        return currentOutputStream;
    }

返回currentOuputStream转唉,一開始默認(rèn)是內(nèi)存流ByteArrayOutputStream。

DeferredFileOutputStream.thresholdReached

@Override
    protected void thresholdReached() throws IOException
    {
        //如果prefix,suffix,directory不為null稳捆,outputFile不管是否已經(jīng)引用了對(duì)象赠法,都會(huì)重新調(diào)用File.createTempFile(String,String,File)來(lái)
        //創(chuàng)建指定的臨時(shí)文件,并讓后outputFile指向這個(gè)文件
        if (prefix != null) {
            //創(chuàng)建臨時(shí)文件
            outputFile = File.createTempFile(prefix, suffix, directory);
        }
        FileUtils.forceMkdirParent(outputFile);//檢查父級(jí)目錄是否存在,不存在則創(chuàng)建砖织,創(chuàng)建父級(jí)文件目錄
        final FileOutputStream fos = new FileOutputStream(outputFile);
        try {
            memoryOutputStream.writeTo(fos);//將已經(jīng)內(nèi)容流的數(shù)據(jù)寫入進(jìn)文件流
        } catch (final IOException e){
            fos.close();//當(dāng)前出現(xiàn)異常時(shí)關(guān)閉流款侵,并拋出異常
            throw e;
        }
        currentOutputStream = fos;
        memoryOutputStream = null;
    }

當(dāng)達(dá)到閥值的時(shí)候,thresholdReached()就會(huì)被調(diào)用侧纯,會(huì)將內(nèi)存流memoryOutputStream中的數(shù)據(jù)轉(zhuǎn)換到寫入硬盤的流FileOuputStream中新锈,并將FileOuputStream重新賦值currentOuptputStream

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市眶熬,隨后出現(xiàn)的幾起案子妹笆,更是在濱河造成了極大的恐慌,老刑警劉巖娜氏,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拳缠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡贸弥,警方通過(guò)查閱死者的電腦和手機(jī)窟坐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)绵疲,“玉大人狸涌,你說(shuō)我怎么就攤上這事∽罡冢” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵朝捆,是天一觀的道長(zhǎng)般渡。 經(jīng)常有香客問我,道長(zhǎng)芙盘,這世上最難降的妖魔是什么驯用? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮儒老,結(jié)果婚禮上蝴乔,老公的妹妹穿的比我還像新娘。我一直安慰自己驮樊,他們只是感情好薇正,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著囚衔,像睡著了一般挖腰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上练湿,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天猴仑,我揣著相機(jī)與錄音,去河邊找鬼肥哎。 笑死辽俗,一個(gè)胖子當(dāng)著我的面吹牛疾渣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播崖飘,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼榴捡,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了坐漏?” 一聲冷哼從身側(cè)響起薄疚,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赊琳,沒想到半個(gè)月后街夭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡躏筏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年板丽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片趁尼。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡埃碱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出酥泞,到底是詐尸還是另有隱情砚殿,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布芝囤,位于F島的核電站似炎,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏悯姊。R本人自食惡果不足惜羡藐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望悯许。 院中可真熱鬧仆嗦,春花似錦、人聲如沸先壕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)启上。三九已至邢隧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冈在,已是汗流浹背倒慧。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人纫谅。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓炫贤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親付秕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子兰珍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354