[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Performing simultaneous reading and writing from a single file

Posted on 2004-10-22
12
Medium Priority
?
328 Views
Last Modified: 2008-02-01
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
Comment
Question by:blaze_wk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 12387184
nope files are not good for that.
how about writing to file and writing into memory in parrallel as you recieve the stream.
0
 

Author Comment

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

0
 
LVL 6

Expert Comment

by:mightyone
ID: 12387605
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:blaze_wk
ID: 12387761
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12388109
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
 

Author Comment

by:blaze_wk
ID: 12388341
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12388448
Yes, yes and yes ;-) Although the second yes is more that you should probably be buffering the stream and reading from that buffer
0
 

Author Comment

by:blaze_wk
ID: 12388709
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 12388752
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12388770
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
 
LVL 92

Expert Comment

by:objects
ID: 12390839
Try using a PipedInputStrea/PipedOutputStream.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12392747
8-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question