public String encode ( String str ) throws IOException { byte [ ] array ; try { array = str . getBytes ( encoding ) ; } catch ( IOException e ) { array = str . getBytes ( ) ; // take default } // Calculate expected size to avoid any buffer expansion copies within the ByteArrayOutput code ByteArrayOutputStream baos = new ByteArrayOutputStream ( ( int ) ( array . length * 1.363 ) ) ; // account for 4 bytes for 3 and a byte or two more encode ( new ByteArrayInputStream ( array ) , baos ) ; return baos . toString ( encoding ) ; }
Helper function for String API of Encode use getBytes with appropriate char encoding etc .