aspose file tools*
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
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: 14352
    
  22

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 8 API documentation
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
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: 19762
    
  20

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