From 6f1747409bbf134703936018c9020b9c0dac3201 Mon Sep 17 00:00:00 2001 From: plough Date: Fri, 10 May 2019 10:58:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?REPORT-16986=20=E6=A8=A1=E6=9D=BF=E5=88=B6?= =?UTF-8?q?=E4=BD=9C=E5=9F=8B=E7=82=B9=E7=9A=84tpl.info=E9=87=8C=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=A6=82=E6=9E=9C=E6=98=AF=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E5=90=8D=E4=BC=9A=E4=B9=B1=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/info/TemplateInfoCollector.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java index 0996c260ce..342213e0f5 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -12,12 +12,14 @@ import com.fr.stable.xml.XMLTools; import com.fr.stable.xml.XMLWriter; import com.fr.stable.xml.XMLableReader; import com.fr.third.javax.xml.stream.XMLStreamException; -import com.fr.workspace.WorkContext; +import com.fr.third.org.apache.commons.io.FileUtils; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -133,7 +135,8 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { return; } try { - XMLableReader xmlReader = XMLableReader.createXMLableReader(new FileReader(getInfoFile())); + InputStreamReader isReader = new InputStreamReader(new FileInputStream(getInfoFile()), StandardCharsets.UTF_8); + XMLableReader xmlReader = XMLableReader.createXMLableReader(isReader); xmlReader.readXMLObject(this); } catch (XMLStreamException e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); @@ -160,8 +163,12 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { */ private void saveInfo() { try { - FileOutputStream out = new FileOutputStream(getInfoFile()); + ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLTools.writeOutputStreamXML(this, out); + out.flush(); + out.close(); + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); + FileUtils.writeStringToFile(getInfoFile(), fileContent, StandardCharsets.UTF_8); } catch (Exception ex) { FineLoggerFactory.getLogger().error(ex.getMessage()); } From d89a2bedde93343e581e4894ee9303520f0c7724 Mon Sep 17 00:00:00 2001 From: plough Date: Fri, 10 May 2019 11:43:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?REPORT-16986=20=E6=A8=A1=E6=9D=BF=E5=88=B6?= =?UTF-8?q?=E4=BD=9C=E5=9F=8B=E7=82=B9=E7=9A=84tpl.info=E9=87=8C=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=A6=82=E6=9E=9C=E6=98=AF=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E5=90=8D=E4=BC=9A=E4=B9=B1=E7=A0=81=3D>=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/info/TemplateInfoCollector.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java index 342213e0f5..330b9304a8 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -1,6 +1,7 @@ package com.fr.design.mainframe.template.info; import com.fr.base.FRContext; +import com.fr.base.io.XMLReadHelper; import com.fr.design.DesignerEnvManager; import com.fr.log.FineLoggerFactory; import com.fr.stable.ProductConstants; @@ -18,7 +19,8 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.InputStreamReader; +import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; @@ -134,14 +136,24 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { if (!getInfoFile().exists()) { return; } - try { - InputStreamReader isReader = new InputStreamReader(new FileInputStream(getInfoFile()), StandardCharsets.UTF_8); - XMLableReader xmlReader = XMLableReader.createXMLableReader(isReader); - xmlReader.readXMLObject(this); - } catch (XMLStreamException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); + + XMLableReader reader = null; + try (InputStream in = new FileInputStream(getInfoFile())) { + // XMLableReader 还是应该考虑实现 Closable 接口的,这样就能使用 try-with 语句了 + reader = XMLReadHelper.createXMLableReader(in, XMLPrintWriter.XML_ENCODER); + reader.readXMLObject(this); } catch (FileNotFoundException e) { // do nothing + } catch (XMLStreamException | IOException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } finally { + try { + if (reader != null) { + reader.close(); + } + } catch (XMLStreamException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } } }