[英]Java11, what shall MalformedByteSequenceException be replaced with?
I'm moving a project to Java11我正在将项目转移到 Java11
I already changed sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
with java.lang.reflect.ParameterizedType
(as specified here ), now is the turn of MalformedByteSequenceException
:我已经改变了
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
与java.lang.reflect.ParameterizedType
(作为指定在这里),现在之交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;
警告:MalformedByteSequenceException 是内部专有 API,可能会在未来版本中删除 import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException;
it's being use in a snippet of code that create objects from XML files.它被用于从 XML 文件创建对象的代码片段中。 More precisely in a
try-catch
.更准确地说是在
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) {
...
}
Anyway I cant find anything online regarding this.无论如何,我在网上找不到任何关于此的信息。
The closest I can think of is UnsupportedEncodingException
, but I'm not sure.我能想到的最接近的是
UnsupportedEncodingException
,但我不确定。
Also, it's possible this is a relict of some old code copied from the net, and apparently if I remove it everything looks still fine for the compiler.此外,这可能是从网上复制的一些旧代码的残余,显然如果我删除它,对于编译器来说一切看起来仍然很好。
So, is there any general/good recommendations about it regarding Java11?那么,关于 Java11 有没有关于它的一般/好的建议?
Edit: for the guy seeking to close this question because编辑:对于寻求关闭此问题的人,因为
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.
请参阅:如何创建最小的、可重现的示例。
I'll try to be explain clearer.我会尽量解释得更清楚。
There is no debugging help here, in Java11 some packages are not exported to public, like sun.*
and they shouldnt be used normally (unless specific conditions).这里没有调试帮助,在 Java11 中有些包没有导出到 public,比如
sun.*
并且它们不应该被正常使用(除非特定条件)。 Code works fine under jdk8, but under ojdk11 it complains, so I could either use some workarounds to make them visible anyway at runtime or make it right and get rid of them.代码在 jdk8 下工作正常,但在 ojdk11 下它会抱怨,所以我可以使用一些变通方法让它们在运行时仍然可见,或者使其正确并摆脱它们。 The latter is what I'm trying to do.
后者是我正在尝试做的。 Given I couldn't find anything online, I'm asking here.
网上没找到,所以在这里问一下。
MalformedByteSequenceException
extends CharConversionException
, which extends IOException
, which the parse
method forces you to catch anyway. MalformedByteSequenceException
扩展了CharConversionException
,它扩展了IOException
, parse
方法强制您无论如何都要捕获它。 So when catching IOException
or CharConversionException
, you'll also catch MalformedByteSequenceException
if thrown directly.因此,当捕获
IOException
或CharConversionException
,如果直接抛出,您也会捕获MalformedByteSequenceException
。
However there seems to be a behavioral change of the XML parser in JDK 11. When I try, eg但是,JDK 11 中 XML 解析器的行为似乎发生了变化。当我尝试时,例如
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(new byte[] { 1, 2, 3, -5 }));
I get我得到
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
instead of the而不是
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)
of previous versions.以前的版本。
In other words, parse
now throws a SAXParseException
with a cause initialized to a MalformedByteSequenceException
.换句话说,
parse
现在抛出一个SAXParseException
,其原因初始化为MalformedByteSequenceException
。 So, to detect that the problem is a malformed byte sequence, resp.因此,要检测问题是格式错误的字节序列,请分别执行。 text in the wrong encoding, you'd need something like
文本编码错误,你需要类似的东西
catch(SAXException ex) {
if(ex.getCause() instanceof CharConversionException) {
System.out.println("maformed bytes or wrong encoding: "+ex.getMessage());
}
}
To handle newer and older versions, you'd need something like要处理较新和较旧的版本,您需要类似的东西
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());
}
}
resp.分别
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.