java - Eliminate default zeros while creating string from byte array -


i getting bytes iostream , converting string. string extracting sequence using substring api.

size of bytearray 128 bytes. if stream contains 10 bytes , remaining filled zero[initially filled]. converting byte array string passing string constructor new string(byte[]) , checking length. length 128. why showing 128? should show 10 byte character length. how eliminate 0 while converting string. there api's eliminate default zeros in byte array. it's creating problem while creating substring constructed string.

    byte[] b = { 99, 116, 101, 100, 46, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     0, 0, 0, 0}  system.out.println("byte length = " + b.length);             string str;             try {                 str = new string(b, "utf-8");                 system.out.println("string length = " + str.length());                 system.out.println(str);                 system.out.println("  ## substring  =  " + str.substring(0));                 system.out.println(" substring length = "                         + str.substring(0).length());                 system.out.println("done......");             } catch (unsupportedencodingexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }0, 0, 0 }; 

when read inputstream, tell how many bytes read. length of byte[] irrelevant (other defining max number of bytes read in single call). there should no need later go examine byte[] try , determine how of data relevant. pay attention return value read , use when creating string.

additionally, if of data text, consider using inputstreamreader, perhaps in combination bufferedreader.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -