📄 chapter10.html
字号:
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a source of data. Connect it to
a <B>FilterInputStream</B> object to provide a useful
interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Filter-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Abstract class which is an
interface for decorators that provide useful functionality to the other
<B>InputStream</B> classes. See Table 10-3.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">See Table 10-3.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">See Table 10-3.</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545385"></A><A NAME="_Toc408018612"></A></TABLE></P></DIV>
<A NAME="Heading305"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Types of OutputStream<BR><A NAME="Index1018"></A><A NAME="Index1019"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This category includes the classes
that decide where your output will go: an array of bytes (no <B>String</B>,
however; presumably you can create one using the array of bytes), a file, or a
“pipe.”</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In addition, the
<B>FilterOutputStream</B> provides a base class for "decorator" classes that
attach attributes or useful interfaces to output streams. This is discussed
later.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Table 10-2. Types of
OutputStream<A NAME="Index1020"></A><A NAME="Index1021"></A><A NAME="Index1022"></A><A NAME="Index1023"></A><A NAME="Index1024"></A><A NAME="Index1025"></A><A NAME="Index1026"></A><A NAME="Index1027"></A></B></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=85 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Class</FONT><BR></P></DIV>
</TH>
<TH WIDTH=94 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Function</FONT><BR></P></DIV>
</TH>
<TH WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Constructor
Arguments</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TH WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">How to use it</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>ByteArray-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Creates a buffer in memory. All the
data that you send to the stream is placed in this buffer.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Optional initial size of the
buffer.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To designate the destination of
your data. Connect it to a <B>FilterOutputStream</B> object to provide a useful
interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>File-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For sending information to a
file.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>String</B> representing the
file name, or a <B>File</B> or <B>FileDescriptor</B> object.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To designate the destination of
your data. Connect it to a <B>FilterOutputStream</B> object to provide a useful
interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Piped-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Any information you write to this
automatically ends up as input for the associated <B>PipedInput-Stream.</B>
Implements the “piping” concept.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PipedInputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To designate the destination of
your data for multithreading. Connect it to a <B>FilterOutputStream</B> object
to provide a useful interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Filter-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Abstract class which is an
interface for decorators that provide useful functionality to the other
<B>OutputStream</B> classes. See Table
</FONT><BR><FONT FACE="Georgia">10-4.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">See Table 10-4.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">See Table 10-4.</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545386"></A><A NAME="_Toc408018613"></A></TABLE></P></DIV>
<A NAME="Heading306"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Adding attributes <BR>and useful interfaces</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The use of layered objects to
dynamically and transparently add responsibilities to individual objects is
referred to as the
<A NAME="Index1028"></A><A NAME="Index1029"></A><I>decorator</I> pattern.
(Patterns</FONT><A NAME="fnB44" HREF="#fn44">[44]</A><FONT FACE="Georgia">
are the subject of Chapter 16.) The decorator pattern specifies that all objects
that wrap around your initial object have the same interface, to make the use of
the decorators transparent – you send the same message to an object
whether it’s been decorated or not. This is the reason for the existence
of the “filter” classes in the Java IO library: the abstract
“filter” class is the base class for all the decorators. (A
decorator must have the same interface as the object it decorates, but the
decorator can also extend the interface, which occurs in several of the
“filter” classes).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Decorators are often used when
subclassing requires a large number of subclasses to support every possible
combination needed – so many that subclassing becomes impractical. The
Java IO library requires many different combinations of features which is why
the decorator pattern is a good approach. There is a drawback to the decorator
pattern, however. Decorators give you much more flexibility while you’re
writing a program (since you can easily mix and match attributes), but they add
complexity to your code. The reason that the Java IO library is awkward to use
is that you must create many classes – the “core” IO type plus
all the decorators – in order to get the single IO object that you want.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The classes that provide the
decorator interface to control a particular <B>InputStream</B> or
<B>OutputStream</B> are the <B>FilterInputStream</B> and
<B>FilterOutputStream</B> – which don’t have very intuitive names.
They are derived, respectively, from <B>InputStream</B> and <B>OutputStream</B>,
and they are abstract classes, in theory to provide a common interface for all
the different ways you want to talk to a stream. In fact,
<B>FilterInputStream</B> and <B>FilterOutputStream</B> simply mimic their base
classes, which is the key requirement of the
decorator.</FONT><A NAME="_Toc375545387"></A><A NAME="_Toc408018614"></A><BR></P></DIV>
<A NAME="Heading307"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Reading from an InputStream <BR>with FilterInputStream</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>FilterInputStream</B>
classes accomplish two significantly different things. <B>DataInputStream</B>
allows you to read different types of primitive data as well as <B>String</B>
objects. (All the methods start with “read,” such as
<B>readByte( )</B>, <B>readFloat( )</B>, etc.) This, along with its
companion <B>DataOutputStream</B>, allows you to move primitive data from one
place to another via a stream. These “places” are determined by the
classes in Table 10-1. If you’re reading data in blocks and parsing it
yourself, you won’t need <B>DataInputStream</B>, but in most other cases
you will want to use it to automatically format the data you
read.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The remaining classes modify the
way an <B>InputStream</B> behaves internally: whether it’s buffered or
unbuffered, if it keeps track of the lines it’s reading (allowing you to
ask for line numbers or set the line number), and whether you can push back a
single character. The last two classes look a lot like support for building a
compiler (that is, they were added to support the construction of the Java
compiler), so you probably won’t use them in general programming.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You’ll probably need to
buffer your input almost every time, regardless of the IO device you’re
connecting to, so it would have made more sense for the IO library to make a
special case for unbuffered input rather than buffered input.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Table 10-3. Types of
FilterInputStream<A NAME="Index1030"></A><A NAME="Index1031"></A><A NAME="Index1032"></A><A NAME="Index1033"></A><A NAME="Index1034"></A><A NAME="Index1035"></A><A NAME="Index1036"></A><A NAME="Index1037"></A></B></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Class</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Function</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Constructor
Arguments</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">How to use it</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Data-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Used in concert with
<B>DataOutputStream</B>, so you can read primitives (int, char, long, etc.) from
a stream in a portable fashion.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Contains a full interface to allow
you to read primitive types.</FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Buffered-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Use this to prevent a physical read
every time you want more data. You’re saying “Use a
buffer.”</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B>, with optional
buffer size.</FONT><BR></P></DIV>
</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -