The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java IO (ByteArrayInputStream Copy to destArray)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java IO (ByteArrayInputStream Copy to destArray) Posted: Aug 26, 2016 9:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java IO (ByteArrayInputStream Copy to destArray)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube : 
https://www.youtube.com/watch?v=ujGc_2bB_Ec&list=UUhwKlOVR041tngjerWxVccw

ByteArrayInputStreamDemo.java
import java.io.ByteArrayInputStream;
import java.io.IOException;

/*
* public int read(byte[] b, int off, int len)
*
* b - the buffer into which the data is read.
*
* off - the start offset in the destination
* array b
*
* len - the maximum number of bytes
* read.
*
* Reads up to len bytes of data into an array
* of bytes from this input stream.
*/

public class ByteArrayInputStreamDemo
{

public static void main(String[] args) throws IOException
{
ByteArrayInputStream byteArrayInputStream = null;

try
{
byte[] srcBuffer = new byte[10];

int j = 11;
for (int i = 0; i < srcBuffer.length; i++)
{
srcBuffer[i] = (byte) j++;
}

System.out.println("All elements form srcBuffer:");
for (int i = 0; i < srcBuffer.length; i++)
{
System.out.print(srcBuffer[i] + " ");
}

byteArrayInputStream = new ByteArrayInputStream(srcBuffer);

byte[] destBuffer = new byte[6];

/*
* We put 4 first elements of the
* ByteArrayInputStream instance
* 'byteArrayInputStream' to the destBuffer
* array, starting at the position with index 2.
* This is why the two first indexes will be
* '0'.
*/

byteArrayInputStream.read(destBuffer, 2, 4);

System.out.println("\n\nAll elements form destBuffer:");
for (int i = 0; i < destBuffer.length; i++)
{
System.out.print(destBuffer[i] + " ");
}

}
finally
{
if (byteArrayInputStream != null)
{
/*
* Closing a ByteArrayInputStream has no
* effect. The methods in this class can be
* called after the stream has been closed
* without generating an IOException.
*/

byteArrayInputStream.close();
}
}

}
}
Output
All elements form srcBuffer:
11 12 13 14 15 16 17 18 19 20

All elements form destBuffer:
0 0 11 12 13 14
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/94d38fdd49b2d33c14c088526d40f902c21a1e0d/BasicJava/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Tutorial : Java IO (ByteArrayInputStream Copy to destArray)

    Topic: So, Oracle killed java.net Previous Topic   Next Topic Topic: 31% off Logitech Harmony Smart Control with Smartphone App and Simple All In One Remote - Deal Alert

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use