This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have code that allows me to send and receive byte arrays (from a file). The server sends the byte array. The problem starts when I receive the byte array. I do not have enough ram to receive the file as a whole. As a result, I have to receive it in chunks. When I save it to a file, then play the audio file, there are "holes" in the song. I can reduce the holes by making the byte array length (when received and saved to the file) less. If I get it down to The holes are very tiny and you can barely notice. When I try to NOT save them to a file, but play them, one of two things happens. 1. If the file is more than 3 million bytes, I run out of RAM. 2. If the file is small enough to be fed into RAM...
I will explain how I deal with it :
I take the incoming bytes and append them to the "song" (bytes array). The byte array is created with . I keep appending the incoming byte arrays to the song, until it is complete. Then I try to play it. Because of the spacing of the byte arrays, the music player does not find the file type, and gives me an exception :
The error is called on line 8, below.
1. I need to make it play.
2. I need to make it more efficient.
The file is a valid .au file. If I save the incoming byte arrays to a file and play them, it play fine, except for the blank sound "error" (as I said before). If you need me to post the receiving (or sending) code, I will.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
No idea why you do have "holes" in the song - usually sending a file as byte array leads to recieving it "as is", even if to send it "in chunks".
But if you do not want to save it to disk, you could try to use ByteArrayOutputStream to store it in memory until recieving the whole file and then get it as byte array via toByteArray() method. It is much better then creating new byte array for every file chunk.
I don't understand what all that byte-array business is for. The AudioSystem.getAudioInputStream method takes an InputStream. So just give it the socket's InputStream and you're done. No wasting time and memory copying the data into a big byte array.