{"record":{"id":"7d720707a9a03569","repo":"pentaho/pentaho-kettle","slug":"error-converting-string-to-binary","errorCode":null,"errorMessage":"Error converting string to binary","messagePattern":"Error converting string to binary","errorType":"exception","errorClass":"KettleException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java","lineNumber":1231,"sourceCode":"          byte[] tmp = new byte[ newSize ];\n          for ( int i = 0; i < result.length; i++ ) {\n            tmp[ i ] = result[ i ];\n          }\n          for ( int i = 0; i < nrExtra; i++ ) {\n            tmp[ result.length + i ] = extra[ i ];\n          }\n\n          // change the result\n          result = tmp;\n          nrExtra = bi.read( extra );\n        }\n        bytes = result;\n        gzip.close();\n      }\n\n      return bytes;\n    } catch ( Exception e ) {\n      throw new KettleException( \"Error converting string to binary\", e );\n    }\n  }\n\n  public static String buildCDATA( String string ) {\n    return buildCDATA( new StringBuilder(), string ).toString();\n  }\n\n  public static StringBuilder buildCDATA( StringBuilder builder, String string ) {\n    return builder.append( \"<![CDATA[\" ).append( Const.NVL( string, \"\" ) ).append( \"]]>\" );\n  }\n\n  public static String openTag( String tag ) {\n    return openTag( new StringBuilder(), tag ).toString();\n  }\n\n  public static StringBuilder openTag( StringBuilder builder, String tag ) {\n    return builder.append( '<' ).append( tag ).append( '>' );\n  }","sourceCodeStart":1213,"sourceCodeEnd":1249,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java#L1213-L1249","documentation":"The string-to-binary helper (used for encoding/optionally gzip-compressing a string into bytes) wraps any exception during the conversion into KettleException(\"Error converting string to binary\"). Causes (unsupported charset name, gzip stream failure) are chained in the cause.","triggerScenarios":"Calling the XMLHandler stringToBinary-style method with a charset name unsupported by the JVM, or when the GZIPOutputStream fails mid-write (out of memory, stream closed prematurely).","commonSituations":"Hardcoded charset names with typos (e.g. \"UTF8\" vs typo), running on a JVM with a restricted charset provider, very large strings exhausting memory during gzip.","solutions":["Check e.getCause() for UnsupportedEncodingException and correct the charset name (use StandardCharsets.UTF_8.name()).","Use a charset constant instead of a free-form string to avoid typos.","Increase heap (-Xmx) if compressing very large strings.","Avoid gzip if the caller only needs raw bytes and compression is optional."],"exampleFix":"// before\nbyte[] bytes = XMLHandler.stringToBinary(str, \"utf-8\", true); // typo charset\n// after\nbyte[] bytes = XMLHandler.stringToBinary(str, StandardCharsets.UTF_8.name(), true);","handlingStrategy":"try-catch","validationCode":"try { \"x\".getBytes(charsetName); } catch (UnsupportedEncodingException e) { /* fix charset */ }","typeGuard":"boolean isSupportedCharset(String name) {\n  try { java.nio.charset.Charset.forName(name); return true; }\n  catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n  bytes = XMLHandler.stringToBinary(str, charset, compress);\n} catch (KettleException e) {\n  throw new KettleException(\"Encode failed with charset \" + charset + \": \" + e.getCause(), e);\n}","preventionTips":["Use StandardCharsets.UTF_8.name() instead of hardcoded strings","Guard memory when gzip-compressing very large strings","Catch UnsupportedEncodingException early with a charset smoke test"],"tags":["encoding","gzip","conversion"],"backgroundTag":"invalid-argument-value","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}