473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to convert Input Stream to bytes array?

Hi ,
I was trying to convert a Input Stream data to a byte array but was not sure , how to proceed. Please help me if you know the best way to get this done
Jan 26 '11 #1
8 10095
horace1
1,510 Expert 1GB
you can use InputStream to read an array of bytes, e.g.
http://download.oracle.com/javase/1....putStream.html

if you are having problems post your code
Jan 26 '11 #2
this is the code I am using
java.io.File file = new java.io.File("MyFile.txt");//add a file
int fileLength = (int) file.length();
System.out.println("fileLength of asciifile.txt is "+fileLength);// create an input stream


try {
fin = new java.io.FileInputStream(file);
} catch (FileNotFoundException e1) {
System.out.println("error !!File Not Found");
throw (new SQLException("error !!File Not Found"));
}


I want to convert this 'fin' object to a byte array
the link that you gave me is not opening
Jan 26 '11 #3
horace1
1,510 Expert 1GB
strange the link works OK for me
you use it so
Expand|Select|Wrap|Line Numbers
  1. java.io.File file = new java.io.File("test.txt");//add a file
  2.  
  3. try {
  4.     java.io.FileInputStream fin = new java.io.FileInputStream(file);
  5.     byte data[]=new byte[100];
  6.     int size=fin.read(data,0,100);
  7.     for (int i=0; i<size; i++)
  8.      System.out.print(data[i] + " ");
  9.     } 
  10.     catch (FileNotFoundException e1) 
  11.        { System.out.println ("error !!File Not Found");}
  12.     catch (IOException e1) {System.out.println("error !!" + e1);}
  13.  
if test.txt is
Expand|Select|Wrap|Line Numbers
  1. abcdef
  2. xyz
  3.  
the program gives
Expand|Select|Wrap|Line Numbers
  1. 97 98 99 100 101 102 10 120 121 122 10 
  2.  
which are the ASCII codes for the above characters (10 being the RETURN character)
Jan 26 '11 #4
the problem here is that my file size is 12994 bytes
and when I use this code
try {
fin = new java.io.FileInputStream(file);

byte data[]=new byte[12994];
int size=fin.read(data);
for (int i=0; i<size; i++)
System.out.print(data[i] + " ");

} catch (FileNotFoundException e1) {
System.out.println("error !!File Not Found");
throw (new SQLException("error !!File Not Found"));
}



I see the output as:
just a blank
Jan 26 '11 #5
horace1
1,510 Expert 1GB
I tried the following with a .exe file
Expand|Select|Wrap|Line Numbers
  1. java.io.File file = new java.io.File("t.exe");//add a file
  2. int fileLength = (int) file.length();
  3. System.out.println("fileLength of asciifile.txt is "+fileLength);// create an input stream
  4. try {
  5.     java.io.FileInputStream fin = new java.io.FileInputStream(file);
  6.     byte data[]=new byte[fileLength];
  7.     int size=fin.read(data,0,fileLength);
  8.     System.out.println("size is " + size);
  9.     for (int i=0; i<size; i++)
  10.      System.out.print(data[i] + " ");
  11.     } 
  12.     catch (FileNotFoundException e1) 
  13.        { System.out.println ("error !!File Not Found");}
  14.     catch (IOException e1) {System.out.println("error !!" + e1);}
  15. }
and the output was
Expand|Select|Wrap|Line Numbers
  1. fileLength of asciifile.txt is 25264
  2. size is 25264
  3. 77 90 -112 0 3 0 0 0 4 0 0 0 -1 -1 0 0 -72 0 0 0 0 0 0 0 64 0 0 0 0 0   .......
  4.  
I cut short the line of bytes as it printed 25264 values
Jan 26 '11 #6
Thank you that worked for me , for a file of smaller size !!
Jan 26 '11 #7
do you know how I can generate this byte array with the ccsid of 37?
Jan 26 '11 #8
horace1
1,510 Expert 1GB
have a look at the EBCDIC 37 page
http://en.wikipedia.org/wiki/EBCDIC_037
Jan 26 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Thomas Engelmeier | last post by:
Hi, I need an expat-alike parser (ABI-wise as much a drop-in replacement as possible) for C/C++ with the following additions: - in order to handle some tags with nesting in completely...
1
by: Srilatha | last post by:
Hi, I want to read a file as a Byte Array Input Stream in VB.Net. How can I declare a Byte Array Input Stream?
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
1
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is...
8
by: piggy | last post by:
I am trying to convert an image to an byte array (tobytes() method) and from byte array back to an image (frombytes() method). The problem i have here is say the image size is 285 bytes but the...
0
by: Tommaso Caldarola | last post by:
I got the following exception System.Runtime.Serialization.SerializationException: The input stream is not a valid binary format. The starting contents (in bytes) are:...
1
by: sandeepbhutani304 | last post by:
have 2 projects communicating each other with .NET remoting. But when I am trying to call these functions I am getting the error: The input stream is not a valid binary format. The starting...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: kvlkarthik | last post by:
Hi All, I am using fileupload control to save a tiff and PDF files in SQL database. I am saving the file in byte format. I am using the below code to display in PDF file where the byte array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.