{"id":90026,"date":"2024-10-17T18:00:42","date_gmt":"2024-10-17T12:30:42","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=90026"},"modified":"2024-10-17T17:47:38","modified_gmt":"2024-10-17T12:17:38","slug":"java-sequenceinputstream-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/","title":{"rendered":"Java SequenceInputStream Class with Examples"},"content":{"rendered":"<p>Sequence As sources, InputStream accepts two or more InputStream objects. It reads from one source after another in the order specified. When it has finished reading from the first InputStream, it will automatically begin reading from the second. This process is repeated until all of the source streams have been read.<\/p>\n<h2>Object Creation<\/h2>\n<p><strong>We can use two InputStream objects to initialize a SequenceInputStream:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">first InputStream = new FileInputStream(file1);\r\nsecond InputStream = new FileInputStream(file2);\r\nInputStream SequenceInputStream SequenceSequenceInputStream(first, second) = new SequenceInputStream(first, second);\r\nCopy<\/pre>\n<p><strong>We can also use an Enumeration of InputStream objects to create it:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Vector&lt;InputStream&gt; inputStreams = new Vector&lt;&gt;();\r\nfor (String fileName: fileNames) {\r\n    inputStreams.add(new FileInputStream(fileName));\r\n}\r\nsequenceInputStream = new SequenceInputStream(inputStreams.elements());<\/pre>\n<h3>Reading from a stream:<\/h3>\n<p>Reading From the Stream SequenceTo read from input sources, InputStream provides two simple methods. The first method reads a single byte, while the second reads an array of bytes.<\/p>\n<p><strong>To read a single byte of data, we use the read() method:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sequenceInputStream.read(); int byteValue<\/pre>\n<p>The read method in the preceding example returns the next byte (0 &#8211; 255) value from the stream. If the stream is exhausted, it returns -1.<\/p>\n<h4>Example:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.ByteArrayInputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.io.SequenceInputStream;\r\n\r\npublic class TechVidvan {\r\n\r\n    public static void main(String[] args) {\r\n        try (InputStream first = new ByteArrayInputStream(\"Learning Java in \".getBytes());\r\n             InputStream second = new ByteArrayInputStream(\"TechVidvan Tutorials\".getBytes());\r\n             SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second)) {\r\n\r\n            StringBuilder stringBuilder = new StringBuilder();\r\n            int byteValue;\r\n\r\n            while ((byteValue = sequenceInputStream.read()) != -1) {\r\n                stringBuilder.append((char) byteValue);\r\n            }\r\n\r\n            System.out.println(\"Result: \" + stringBuilder.toString());\r\n\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nResult: Learning Java in TechVidvan Tutorials<\/p>\n<h4>Notes:<\/h4>\n<p>public int available() raises an exception IOException Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without the following method invocation for the current underlying input stream blocking. The subsequent invocation could be from the same thread or a different one. A single read or skip of these many bytes will not cause a blocking error, but it may read or skip fewer bytes.<\/p>\n<p>This method calls the current underlying input stream&#8217;s available method and returns the result.<\/p>\n<h3>Overrides:<\/h3>\n<p>These are available in class. InputStream Returns: an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking, or 0 if the input stream has been closed by invoking closeInputStream().<\/p>\n<p>IOException is thrown by public void close().<\/p>\n<p>This function closes this input stream and releases any associated system resources. A closed SequenceInputStream cannot be used for input or reopened.<\/p>\n<p>If this stream was created from an enumeration, the enumeration&#8217;s remaining elements are requested and closed before the close method returns.<\/p>\n<ul>\n<li>close in interface is specified Closeable<\/li>\n<li>close in interface is specified AutoCloseable<\/li>\n<\/ul>\n<p><strong>Overrides:<\/strong> close in class InputStream Throws: IOException &#8211; if there is an I\/O error.<\/p>\n<h3>Constructors of SequenceInputStream class in Java<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Constructor<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">The subsequent byte is read from the input stream.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Creates a new input stream by sequentially reading the data from two input streams, s1 and s2.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Enumeration SequenceInputStream<\/span><\/td>\n<td><span style=\"font-weight: 400;\">reads the data of an enumeration type InputStream to create a new input stream.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Methods of SequenceInputStream class in Java<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Method<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int read()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It reads the subsequent data byte from the input stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int read(byte[] ary, int off, int len)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The next data byte is read from the input stream by it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int available()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It accepts the subsequent data byte from the input stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void close()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It reads the subsequent byte from the input stream.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Method Details:<\/h3>\n<p>1. IOException is thrown by public int available().<\/p>\n<p>2. Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without the following method invocation for the current underlying input stream blocking. The subsequent invocation could be from the same thread or a different one. A single read or skip of these many bytes will not cause a blocking error, but it may read or skip fewer bytes.<\/p>\n<p>3. This method calls the current underlying input stream&#8217;s available method and returns the result.<\/p>\n<p><strong>4. Overrides:<\/strong> These are available in class. InputStream Returns: an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking, or 0 if the input stream has been closed by invoking closeInputStream().<\/p>\n<p><strong>5. Constructor Detail:<\/strong><\/p>\n<p>SequenceInputStream public<\/p>\n<p>SequenceInputStream(Enumeration? extends InputStream&gt; e) creates a new SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects with the run-time type InputStream. The enumerated input streams will be read to provide the bytes to be read from this SequenceInputStream. After the enumeration&#8217;s input streams have been exhausted, they are closed by calling their close method.<\/p>\n<ul>\n<li><strong>Parameters:<\/strong><\/li>\n<li>e &#8211; an array of input streams.<\/li>\n<li><strong>Also see:<\/strong> enumeration<\/li>\n<\/ul>\n<h3>SequenceInputStream:<\/h3>\n<p>public SequenceInputStream(InputStream s1, InputStream s2) creates a new SequenceInputStream by remembering the two arguments that will be read in order, s1 and then s2, to provide the bytes to be read from this SequenceInputStream.<\/p>\n<ul>\n<li>s1 is the first input stream to be read.<\/li>\n<li>s2 denotes the second input stream to be read.<\/li>\n<\/ul>\n<h4>Read<\/h4>\n<p>public int read() raises an exception IOException<\/p>\n<p>This function reads the next byte of data from this input stream. The byte is returned as an int with a value between 0 and 255. If no byte is available because the stream has reached its end, the value -1 is returned. This method will block until input data is available, the stream&#8217;s end is detected, or an exception is thrown.<\/p>\n<p>This method attempts to read one character from the currently selected substream. If it reaches the end of the stream, it calls the current substream&#8217;s close method and starts reading from the next substream.<\/p>\n<p><strong>Specified by:<\/strong> class reading InputStream returns the next byte of data, or -1 if the stream has reached its end.<\/p>\n<h4>close:<\/h4>\n<p>public void close() throws an IOException<\/p>\n<p>This function closes this input stream and releases any associated system resources. A closed SequenceInputStream cannot be used for input or reopened.<\/p>\n<p>If this stream was created from an enumeration, the enumeration&#8217;s remaining elements are requested and closed before the close method returns.<\/p>\n<ul>\n<li>close in interface is specified Closeable<\/li>\n<li>close in interface is specified AutoCloseable<\/li>\n<\/ul>\n<p><strong>Overrides:<\/strong> close in class InputStream Throws: IOException &#8211; if there is an I\/O error.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this short article, we&#8217;ve seen how to work with SequenceInputStream. It simply merges all underlying input streams into a single stream.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sequence As sources, InputStream accepts two or more InputStream objects. It reads from one source after another in the order specified. When it has finished reading from the first InputStream, it will automatically begin&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447218,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,361,362,5679,299,263,250,363,364],"class_list":["post-90026","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-sequenceinputstream","tag-java-sequenceinputstream-class","tag-java-sequenceinputstream-class-with-examples","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java","tag-sequenceinputstream","tag-sequenceinputstream-class-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java SequenceInputStream Class with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java SequenceInputStream Class reads from one source after another in the order specified. It simply merges all underlying input streams into a single stream.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java SequenceInputStream Class with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java SequenceInputStream Class reads from one source after another in the order specified. It simply merges all underlying input streams into a single stream.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-17T12:30:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-sequenceinputstream.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java SequenceInputStream Class with Examples - TechVidvan","description":"Java SequenceInputStream Class reads from one source after another in the order specified. It simply merges all underlying input streams into a single stream.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/","og_locale":"en_US","og_type":"article","og_title":"Java SequenceInputStream Class with Examples - TechVidvan","og_description":"Java SequenceInputStream Class reads from one source after another in the order specified. It simply merges all underlying input streams into a single stream.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-10-17T12:30:42+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-sequenceinputstream.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java SequenceInputStream Class with Examples","datePublished":"2024-10-17T12:30:42+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/"},"wordCount":971,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-sequenceinputstream.webp","keywords":["java","java sequenceinputstream","java sequenceinputstream class","java sequenceinputstream class with examples","Java Tutorial","java tutorial for beginners","Learn Java","sequenceinputstream","sequenceinputstream class in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/","name":"Java SequenceInputStream Class with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-sequenceinputstream.webp","datePublished":"2024-10-17T12:30:42+00:00","description":"Java SequenceInputStream Class reads from one source after another in the order specified. It simply merges all underlying input streams into a single stream.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-sequenceinputstream.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-sequenceinputstream.webp","width":1200,"height":628,"caption":"java sequenceinputstream"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-sequenceinputstream-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java SequenceInputStream Class with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=90026"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90026\/revisions"}],"predecessor-version":[{"id":447742,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90026\/revisions\/447742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447218"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=90026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=90026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=90026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}