Solved

Performing simultaneous reading and writing from a single file

Posted on 2004-10-22
278 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
Question by:blaze_wk
    12 Comments
     
    LVL 15

    Expert Comment

    by:JakobA
    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
    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
    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
     

    Author Comment

    by:blaze_wk
    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
    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
    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
    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
    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:
    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
    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
    Try using a PipedInputStrea/PipedOutputStream.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now