Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-07-2009
Member
 
Join Date: Apr 2008
Posts: 19
Confusion converting InputStream to ByteArray

Hi,

I am a little bit confused with the conversion of InputStream to ByteArray.

Can you guys please help me out with this confusion with some example?

Thanks in advance.

Regards,
Lineman
Reply With Quote
  #2  
Old 28-07-2009
Member
 
Join Date: Aug 2008
Posts: 71
Re: Confusion converting InputStream to ByteArray

Hey Sorry to say but I have a question for you.

Why do you want to 'convert' an ObjectInputStream into a ByteArray ?
Reply With Quote
  #3  
Old 28-07-2009
Member
 
Join Date: May 2008
Posts: 32
Re: Confusion converting InputStream to ByteArray

Object to byte array.

First create a ByteArrayOutputStream and wrap it in an ObjectOutputStream now pass to a constructor. Here you can write your object to a ObjectOutputStream & close it. Now you can get the byte array from the ByteArrayOutputStream.

I hope this helps you.
Reply With Quote
  #4  
Old 28-07-2009
Member
 
Join Date: May 2008
Posts: 22
Re: Confusion converting InputStream to ByteArray

Convert InputStream to ByteArray

To convert the InputStream into a ByteArray first we read a file from the system by using FileInputStream. Since class InputStream is super class of the FileInputStream so we can store file into InputStream to take the file as input stream. To convert the input stream into string we use toString() method and then we use the method getBytes() to convert the string into byte array. Finally we display the string as output.

Code:
import java.io.*;
public class InputstramToByteArray
{
public static void main(String args[])throws IOException
  {
  InputStream itStrm=new FileInputStream(
 "InputstramToByteArray.java" );
  String str=itStrm.toString();
  byte[] b3=str.getBytes();
  for(int i=0;i<b3.length;i++)
  System.out.print(b3[i]+"\t");
  }
}
output

Code:
C:\Grundy\io>javac InputstramToByteArray.java

C:\Grundy\io>java InputstramToByteArray
106 97 118 97 46 105 111 46 70 105
108 101 73 110 112 117 116 83 116 114
101 97 109 64 51 101 50 53 97 53
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Confusion converting InputStream to ByteArray"
Thread Thread Starter Forum Replies Last Post
Confusion with tata photon parth mamgain India BroadBand 1 10-12-2011 12:42 PM
Confusion in Downloading some videos? loveenglish Technology & Internet 2 21-08-2011 10:43 PM
Confusion in Android phone Protected Portable Devices 3 13-02-2011 05:19 PM
Confusion regarding Post Graduation....... krinku Education Career and Job Discussions 1 09-09-2010 01:17 AM
How to convert InputStream to String in java? KALIDA Software Development 4 20-01-2010 06:16 PM


All times are GMT +5.5. The time now is 11:58 AM.