简体   繁体   English

Java11,MalformedByteSequenceException 应该用什么代替?

[英]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.ParameterizedTypeImpljava.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 ,它扩展了IOExceptionparse方法强制您无论如何都要捕获它。 So when catching IOException or CharConversionException , you'll also catch MalformedByteSequenceException if thrown directly.因此,当捕获IOExceptionCharConversionException ,如果直接抛出,您也会捕获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());
}

转换 ArrayList 列表的最佳方法是什么<object>到自定义 object java11 Spring 引导 2.1<div id="text_translate"><p> 我有 ArrayList 对象列表,其中对象可以是文本、BigDecimal、Long</p><pre> List&lt;Object&gt; data = { {"date","prize1","cardsNumers","cardObtained","totalCards","anotherPrize"}, {"2020-05-01",435345.67,43,56,67,345345345}, {"2020-03-01",45656.2,34,67,23,456456}, {"2020-02-01",56565,34,56,67,878987} }</pre><p> arrayList 中的第 1、2、3 等对象是第 0 个 arraylist 中提到的每个字段的值。 在如下表中,</p><p> <a href="https://i.stack.imgur.com/Tmdw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tmdw2.png" alt="在此处输入图像描述"></a></p><p> 我想从上面获取每个文件的总和并分配给下面自定义 Object 的同一字段,</p><pre> public class SumObject { private Date date; private BigDecimal sumPrize1; private BigDecimal sumCardsNumers; private long sumCardObtained; private long sumTotalCards; private long sumAnotherPrize; }</pre><p> 我通过遍历 List 中的 arrayList 来实现这一点,并通过 get(index) 方法获取值。 但我想知道是否有任何其他好的方法可以做到这一点。</p><p> 如果有人有任何建议,请发布答案或给我一个我可以阅读的文件的方向。</p><p> 提前致谢。</p></div></object> - what would be best way to convert List of ArrayList<Object> to a custom object java11 Spring boot 2.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迁移到Java11 - Migrate to java11 Google Cloud Dataflow 使用什么 java 运行时? java8 或 java11 - What java runtime does Google Cloud Dataflow use?java8 or java11 与Java11兼容的JBoss版本 - JBoss compatible version with Java11 在Java11模块中使用@PostConstruct或@PreDestroy - Using @PostConstruct or @PreDestroy in Java11 module 将 Mockito 与 Java11 一起使用时的异常 - Exception when using Mockito with Java11 Eclipse GWT SDK 2.8.2 与 Java11 - Eclipse GWT SDK 2.8.2 with Java11 具有Java11的Openliberty有警告 - Openliberty with java11 has warnings Java11中的谓词过滤所有元素 - Predicate in Java11 filters all elements 转换 ArrayList 列表的最佳方法是什么<object>到自定义 object java11 Spring 引导 2.1<div id="text_translate"><p> 我有 ArrayList 对象列表,其中对象可以是文本、BigDecimal、Long</p><pre> List&lt;Object&gt; data = { {"date","prize1","cardsNumers","cardObtained","totalCards","anotherPrize"}, {"2020-05-01",435345.67,43,56,67,345345345}, {"2020-03-01",45656.2,34,67,23,456456}, {"2020-02-01",56565,34,56,67,878987} }</pre><p> arrayList 中的第 1、2、3 等对象是第 0 个 arraylist 中提到的每个字段的值。 在如下表中,</p><p> <a href="https://i.stack.imgur.com/Tmdw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tmdw2.png" alt="在此处输入图像描述"></a></p><p> 我想从上面获取每个文件的总和并分配给下面自定义 Object 的同一字段,</p><pre> public class SumObject { private Date date; private BigDecimal sumPrize1; private BigDecimal sumCardsNumers; private long sumCardObtained; private long sumTotalCards; private long sumAnotherPrize; }</pre><p> 我通过遍历 List 中的 arrayList 来实现这一点,并通过 get(index) 方法获取值。 但我想知道是否有任何其他好的方法可以做到这一点。</p><p> 如果有人有任何建议,请发布答案或给我一个我可以阅读的文件的方向。</p><p> 提前致谢。</p></div></object> - what would be best way to convert List of ArrayList<Object> to a custom object java11 Spring boot 2.1 Java:MalformedByteSequenceException(XML) - Java: MalformedByteSequenceException (XML)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM