Java:FilterInputStream 与其他流相比有哪些优点和用途
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17494629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: FilterInputStream what are the advantages and use compared to other streams
提问by Luther
I've been reading on InputStream, FileInputStream, ByteArrayInputStreamand how their use seems quite clear (output streams too).
我一直在阅读InputStream、FileInputStream、ByteArrayInputStream以及它们的使用似乎很清楚(输出流也是如此)。
What I'm struggling is to understand the use of FilterInputStream & FilterOutputStream:
我正在努力了解FilterInputStream 和 FilterOutputStream的使用:
- What is the advantage of using it compared to the other stream classes?
- When should I use it?
- Please provide a theoretical explanation and a basic example.
- 与其他流类相比,使用它的优势是什么?
- 我应该什么时候使用它?
- 请提供理论解释和基本示例。
回答by erickson
FilterInputStream
is an example of the the Decorator pattern.
This class must be extended, since its constructor is protected
. The derived class would add additional capabilities, but still expose the basic interface of an InputStream
.
这个类必须被扩展,因为它的构造函数是protected
. 派生类会添加额外的功能,但仍会公开InputStream
.
For example, a BufferedInputStream
provides buffering of an underlying input stream to make reading data faster, and a DigestInputStream
computes a cryptographic hash of data as it's consumed.
例如,aBufferedInputStream
提供对底层输入流的缓冲以加快读取数据的速度,aDigestInputStream
在使用数据时计算数据的加密散列。
You would use this to add functionality to existing code that depends on the InputStream
or OutputStream
API. For example, suppose that you use some library that saves data to an OutputStream
. The data are growing too large, so you want to add compression. Instead of modifying the data persistence library, you can modify your application so that it "decorates" the stream that it currently creates with a ZipOutputStream
. The library will use the stream just as it used the old version that lacked compression.
您将使用它向依赖于InputStream
或OutputStream
API 的现有代码添加功能。例如,假设您使用某个库将数据保存到OutputStream
. 数据增长过大,因此您要添加压缩。您可以修改您的应用程序,而不是修改数据持久性库,以便它“装饰”当前创建的流ZipOutputStream
。该库将使用该流,就像它使用缺乏压缩的旧版本一样。
回答by Lee Meador
You use them when you want to decorate the stream of data.
当您想要装饰数据流时,您可以使用它们。
Remember that these stream class instances wrap themselves around another stream instance (whether another subclass of one of these or not) and add some feature, add some processing, make some changes to the data as it passes through.
请记住,这些流类实例将自己包裹在另一个流实例(无论是否是其中一个的另一个子类)并添加一些功能、添加一些处理、在数据通过时对其进行一些更改。
For example, you might want to remove all the multiple spaces from some stream. You make your own subclass of FilterInputStream and override the read()
method. I'm not going to bother all the details but here's some sorta-java for the method in the subclass:
例如,您可能希望从某个流中删除所有多个空格。您创建自己的 FilterInputStream 子类并覆盖该read()
方法。我不打算打扰所有的细节,但这里有一些关于子类中方法的 java:
private boolean lastWasBlank = false;
public int read() {
int chr = super.read();
if (chr == ' ') {
if (lastWasBlank) {
return read();
} else {
lastWasBlank = true;
}
} else {
lastWasBlank = false;
}
return chr;
}
In real life, you would probably mess with the other two read()
methods too.
在现实生活中,您可能也会弄乱其他两种read()
方法。
Other uses:
其他用途:
- Log everything flowing through the stream
- Duplicate the 'tee' utility so the stream being read is handled two ways.
- Convert line endings between Windows, Mac and Unix/Linux formats
- Add delays to simulate slow transmission methods like modems or serial ports or wireless network connections.
- 记录流经流的所有内容
- 复制 'tee' 实用程序,以便以两种方式处理正在读取的流。
- 在 Windows、Mac 和 Unix/Linux 格式之间转换行尾
- 添加延迟以模拟慢速传输方法,如调制解调器或串行端口或无线网络连接。
回答by George Aristy
FilterInputStreamand FilterOutputStreamare there to ease the job of developers who wish to implement their own input/output streams. Implementations such as BufferedInputStreammay add their own decorations around the basic InputStream
API while delegating on the super
class - FilteredInputStream in this case - the methods they don't need to override.
FilterInputStream和FilterOutputStream可以减轻希望实现自己的输入/输出流的开发人员的工作。诸如BufferedInputStream 之类的实现可能会在基本InputStream
API周围添加自己的装饰,同时委托super
类 - 在这种情况下是 FilteredInputStream - 它们不需要覆盖的方法。
Neither FilterInputStream
nor FilterOutputStream
are designed for end users to use directly.
既不是FilterInputStream
也不FilterOutputStream
是为最终用户直接使用而设计的。