{"id":89815,"date":"2024-05-02T18:00:37","date_gmt":"2024-05-02T12:30:37","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89815"},"modified":"2024-05-02T18:32:33","modified_gmt":"2024-05-02T13:02:33","slug":"java-bytearrayoutputstream-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/","title":{"rendered":"Java ByteArrayOutputStream Class with Examples"},"content":{"rendered":"<p>Java is the most widely and useful language.<\/p>\n<p>An array of output data (in bytes) can be written using the Java.io package&#8217;s ByteArrayOutputStream class.<br \/>\nThe abstract class OutputStream is extended by it.<\/p>\n<h2>Explanation<\/h2>\n<ul>\n<li>Common data can be written into several files using the Java ByteArrayOutputStream class.<\/li>\n<li>The information is written to a byte array in this stream, which can then be written to other streams.<\/li>\n<li>Data is copied and sent to several streams via the ByteArrayOutputStream.<\/li>\n<\/ul>\n<h3>Declaration<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ByteArrayOutputStream extends OutputStream<\/pre>\n<h3>How to create:<\/h3>\n<ul>\n<li>The java.io.ByteArrayOutputStream package must be imported before we can create a byte array output stream. Here&#8217;s how to generate an output stream after importing the package.<\/li>\n<li>ByteArrayOutputStream out = new ByteArrayOutputStream();<\/li>\n<li>An output stream has been created that will write data to a byte array with a default size of 32 bytes. However, we can change the array&#8217;s initial size.<\/li>\n<li>ByteArrayOutputStream out = new ByteArrayOutputStream(int size);<\/li>\n<\/ul>\n<h3>Constructors:<\/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;\">ByteArrayOutputStream()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Generates a new output stream for the byte array, initially with a capacity of 32 bytes, but it can be expanded if needed.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">ByteArrayOutputStream(int size)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Generates a new output stream for a byte array with a buffer capacity of the given size, expressed in bytes.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Methods Table:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Methods<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int size()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to provide the buffer&#8217;s current size.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">byte[] toByteArray()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It&#8217;s employed to generate a freshly assigned byte array.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">String toString()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to transform the content into a string by decoding the character set by default on the platform.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">String toString(String charsetName)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to transform the data into a string by decoding bytes with a given charsetName.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void write(int b)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to write the given byte to the output stream of the byte array.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void write(byte[] b, int off, int len<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to write len bytes to the byte array output stream from a given byte array, beginning at the offset.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeTo(OutputStream out)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to write all of a byte array output stream&#8217;s content to the designated output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void reset()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to set a byte array output stream&#8217;s count field to zero.<\/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&#8217;s employed to terminate the ByteArrayOutputStream.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Fields Tables:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Modifier<\/strong><\/td>\n<td><strong>Description<\/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 data is stored in the 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;\">Count<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The buffer consists of number of valid bytes.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.java;  \r\nimport java.io.*;  \r\npublic class TechVidvan{  \r\npublic static void main(String args[])throws Exception{    \r\n      FileOutputStream fout1=new FileOutputStream(\"f1.txt\");    \r\n      FileOutputStream fout2=new FileOutputStream(\"f2.txt\");    \r\n        \r\n      ByteArrayOutputStream bout=new ByteArrayOutputStream();    \r\n      bout.write(65);    \r\n      bout.writeTo(fout1);    \r\n      bout.writeTo(fout2);    \r\n        \r\n      bout.flush();    \r\n      bout.close();\/\/has no effect    \r\n      System.out.println(\"TechVidvan Tutorials\");    \r\n     }    \r\n    }<\/pre>\n<p><strong>Output:<\/strong><br \/>\nTechVidvan Tutorials<\/p>\n<p>f1.txt:<br \/>\nA<br \/>\nf2.txt:<br \/>\nA<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\n\r\npublic class TechVidvan {\r\n\r\n    public static void main(String args[]) {\r\n        ByteArrayOutputStream aOutput = new ByteArrayOutputStream(10);\r\n\r\n        int targetSize = 10;\r\n\r\n        while (aOutput.size() &lt; targetSize) {\r\n            aOutput.write(\"TechVidvan\".getBytes(), 0, 10);\r\n        }\r\n\r\n        byte a[] = aOutput.toByteArray();\r\n        System.out.println(\"Printing the content\");\r\n\r\n        for (int x = 0; x &lt; a.length; x++) {\r\n            System.out.print((char) a[x] + \"   \");\r\n        }\r\n        System.out.println(\"  \");\r\n\r\n        int c;\r\n        ByteArrayInputStream aInput = new ByteArrayInputStream(a);\r\n        System.out.println(\"Converting into Upper case \");\r\n\r\n        for (int y = 0; y &lt; 1; y++) {\r\n            while ((c = aInput.read()) != -1) {\r\n                System.out.println(Character.toUpperCase((char) c));\r\n            }\r\n            aInput.reset();\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nPrinting the content<br \/>\nT e c h V i d v a n<br \/>\nConverting into Upper case<br \/>\nT<br \/>\nE<br \/>\nC<br \/>\nH<br \/>\nV<br \/>\nI<br \/>\nD<br \/>\nV<br \/>\nA<br \/>\nN<\/p>\n<h3>Fields:<\/h3>\n<ul>\n<li><strong>protected byte[] buf: <\/strong>The buffer used to hold data.<\/li>\n<li><strong>protected int count: <\/strong>the buffer&#8217;s total number of valid bytes.<\/li>\n<\/ul>\n<h3>Access Data from Java ByteArrayOutputStream<\/h3>\n<ul>\n<li><strong>toString() &#8211;<\/strong> returns all of the output stream&#8217;s data in string form;<\/li>\n<li><strong>toByteArray() &#8211;<\/strong> returns the array that is present inside the output stream.<\/li>\n<\/ul>\n<h3>Methods of Java ByteArrayOutputStream:<\/h3>\n<ul>\n<li><strong>size() \u2013<\/strong> provides the array&#8217;s size in the output stream.<\/li>\n<li><strong>flush() &#8211;<\/strong> makes the output stream empty<\/li>\n<\/ul>\n<h3>Difference between ByteArrayOutputStream and FioeOutputStream:<\/h3>\n<ul>\n<li>Since the ByteArrayOutputStream saves the entire content in Java&#8217;s memory (in the form of a byte[]), it consumes more memory.<\/li>\n<li>Because the FileOutputStream writes straight to disk, it uses less memory.<\/li>\n<\/ul>\n<h3>Uses:<\/h3>\n<ul>\n<li>The entire byte array&#8217;s contents are written to the OutputStream using the write(byte[] bytes) method.<\/li>\n<li>The byte array&#8217;s length bytes are sent to the OutputStream by the write(byte[] bytes, int offset, int length) method, which begins at index offset.<\/li>\n<li>32-byte data makes a fresh output stream for a byte array. The buffer&#8217;s initial capacity is 32 bytes, however it can be expanded if needed.<\/li>\n<\/ul>\n<h3>Benefits of Java ByteArrayOutputStream:<\/h3>\n<ul>\n<li>It is employed to transform the content into a string by decoding the character set by default on the platform.<\/li>\n<li>It is employed to transform the material into a string by utilizing a given charsetName to decode the bytes.<\/li>\n<li>To save the data, ByteArrayOutputStream keeps an internal array of bytes.<\/li>\n<li>An array of bytes is created from a string using the program&#8217;s getBytes() method.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is the most widely and useful language. An array of output data (in bytes) can be written using the Java.io package&#8217;s ByteArrayOutputStream class. The abstract class OutputStream is extended by it. Explanation Common&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447206,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5640,5637,296,5638,5639,263,327,250],"class_list":["post-89815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-bytearrayoutputstream","tag-bytearrayoutputstream-in-java","tag-java","tag-java-bytearrayoutputstream","tag-java-bytearrayoutputstream-with-examples","tag-java-tutorial-for-beginners","tag-java-tutorials","tag-learn-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java ByteArrayOutputStream Class with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.\" \/>\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-bytearrayoutputstream-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java ByteArrayOutputStream Class with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-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-05-02T12:30:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-02T13:02:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-bytearray-outputstream.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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java ByteArrayOutputStream Class with Examples - TechVidvan","description":"The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.","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-bytearrayoutputstream-class\/","og_locale":"en_US","og_type":"article","og_title":"Java ByteArrayOutputStream Class with Examples - TechVidvan","og_description":"The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-05-02T12:30:37+00:00","article_modified_time":"2024-05-02T13:02:33+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-bytearray-outputstream.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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java ByteArrayOutputStream Class with Examples","datePublished":"2024-05-02T12:30:37+00:00","dateModified":"2024-05-02T13:02:33+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/"},"wordCount":705,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-bytearray-outputstream.webp","keywords":["ByteArrayOutputStream","ByteArrayOutputStream in java","java","java ByteArrayOutputStream","java ByteArrayOutputStream with examples","java tutorial for beginners","java tutorials","Learn Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/","name":"Java ByteArrayOutputStream Class with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-bytearray-outputstream.webp","datePublished":"2024-05-02T12:30:37+00:00","dateModified":"2024-05-02T13:02:33+00:00","description":"The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-bytearray-outputstream.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-bytearray-outputstream.webp","width":1200,"height":628,"caption":"java bytearray outputstream"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-bytearrayoutputstream-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java ByteArrayOutputStream 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\/89815","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=89815"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89815\/revisions"}],"predecessor-version":[{"id":447481,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89815\/revisions\/447481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447206"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}