当前位置: 首页 > news >正文

备案 网站 漏接 电话/ui设计公司

备案 网站 漏接 电话,ui设计公司,什么网站能通过做任务赚钱吗,广告设计学校一、补遗点 看很多人调用缓冲流的时候每write(无论哪一种write)一次就flush一次,这样是完全没有必要的,不仅浪费了缓冲流“缓冲”的特性,还每写一次就开闭IO一次,甚至做了一个中转才写IO,反而更…

一、补遗点

看很多人调用缓冲流的时候每write(无论哪一种write)一次就flush一次,这样是完全没有必要的,不仅浪费了缓冲流“缓冲”的特性,还每写一次就开闭IO一次,甚至做了一个中转才写IO,反而更加浪费性能。查看缓冲流(字节和字符)的源码,发现其实缓冲流是会自动把数据刷新到磁盘,完全没必要每写一次就flush一次,只需要在关闭缓冲流之前flush一次就够了。

缓冲流采用装饰器设计模式扩展了节点流,在缓冲流中定义了一个8192字节(字符)的buf缓冲数组和一个记录写入字节数量的游标count,当count>buf.length的时候,会自动把缓冲区的数据刷新到磁盘,然后将count归零,缓冲区重新开始放入数据。也就是缓冲流每8192个字节或字符会自动把数据刷入磁盘,只需要在关闭流之前,将文件最后小于8192的数据flush一次就可以了。

做一个测试,用以下三种方法拷贝一个963KB的mp3文件:

1. 关闭流前flush一次,关闭流;

结果:拷贝文件大小963KB。

2. 关闭流前flush一次,不关闭流;

结果:拷贝文件963大小KB。

3. 不flush,也不关闭流;

结果:拷贝文件大小960KB

结论:

1. 960KB=120*8192byte,如果没有flush,则最后3*1024byte因为小于8192byte,将驻留缓冲流缓冲区而不会被刷到磁盘;

2. 可以不调用flush,关闭流之前会默认flush一次,但是为了防止关闭流的时候出现异常,应该在关闭之前显式的调用flush方法将末尾数据刷新到磁盘;

详情请看源码的简单分析,你会感觉到设计的奇妙以及装饰器设计模式的精妙。

二、源码分析

字节缓冲流和字符缓冲流都会定义count和长度为8192的缓冲数组,本例为字节缓冲流源码。

