| 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
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
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: 681
|
|
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: 19216
|
|
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
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Reading from Byte Array
|
|
|