{"record":{"id":"5a5e1351b0173742","repo":"elastic/elasticsearch","slug":"74-5a5e13","errorCode":"74","errorMessage":"Please redirect binary output to a file instead","messagePattern":"Please redirect binary output to a file instead","errorType":"exception","errorClass":"UserException","httpStatus":null,"severity":"error","filePath":"distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/ShowKeyStoreCommand.java","lineNumber":82,"sourceCode":"\n                // This is not strictly true, but it's the best heuristic we have.\n                // Without it we risk appending a newline to a binary file that happens to be valid UTF8\n                final boolean isFileOutput = terminal.getOutputStream() != null;\n                if (isFileOutput) {\n                    terminal.print(Terminal.Verbosity.SILENT, text.toString());\n                } else {\n                    terminal.println(Terminal.Verbosity.SILENT, text);\n                }\n            } catch (CharacterCodingException e) {\n                final OutputStream output = terminal.getOutputStream();\n                if (output != null) {\n                    bytes.writeTo(output);\n                } else {\n                    terminal.errorPrintln(Terminal.Verbosity.VERBOSE, e.toString());\n                    terminal.errorPrintln(\n                        \"The value for the setting [\" + settingName + \"] is not a string and cannot be printed to the console\"\n                    );\n                    throw new UserException(ExitCodes.IO_ERROR, \"Please redirect binary output to a file instead\");\n                }\n            }\n        }\n    }\n\n}\n","sourceCodeStart":64,"sourceCodeEnd":89,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/ShowKeyStoreCommand.java#L64-L89","documentation":"Thrown by `show` after the value failed to decode as UTF-8 (CharacterCodingException) AND the terminal has no redirectable output stream (getOutputStream() == null). Exits IO_ERROR (74). When an output stream IS available, the raw bytes are written there instead; this error only fires in headless contexts where binary content cannot be safely emitted.","triggerScenarios":"Showing a binary-valued setting (e.g. an added-file certificate/key) in a Terminal that has no OutputStream attached; running show in a pure-logging harness that did not wire stdout.","commonSituations":"Treating a file-backed setting as a string; CI harnesses that capture only `terminal.println` text and not the byte stream.","solutions":["Redirect to a file instead of printing to the console: pipe the command's stdout to a file.","If you need the raw bytes programmatically, attach an OutputStream to the Terminal or read the keystore via the Java API directly.","Confirm whether the setting was added with `add-file` (binary) vs `add-string` and adjust retrieval accordingly."],"exampleFix":"// before\nbin/elasticsearch-keystore show ssl.key   # binary value, no output stream\n// after\nbin/elasticsearch-keystore show ssl.key > /tmp/out.bin","handlingStrategy":"validation","validationCode":"// Detect binary before printing: attempt UTF-8 decode and fall back to redirect\nCharsetDecoder dec = StandardCharsets.UTF_8.newDecoder()\n    .onMalformedInput(CodingErrorAction.REPORT)\n    .onUnmappableCharacter(CodingErrorAction.REPORT);\nboolean isText;\ntry { dec.decode(ByteBuffer.wrap(bytes)); isText = true; }\ncatch (CharacterCodingException e) { isText = false; }\nif (!isText) {\n    // ensure terminal.getOutputStream() != null, or write to a file directly\n}","typeGuard":null,"tryCatchPattern":"try {\n    printValue(name);\n} catch (UserException e) {\n    if (e.exitCode == ExitCodes.IO_ERROR && e.getMessage().contains(\"binary\")) {\n        writeValueToFile(name, outFile);\n    }\n}","preventionTips":["For binary settings, redirect stdout to a file rather than printing.","When embedding the keystore CLI, attach an OutputStream to the Terminal so binary can be written.","Distinguish file-backed settings from string ones to choose retrieval method."],"tags":["elasticsearch","keystore-cli","io-error","binary","terminal"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}