public void encode ( InputStream is , OutputStream os ) throws IOException { // StringBuilder sb = new StringBuilder((int)(estimate*1.255)); // try to get the right size of StringBuilder from start.. slightly more than 1.25 times int prev = 0 ; int read , idx = 0 , line = 0 ; boolean go ; do { read = is . read ( ) ; if ( go = read >= 0 ) { if ( line >= splitLinesAt ) { os . write ( ' ' ) ; line = 0 ; } switch ( ++ idx ) { // 1 based reading, slightly faster ++ case 1 : // ptr is the first 6 bits of read os . write ( codeset [ read >> 2 ] ) ; prev = read ; break ; case 2 : // ptr is the last 2 bits of prev followed by the first 4 bits of read os . write ( codeset [ ( ( prev & 0x03 ) << 4 ) | ( read >> 4 ) ] ) ; prev = read ; break ; default : //(3+) // Char 1 is last 4 bits of prev plus the first 2 bits of read // Char 2 is the last 6 bits of read os . write ( codeset [ ( ( ( prev & 0xF ) << 2 ) | ( read >> 6 ) ) ] ) ; if ( line == splitLinesAt ) { // deal with line splitting for two characters os . write ( ' ' ) ; line = 0 ; } os . write ( codeset [ ( read & 0x3F ) ] ) ; ++ line ; idx = 0 ; prev = 0 ; } ++ line ; } else { // deal with any remaining bits from Prev, then pad switch ( idx ) { case 1 : // just the last 2 bits of prev os . write ( codeset [ ( prev & 0x03 ) << 4 ] ) ; if ( endEquals ) os . write ( DOUBLE_EQ ) ; break ; case 2 : // just the last 4 bits of prev os . write ( codeset [ ( prev & 0xF ) << 2 ] ) ; if ( endEquals ) os . write ( ' ' ) ; break ; } idx = 0 ; } } while ( go ) ; }
encode InputStream onto Output Stream