• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to receive/send byte array properly

 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic