Link to home
Start Free TrialLog in
Avatar of blaze_wk
blaze_wk

asked on

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
Avatar of JakobA
JakobA

nope files are not good for that.
how about writing to file and writing into memory in parrallel as you recieve the stream.
Avatar of blaze_wk

ASKER

that's actually what i want to do, but how do you write datagram packets into a file? what methods do you use?

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!
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?
Avatar of CEHJ
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.
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......

Yes, yes and yes ;-) Although the second yes is more that you should probably be buffering the stream and reading from that buffer
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?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Try using a PipedInputStrea/PipedOutputStream.
8-)