• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to receive/send byte array properly

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic