Java-IO之FilterInputStream和FilterOuptStream
public class FilterInputStream extends InputStream {
/**
* The input stream to be filtered.
*/
//InputStream
protected volatile InputStream in;
/**
* Creates a <code>FilterInputStream</code>
* by assigning the argument <code>in</code>
* to the field <code>this.in</code> so as
* to remember it for later use.
*
* @param in the underlying input stream, or <code>null</code> if
* this instance is to be created without an underlying stream.
*/
protected FilterInputStream(InputStream in) {
this.in = in;
}
//从输入流读取下一个字节
public int read() throws IOException {
return in.read();
}
//从输入流中读取len长度的字节
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
public int read(byte b[], int off, int len) throws IOException {
return in.read(b, off, len);
}
//跳过长度
public long skip(long n) throws IOException {
return in.skip(n);
}
//是否还有数据
public int available() throws IOException {
return in.available();
}
//关闭资源
public void close() throws IOException {
in.close();
}
//标记在输入流的当前位置
public synchronized void mark(int readlimit) {
in.mark(readlimit);
}
//重置上次位置
public synchronized void reset() throws IOException {
in.reset();
}
//判断是否支持重置和标记
public boolean markSupported() {
return in.markSupported();
}
}
基于JDK8的FilterOutputStream源码:
public class FilterOutputStream extends OutputStream {
/**
* The underlying output stream to be filtered.
*/
protected OutputStream out;
public FilterOutputStream(OutputStream out) {
this.out = out;
}
//写到输入流中
public void write(int b) throws IOException {
out.write(b);
}
//写b到输入流中
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
//写b中的起始位置为off,长度为len
public void write(byte b[], int off, int len) throws IOException {
if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
throw new IndexOutOfBoundsException();
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
/**
* Flushes this output stream and forces any buffered output bytes
* to be written out to the stream.
* <p>
* The <code>flush</code> method of <code>FilterOutputStream</code>
* calls the <code>flush</code> method of its underlying output stream.
*
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
//刷新,强制所有的缓冲数据都被写出
public void flush() throws IOException {
out.flush();
}
//关闭资源
@SuppressWarnings("try")
public void close() throws IOException {
try (OutputStream ostream = out) {
flush();
}
}
}
Java-IO之FilterInputStream和FilterOuptStream的更多相关文章
- java.io包中的字节流—— FilterInputStream和FilterOutputStream
接着上篇文章,本篇继续说java.io包中的字节流.按照前篇文章所说,java.io包中的字节流中的类关系有用到GoF<设计模式>中的装饰者模式,而这正体现在FilterInputStre ...
- java io系列10之 FilterInputStream
FilterInputStream 介绍 FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”.它的常用的子类有BufferedInputStream和Data ...
- Java IO(九)FilterInputStream 和 FilterOutputStream
Java IO(九)FilterInputStream 和 FilterOutputStream 一.介绍 FilterInputStream 和 FilterOutputStream 是过滤字节输入 ...
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- Java IO流学习总结
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- [Java IO]02_字节流
概要 字节流有两个核心抽象类:InputStream 和 OutputStream.所有的字节流类都继承自这两个抽象类. InputStream 负责输入,OutputStream 负责输出. 字节流 ...
- Java IO之字节流
Java中的输入是指从数据源等读到Java程序中,这里的数据源可以是文件,内存或网络连接,输出则是指从Java程序中写到目的地. 输入输出流可以分为以下几种类型(暂时不考虑File类) 类名 中文名 ...
- java io 流分类表
Java输入/输出流体系中常用的流分类(表内容来自java疯狂讲义) 注:下表中带下划线的是抽象类,不能创建对象.粗体部分是节点流,其他就是常用的处理流. 流分类 使用分类 字节输入流 字节输出流 字 ...
随机推荐
- 清空dataset中的某行某列的数据
string tempSFZH = ""; foreach (DataRow rs in ds.Tables[0].Rows) { tempSFZH = rs[ht[&qu ...
- tensorflow共享变量 the difference between tf.Variable() and get_variable()
一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...
- CI下载与安装_基础配置_MVC
CI:CodeIgniter -- 由Ellislab公司的CEORickEllis开发,是一个简单快速的PHP MVC框架. =============下载和安装================地址 ...
- ACM KMP 格式输入导致TLE
在写 Oulipo POJ - 3461 时候遇上的奇怪的问题 在格式输入上不一样,提交的时候返回TLE,两段代码如下: A#include<iostream> #include< ...
- Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- ACM Self Number
In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. Fo ...
- Linux(十七)动态监控进程
17.1 介绍 top与ps命令很相似.它们都用来显示正在执行的进程.top与ps最大的不同之处,在于top在执行一段时间可以更新正在运行的进程 17.2 语法 top [选项] 常用选项 ...
- 自定义view实现阻尼效果的加载动画
效果: > 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减 ...
- MyBatis批量新增和更新
之前有开发任务一个接口里面有大量的数据新增和更新操作,导致十分缓慢.使用了批量操作之后速度有明显提升,几乎百倍千倍的速度提升. 博主之前统计过,通过普通接口一次数据库插入大概需要200ms,对于大量新 ...
- 20160219.CCPP体系详解(0029天)
程序片段(01):ReplaceAll.c 内容概要:ReplaceAll #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #incl ...