📄 tij314.htm
字号:
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">As a source of data in multithreading: Connect it to a <b>FilterInputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Sequence-InputStream</b><br></p>
</td>
<td width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table">Converts two or more <b>InputStream</b> objects into a single <b>InputStream</b>.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">Two <b>InputStream</b> objects or an <b>Enumeration</b> for a container of <b>InputStream</b> objects.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">As a source of data: Connect it to a <b>FilterInputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Filter-InputStream</b><br></p>
</td>
<td width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table">Abstract class that is an interface for decorators that provide useful functionality to the other <b>InputStream</b> classes. See Table 12-3.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">See Table 12-3.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">See Table 12-3.<br></p>
</td>
</tr>
</table></div>
<h3>
<a name="_Toc375545385"></a><a name="_Toc24775789"></a><a name="Heading14113"></a>Types
of <b>OutputStream</b><br></h3>
<p><a name="Index1222"></a><a name="Index1223"></a>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1644" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1645" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>Table 12-2. Types of OutputStream</b><br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><a name="Index1224"></a><a name="Index1225"></a><a name="Index1226"></a><a name="Index1227"></a><a name="Index1228"></a><a name="Index1229"></a><a name="Index1230"></a><a name="Index1231"></a><b>Class</b><br></p>
</th>
<th width="155.999961" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Function</b><br></p>
</th>
<th width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Constructor Arguments</b><br></p>
</th>
</tr>
<tr valign="top">
<th width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>How to use it</b><br></p>
</th>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>ByteArray-OutputStream</b><br></p>
</td>
<td width="155.999961" colspan="1" rowspan="2" valign="top">
<p class="Table">Creates a buffer in memory. All the data that you send to the stream is placed in this buffer.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">Optional initial size of the buffer.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">To designate the destination of your data: Connect it to a <b>FilterOutputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>File-OutputStream</b><br></p>
</td>
<td width="155.999961" colspan="1" rowspan="2" valign="top">
<p class="Table">For sending information to a file.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">A String representing the file name, or a <b>File</b> or <b>FileDescriptor</b> object.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">To designate the destination of your data: Connect it to a <b>FilterOutputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Piped-OutputStream</b><br></p>
</td>
<td width="155.999961" colspan="1" rowspan="2" valign="top">
<p class="Table">Any information you write to this automatically ends up as input for the associated <b>PipedInput-Stream</b>. Implements the “piping” concept.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>PipedInputStream</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">To designate the destination of your data for multithreading: Connect it to a <b>FilterOutputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Filter-OutputStream</b><br></p>
</td>
<td width="155.999961" colspan="1" rowspan="2" valign="top">
<p class="Table">Abstract class that is an interface for decorators that provide useful functionality to the other <b>OutputStream</b> classes. See Table 12-4.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">See Table 12-4.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">See Table 12-4.<br></p>
</td>
</tr>
</table></div>
<h2>
<a name="_Toc375545386"></a><a name="_Toc24775790"></a><a name="Heading14147"></a>Adding
attributes <br>and useful interfaces</h2>
<p>The use of layered objects to dynamically and transparently add responsibilities to individual objects is referred to as the <a name="Index1232"></a><a name="Index1233"></a><i>Decorator</i> pattern. (Patterns<sup><a name="fnB61" href="#fn61">[61]</a></sup> are the subject of <i>Thinking in Patterns (with Java)</i> at <i>www.BruceEckel.com</i>.) The decorator pattern specifies that all objects that wrap around your initial object have the same interface. This makes the basic use of the decorators transparent—you send the same message to an object whether it has been decorated or not. This is the reason for the existence of the “filter” classes in the Java I/O 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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1646" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Decorators are often used when simple subclassing results in a large number of classes in order to satisfy every possible combination that is needed—so many classes that it becomes impractical. The Java I/O library requires many different combinations of features, and this is the justification for using the decorator pattern.<sup><a name="fnB62" href="#fn62">[62]</a></sup> 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 I/O library is awkward to use is that you must create many classes—the “core” I/O type plus all the decorators—in order to get the single I/O object that you want. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1647" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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. <b>FilterInputStream</b> and <b>FilterOutputStream</b> are derived from the base classes of the I/O library, <b>InputStream</b> and <b>OutputStream</b>, which is the key requirement of the decorator (so that it provides the common interface to all the objects that are being decorated). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1648" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545387"></a><a name="_Toc24775791"></a><a name="Heading14153"></a>Reading
from an <b>InputStream </b><br>with<b> FilterInputStream</b></h3>
<p>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 12-1. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1649" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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 probably added to support the construction of the Java compiler), so you probably won’t use them in general programming. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1650" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You’ll need to buffer your input almost every time, regardless of the I/O device you’re connecting to, so it would have made more sense for the I/O library to make a special case (or simply a method call) for unbuffered input rather than buffered input. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1651" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>Table 12-3. Types of FilterInputStream</b><br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><a name="Index1234"></a><a name="Index1235"></a><a name="Index1236"></a><a name="Index1237"></a><a name="Index1238"></a><a name="Index1239"></a><a name="Index1240"></a><a name="Index1241"></a><b>Class</b><br></p>
</td>
<td width="178.666622" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Function</b><br></p>
</td>
<td width="173.333290" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Constructor Arguments</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="173.333290" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>How to use it</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Data-InputStream</b><br></p>
</td>
<td width="178.666622" colspan="1" rowspan="2" valign="top">
<p class="Table">Used in concert with <b>DataOutputStream</b>, so you can read primitives (<b>int</b>, <b>char</b>, <b>long</b>, etc.) from a stream in a portable fashion.<br></p>
</td>
<td width="173.333290" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>InputStream</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="173.333290" colspan="1" rowspan="1" valign="top">
<p class="Table">Contains a full interface to allow you to read primitive types.<br></p>
</td>
</tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -