Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading from Byte Array

 
Josweth Reddy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15483
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Bartender
Posts: 1345
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic