We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

ByteArrayInputStream problems.

poweranger77
poweranger77 asked
on
Medium Priority
483 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?
Comment
Watch Question

Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
why are u declaring 3 ByteArrayInputStream objects ???
and wht is the error u r getting?
CERTIFIED EXPERT
Top Expert 2016

Commented:
All you need is

Document doc = builder.build(new StringReader(xml_data));
Leo EikelmanDirector, IT and Business Development

Commented:
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

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> SAXBuilder builder = new SAXBuilder();

You're also declaring that three times. You can remove the other two.
CERTIFIED EXPERT
Top Expert 2016

Commented:
You'd be better using Reader/Writer with text-based input
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.