{"id":89855,"date":"2026-06-13T18:13:21","date_gmt":"2026-06-13T12:43:21","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89855"},"modified":"2026-06-13T18:16:45","modified_gmt":"2026-06-13T12:46:45","slug":"java-pushbackinputstream","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/","title":{"rendered":"Java PushbackInputStream"},"content":{"rendered":"<p>Java is used in various applications. It also used in real time projects. Lets we discuss about the PushbackInputStream.<\/p>\n<h2>Explanation<\/h2>\n<ul>\n<li>An input stream can have a byte read and then &#8220;pushed back&#8221; (or restored) to the stream by using pushback. The PushbackInputStream class.<br \/>\nconcept was implemented.<\/li>\n<li>It offers a way to &#8220;peek&#8221; at input stream data without interfering with it.<\/li>\n<li>FilterInputStream is extended by it.<\/li>\n<\/ul>\n<h3>Class Declaration :<\/h3>\n<ul>\n<li>public class PushbackInputStream extends<\/li>\n<\/ul>\n<h3>Fields :<\/h3>\n<ul>\n<li><strong>protected byte[] buf:<\/strong> This buffer is used for pushback.<\/li>\n<li><strong>protected int pos :<\/strong> This is where the next byte will be read from in the pushback buffer.<\/li>\n<li><strong>protected InputStream in :<\/strong> It needs to be filtered in the input stream.<\/li>\n<\/ul>\n<h3>Table<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Method\u00a0<\/strong><\/td>\n<td><strong>Description\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">protected byte[]<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Buf<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The pushback buffer\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">protected int<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pos<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The next byte is read the position within the\u00a0 pushback buffer.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>How to create PushbackInput:<\/h3>\n<p>You must first build an instance of a Java PushbackInputStream before you can utilize it. This is an illustration of how to make a PushbackInputStream in Java:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PushbackInputStream input =new PushbackInputStream (new FileInputStream )<\/pre>\n<h4>Read Bytes :<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int aByte = pushbackInputStream.read();\r\n\r\nwhile(aByte != -1) {\r\n        byte byteRead = (byte) aByte;\r\n\r\n    aByte = pushbackInputStream.read();\r\n}<\/pre>\n<h4>Push a byte :<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int aByte = pushbackInputStream.read();\r\n\r\npushbackInputStream.unread(aByte);\r\n\r\naByte = pushbackInputStream.read()<\/pre>\n<h3>Methods Table :<\/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 available()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The input stream thait.number of bytes that can be read and returned it.<\/span><\/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;\">boolean markSupported()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It reads the boolean that supported from the input stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void mark(int readlimit)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to indicate where the input stream is at that moment.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">long skip(long x)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to bypass and eliminate x bytes of information.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void unread(int b)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">By copying the byte to the pushback buffer, it is utilized to push the byte back.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void unread(byte[] b)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It copies the array of bytes to the pushback buffer in order to push it back.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void reset()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The input stream is reset using it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void close()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The input stream is closed using it.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Example :<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;  \r\npublic class InputStreamExample {  \r\npublic static void main(String[] args)\r\n{  \r\n          String srg = \"1##2#34##12\";  \r\n          byte ary[] = srg.getBytes();  \r\n          ByteArrayInputStream array = new ByteArrayInputStream(ary);  \r\n          PushbackInputStream push = new PushbackInputStream(array);  \r\n          int i;        \r\n              while( (i = push.read())!= -1) {  \r\n                  if(i == '#') {  \r\n                      int j;  \r\n                      if( (j = push.read()) == '#'){  \r\n                           System.out.print(\"**\");  \r\n                      }else {  \r\n                          push.unread(j);  \r\n                          System.out.print((char)i);  \r\n                      }  \r\n                  }else {  \r\n                              System.out.print((char)i);  \r\n                  }  \r\n             }        \r\n  }   \r\n}<\/pre>\n<p><strong>Output :<\/strong><br \/>\n1**2#34**#12<\/p>\n<h4>Example 2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.ByteArrayInputStream;\r\nimport java.io.IOException;\r\nimport java.io.PrintWriter;\r\nimport java.io.PushbackInputStream;\r\n \r\n \r\npublic class PushbackInputStream\r\n{\r\n    public static void main(String arg[]) \r\n    {\r\n        PrintWriter pw = new PrintWriter(System.out, true);\r\n        String str = \"Hey geeks \";\r\n        byte b[] = str.getBytes();\r\n        ByteArrayInputStream bout = new ByteArrayInputStream(b);\r\n        PushbackInputStream push = new PushbackInputStream(bout);\r\n         \r\n                pw.println(\"Bytes: \" + push.available());\r\n         \r\n                pw.println(\"Supported :\" + push.markSupported());\r\n         \r\n        pw.close();\r\n    }\r\n}<\/pre>\n<p><strong>Output :<\/strong><br \/>\n<strong>Bytes :<\/strong>10<br \/>\n<strong>Supported :<\/strong>False<\/p>\n<h3>Constructors:<\/h3>\n<ul>\n<li><strong>PushbackInputStream(InputStream in) :<\/strong>One byte can be returned to the input stream thanks to the stream object that is created in this way.<\/li>\n<li><strong>PushbackInputStream(InputStream in, int numBytes):<\/strong> This produces a stream with a numBytes-long pushback buffer. As a result, the input stream can receive several bytes back.<\/li>\n<\/ul>\n<h3>Difference between PushbackInputStream and BufferedInputStream:<\/h3>\n<ul>\n<li>When buffering is not supported by your input stream, use PushbackInputStream.<\/li>\n<li>After reading, you return it to the stream. We just use BufferedInputStream to call reset() after reading the 10 bytes with read(10) and marking(10).<\/li>\n<\/ul>\n<h4>Uses :<\/h4>\n<ul>\n<li>By adding the capacity to buffer input and support the mark and reset methods, a BufferedInputStream enhances the usefulness of another input stream.<\/li>\n<li>When BufferedInputStream is created, the internal buffer array is generated during the time.<\/li>\n<\/ul>\n<h3>Closing the PushbackInput:<\/h3>\n<ul>\n<li>It is important to remember to close a Java PushbackInputStream once you have completed reading its contents.<\/li>\n<li>The InputStream instance that a PushbackInputStream is reading from will also close when a PushbackInputStream is closed.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pushbackInputStream.close();<\/pre>\n<h3>Conclusion<\/h3>\n<p>A Java class called PushbackInputStream lets you &#8220;push back&#8221; or unread some bytes into the stream. This comes in handy when you read a few bytes from an input stream and determine that you would like to &#8220;unread&#8221; the bytes by pushing them back into the stream so that they can be read once more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is used in various applications. It also used in real time projects. Lets we discuss about the PushbackInputStream. Explanation An input stream can have a byte read and then &#8220;pushed back&#8221; (or restored)&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447383,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,436,437,327,5538,250,438,5578,439],"class_list":["post-89855","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-pushbackinputstream","tag-java-pushbackinputstream-class","tag-java-tutorials","tag-java-tutorials-for-beginners","tag-learn-java","tag-pushbackinputstream","tag-pushbackinputstream-class-in-java","tag-pushbackinputstream-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java PushbackInputStream - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java PushbackInputStream It gives detailed explanation about input stream Java is used in various applications It used in real time projects.\" \/>\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-pushbackinputstream\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java PushbackInputStream - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java PushbackInputStream It gives detailed explanation about input stream Java is used in various applications It used in real time projects.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/\" \/>\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=\"2026-06-13T12:43:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-13T12:46:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-pushbackinputstream.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java PushbackInputStream - TechVidvan","description":"Java PushbackInputStream It gives detailed explanation about input stream Java is used in various applications It used in real time projects.","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-pushbackinputstream\/","og_locale":"en_US","og_type":"article","og_title":"Java PushbackInputStream - TechVidvan","og_description":"Java PushbackInputStream It gives detailed explanation about input stream Java is used in various applications It used in real time projects.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2026-06-13T12:43:21+00:00","article_modified_time":"2026-06-13T12:46:45+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-pushbackinputstream.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java PushbackInputStream","datePublished":"2026-06-13T12:43:21+00:00","dateModified":"2026-06-13T12:46:45+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/"},"wordCount":530,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-pushbackinputstream.webp","keywords":["java","java pushbackinputstream","java pushbackinputstream class","java tutorials","java tutorials for beginners","Learn Java","pushbackinputstream","pushbackinputstream class in java","pushbackinputstream in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/","url":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/","name":"Java PushbackInputStream - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-pushbackinputstream.webp","datePublished":"2026-06-13T12:43:21+00:00","dateModified":"2026-06-13T12:46:45+00:00","description":"Java PushbackInputStream It gives detailed explanation about input stream Java is used in various applications It used in real time projects.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-pushbackinputstream.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-pushbackinputstream.webp","width":1200,"height":628,"caption":"java pushbackinputstream"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackinputstream\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java PushbackInputStream"}]},{"@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\/89855","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=89855"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89855\/revisions"}],"predecessor-version":[{"id":448177,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89855\/revisions\/448177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447383"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}