I am reading a request body and put it into an input stream. While I am explicitly saying the decode method, I still get many \u0000 (Null) after the string.
InputStream is = exchange.getRequestBody();
byte[] header = new byte[100];
is.read(header);
String s = new String(header, "UTF-8");
How can I avoid this with Standard Java Library? I cannot use third party libraries.
The minimum value char can hold is 'u0000' which is a Unicode value denoting 'null' or 0 in decimal. The maximum value it can hold is 'uffff' or 65,535 inclusive. The minimum value which is 'u0000' is also the default value of char.
To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.
is.read(header); returns the number of bytes that were actually read. Change your code as
byte[] header = new byte[100];
int n = is.read(header);
String s = new String(header, 0, n, "UTF-8");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With