BufferedReader.readline () returns null

I am creating this method that takes an InputStream

as parameter , but the function readLine()

returns null

. During debugging, the input stream is not empty.

else if (requestedMessage instanceof BytesMessage) {                    
    BytesMessage bytesMessage = (BytesMessage) requestedMessage;
    byte[] sourceBytes = new byte[(int) bytesMessage.getBodyLength()];
    bytesMessage.readBytes(sourceBytes);
    String strFileContent = new String(sourceBytes);                 
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(sourceBytes);
    InputStream inputStrm = (InputStream) byteInputStream;
    processMessage(inputStrm, requestedMessage);
}


 public void processMessage(InputStream inputStrm, javax.jms.Message requestedMessage) {
    String externalmessage = tradeEntryTrsMessageHandler.convertInputStringToString(inputStrm);
}

public String convertInputStringToString(InputStream inputStream) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

    StringBuilder sb = new StringBuilder();

    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }

    br.close();
    return sb.toString();
}

      

+3


source to share


2 answers


Please try

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

      



I believe the raw data that was received is not formatted to follow the character set. so by mentioning UTF-8 (U from universal character set + conversion format-8-bit might help

0


source


Are you sure you are initializing and passing a valid InputStream to the function?

Also, just FYI, maybe you were trying to call your function convertInputStreamToString instead of convertInputStringToString?

Here are two other ways to convert InputStream to String, maybe they could be?

1.

String theString = IOUtils.toString(inputStream, encoding); 

      



2.

public String convertInputStringToString(InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is, encoding).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

      

EDIT: You don't need to explicitly convert ByteArrayInputStream to InputStream. You can do directly:

InputStream inputStrm = new ByteArrayInputStream(sourceBytes);

      

0


source







All Articles