• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data loss converting StringBuilder to byte[]?

 
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
Is there data loss when transferring from StringBuilder to a byte array? I think so, because the music I'm trying to play sounds like crap when I play it. I send the audio file as a string. I receive it as a StringBuilder. I append to the StringBuilder until all the data gets to the client. Then, I convert the StringBuilder to a String. I then convert the String to a byte array. I'm wondering if there is any data loss between these transfers? If it is, could you please suggest an easy solution? Please read the code below. It is my actual code from my server and client. I only included the transfer part/converting part, because that is all I am asking about. It's only about 50 lines.

Server :


Client :


Thank you all so very much,
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
Why use such complicated way for sending a file? Why not just send byte array directly? Such approach worked fine for me, without any data loss.
 
john price
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
But I receive it as a string...How do I receive as bytes and still have ability to receive as string? I can show you the full receiving options code (not that long) if you wish. I receive it as a string because certain commands start with certain strings. How to mix string and bytes?

Thanks,
John Price
 
Marshal
Posts: 28193
95
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
You're receiving binary data as a String? Stop doing that because it can lead to data loss.
 
john price
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 am definitely experiencing data loss. This is my full client receiving code. How can I accept the data as a byte array or input stream while still being able to receive "commands" as Strings? I don't have a problem sending and receiving as a byte stream or array or stream array or whatever, I just don't know how to receive as both and how to differentiate between them. Should I have a whole separate port for streaming data (with another thread)?



Thanks,
John Price
 
Paul Clapham
Marshal
Posts: 28193
95
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 get it. You have a file which contains binary data, like an audio file, and yet you're reading data from it as if it were a text file? That isn't going to work. You're going to have to read the bytes and convert them to strings in your code as necessary. Don't use a Reader.
 
john price
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
"reader" is a BufferedReader.

So here is what I am thinking. I create a new socket connection. This second one handles getting the audio file. I send the file through a DataOutputStream from a ByteArrayOutputStream. I receive the file through the second socket connection using a DataInputStream to a ByteArrayOutputStream. I then send these bytes to the play method. Would this work? If this does not work, plesae tell me how I can do it or what is the best way. I do realize this is more of a networking question, so please move it if you feel led too.

Thanks,
John Price
 
Paul Clapham
Marshal
Posts: 28193
95
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
It's hard to say much, because what you are showing us is code, and the real problem is that you have some protocol which mixes text and binary data. From the last post it sounds like you're the one who designed that protocol, so the next step is to redesign it in some practical way.

That would involve looking at a description of the protocol, not the code which attempts to implement it.
 
john price
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'll give it a go on the description :
I have a list of values that I load into the server. On, request, I send the String values of these (proper format is String). Using these Strings, the user can request to download (into memory) an audio file from the server. The server needs to send the audio data back to the client, who then plays the music.

Here is how I think I'll be able to do this :
Create two different connection ports.
One is for the command of getting the String list (it's a list of music files available). The other is for requesting the audio data. When someone requests the list, it sends the request to the second connection, who then sends the audio back to the second connection on the client.

Where do I go from here? I need to know how to send and receive byte arrays/datainputstream/dataoutputstream/bytearrayinputstream/bytearrayoutputstream. These are some of the solutions I've been looking at. I really want to build this part today, so if you could respond ASAP, that would be wonderful. I appreciate your help!

John Price
 
Paul Clapham
Marshal
Posts: 28193
95
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 see why you need separate ports. Couldn't you just use the Reader on responses which you know will be text and the InputStream on responses which you know will be binary data?
 
john price
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
How do I have to change this (setup) to use what you have said ?

Later...


John Price
 
john price
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 just thought of another way to do it. I keep everything the same, except that I make the reader.readLine() equal a byte array. I then convert the byte array to a string value. If the string value matches any of the commands, I have a match. If it doesn't, I can add it to the File. This is just something I thought of. Would it work?

Thanks,
John Price
 
john price
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
Think I'm close...Getting one error though. The client is not receiving output.
Client :

Server :
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic