美文网首页
从IO类看buffer

从IO类看buffer

作者: packet | 来源:发表于2018-07-20 20:00 被阅读0次

先看一段关于Buffer的英文:

Buffer can speed up IO quite a bit.

The BufferedInputStream class provides buffering to your input streams.

With both disk and network streams, the optimal buffer size may also depend on the concrete hardware in the computer. If the hard disk is anyways reading a minimum of 4KB at a time, it's stupid to use less than a 4KB buffer. It is also better to then use a buffer size that is a multiple of 4KB.

The main difference between BufferedReader and BufferedInputStream is that BufferedReader reads characters (text), whereas the BufferedInputStream reads raw bytes.

The BufferedWriter adds one extra method though: The newLine() method which can write a new-line character to the underlying Writer. In addition, you may need to call flush() if you need to be absolutely sure that the characters written until now is flushed out of the buffer and **onto **the network or disk.

为什么在IO中,buffer不可或缺?

我们知道,数据可能存储在不同的介质中,比如寄存器,内存,硬盘等。不同介质中的数据需要通信,而不同介质的数据存取速度往往差异很大,这样就会造成快者等待慢者的现象。为了解决这种速度不匹配的问题,buffer就大展身手了。

比如读硬盘,最小读取数据单元是4KB,那么就会一次读取4KB的整数倍,放在buffer中,这样内存就不必须频繁读硬盘了。这就是预读取的思想。

不仅仅是读,写操作也会用到buffer,也是为了频繁地写。写操作有一个特殊的函数flush(),用于把buffer中的数据刷出去,写到硬盘或者网络中去。

基于这种设计,CPU会把常用数据从内存中放到自己的缓存中,操作系统会把常用数据从硬盘读取到内存。

参考资料:http://tutorials.jenkov.com/java-io/overview.html

相关文章

  • 从IO类看buffer

    先看一段关于Buffer的英文: Buffer can speed up IO quite a bit. The ...

  • netty内存分析

    netty内存规格 netty内存分配器类图如下 我们直接看io.netty.buffer.PooledByteB...

  • python3从零学习-5.6.2、io.py模块

    源代码: Lib/io.py io.DEFAULT_BUFFER_SIZE 包含模块缓冲 I/O 类使用的默认缓冲...

  • JAVA NIO - Buffer

    NIO是基于Buffer的,通过channel将buffer中的内容发送给IO或保存从IO中读取的内容到buffe...

  • IO buffer

    今天要介绍的数字后端概念是IO Buffer。主要是指放置在block level的port附近的buffer[h...

  • oracle中的buffer cache 详解

    1、block、buffer的概念 复习段区块 2、buffer cache的意义 减少IO 物理IO:磁盘读 逻...

  • JAVA之道丨文件复制的四种方法

    import java.io.BufferedInputStream; import java.io.Buffer...

  • Node.js-Buffer

    Buffer 在Node.js中提供了Buffer类,通过Buffer类中的各种构造函数创建Buffer对象,从而...

  • JAVA NIO BUFFER (八)byte Buffer

    除了布尔类型,其他基本类型都有自身的Buffer类,但是byteBuffer还有不少其他特性。操作系统和他的IO设...

  • NIO java编程

    NIO 同步式非阻塞式IO NIO组件:Buffer channel selector Buffer 缓冲区 1....

网友评论

      本文标题:从IO类看buffer

      本文链接:https://www.haomeiwen.com/subject/cmoqmftx.html