aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes How to receive/send byte array properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to receive/send byte array properly" Watch "How to receive/send byte array properly" New topic
Author

How to receive/send byte array properly

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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 :
Error :

The error is called on line 8, below.
Code :

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.

Thanks,
John Price


“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)
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to receive/send byte array properly