private FromHostPrimitiveResult < String > fromHostString ( CobolContext cobolContext , byte [ ] hostData , int start , int bytesLen ) { // Trim trailing low-values and, optionally, trailing spaces int spaceCode = cobolContext . getHostSpaceCharCode ( ) ; boolean checkSpace = cobolContext . isTruncateHostStringsTrailingSpaces ( ) ; int end = start + bytesLen ; while ( end > start ) { int code = hostData [ end - 1 ] & 0xFF ; if ( code != 0 && ( ( checkSpace && code != spaceCode ) || ! checkSpace ) ) { break ; } end -- ; } // Return early if this is an empty string if ( start == end ) { return new FromHostPrimitiveResult < String > ( "" ) ; } // Host strings are sometimes dirty boolean isDirty = false ; for ( int i = start ; i < end ; i ++ ) { if ( hostData [ i ] == 0 ) { isDirty = true ; break ; } } // Cleanup if needed then ask java to convert using the proper host // character set String result = null ; try { if ( isDirty ) { byte [ ] work = new byte [ end - start ] ; for ( int i = start ; i < end ; i ++ ) { work [ i - start ] = hostData [ i ] == 0 ? ( byte ) cobolContext . getHostSpaceCharCode ( ) : hostData [ i ] ; } result = new String ( work , cobolContext . getHostCharsetName ( ) ) ; } else { result = new String ( hostData , start , end - start , cobolContext . getHostCharsetName ( ) ) ; } } catch ( UnsupportedEncodingException e ) { return new FromHostPrimitiveResult < String > ( "Failed to use host character set " + cobolContext . getHostCharsetName ( ) , hostData , start , bytesLen ) ; } return new FromHostPrimitiveResult < String > ( result ) ; }
Convert host data to a java String .