Writing objects to random file

R

Ron Albright

You use a ByteArrayOutputStream but only way you can access the result
is by getting a copy of the byte array each time you write. Is there a
better way to do this that doesn't require creating a new byte array
for each write? I looked at the nio package and didn't see any support
for writing objects to a file.
 
A

Andrew Thompson

Ron said:
You use a ByteArrayOutputStream but only way you can access the result
is by getting a copy of the byte array each time you write. Is there a
better way to do this that doesn't require creating a new byte array
for each write?

'Better'? In what way is the existing situation 'worse'?

[ I do not know much about the specific area
that you are speaking of, but wonder if you
are not attempting to optimize something
that does not require optimization?]
 
A

Andrew Hobbs

Ron Albright said:
You use a ByteArrayOutputStream but only way you can access the result
is by getting a copy of the byte array each time you write. Is there a
better way to do this that doesn't require creating a new byte array
for each write?

In what way are you using the ByteArrayOutputStream. You don't need to
create an array each time you write. Surely that is the objective of using
this class.
I looked at the nio package and didn't see any support
for writing objects to a file.

And how does the above relate to writing to a file. If you want to write
your bytes to a file then have a look at the java.io package. You need to
include something like a DataOutputStream and a FileWriter.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************

 
R

Ron Albright

In what way are you using the ByteArrayOutputStream. You don't need to
create an array each time you write. Surely that is the objective of using
this class.

The only way to write the byte array from a
ByteArrayOutputStream is the getByteArray() method or the
writeTo() method. The first gives you a copy of the array
which has to be alloced and garbage collected every time you
write an object. The second requires opening a stream and
skipping to the position you want to write to every time you
write an object. Neither seems very efficient.
And how does the above relate to writing to a file. If you want to write
your bytes to a file then have a look at the java.io package. You need to
include something like a DataOutputStream and a FileWriter.

You can't write to a random position with streams.

The context of the questions is a fast guarantied persistent
queue or stack. The idea is a collector getting data and
persisting it before acking and a distributor passing it on
and getting an ack before it is removed.

Ron Albright
moron(at)KILLSPAM.pobox.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top