{"record":{"id":"cd7635a8232f3738","repo":"jenkinsci/jenkins","slug":"failed-to-detect-encoding-of","errorCode":null,"errorMessage":"Failed to detect encoding of {}","messagePattern":"Failed to detect encoding of (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/hudson/XmlFile.java","lineNumber":370,"sourceCode":"                private void attempt() throws Eureka {\n                    if (loc == null)   return;\n                    if (loc instanceof Locator2 loc2) {\n                        String e = loc2.getEncoding();\n                        if (e != null)\n                            throw new Eureka(e);\n                    }\n                }\n            });\n            // can't reach here\n            throw new AssertionError();\n        } catch (Eureka e) {\n            if (e.encoding != null)\n                return e.encoding;\n            // the environment can contain old version of Xerces and others that do not support Locator2\n            // in such a case, assume UTF-8 rather than fail, since Jenkins internally always write XML in UTF-8\n            return \"UTF-8\";\n        } catch (SAXException e) {\n            throw new IOException(\"Failed to detect encoding of \" + file, e);\n        } catch (InvalidPathException e) {\n            throw new IOException(e);\n        } catch (ParserConfigurationException e) {\n            throw new AssertionError(e);    // impossible\n        }\n    }\n\n    /**\n     * {@link XStream} instance is supposed to be thread-safe.\n     */\n\n    private static final Logger LOGGER = Logger.getLogger(XmlFile.class.getName());\n\n    private static final HierarchicalStreamDriver DEFAULT_DRIVER = XStream2.getDefaultDriver();\n\n    private static final XStream DEFAULT_XSTREAM = new XStream2(DEFAULT_DRIVER);\n}\n","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/jenkinsci/jenkins/blob/2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc/core/src/main/java/hudson/XmlFile.java#L352-L388","documentation":"Wraps SAXException thrown during XML encoding detection in XmlFile.sniffEncoding() — a method that parses the XML declaration (<?xml ... encoding=...?>) using a SAX reader with a custom handler (Eureka). If the SAX parser fails to parse the file at all, a SAXException is thrown and wrapped. Note: InvalidPathException is wrapped separately as a plain IOException, and ParserConfigurationException is wrapped as AssertionError (impossible for a standard JDK).","triggerScenarios":"The SAX reader throws SAXException while attempting to read the XML declaration — this happens when the file is not valid XML at all (e.g., binary data, truncated content, null bytes, or completely malformed structure before the parser can determine encoding).","commonSituations":"Config file was corrupted by a partial write (Jenkins killed mid-save); file is empty or contains only null bytes; file was overwritten with non-XML content (e.g., a binary download saved to a config.xml path); encoding issues at the byte level that prevent the SAX parser from even starting.","solutions":["Inspect the cause SAXException for the specific parse error (line/column number, unexpected character).","Restore the file from Jenkins' automatic backup (config.xml.bak, config.xml.bak1, etc. in the same directory).","If no backup exists, recreate the configuration manually through the Jenkins UI.","Check disk health and write permissions to prevent future partial writes."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Validate file is valid XML before sniffing encoding\ntry (InputStream is = Files.newInputStream(file.toPath())) {\n    byte[] head = is.readNBytes(5);\n    if (head.length < 5 || head[0] != '<') {\n        throw new IOException(\"File does not appear to be XML: \" + file);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    String encoding = xmlFile.sniffEncoding();\n} catch (IOException e) {\n    if (e.getCause() instanceof SAXException) {\n        // File is not valid XML — assume UTF-8 as fallback or restore from backup\n        encoding = \"UTF-8\";\n    } else {\n        throw e;\n    }\n}","preventionTips":["Ensure atomic writes (AtomicFileWriter) for config files to prevent truncated XML.","Monitor disk space — full disks cause partial writes.","Regularly back up JENKINS_HOME to recover from corruption."],"tags":["xml","encoding","parsing","sax","config"],"backgroundTag":null,"analyzedSha":"2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc","analyzedAt":"2026-08-14T07:07:15.274Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}