From 3b54e720080c85f0a855ad1373453294b1d2657a Mon Sep 17 00:00:00 2001 From: Hades Date: Fri, 2 Aug 2019 17:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0jira=20sonar=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/file/HistoryTemplateListCache.java | 15 +++----- .../design/update/actions/FileDownloader.java | 25 ++++++------ .../factory/DirectoryOperationFactory.java | 15 +++----- .../update/ui/dialog/EncodingDetect.java | 31 +++++++-------- .../update/ui/dialog/UpdateMainDialog.java | 38 +++++++++---------- .../manager/impl/PluginSearchManager.java | 19 +++++----- .../manager/impl/RecommendSearchManager.java | 20 +++++----- .../manager/impl/SegmentationManager.java | 18 ++++----- .../manager/impl/SimilarSearchManager.java | 20 +++++----- 9 files changed, 93 insertions(+), 108 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java b/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java index fc46ff59b..cb839c6d7 100644 --- a/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java +++ b/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java @@ -41,17 +41,12 @@ public class HistoryTemplateListCache implements CallbackEvent { private List> historyList; private JTemplate editingTemplate; - private static volatile HistoryTemplateListCache instacne; - public static HistoryTemplateListCache getInstance() { - if (instacne == null) { - synchronized (HistoryTemplateListCache.class) { - if (instacne == null) { - instacne = new HistoryTemplateListCache(); - } - } - } - return instacne; + return Holder.INSTANCE; + } + + private static class Holder { + private static final HistoryTemplateListCache INSTANCE = new HistoryTemplateListCache(); } private HistoryTemplateListCache() { diff --git a/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java b/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java index 556fa5113..4da324f29 100644 --- a/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java +++ b/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java @@ -80,22 +80,21 @@ public abstract class FileDownloader extends SwingWorker URLConnection connection = url.openConnection(); int total = connection.getContentLength(); item.setTotalLength(total); - InputStream reader = connection.getInputStream(); File tempFile = new File(StableUtils.pathJoin(saveDir, item.getName())); StableUtils.makesureFileExist(tempFile); - FileOutputStream writer = new FileOutputStream(tempFile); - byte[] buffer = new byte[UpdateConstants.BYTE]; - int bytesRead = 0; - int totalBytesRead = 0; - while ((bytesRead = reader.read(buffer)) != -1) { - writer.write(buffer, 0, bytesRead); - buffer = new byte[UpdateConstants.BYTE]; - totalBytesRead += bytesRead; - item.setDownloadLength(totalBytesRead); - publish(item); + try ( InputStream reader = connection.getInputStream(); + FileOutputStream writer = new FileOutputStream(tempFile)) { + byte[] buffer = new byte[UpdateConstants.BYTE]; + int bytesRead = 0; + int totalBytesRead = 0; + while ((bytesRead = reader.read(buffer)) != -1) { + writer.write(buffer, 0, bytesRead); + buffer = new byte[UpdateConstants.BYTE]; + totalBytesRead += bytesRead; + item.setDownloadLength(totalBytesRead); + publish(item); + } } - reader.close(); - writer.close(); } /** diff --git a/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java b/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java index 906b71412..48c151488 100644 --- a/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java +++ b/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java @@ -82,15 +82,12 @@ public class DirectoryOperationFactory { } private static void copy(String path1, String path2) throws IOException { - DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path1))); - DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path2))); - byte[] date = new byte[in.available()]; - - in.read(date); - out.write(date); - - in.close(); - out.close(); + try (DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path1))); + DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path2)))) { + byte[] date = new byte[in.available()]; + in.read(date); + out.write(date); + } } /** diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/EncodingDetect.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/EncodingDetect.java index 41bba0f4f..e21bb1dcc 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/EncodingDetect.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/EncodingDetect.java @@ -11,12 +11,12 @@ import java.io.FileInputStream; * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * EncodingDetect.java
* 自动获取文件的编码 * @author Mata * @since Create on 2018-08-23 - * @version 1.0 + * @version 1.0 */ public class EncodingDetect { /** @@ -25,11 +25,11 @@ public class EncodingDetect { * @return 文件的编码 */ public static String getJavaEncode(File file){ - BytesEncodingDetect s = new BytesEncodingDetect(); + BytesEncodingDetect s = new BytesEncodingDetect(); String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(file)]; return fileCode; } - + } @@ -75,17 +75,14 @@ class BytesEncodingDetect extends Encoding { * type. The encoding type with the highest probability is returned. */ public int detectEncoding(File testfile) { - FileInputStream chinesefile; - byte[] rawtext; - rawtext = new byte[(int) testfile.length()]; - try { - chinesefile = new FileInputStream(testfile); - chinesefile.read(rawtext); - chinesefile.close(); - } catch (Exception e) { - System.err.println("Error: " + e); - } - return detectEncoding(rawtext); + byte[] rawtext = new byte[(int) testfile.length()]; + try(FileInputStream chinesefile = new FileInputStream(testfile)) { + chinesefile.read(rawtext); + chinesefile.close(); + } catch (Exception e) { + System.err.println("Error: " + e); + } + return detectEncoding(rawtext); } /** @@ -647,7 +644,7 @@ class BytesEncodingDetect extends Encoding { * (byte)0xDF) == rawtext[i]) { // Two bytes if (i+1 < rawtextlen && (rawtext[i+1] & (byte)0xBF) == rawtext[i+1]) { * goodbytes += 2; i++; } } else if ((rawtext[i] & (byte)0xEF) == rawtext[i]) { // Three bytes if (i+2 < rawtextlen && * (rawtext[i+1] & (byte)0xBF) == rawtext[i+1] && (rawtext[i+2] & (byte)0xBF) == rawtext[i+2]) { goodbytes += 3; i+=2; } } } - * + * * score = (int)(100 * ((float)goodbytes/(float)rawtext.length)); // An all ASCII file is also a good UTF8 file, but I'd * rather it // get identified as ASCII. Can delete following 3 lines otherwise if (goodbytes == asciibytes) { score = 0; } // * If not above 90, reduce to zero to prevent coincidental matches if (score > 90) { return score; } else { return 0; } @@ -4551,5 +4548,5 @@ class BytesEncodingDetect extends Encoding { nicename[ASCII] = "ASCII"; nicename[OTHER] = "OTHER"; } - + } \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java index e59cc09ff..81239d84b 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java @@ -468,30 +468,28 @@ public class UpdateMainDialog extends UIDialog { return; } if (cacheFile.exists()) { - InputStreamReader streamReader = new InputStreamReader(new FileInputStream(cacheFile), "UTF-8"); - BufferedReader br = new BufferedReader(streamReader); - String readStr, updateTimeStr; - - while ((readStr = br.readLine()) != null) { - String[] updateInfo = readStr.split("\\t"); - if (updateInfo.length == 2) { - updateTimeStr = updateInfo[0]; - Date updateTime = CHANGELOG_FORMAT.parse(updateTimeStr); - //形如 Build#release-2018.07.31.03.03.52.80 - String currentNO = GeneralUtils.readBuildNO(); - Date curJarDate = UPDATE_INFO_TABLE_FORMAT.parse(currentNO, new ParsePosition(currentNO.indexOf("-") + 1)); - if (!ComparatorUtils.equals(keyword, StringUtils.EMPTY)) { - if (!containsKeyword(UPDATE_INFO_TABLE_FORMAT.format(updateTime), keyword) && !containsKeyword(updateInfo[1], keyword)) { - continue; + try (InputStreamReader streamReader = new InputStreamReader(new FileInputStream(cacheFile), "UTF-8"); + BufferedReader br = new BufferedReader(streamReader)) { + String readStr, updateTimeStr; + while ((readStr = br.readLine()) != null) { + String[] updateInfo = readStr.split("\\t"); + if (updateInfo.length == 2) { + updateTimeStr = updateInfo[0]; + Date updateTime = CHANGELOG_FORMAT.parse(updateTimeStr); + //形如 Build#release-2018.07.31.03.03.52.80 + String currentNO = GeneralUtils.readBuildNO(); + Date curJarDate = UPDATE_INFO_TABLE_FORMAT.parse(currentNO, new ParsePosition(currentNO.indexOf("-") + 1)); + if (!ComparatorUtils.equals(keyword, StringUtils.EMPTY)) { + if (!containsKeyword(UPDATE_INFO_TABLE_FORMAT.format(updateTime), keyword) && !containsKeyword(updateInfo[1], keyword)) { + continue; + } + } + if (isValidLogInfo(updateInfo[1])) { + updateInfoList.add(new Object[]{UPDATE_INFO_TABLE_FORMAT.format(updateTime), updateInfo[1], updateTime.after(curJarDate)}); } - } - if (isValidLogInfo(updateInfo[1])) { - updateInfoList.add(new Object[]{UPDATE_INFO_TABLE_FORMAT.format(updateTime), updateInfo[1], updateTime.after(curJarDate)}); } } } - br.close(); - streamReader.close(); } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/PluginSearchManager.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/PluginSearchManager.java index ddd3bc0fc..a902d0e00 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/PluginSearchManager.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/PluginSearchManager.java @@ -27,21 +27,20 @@ import java.net.URLEncoder; * Created by XiaXiang on 2017/3/27. */ public class PluginSearchManager implements AlphaFineSearchProvider { - private static volatile PluginSearchManager instance; private SearchResult lessModelList; private SearchResult moreModelList; + private PluginSearchManager() { + + } public static PluginSearchManager getInstance() { - if (instance == null) { - synchronized (PluginSearchManager.class) { - if (instance == null) { - instance = new PluginSearchManager(); - } - } - } - return instance; + return Holder.INSTANCE; + + } + private static class Holder { + private static final PluginSearchManager INSTANCE = new PluginSearchManager(); } private static boolean isCompatibleCurrentEnv(String envVersion) { @@ -105,7 +104,7 @@ public class PluginSearchManager implements AlphaFineSearchProvider { lessModelList.add(new MoreModel(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Plugin_Addon"))); return lessModelList; } - SearchResult noConnectList = AlphaFineHelper.getNoConnectList(instance); + SearchResult noConnectList = AlphaFineHelper.getNoConnectList(Holder.INSTANCE); if(noConnectList != null){ return noConnectList; } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/RecommendSearchManager.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/RecommendSearchManager.java index e15c5b966..05267220b 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/RecommendSearchManager.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/RecommendSearchManager.java @@ -25,22 +25,22 @@ import java.util.List; * Created by XiaXiang on 2017/3/31. */ public class RecommendSearchManager implements AlphaFineSearchProvider { - private static volatile RecommendSearchManager instance; private SearchResult modelList = new SearchResult(); private SearchResult recommendModelList = new SearchResult(); private SearchResult complementAdviceModelList; private SearchResult moreModelList = new SearchResult(); + private RecommendSearchManager() { + + } + public static RecommendSearchManager getInstance() { - if (instance == null) { - synchronized (RecentSearchManager.class) { - if (instance == null) { - instance = new RecommendSearchManager(); - } - } - } - return instance; + return Holder.INSTANCE; + } + + private static class Holder { + private static final RecommendSearchManager INSTANCE = new RecommendSearchManager(); } @Override @@ -51,7 +51,7 @@ public class RecommendSearchManager implements AlphaFineSearchProvider { if (ArrayUtils.isEmpty(searchText)) { return new SearchResult(); } - SearchResult noConnectList = AlphaFineHelper.getNoConnectList(instance); + SearchResult noConnectList = AlphaFineHelper.getNoConnectList(Holder.INSTANCE); if (noConnectList != null) { return noConnectList; } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SegmentationManager.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SegmentationManager.java index 40c37341c..c9622193c 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SegmentationManager.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SegmentationManager.java @@ -13,18 +13,18 @@ import java.util.regex.Pattern; * Created by alex.sung on 2018/8/3. */ public class SegmentationManager { - private static volatile SegmentationManager segmentationManager = null; private static final int MAX_CHINESE_CHARACTERS_NUM = 4; + private SegmentationManager() { + + } + public static SegmentationManager getInstance() { - if (segmentationManager == null) { - synchronized (SegmentationManager.class) { - if (segmentationManager == null) { - segmentationManager = new SegmentationManager(); - } - } - } - return segmentationManager; + return Holder.INSTANCE; + } + + private static class Holder { + private static final SegmentationManager INSTANCE = new SegmentationManager(); } /** diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SimilarSearchManager.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SimilarSearchManager.java index 1754b1284..f353573b4 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SimilarSearchManager.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/manager/impl/SimilarSearchManager.java @@ -18,19 +18,19 @@ import com.fr.third.org.apache.commons.codec.digest.DigestUtils; * Created by alex.sung on 2018/8/3. */ public class SimilarSearchManager implements AlphaFineSearchProvider { - private static volatile SimilarSearchManager instance; private SearchResult lessModelList; private SearchResult moreModelList = new SearchResult(); + private SimilarSearchManager() { + + } + public static SimilarSearchManager getInstance() { - if (instance == null) { - synchronized (SimilarSearchManager.class) { - if (instance == null) { - instance = new SimilarSearchManager(); - } - } - } - return instance; + return Holder.INSTANCE; + } + + private static class Holder { + private static final SimilarSearchManager INSTANCE = new SimilarSearchManager(); } @Override @@ -40,7 +40,7 @@ public class SimilarSearchManager implements AlphaFineSearchProvider { if (ArrayUtils.isEmpty(searchText)) { return new SearchResult(); } - SearchResult noConnectList = AlphaFineHelper.getNoConnectList(instance); + SearchResult noConnectList = AlphaFineHelper.getNoConnectList(Holder.INSTANCE); if (noConnectList != null) { return noConnectList; }