Read byte/char from socket inputstream

Hey fellow coders.
I ran into a bit of coding of a http client drive problem for my assignment. I created a simple program that opens a connection with a server and the request for a file to download. The socket inputstream is encapsulated in a bufferedreader and I can successfully retrieve the response of the full text of the server via the player. Unfortunately, every time I download an mp3 file, the data is corrupted. The problem lies in the fact that bufferedreader decodes the stream of bytes into a string with the utf8. I can't use the underlying socket inputstream as bufferedreader has already read before in and perhaps spent the beginning of the content area. Analysis of the chain with getBytes ("UTF 8") does not work that the audio file is damaged.
My question is, is there any player that can send me data from a stream unique both as char or byte? Everyone fell on this problem and found a solution for it?

Thank you.

The socket inputstream is encapsulated in a bufferedreader

Why?

and I can successfully retrieve the response of the full text of the server via the player.

Why is the server sends a response text followed by binary data?

In this circumstance, I would use DataInputStream.readLine (), despite his disapproval, followed to call read() to get binary bytes.

Tags: Java

Similar Questions

  • How can I read from the InputStream when I use StreamConnectionNotifier

    I want to make a program that will listen on port 100 and exchange with PC message.

    And the program uses wifi to connect with the PC.

    I have StreamConnectionNotifier choice and I can receive request for socket of PC, but when I open imputStream, Exception throw.

    My code:

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    Import javax.microedition.io.Connector;
    Import javax.microedition.io.StreamConnection;
    Import javax.microedition.io.StreamConnectionNotifier;

    SerializableAttribute public class ServerThread extends Thread {}
    constants
    private public static final String URL = ""http://:100;interface=wifi;deviceside=false "; "
    fields
    private StreamConnectionNotifier _notify;
    private InputStream _in;
    private OutputStream _out;
    private StreamConnection stream = null;
    private HelloWorld _app;
       
    Private boolean isStop = false;
     
    public ServerThread() {}
      
    }
     
    {} public void setApp (HelloWorld app)
    _APP = app;
    }
     
    public void run() {}
    try {}
    {while(!isStop)}
    {Synchronized (this)}
    _notify = Connector.open (URL) (StreamConnectionNotifier);
    }
    Stream = _notify.acceptAndOpen ();
    _in = stream.openDataInputStream ();  //<-- i="" think="" this="" line throw="" excepiton,="" excepiotn="" 's="" message="" is:request line="" is="">
    _out = stream.openDataOutputStream ();
    extract the data from the input stream
    StringBuffer sb = new StringBuffer();
    Reference int = - 1;
    {While (-1! = (reference = {_in.read ()))}
    Reference of the SB. Append ((Char));
    }
        
    _APP.updateText (SB. ToString());
        
    StringBuffer res = new StringBuffer();
    Res. Append ("HTTP/1.0 200 OK \r\n");
    Res. Append ("Content-type: text/html \r\n");
    Res. Append ("Content-Length: 1 \r\n");
    Res. Append ("1");
    _out. Write (res. ToString(). GetBytes());
        
    _in. Close();
    _out. Close();
    _notify. Close();
    }
    } catch (IOException e) {}
    probably the stream has been closed.
    System.Err.println (try ());
    _APP.updateText (try ());
    } {Finally
    try {}
    If (stream! = null) {}
    Stream.Close ();
    }
    } catch (Exception ex) {}
    }
    try {}
    If (_notify! = null) {}
    _notify. Close();
    }
    } catch (Exception ex) {}
    }
    }
    }
     
    {} public void stop()
    isStop = true;
    try {}
    If (stream! = null) {}
    Stream.Close ();
    }
    } catch (Exception ex) {}
    }
    try {}
    If (_notify! = null) {}
    _notify. Close();
    }
    } catch (Exception ex) {}
    }
    }

    }

    When I use IE application this port the program can block reading of inputStream.

    What's wrong? Please help me.

    What could happen is the following (Please check if this is the case however). Your code side device receives an incoming connection, but is then stuck on waiting for InputStream.read return anything. This translates your code aside PC it also blocks its BufferedReader.readLine, because the device-side code is not send anything (yet). So, you have a classic network impasse. If it is exactly what is happening, then you can try to fix the problem in several ways.

    1. change the code aside device to read byte-by-byte (int InputStream.read () instead of void InputStream.read (byte [],...)). It might help because on a few BlackBerrys the TCP stack returns the data received so that the stream is closed by a peer until you start viewing byte-by-byte.

    AND/OR

    2. change the PC-side code to close the output stream before reading the input stream (that's how works HTTP/1.0, for example). It might help because on a few BlackBerrys the TCP stack does not return data received until the stream is closed by a peer.

  • Don't know how to read the results of netstat EI s list? If I received 860592506 bytes sent from 43421482 how to convert MB? And does this mean byte received?

    Don't know how to read the results of netstat EI s list? If I received 860592506 bytes sent from 43421482 how to convert MB? And does this mean byte received?

    Hello

    This shows the total number of bytes transmitted and received via the Ethernet card. This is the total of the amount of use up to this day.

    This includes all the Web sites you have visited and emails you sent, etc..

    You can calculate using one of the following methods:

    Method 1:

    860592506 / 1048576 = 820,72 MB

    Method 2:

    Multiply 860592506 with 9.53674316 * 10 ^-7 that will give us the result in mega bytes.

    860592506 bytes = 820 MB 9MB not, accept my apology for the miscalculation.
    Similarly 43421482 acroos = 40MB

  • Problem with read (byte [], int int.) in BONE 5.0.0.604

    Is there a known issue with reading does not correctly in the latest versions of OS 5?

    I have an existing application that works perfectly on all devices 4.2.1 BB + OS... except for devices running the latest versions of OS 5.0.0 when I test on the Simulator for OS 5.0.0.604 (generic), I get strange behaviors using the read() method on the InputStream created from InputConnection.openInputStream () of an HttpConnection.  The problem occurs only when connecting using deviceside = true.  It does not affect MDS.  What I see is:

    read (buffer, index, length) fills a 1024 bytes of the buffer with the http response data internally.

    then, it ignores the first 152 bytes and returns the rest of 872 in the buffer parameter.  The returned value for the number of bytes read int is correct.  that is, 872.

    Subsequent calls to read() to return the same 872 bytes. ... He never advances through the flow to return the rest of the data.

    Method available() at any point returns the length of the http response (in this case, 2685)

    In all previous versions of the operating system, using the same code, it returns all the 2685 bytes in a single call to read()... I'm puzzled.  Also has anyone seen this behavior?

    I had problems similar to the 8830, but with an o/s 4.2 or 4.5

    Finally I changed my read() to read a single byte at a time, rather than a buffer and that solved

    My question.

    If I remember correctly, in my case, I knew the total length and was reading in pieces,

    move my shift in the buffer, the amount of data returned by read(byte[],init,int)

  • read byte serial number

    Hi all

    I have a small application that consists of reading data from a serial port using the VISA READ function. The data source that I am trying to acquire the serial port is a microcontroller that sends a word or a byte (8bits). However, when you try to download the programming code in the microcontroller, the programming software backup code in the HEX file into the chip.

    It's all just an explanation into the problem.

    in oreder to get my application actually, I used the attached VI have not program but adds some functions for multiple purposes. The Vi works perfectly and everything works fine, but it seems that I had some misunderstanding here.

    what I'm trying to understand here is why a function (hexadecimal string to number) is used for as much that I have configured everything to read bytes (8bits)

    I am very confused

    any suggestion would be appreciated.

    Thank you

    Converted to 8.5 and attached.

    You can also use

    http://zone.NI.com/reference/en-XX/help/371361H-01/Glang/byte_array_to_string/

    http://zone.NI.com/reference/en-XX/help/371361H-01/Glang/string_to_byte_array/

    to perform the conversion.

  • Binary file read byte unmatch

    I tried to read a line from the binary as the top of the screenshot.

    The result is that the left side. It shows the first part with the text are correct. The final part is not match with that gross.

    I also tried read by byte, but still the same.

    Any suggestion, thank you.

    I couldn't find a sequence of 3 30 2 31 30 30 00 04 3F... any where in the two files contained in the zip file that was in the other thread. I think you open a different file with LabVIEW.

    Have you tried the converter program that David mentioned in your original thread?

  • Upgrade Express on 7, that an error has occurred in the script on this page. Line 0. Char from 0. Script error. code 0. "with a URL

    Hello

    After looking on any 3rd party retailers appear Anytime Upgrade key for 7 to professional.

    When I try Anytime Upgrade system that is integrated and use 'go online to choose Windows7 edition which is best', an error box comes in reading "an error has occurred in the script on this page. Line 0. Char from 0. Script error. code 0. "with a URL.

    When I try the store I find nothing either. Can I deduce that Microsoft no longer sell upgrades between versions of windows 7?

    Whenever upgrades have long past gone

    Finally unless you are interested in win-10 which is.

    Many win-7 pro autour on Newegg and Amazon, but they are not at level only oem / system integrator.

    You could buy one and a new ssd / HDD to install on and use the original win-7 to move to 10 - win

    Then passed to win-10 pro for 99.us.

  • Large array of bytes sent via socket?

    Hello

    I have had this problem for a while and have never really stood under how to solve it properly. I don't understand how you are supposed to transfer arrays of bytes that are larger than the size of the buffer of the socket. I have semi overcome them chapter by chapter from the table and write through the sleeve and reassemble frequently on the other side, but this causes problems. What is the right way to send a byte array that is larger than the buffer socket on an outputstream?

    I suspect that I am thick here, but I'm not aware that there is something called a socket buffer.  Actually, let me rephrase.  I'm not aware that there is a specific size that was a little to the amount of data to a socket can accept.

    And you should, in theory, be able to send constantly bytes on a socket and the IP protocol should ensure that they arrive in the right order, at the other end, providing the connection is maintained.

    In practice there are limits, but I wasn't aware of a buffer size of socket specific related to the shots of Blackberry (or any Sockets besides /).

    The original poster might explain what determines the size of this buffer?

  • Read a file from SD card

    I have problems to read a file from SD card. I watched EmbeddedMediaDemo sample and I changed the line:

    InputStream is = getClass().getResourceAsStream("/media/BlackBerry.mp4");
    InputStream is = getClass().getResourceAsStream("/SDCard/BlackBerry/videos/VID00019.3GP");

    "' and I get the message:" flow Eception exception: null ".

    I also tried so:

    InputStream is = getClass().getResourceAsStream("file:///SDCard/BlackBerry/videos/VID00019.3GP");

    but same problem

    I try to include VID00019.3GP in the proj, then use this line:

    InputStream is = getClass().getResourceAsStream("/media/VID00019.3GP");

    It works.

    Please urgent help.

    Thank you

    Passing the uri is the easiest way.  You can open an input stream of the file using the FileConnection system:

    InputStream inputStream = null;
    FileConnection fileConnection = (FileConnection) Connector.open( "file:///SDCard/BlackBerry/videos/VID00019.3GP");
    if (fileConnection.exists()) {
        inputStream = fconn.openInputStream();
    }
    
  • Read VeriStand channel from inside the custom device that are not part of the custom device?

    Is it possible to read VeriStand channels from within a custom device VeriStand (RT Driver) that are not part of this custom device?

    For example, suppose I want to be able to configure a channel in my custom device to always be twice the value of a string of material that my user selects (I know, better/more simple ways to do this, but this is just a simple example ).  Can I do this from the device custom without programmatically create a mapping of the system? (I've done this before and it is messy and VERY prone to error of the user).

    Thank you!

    good point

    the channel ID (U64s) are not the same in the engine config of vs. Instead, you must set 'references to dependent item' in the config and then get these references in the engine.

    See the source jarrod linked, which makes it

  • How to read a number from a web page

    Hello everyone
    I need to read a number from a Web page. This number is the price of electricity online that is updated every 5 minutes. I need to use this award as a parameter in my controller

    any suggestion will be appreciated in advance

    Reza

    Just read the Web page as a text file and analyzed as mentioned here

  • How to read the data from a channel named in windows using labview

    Hello

    I need to read the channel named in windows data. The pipe is created by python by another application. This application post permanent data on the pipe. But I don't know how to read data using labview. is it possible to read the data from different application? If possible, how to start?

    Thanks in advance

    concerning

    RJ

    Hi Rolf,.

    It's just, because of driving is not initialized completely, pipe read returns error 42, I added 2 seconds late, then its work perfect.

    Thank you very much.

    Concerning

    RJ

  • Remove the read-only attribute from all files in a folder

    How to remove the read-only attribute from all files in a folder. I tried to use the properties and used the attrib command, but all I see is when I right click on the folder, selecting Properties, it shows once again

    I followed the forums, but I'm really confused. Can someone help me with this. Thanks in advance

    I would like to start by finding which file is read-only again. This is probably a file as well as a miniature file desktop.ini.  An easy way to find it is to go to the details view, turn on the column heading "Attributes" and search for all files with the attribute value "R":

    In this example, the key of SB file is read-only:

  • How to read a value from a cell in a row

    Hello;

    I need the name of a function that is able to read a value from a cell in a row. the line contains multiple values.

    If you can post for me, I grow


  • Is it possible that I can read or convert from a floppy midi data?

    Floppy disk

    Vista will not read data Midi from a floppy sequenced on a roland mc 50, purchased in the early 1990s, is there any software that could convert my files so that Vista will read? I have hundreds of songs, I need to back up any help would be appreciated.

    If you connect the player to your PC using a floppy drive cable USB and go...
    Start / computer... is recognized with a drive letter of the floppy drive?

    If_yes_can you make a left click of the letter reader and view the directory
    the floppy?

    These files have an extension .mid?

    Have you tried to copy and paste to a folder on your hard drive?

    FWIW... the Format Factory free software can convert MIDI files
    a digital audio format. WMA or. MP3...

Maybe you are looking for