This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reading from Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading from Byte Array" Watch "Reading from Byte Array" New topic
Author

Reading from Byte Array

Josweth Reddy
Ranch Hand

Joined: Nov 02, 2005
Posts: 30
Hi All,

As per my requirement, I'm retrieving the data from database and storing it into Input stream. Then reading the data length from byte array. I need to remove the junk data from byte array before storing it.

But if you look into the below mentioned code, it is throwing an ArrayIndex out of bound exception. The output file contains the junk data.

Hope you will help me out regarding the same.

// Read Char from Byte Array

byte[] newbuf = new byte[4 * 8192];
String str = new String(new char[]{(char)0x4EC1,(char)0x4E01});
byte buff[] = str.getBytes("UTF8");

// retrieving the data from database result set
// Storing the retrieved data in InputStream format

InputStream in=rs.getBinaryStream(1);
OutputStream out;

int newread = in.read(newbuf);
System.out.println("\n newread length >> "+newread);
try{
for(int i =0; i<newread; i++) {
System.out.print(newread+"\n newread >> ");
System.out.print(buff[i]+" ");

//System.out.print("\n newread >> "+newread);
out.write(newbuf, 0, newread);

System.out.println(newbuf[i]);
}
}catch (Exception e){
e.printStackTrace();
}




Thaknks in advance,
Josweth Reddy
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

It looks like this question does not have anything to do with the SCJP exam. I am moving it to a more appropriate forum.

In which line does the ArrayIndexOutOfBoundsException happen? Look at that line in your source code. The exception means that you are using a wrong value to index an array.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
it is throwing an ArrayIndex out of bound exception.

Please post the full text of any error messages. There is very IMPORTANT
info in the message.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

I'd say this line is causing the problems:
System.out.print(buff[i]+" ");

buff is an array of a very small size, definitely a lot smaller than newbuf. If newread is larger than its size you will get an exception.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from Byte Array
 
Similar Threads
byte array to object casting
Database file reader
Merging byte arrays using SequenceInputStream
FileInputStream problem
Problem in file transfer