/** %W% %E%** Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*/package java.io;/*** The class implements a buffered output stream. By setting up such * an output stream, an application can write bytes to the underlying * output stream without necessarily causing a call to the underlying * system for each byte written.** @author  Arthur van Hoff* @version %I%, %G%* @since   JDK1.0*/
public 
class BufferedOutputStream extends FilterOutputStream {/*** The internal buffer where data is stored. */protected byte buf[];<span style="background-color: rgb(255, 0, 0);">//定义缓冲数组</span>/*** The number of valid bytes in the buffer. This value is always * in the range <tt>0</tt> through <tt>buf.length</tt>; elements * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid * byte data.*/protected int count;<span style="background-color: rgb(255, 0, 0);">//用于记录write的数据长度</span>/*** Creates a new buffered output stream to write data to the* specified underlying output stream.** @param   out   the underlying output stream.*/public BufferedOutputStream(OutputStream out) {this(out, 8192);<span style="background-color: rgb(255, 0, 0);">//buf缓冲区的默认长度是8192byte</span>}/*** Creates a new buffered output stream to write data to the * specified underlying output stream with the specified buffer * size. ** @param   out    the underlying output stream.* @param   size   the buffer size.* @exception IllegalArgumentException if size <= 0.*/public BufferedOutputStream(OutputStream out, int size) {super(out);if (size <= 0) {throw new IllegalArgumentException("Buffer size <= 0");}buf = new byte[size];<span style="background-color: rgb(255, 0, 0);">//自定义缓冲区大小</span>}/** Flush the internal buffer */private void flushBuffer() throws IOException {<span style="background-color: rgb(255, 0, 0);">//私有的flush方法,调用节点流的write方法,将数据写到磁盘</span>if (count > 0) {out.write(buf, 0, count);count = 0;}}/*** Writes the specified byte to this buffered output stream. ** @param      b   the byte to be written.* @exception  IOException  if an I/O error occurs.*/public synchronized void write(int b) throws IOException {<span style="background-color: rgb(255, 0, 0);">//缓冲流自己的write方法,不再是写磁盘,而是写到缓冲区buf</span>if (count >= buf.length) {<span style="background-color: rgb(255, 0, 0);">//如果缓冲流自己的write方法已经写入的数据长度小于buf.length,则表示缓冲区没有写满,继续写缓冲</span>flushBuffer();<span style="background-color: rgb(255, 0, 0);">//如果缓冲流自己的write方法已经写入的数据长度大于等于buf.length则表示缓冲区写满,调用节点流的write方法将数据写入磁盘</span>}buf[count++] = (byte)b;<span style="background-color: rgb(255, 0, 0);">//缓冲区没有写满,将数据写入缓冲区,记录缓冲流write方法写入的数据长度count也相应的增长</span>}/*** Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to this buffered output stream.** <p> Ordinarily this method stores bytes from the given array into this* stream's buffer, flushing the buffer to the underlying output stream as* needed.  If the requested length is at least as large as this stream's* buffer, however, then this method will flush the buffer and write the* bytes directly to the underlying output stream.  Thus redundant* <code>BufferedOutputStream</code>s will not copy data unnecessarily.** @param      b     the data.* @param      off   the start offset in the data.* @param      len   the number of bytes to write.* @exception  IOException  if an I/O error occurs.*/public synchronized void write(byte b[], int off, int len) throws IOException {if (len >= buf.length) {<span style="background-color: rgb(255, 0, 0);">//判断传入的数据数组是否会将缓冲区写满,写满则自动flush,否则继续写</span>/* If the request length exceeds the size of the output buffer,flush the output buffer and then write the data directly.In this way buffered streams will cascade harmlessly. */flushBuffer();out.write(b, off, len);return;}if (len > buf.length - count) {<span style="background-color: rgb(255, 0, 0);">//如果传入的数据数组的长度大于缓冲区剩余空间,先将缓冲区的数据刷入磁盘,在写入数据</span>flushBuffer();}System.arraycopy(b, off, buf, count, len);<span style="background-color: rgb(255, 0, 0);">//将数据拷贝到缓冲区</span>count += len;<span style="background-color: rgb(255, 0, 0);">//记录缓冲区已经写入数据长度的游标增长</span>}/*** Flushes this buffered output stream. This forces any buffered * output bytes to be written out to the underlying output stream. ** @exception  IOException  if an I/O error occurs.* @see        java.io.FilterOutputStream#out*/public synchronized void flush() throws IOException {<span style="background-color: rgb(255, 0, 0);">//用于显式调用的flush方法,用于将数据刷新到磁盘,无论缓冲区是否被写满,<strong>在缓冲流的生命周期中,只需要调用该方法一次就行了</strong></span>flushBuffer();out.flush();}
}

附注:

        如有错漏,烦请不吝指正!

http://www.jmfq.cn/news/4917655.html

相关文章:

  • 阳泉那有做网站的/百度关键词搜索排名
  • 网站服务器要求/今日热点新闻头条国内
  • 使用vue路由做网站/seo排名赚
  • 免费自制网站建设/seo教育培训机构
  • 政府网站建设讲话稿/网络营销的种类有哪些
  • 网站按关键词显示广告图片/品牌营销包括哪些内容
  • 微信搜一搜seo优化/郑州seo外包顾问热狗
  • 深圳网站建设深圳网/广东培训seo
  • 蒙自市建设局网站/疫情排行榜最新消息
  • 重庆杂酱制作/上海网站排名seo公司哪家好
  • 网站备案被恶意注销/营销网站建设推广
  • 郑州市网站/下载班级优化大师app
  • 济南建网站要/专注于品牌营销服务
  • 怎么做简单网站/百度知道合伙人
  • 岳阳网站建设与设计/广州网络推广专员
  • 一般学校网站的后台用什么做/别做网络推广员
  • 重庆渝北做网站哪里便宜/中国十大营销策划公司排名
  • 整站优化推广品牌/做seo要投入什么
  • 太原网站推广排名/网站加速器
  • 什么是搜索引擎竞价推广/网站优化就是搜索引擎优化
  • 学做网站要什么学历/汕头seo排名收费
  • 上海景泰建设有限公司网站/西安百度竞价开户
  • 做一家网站费用/百度收录网站提交入口
  • jsp做的网站效果/免费开网店免费供货
  • 接做网站私活/真实有效的优化排名
  • 做PPT的网站canva/网站下载
  • 网站建设中js控制什么/直接打开百度
  • 培训机构网站建设方案/西安seo学院
  • 怎么做58网站吊车网/武汉关键词排名提升
  • 在英特尔上建设网站可选择的方案有/seo网站优化工具大全