Performing simultaneous reading and writing from a single file

Hi,

I was wondering if performing simulatneous reading and writing from a single file is able to be done under java? I am streaming audio from one computer to another computer. At the receiving computer, I intend to write received audio data into a file whilst reading data from the file into memory. Does anybody know how to implement this? What are the moethods to use and maybe some example codes?

thanks
blaze_wkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JakobACommented:
nope files are not good for that.
how about writing to file and writing into memory in parrallel as you recieve the stream.
0
blaze_wkAuthor Commented:
that's actually what i want to do, but how do you write datagram packets into a file? what methods do you use?

0
mightyoneCommented:
i see two possibilities, first using a randomacces file and second converting your file to an byteArray

i would prefer the bytearray as you may easiliy play around with it and streaming it as ByteArrayInputStream and ByteArrayOutputstream. to add and extract parts you would have to implement your own methods to cut and add byteArrays, but think this is easyliy done.

for the random acces file i am notz sure if does work on binry files as i have till now only use it on text files.

keep in mind when playing with binary stream do not use readers (if you are not sure about their working) as they may change the encoding of your streams and that may cause errors in your streams.

you should then make sure that your ByteArray is synchronized so thant a real symoultaniously is not accepted!

good luck!
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

blaze_wkAuthor Commented:
mightytone,

yur idea sounds fine but the problem i face is when i receive a continuous stream of bytes with an unknown length. Like maybe say receiving a streaming radio station ....it might continuously be on for a few hours or even a day. I guess I can implement it using memory where i create a byte stream of maybe say 10 megabytes(which is quite large and quite wasteful of memory space) to store received stream andthen loiop back to the first item in the 10 megabyte array when the 10 megabyte array is maxed out.

i was thinking mayb it's actually more sueful if i can store the received data in a file and after a certain amount of time erase the parts of the file that have already been played or utilized. do u get wat i mean?

wat's yur take or anybody's take for that matter?
0
CEHJCommented:
You need to approach this problem in two stages, since you need:

a. a mechanism to periodically fill a buffer from a file
b. mechanisms to coordinate locking of the buffer

a. is necessary so that large memory consumption can be avoided. While data are being received by one part of your program, they can be streamed to file, while another part of the program is occupied with consuming the buffer. If the memory consumption is low enough, you might consider omitting the disk-caching part to simplify implementation.

b. is necessary as you have a 'producer/consumer' relationship, so you might like to refresh you memory on this pattern by reading up on it in the Java thread tutorial.
0
blaze_wkAuthor Commented:
ok i intend to actually to continually write to the file and read the file using audioinputstream from java sound. is tt possible? i guess multithreading would have to be used, am i right? one for writing to the file and another thread running the audioinputstream......

0
CEHJCommented:
Yes, yes and yes ;-) Although the second yes is more that you should probably be buffering the stream and reading from that buffer
0
blaze_wkAuthor Commented:
as in for the second yes, i should buffer the stream by first writing to the file and using java sound to read form the file through java sound's inbuilt buffer capacilities? or buffer the whole stream into memory which would be impossible and inefficient and then read from tt buffer but is good for short audio streams?
0
CEHJCommented:
You should probably be writing to the file as the data come in, but periodically reading from the other end of the file to fill a buffer.

You can in fact see the buffer/file read/write as acting on a synchronized queue. You're consuming the head and the producer is filling the tail.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
You should probably start by smoothing out the distinction between the file and the buffer and gradually introducing more sohpistication as necessary. mightyone's idea of RandomAccessFile is good here.
0
objectsCommented:
Try using a PipedInputStrea/PipedOutputStream.
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.