{"id":1292,"date":"2022-02-27T06:01:45","date_gmt":"2022-02-27T06:01:45","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1292"},"modified":"2022-02-27T06:01:45","modified_gmt":"2022-02-27T06:01:45","slug":"byte-array-input-stream-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/byte-array-input-stream-class\/","title":{"rendered":"Byte Array Input Stream Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java ByteArrayInputStream and its methods with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>ByteArrayInputStream<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package can be used to read an array of input data (in bytes).<\/p>\n\n\n\n<p>It extends the&nbsp;<code>InputStream<\/code>&nbsp;abstract class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-bytearrayinputstream.png\" alt=\"The ByteArrayInputStream is a subclass of the Java InputStream.\" title=\"Java ByteArrayInputStream Class\"\/><\/figure>\n\n\n\n<p><strong>Note<\/strong>: In&nbsp;<code>ByteArrayInputStream<\/code>, the input stream is created using the array of bytes. It includes an internal array to store data of that particular byte array.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create a ByteArrayInputStream<\/h2>\n\n\n\n<p>In order to create a byte array input stream, we must import the&nbsp;<code>java.io.ByteArrayInputStream<\/code>&nbsp;package first. Once we import the package, here is how we can create an input stream.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates a ByteArrayInputStream that \nreads entire array\nByteArrayInputStream input = new ByteArrayInputStream(byte&#91;] arr);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an input stream that reads entire data from the&nbsp;<code>arr<\/code>&nbsp;array. However, we can also create the input stream that reads only some data from the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates a ByteArrayInputStream that reads a \nportion of array\nByteArrayInputStream input = new ByteArrayInputStream(byte&#91;] arr, int start, int length);\n<\/code><\/pre>\n\n\n\n<p>Here the input stream reads the number of bytes equal to&nbsp;<var>length<\/var>&nbsp;from the array starting from the&nbsp;<var>start<\/var>&nbsp;position.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of ByteArrayInputStream<\/h2>\n\n\n\n<p>The&nbsp;<code>ByteArrayInputStream<\/code>&nbsp;class provides implementations for different methods present in the&nbsp;<code>InputStream<\/code>&nbsp;class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"read\">read() Method<\/h3>\n\n\n\n<ul><li><code>read()<\/code>&nbsp;&#8211; reads the single byte from the array present in the input stream<\/li><li><code>read(byte[] array)<\/code>&nbsp;&#8211; reads bytes from the input stream and stores in the specified array<\/li><li><code>read(byte[] array, int start, int length)<\/code>&nbsp;&#8211; reads the number of bytes equal to&nbsp;<var>length<\/var>&nbsp;from the stream and stores in the specified array starting from the position&nbsp;<var>start<\/var><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example: ByteArrayInputStream to read data<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.ByteArrayInputStream;\n\npublic class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ Creates an array of byte\n    byte&#91;] array = {1, 2, 3, 4};\n\n    try {\n      ByteArrayInputStream input = new ByteArrayInputStream(array);\n\n      System.out.print(\"The bytes read from the input stream: \");\n\n      for(int i= 0; i &lt; array.length; i++) {\n\n        \/\/ Reads the bytes\n        int data = input.read();\n        System.out.print(data + \", \");\n      }\n      input.close();\n    }\n\n    catch(Exception e) {\n      e.getStackTrace();\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The bytes read from the input stream: 1, 2, 3, 4,\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a byte array input stream named&nbsp;<code>input<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ByteArrayInputStream input = new ByteArrayInputStream(array);\n<\/code><\/pre>\n\n\n\n<p>Here, the input stream includes all the data from the specified array. To read data from the input stream, we have used the&nbsp;<code>read()<\/code>&nbsp;method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"available\">available() Method<\/h3>\n\n\n\n<p>To get the number of available bytes in the input stream, we can use the&nbsp;<code>available()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.ByteArrayInputStream;\n\npublic class Main {\n\n  public static void main(String args&#91;]) {\n\n    \/\/ Creates an array of bytes\n    byte&#91;] array = { 1, 2, 3, 4 };\n\n    try {\n      ByteArrayInputStream input = new ByteArrayInputStream(array);\n\n      \/\/ Returns the available number of bytes\n      System.out.println(\"Available bytes at the beginning: \" + input.available());\n\n      \/\/ Reads 2 bytes from the input stream\n      input.read();\n      input.read();\n\n      \/\/ Returns the available number of bytes\n      System.out.println(\"Available bytes at the end: \" + input.available());\n\n      input.close();\n    }\n\n    catch (Exception e) {\n      e.getStackTrace();\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Available bytes at the beginning: 4\nAvailable bytes at the end: 2\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example,<\/p>\n\n\n\n<ol><li>We have used the&nbsp;<code>available()<\/code>&nbsp;method to check the number of available bytes in the input stream.<\/li><li>We have then used the&nbsp;<code>read()<\/code>&nbsp;method 2 times to read 2 bytes from the input stream.<\/li><li>Now, after reading the 2 bytes, we have checked the available bytes. This time the available bytes decreased by 2.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"skip\">skip() Method<\/h3>\n\n\n\n<p>To discard and skip the specified number of bytes, we can use the&nbsp;<code>skip()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.ByteArrayInputStream;\n\npublic class Main {\n\n  public static void main(String args&#91;]) {\n\n    \/\/ Create an array of bytes\n    byte&#91;] array = { 1, 2, 3, 4 };\n    try {\n      ByteArrayInputStream input = new ByteArrayInputStream(array);\n\n      \/\/ Using the skip() method\n      input.skip(2);\n      System.out.print(\"Input stream after skipping 2 bytes: \");\n\n      int data = input.read();\n      while (data != -1) {\n        System.out.print(data + \", \");\n        data = input.read();\n      }\n\n      \/\/ close() method\n      input.close();\n    }\n\n    catch (Exception e) {\n      e.getStackTrace();\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Input stream after skipping 2 bytes: 3, 4,\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the&nbsp;<var>skip()<\/var>&nbsp;method to skip 2 bytes of data from the input stream. Hence&nbsp;<var>1<\/var>&nbsp;and&nbsp;<var>2<\/var>&nbsp;are not read from the input stream.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">close() Method<\/h3>\n\n\n\n<p>To close the input stream, we can use the&nbsp;<code>close()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>However, the&nbsp;<code>close()<\/code>&nbsp;method has no effect in&nbsp;<code>ByteArrayInputStream<\/code>&nbsp;class. We can use the methods of this class even after the&nbsp;<code>close()<\/code>&nbsp;method is called.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"other-methods\">Other Methods Of ByteArrayInputStream<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Descriptions<\/th><\/tr><tr><td><code>finalize()<\/code><\/td><td>ensures that the <code>close()<\/code> method is called<\/td><\/tr><tr><td><code>mark()<\/code><\/td><td>marks the position in input stream up to which data has been read<\/td><\/tr><tr><td><code>reset()<\/code><\/td><td>returns the control to the point in the input stream where the mark was set<\/td><\/tr><tr><td><code>markSupported()<\/code><\/td><td>checks if the input stream supports <code>mark()<\/code> and <code>reset()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java ByteArrayInputStream and its methods with the help of examples. The&nbsp;ByteArrayInputStream&nbsp;class of the&nbsp;java.io&nbsp;package can be used to read an array of input data (in bytes). It extends the&nbsp;InputStream&nbsp;abstract class. Note: In&nbsp;ByteArrayInputStream, the input stream is created using the array of bytes. It includes an internal array to store [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[483],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1292"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1292"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1292\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}