Java11,MalformedByteSequenceException应该替换成什么?
Java11, what shall MalformedByteSequenceException be replaced with?
我正在将项目移动到 Java11
我已经把sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
改成了java.lang.reflect.ParameterizedType
(按照指定的here),现在轮到MalformedByteSequenceException
:
warning: MalformedByteSequenceException is internal proprietary API and may be removed in a future release
import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException;
它被用于从 XML 文件创建对象的代码片段中。更准确地说是 try-catch
.
try {
...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(stream);
NodeList list = document.getChildNodes();
fillProcessStack(document);
...
list = list.item(0).getChildNodes();
createItems(list, parent);
} catch (MalformedByteSequenceException e) {
//"Any char in your xml file has a wrong format: " + e.getLocalizedMessage()
} catch (SAXParseException sax) {
...
} catch (Exception e) {
...
}
反正我在网上找不到任何关于这个的东西。
我能想到的最接近的是 UnsupportedEncodingException
,但我不确定。
此外,这可能是从网上复制的一些旧代码的遗留物,显然如果我删除它,编译器看起来一切都很好。
那么,关于Java11有什么general/good建议吗?
编辑:对于想结束这个问题的人,因为
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Reproducible Example.
我会尽量解释得更清楚。
这里没有调试帮助,在Java11中有些包没有导出到public,比如sun.*
,它们不应该正常使用(除非特定条件)。代码在 jdk8 下工作正常,但在 ojdk11 下它会抱怨,所以我可以使用一些变通方法使它们在运行时无论如何都可见,或者使其正确并摆脱它们。后者是我正在尝试做的。
鉴于我在网上找不到任何东西,我在这里问。
MalformedByteSequenceException
扩展了 CharConversionException
,它扩展了 IOException
,parse
方法强制你抓住它。所以在捕获IOException
或CharConversionException
时,如果直接抛出也会捕获MalformedByteSequenceException
。
然而,XML 解析器在 JDK11 中似乎发生了行为变化。当我尝试时,例如
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(new byte[] { 1, 2, 3, -5 }));
我明白了
Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Invalid byte 1 of 1-byte UTF-8 sequence.
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:262)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:122)
at TestXmlParser.main(TestXmlParser.java:14)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at java.xml/com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:702)
at java.xml/com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:568)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1904)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1770)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1808)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:158)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:860)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
... 3 more
而不是
Exception in thread "main" com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1619)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1657)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:193)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
at TestXmlParser.main(TestXmlParser.java:14)
以前的版本。
换句话说,parse
现在抛出 SAXParseException
,其原因初始化为 MalformedByteSequenceException
。因此,要检测问题是格式错误的字节序列,resp。编码错误的文本,你需要像
这样的东西
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
System.out.println("maformed bytes or wrong encoding: "+ex.getMessage());
}
}
要处理新版本和旧版本,您需要像
这样的东西
catch(CharConversionException ex) {
System.out.println("malformed bytes or wrong encoding: "+ex.getMessage());
}
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
System.out.println("malformed bytes or wrong encoding: "+ex.getMessage());
}
}
相应
catch(CharConversionException ex) {
handle(ex);
}
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
handle((CharConversionException)ex.getCause());
}
}
…
static void handle(CharConversionException ex) {
// just an example
System.out.println("malformed bytes or wrong encoding: "+ex.getMessage());
}
我正在将项目移动到 Java11
我已经把sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
改成了java.lang.reflect.ParameterizedType
(按照指定的here),现在轮到MalformedByteSequenceException
:
warning: MalformedByteSequenceException is internal proprietary API and may be removed in a future release import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException;
它被用于从 XML 文件创建对象的代码片段中。更准确地说是 try-catch
.
try {
...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(stream);
NodeList list = document.getChildNodes();
fillProcessStack(document);
...
list = list.item(0).getChildNodes();
createItems(list, parent);
} catch (MalformedByteSequenceException e) {
//"Any char in your xml file has a wrong format: " + e.getLocalizedMessage()
} catch (SAXParseException sax) {
...
} catch (Exception e) {
...
}
反正我在网上找不到任何关于这个的东西。
我能想到的最接近的是 UnsupportedEncodingException
,但我不确定。
此外,这可能是从网上复制的一些旧代码的遗留物,显然如果我删除它,编译器看起来一切都很好。
那么,关于Java11有什么general/good建议吗?
编辑:对于想结束这个问题的人,因为
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Reproducible Example.
我会尽量解释得更清楚。
这里没有调试帮助,在Java11中有些包没有导出到public,比如sun.*
,它们不应该正常使用(除非特定条件)。代码在 jdk8 下工作正常,但在 ojdk11 下它会抱怨,所以我可以使用一些变通方法使它们在运行时无论如何都可见,或者使其正确并摆脱它们。后者是我正在尝试做的。
鉴于我在网上找不到任何东西,我在这里问。
MalformedByteSequenceException
扩展了 CharConversionException
,它扩展了 IOException
,parse
方法强制你抓住它。所以在捕获IOException
或CharConversionException
时,如果直接抛出也会捕获MalformedByteSequenceException
。
然而,XML 解析器在 JDK11 中似乎发生了行为变化。当我尝试时,例如
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(new byte[] { 1, 2, 3, -5 }));
我明白了
Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Invalid byte 1 of 1-byte UTF-8 sequence.
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:262)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:122)
at TestXmlParser.main(TestXmlParser.java:14)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at java.xml/com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:702)
at java.xml/com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:568)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1904)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1770)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1808)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:158)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:860)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
... 3 more
而不是
Exception in thread "main" com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1619)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1657)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:193)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
at TestXmlParser.main(TestXmlParser.java:14)
以前的版本。
换句话说,parse
现在抛出 SAXParseException
,其原因初始化为 MalformedByteSequenceException
。因此,要检测问题是格式错误的字节序列,resp。编码错误的文本,你需要像
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
System.out.println("maformed bytes or wrong encoding: "+ex.getMessage());
}
}
要处理新版本和旧版本,您需要像
这样的东西catch(CharConversionException ex) {
System.out.println("malformed bytes or wrong encoding: "+ex.getMessage());
}
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
System.out.println("malformed bytes or wrong encoding: "+ex.getMessage());
}
}
相应
catch(CharConversionException ex) {
handle(ex);
}
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
handle((CharConversionException)ex.getCause());
}
}
…
static void handle(CharConversionException ex) {
// just an example
System.out.println("malformed bytes or wrong encoding: "+ex.getMessage());
}