{"id":108,"date":"2021-01-16T22:13:08","date_gmt":"2021-01-16T22:13:08","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/?page_id=108"},"modified":"2021-01-18T22:38:02","modified_gmt":"2021-01-18T22:38:02","slug":"byte-streams","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/","title":{"rendered":"Byte Streams"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a6d4a9c9b19f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a6d4a9c9b19f\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#The_InputStream_Class\" >The InputStream Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#The_OutputStream_Class\" >The OutputStream Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#Data_SourceSink_Byte_Streams\" >Data Source\/Sink Byte Streams<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#Processing_Byte_Streams\" >Processing Byte Streams<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#BufferedInputStream_and_BufferedOutputStream\" >BufferedInputStream and BufferedOutputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#FilterInputStream_and_FilterOutputStream\" >FilterInputStream and FilterOutputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#SequenceInputStream\" >SequenceInputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#ObjectInputStream_and_ObjectOutputStream\" >ObjectInputStream and ObjectOutputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#LineNumberInputStream\" >LineNumberInputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#DataInputStream_and_DataOutputStream\" >DataInputStream and DataOutputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#PushbackInputStream\" >PushbackInputStream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#PrintStream\" >PrintStream<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#Concatenating_Input_Streams\" >Concatenating Input Streams<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/byte-streams\/#Wrapping_Streams_within_Streams\" >Wrapping Streams within Streams<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">If you wish to move something other than characters or strings through a stream, then this is the set of classes you will need to use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>java.io<\/strong><\/code>&nbsp;package contains the following set of&nbsp;<em>byte streams<\/em>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(Package&nbsp;<strong><code>java.io<\/code>)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Object<strong>\n    InputStream\n        ByteArrayInputStream\n        FileInputStream\n        FilterInputStream\n            BufferedInputStream\n            DataInputStream\n            LineNumberInputStream <\/strong><strong>(deprecated)<\/strong><strong>\n            PushbackInputStream\n        ObjectInputStream\n        PipedInputStream\n        SequenceInputStream\n        StringBufferInputStream <\/strong><strong>(deprecated)<\/strong><strong>\n    OutputStream\n        ByteArrayOutputStream\n        FileOutputStream\n        FilterOutputStream\n            BufferedOutputStream\n            DataOutputStream\n            PrintStream\n        ObjectOutputStream\n        PipedOutputStream<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_InputStream_Class\"><\/span>The InputStream Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>InputStream<\/strong><\/code>&nbsp;is the abstract base class (abstract superclass) of all input byte streams. It provides the following methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code>public int available()<br>throws IOException<\/code><\/td><td>Returns the number of bytes that can be read from this input stream without blocking. The <strong>available<\/strong> method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;returns 0. This method&nbsp;<em><strong>should<\/strong><\/em>&nbsp;be overridden by subclasses.<\/td><\/tr><tr><td><code>public void close() throws IOException<\/code><\/td><td>Closes this input stream and releases any system resources associated with the stream.<br>The <strong>close<\/strong> method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;does nothing.<\/td><\/tr><tr><td><code>public synchronized void mark(int readLimit)<br>throws IOException<\/code><\/td><td>Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.<br>The&nbsp;<code><strong>readlimit<\/strong><\/code>&nbsp;argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.<br>The <strong>mark<\/strong> method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;does nothing.<\/td><\/tr><tr><td><code>public boolean markSupported()<\/code><\/td><td>Tests if this input stream supports the mark and reset methods. The&nbsp;<code><strong>markSupported<\/strong><\/code>&nbsp;method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;returns false.<\/td><\/tr><tr><td><code>public int read(byte[] b)<br>throws IOException<\/code><\/td><td>Reads up to&nbsp;<code><strong>b.length<\/strong><\/code>&nbsp;bytes of data from this input stream into an array of bytes.<br>The&nbsp;<strong>read<\/strong>&nbsp;method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;calls the&nbsp;<strong>read<\/strong>&nbsp;method of three arguments with the arguments&nbsp;<code>b<\/code>,&nbsp;<code>0<\/code>, and&nbsp;<code>b.length<\/code>.<\/td><\/tr><tr><td><code>public abstract int read()<br>throws IOException<\/code><\/td><td>Reads the next byte of data from this input stream. The value byte is returned as an&nbsp;<code><strong>int<\/strong><\/code>&nbsp;in the range&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>255<\/code>. If no byte is available because the end of the stream has been reached, the value&nbsp;<code>-1<\/code>&nbsp;is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.<br>A subclass must provide an implementation of this method.<\/td><\/tr><tr><td><code>public int read(byte[] b, int off, int len)<br>throws IOException<\/code><\/td><td>Reads up to&nbsp;<code><strong>len<\/strong><\/code>&nbsp;bytes of data from this input stream into an array of bytes. This method blocks until some input is available. If the argument&nbsp;<code>b<\/code>&nbsp;is null, a&nbsp;<code><strong>NullPointerException<\/strong><\/code>&nbsp;is thrown.<br>The&nbsp;<strong>read<\/strong>&nbsp;method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;reads a single byte at a time using the&nbsp;read&nbsp;method of zero arguments to fill in the array. Subclasses are encouraged to provide a more efficient implementation of this method.<\/td><\/tr><tr><td><code>public synchronized void reset()<br>throws IOException<\/code><\/td><td>Repositions this stream to the position at the time the&nbsp;<code><strong>mark<\/strong><\/code>&nbsp;method was last called on this input stream.<br>The&nbsp;<strong>reset<\/strong>&nbsp;method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;throws an&nbsp;<code><strong>IOException<\/strong><\/code>, because input streams, by default, do not support&nbsp;<code><strong>mark<\/strong><\/code>&nbsp;and&nbsp;<code><strong>reset<\/strong><\/code>.<br>Stream marks are intended to be used in situations where you need to read ahead a little to see what&#8217;s in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parser, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails, which, if it happens within&nbsp;<code><strong>readlimit<\/strong><\/code>&nbsp;bytes, allows the outer code to reset the stream and try another parser.<\/td><\/tr><tr><td><code>public long skip(long n) throws IOException<\/code><\/td><td>Skips over and discards&nbsp;<code>n<\/code>&nbsp;bytes of data from this input stream. The&nbsp;<code><strong>skip<\/strong><\/code>&nbsp;method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.<br>The&nbsp;<code><strong>skip<\/strong><\/code>&nbsp;method of&nbsp;<code><strong>InputStream<\/strong><\/code>&nbsp;creates a byte array of length&nbsp;<code>n<\/code>&nbsp;and then reads into it until&nbsp;<code>n<\/code>&nbsp;bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method.<\/td><\/tr><tr><td><code>public InputStream()<\/code><\/td><td>Creates an instance of&nbsp;<code><strong>InputStream<\/strong><\/code>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>Note:<\/strong><\/em>&nbsp;<code>System.in<\/code>&nbsp;is an instance of&nbsp;<code><strong>InputStream<\/strong><\/code>.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_OutputStream_Class\"><\/span>The OutputStream Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>OutputStream<\/strong><\/code>&nbsp;is the base class (superclass) of all output byte streams. It provides the following methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code>public void close()<br>throws IOException<\/code><\/td><td>Closes this output stream and releases any system resources associated with this stream.<br>The&nbsp;<code><strong>close<\/strong><\/code>&nbsp;method of&nbsp;<code><strong>OutputStream<\/strong><\/code>&nbsp;does nothing.<\/td><\/tr><tr><td><code>public void flush()<br>throws IOException<\/code><\/td><td>Flushes this output stream and forces any buffered output bytes to be written out.<br>The&nbsp;<code><strong>flush<\/strong><\/code>&nbsp;method of&nbsp;<code><strong>OutputStream<\/strong><\/code>&nbsp;does nothing.<\/td><\/tr><tr><td><code>public void write(byte[] b)<br>throws IOException<\/code><\/td><td>Writes&nbsp;<code><strong>b.length<\/strong><\/code>&nbsp;bytes from the specified byte array to this output stream.<br>The&nbsp;<code><strong>write<\/strong><\/code>&nbsp;method of&nbsp;<code><strong>OutputStream<\/strong><\/code>&nbsp;calls the&nbsp;write&nbsp;method of three arguments with the three arguments&nbsp;<code>b<\/code>,&nbsp;<code>0<\/code>, and&nbsp;<code>b.length<\/code>.<\/td><\/tr><tr><td><code>public abstract void write(byte[] b, int off, int len)<br>throws IOException<\/code><\/td><td>Writes&nbsp;<code><strong>len<\/strong><\/code>&nbsp;bytes from the specified byte array starting at offset&nbsp;<code><strong>off<\/strong><\/code>&nbsp;to this output stream.<br>The&nbsp;<code><strong>write<\/strong><\/code>&nbsp;method of&nbsp;<code><strong>OutputStream<\/strong><\/code>&nbsp;calls the&nbsp;<code><strong>write<\/strong><\/code>&nbsp;method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation.<\/td><\/tr><tr><td><code>public abstract void write(int b)<br>throws IOException<\/code><\/td><td>Writes the specified byte to this output stream.<br>Subclasses of&nbsp;<code><strong>OutputStream<\/strong><\/code>&nbsp;must provide an implementation for this method.<\/td><\/tr><tr><td><code>public OutputStream()<\/code><\/td><td>Creates an instance of&nbsp;<code><strong>OutputStream<\/strong><\/code>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Data_SourceSink_Byte_Streams\"><\/span>Data Source\/Sink Byte Streams<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We can classify Data Source\/Sink byte streams as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th><strong>Data Source\/Sink<\/strong><\/th><th><strong>Readers<\/strong><\/th><th><strong>Writers<\/strong><\/th><\/tr><tr><td><strong>Memory<\/strong><\/td><td><code>ByteArrayInputStream<br>StringBufferInputStream<\/code><\/td><td><code>ByteArrayOutputStream<\/code><\/td><\/tr><tr><td><strong>Pipe<\/strong><\/td><td><code>PipedInputStream<\/code><\/td><td><code>PipedOutputStream<\/code><\/td><\/tr><tr><td><strong>File<\/strong><\/td><td><code>FileInputStream<\/code><\/td><td><code>FileOutputStream<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of writing bytes to a file, and reading them back:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.FileInputStream;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\n\npublic class FileBytesReadWrite\n{\n    public static void main(String&#x5B;] args)\n    {\n        try\n        {\n            \/\/ Write the file out\n            FileOutputStream out = new FileOutputStream(&quot;rawdata.lst&quot;);\n            for (int count = 0; count &lt; m_counts.length; count++)\n            {\n                out.write(m_counts&#x5B;count]);\n            }\n            out.close();\n            \n            \/\/ Read the file back in. and print its contents out\n            FileInputStream in = new FileInputStream(&quot;rawdata.lst&quot;);\n            while (true)\n            {\n                int b = in.read();\n                if (b == -1)\n                    break;\t\t\/\/ End of stream\n                System.out.println(&quot;Byte value = &quot; + b);\n            }\n        }\n        catch (IOException e)\n        {\n            e.printStackTrace();\n        }\n    }\n    \n    \/\/\/\/ Private data \/\/\/\/\n    private static final byte&#x5B;] m_counts =\n    {\n        23, 57, 45, 30, \n    };\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Notice how easy it is to change this to write to, and read back from, a byte array stream, by changing <code>FileInputStream<\/code> and&nbsp;<code>FileOutputStream<\/code> to <code>ByteArrayInputStream<\/code> and <code>ByteArrayOutputStream<\/code>, as in the following example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.ByteArrayInputStream;\nimport java.io.ByteArrayOutputStream;\nimport java.io.IOException;\n\npublic class ArrayBytesReadWrite\n{\n    public static void main(String&#x5B;] args)\n    {\n        try\n        {\n            \/\/ Copy the byte array to a ByteArrayOutputStream\n            ByteArrayOutputStream out = new ByteArrayOutputStream();\n            for (int count = 0; count &lt; m_counts.length; count++)\n            {\n                out.write(m_counts&#x5B;count]);\n            }\n            out.close();\n            \n            \/\/ Get the byte array from the above output stream, \n            \/\/ and print its contents out\n            ByteArrayInputStream in = new ByteArrayInputStream(\n\t\t\t\t\tout.toByteArray());\n            while (true)\n            {\n                int b = in.read();\n                if (b == -1)\n                    break;\t\t\/\/ End of stream\n                System.out.println(&quot;Byte value = &quot; + b);\n            }\n        }\n        catch (IOException e)\n        {\n            e.printStackTrace();\n        }\n    }\n    \n    \/\/\/\/ Private data \/\/\/\/\n    private static final byte&#x5B;] m_counts =\n    {\n        23, 57, 45, 30, \n    };\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Processing_Byte_Streams\"><\/span>Processing Byte Streams<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s a set of&nbsp;<em>processing<\/em>&nbsp;byte streams that add a lot of value to the family of streams.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a table of <em>Processing Byte<\/em> streams:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th><strong>Process<\/strong><\/th><th><strong>Readers<\/strong><\/th><th><strong>Writers<\/strong><\/th><\/tr><tr><td><strong>Buffering<\/strong><\/td><td><code>BufferedInputStream<\/code><\/td><td><code>BufferedOutputStream<\/code><\/td><\/tr><tr><td><strong>Filtering<\/strong><\/td><td><code>FilterInputStream<\/code><\/td><td><code>FilterOutputStream<\/code><\/td><\/tr><tr><td><strong>Concatenation<\/strong><\/td><td><code>SequenceInputStream<\/code><\/td><td>&nbsp;<\/td><\/tr><tr><td><strong>Object Serialization<\/strong><\/td><td><code>ObjectInputStream<\/code><\/td><td><code>ObjectOutputStream<\/code><\/td><\/tr><tr><td><strong>Counting<\/strong><\/td><td><code>LineNumberInputStream&nbsp;<\/code><strong>(deprecated)<\/strong><\/td><td>&nbsp;<\/td><\/tr><tr><td><strong>Data Conversion<\/strong><\/td><td><code>DataInputStream<\/code><\/td><td><code>DataOutputStream<\/code><\/td><\/tr><tr><td><strong>Peeking Ahead<\/strong><\/td><td><code>PushbackInputStream<\/code><\/td><td>&nbsp;<\/td><\/tr><tr><td><strong>Printing<\/strong><\/td><td>&nbsp;<\/td><td><code>PrintStream<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"BufferedInputStream_and_BufferedOutputStream\"><\/span>BufferedInputStream and BufferedOutputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">These streams buffer data while reading or writing, which reduces the number of accesses required on the original data source\/sink. Buffered streams are in general more efficient &#8212; often&nbsp;<em>much<\/em>&nbsp;more efficient &#8212; than non-buffered streams, especially when reading from, or writing to, files.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FilterInputStream_and_FilterOutputStream\"><\/span>FilterInputStream and FilterOutputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">These are abstract classes that form a hierarchy of classes which perform a variety of filtering operations on data that is sent through them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"SequenceInputStream\"><\/span>SequenceInputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This is a class that allows you to concatenate multiple input streams together to form a single input stream.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ObjectInputStream_and_ObjectOutputStream\"><\/span>ObjectInputStream and ObjectOutputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">These classes are used to serialize Java objects. See&nbsp;<a href=\"https:\/\/bhiggs.x10hosting.com\/Courses\/HighOctaneJava\/ObjectPersistence\/objectStreams.htm#Object%20Streams%20and%20Serialization\">Object Streams and Serialization<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LineNumberInputStream\"><\/span>LineNumberInputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This class is useful for keeping track of line numbers while reading.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note:<\/em><\/strong>&nbsp;<code><strong>LineNumberInputStream<\/strong><\/code>&nbsp;is deprecated. This class incorrectly assumes that bytes adequately represent characters. As of JDK&nbsp;1.1, the preferred way to operate on character streams is via the new character-stream classes, which include a class for counting line numbers.<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"DataInputStream_and_DataOutputStream\"><\/span>DataInputStream and DataOutputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">These classes can read or write primitive Java datatypes in a machine-independent format. See&nbsp;<a href=\"https:\/\/bhiggs.x10hosting.com\/Courses\/HighOctaneJava\/InputOutput\/dataConversion.htm#Data%20Conversion%20Streams\">Data Conversion Streams<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PushbackInputStream\"><\/span>PushbackInputStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This class (a subclass of&nbsp;<code><strong>FilterReader<\/strong><\/code>) allows you to &#8220;unread&#8221; character(s) that have been read from a stream. This is very useful in some applications (such as language parsers and similar tools) which find it useful to &#8220;peek ahead&#8221; in order to determine what to do next.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PrintStream\"><\/span>PrintStream<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This class contains a number of convenient printing methods, such as&nbsp;<code><strong>print<\/strong><\/code>&nbsp;and&nbsp;<code><strong>println<\/strong><\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note:<\/em><\/strong>&nbsp;<code><strong>System.out<\/strong><\/code>&nbsp;and&nbsp;<code><strong>System.err<\/strong><\/code>&nbsp;are instances of&nbsp;<code><strong>PrintStream<\/strong><\/code>.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Concatenating_Input_Streams\"><\/span>Concatenating Input Streams<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The unlikely named\u00a0<code><strong>SequenceInputStream<\/strong><\/code>\u00a0is used to concatenate multiple input streams into a single input stream:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.FileInputStream;\nimport java.io.FileNotFoundException;\nimport java.io.InputStream;\nimport java.io.SequenceInputStream;\nimport java.io.IOException;\nimport java.util.Enumeration;\nimport java.util.NoSuchElementException;\n\n\/**\n *  This class is an example of how to concatenate \n *  input streams together.\n *\/\npublic class ConcatenateFiles\n{\n    \/**\n     *  Main entry point supplies a set of filenames to use\n     *  for concatenation.\n     *\/\n    public static void main(String&#x5B;] args)\n    {\n        MyFiles files = new MyFiles(args);\n        SequenceInputStream stream = \n                    new SequenceInputStream(files);\n        try\n        {\n            while (true)\n            {\n                \/\/ NOTE: Restricted to ISO Latin-1 characters!!!\n                int c = stream.read();\n                if (c == -1)\n                    break;\n                System.out.write(c);\n            }\n            stream.close();\n        }\n        catch (IOException e)\n        {\n            e.printStackTrace();\n        }\n    }\n}\n\n\/**\n *  This class provides a convenient Enumeration interface\n *  which the SequenceInputStream can use.\n *\/\nclass MyFiles implements Enumeration&lt;InputStream&gt;\n{\n    MyFiles(String&#x5B;] fileNames)\n    {\n        m_fileNames = fileNames;\n    }\n        \n    \/\/\/\/ Methods required to implement Enumeration\n        \n    public boolean hasMoreElements()\n    {\n        return (m_currentFile &lt; m_fileNames.length);\n    }\n        \n    public InputStream nextElement()\n    {\n        if (!hasMoreElements())\n            throw new NoSuchElementException(&quot;No more files&quot;);\n                \n        InputStream fileStream = null;\n        String fileName = m_fileNames&#x5B;m_currentFile++];\n        try\n        {\n            fileStream = new FileInputStream(fileName);\n        }\n        catch (FileNotFoundException e)\n        {\n            throw new NoSuchElementException(&quot;Can&#039;t open file &quot; + \n                                                fileName);\n        }\n            \n        return fileStream;\n    }\n        \n    \/\/\/ Private data \/\/\/\n    private String&#x5B;] m_fileNames = null;\n    private int      m_currentFile = 0;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Wrapping_Streams_within_Streams\"><\/span>Wrapping Streams within Streams<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In order to use any one of the the above set of processing streams, you must wrap streams within other streams.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"329\" src=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-content\/uploads\/2021\/01\/ByteWrapStreams.gif\" alt=\"\" class=\"wp-image-119\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You can wrap as many streams within streams as you wish, to accomplish your task. Here is an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.BufferedInputStream;\nimport java.io.FileInputStream;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\n\npublic class BufferedBytesFileRead\n{\n    \/**\n     *  This main method expects a single argument: a filename.\n     *  It will attempt to read the file, and print its contents.\n     *\/\n    public static void main(String&#x5B;] args)\n    {\n        try\n        {\n            \/\/ Read a file in. and print its contents out\n            BufferedInputStream in = new BufferedInputStream( \n                                            new FileInputStream(\n\t\t\t\t\t\targs&#x5B;0]));\n            while (true)\n            {\n                \/\/ NOTE: Restricted to ISO-Latin-1 characters!!!\n                int c = in.read();\n                if (c == -1)\n                    break;\n                System.out.print((char)c);  \/\/ Cast required\n            }\n        }\n        catch (IOException e)\n        {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>If you wish to move something other than characters or strings through a stream, then this is the set of classes you will need to use. The&nbsp;java.io&nbsp;package contains the following set of&nbsp;byte streams: (Package&nbsp;java.io) Object InputStream ByteArrayInputStream FileInputStream FilterInputStream BufferedInputStream DataInputStream LineNumberInputStream (deprecated) PushbackInputStream ObjectInputStream PipedInputStream SequenceInputStream StringBufferInputStream (deprecated) OutputStream ByteArrayOutputStream FileOutputStream FilterOutputStream BufferedOutputStream DataOutputStream [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":67,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","ocean_front_end_style_editor":"no","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-108","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":5,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/108\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/108\/revisions\/120"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/67"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/media?parent=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}