Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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
0
blaze_wk
Asked:
blaze_wk
1 Solution
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now