?
Solved

ByteArrayInputStream problems.

Posted on 2006-03-24
6
Medium Priority
?
467 Views
Last Modified: 2008-03-10
I have some problems.. and i have tested it's really because of that. I just dont understand why. Anyone can help to tell me why.

i have this:
..
..
String xml_data = rs.getBlob("XML_PARSED_DATA");
byte[] b1 = xml_data.getBytes(1, (int) xml_data.length());
ByteArrayInputStream xml_in_byte1 = new java.io.ByteArrayInputStream(b1);
ByteArrayInputStream xml_in_byte2 = new java.io.ByteArrayInputStream(b1);
ByteArrayInputStream xml_in_byte3 = new java.io.ByteArrayInputStream(b1);

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(xml_in_byte1);
Element root = doc.getRootElement();

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(xml_in_byte2);
Element root = doc.getRootElement();

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(xml_in_byte3);
Element root = doc.getRootElement();
..
..
this variable, if i keep on reusing it, it'll have error with me. And if i didnt repeat to declare multiple time, i will safe. I am wondering any limitation with this [ByteArrayInputStream ] datatype?
0
Comment
Question by:poweranger77
6 Comments
 
LVL 10

Accepted Solution

by:
radarsh earned 500 total points
ID: 16278723
You are wrapping on the same instance of byte[] b1. That's why. If you really want isolated copies,
you either have to clone b1 and use it in the constructor or create copies of b1 and use.

byte[] b1 = xml_data.getBytes(1, (int) xml_data.length());
byte[] b2 = new byte [b1.length];
byte[] b3 = new byte [b1.length];

System.arraycopy(b1, 0, b2, 0, b1.length);
System.arraycopy(b1, 0, b3, 0, b1.length);

ByteArrayInputStream xml_in_byte1 = new java.io.ByteArrayInputStream(b1);
ByteArrayInputStream xml_in_byte2 = new java.io.ByteArrayInputStream(b2);
ByteArrayInputStream xml_in_byte3 = new java.io.ByteArrayInputStream(b3);

________
radarsh
0
 
LVL 6

Expert Comment

by:avinthm
ID: 16278727
why are u declaring 3 ByteArrayInputStream objects ???
and wht is the error u r getting?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16278779
All you need is

Document doc = builder.build(new StringReader(xml_data));
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16279031
Yeah....

I don't understand why you are creating three instances of ByteArrayInputStream and only one of the byte[] array.

If you want to do it that way u need to create three instanes of the byte[] array.

Cheers,

Leo

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16280783
>> SAXBuilder builder = new SAXBuilder();

You're also declaring that three times. You can remove the other two.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16308294
You'd be better using Reader/Writer with text-based input
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question