Search code examples
androidfileinputstreamlibusb

FileInputstream android garbage


I am using this code to read input stream but its not working It s giving garbage value in buffer. what's wrong with the code below: //mInputStream is an object of FileInputStream

buf = new byte[4];
int ret = 0;

l("run");
try {
    while ( (ret = mInputStream.read(buf) )!= -1) {
        l("No.of bytes received:"+ret);
        l("Data received length :"+buf.length);
        l("Data received: "+buf.toString());
    }
} catch (IOException e) {
    l("IO exception in receiving");
}

Solution

  • Could try this code:

    BufferedReader r = new BufferedReader(new InputStreamReader(mInputStream));
    StringBuilder total = new StringBuilder();
    String line;
    while ((line = r.readLine()) != null) {
        total.append(line);
    }
    l("String: " + total);
    

    and post the output?