From 5aa3d48a4e391baa4bb890ddde0d56bfa30b4596 Mon Sep 17 00:00:00 2001 From: popugashkin <139558255+popugashkin@users.noreply.github.com> Date: Fri, 14 Jul 2023 15:05:12 +0200 Subject: [PATCH] Implement `Utf8Helper.getAllowedCharsets()` to lazy-load `allowedCharsets`, make `Utf8Helper.allowedCharsets` private --- .../com/jpexs/helpers/utf8/Utf8Helper.java | 32 +++++++++++-------- .../flash/gui/tagtree/TagTreeContextMenu.java | 18 +++++------ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java index 781d43095..fc453e505 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java @@ -1,16 +1,16 @@ /* * Copyright (C) 2010-2023 JPEXS, All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. */ @@ -39,16 +39,21 @@ public class Utf8Helper { public static String charsetName = "UTF-8"; public static Charset charset = Charset.forName("UTF-8"); - + private static List allowedVariableLengthCharsets = Arrays.asList( "GB2312", "Shift_JIS", "UTF-8", "UTF-16", "UTF16-BE", "UTF-16-LE", "UTF-32", "UTF-32LE", "UTF-32BE"); - - /** - * Allowed charsets. They are limited to single byte charsets + allowedVariableLengthCharsets - */ - public static List allowedCharsets = new ArrayList<>(); - static { + private static List allowedCharsets = null; + + /** + * Get a list of allowed charsets. They are limited to single byte charsets + allowedVariableLengthCharsets + */ + public static List getAllowedCharsets() { + if (allowedCharsets != null) + return allowedCharsets; + + allowedCharsets = new ArrayList<>(); + Map charsets = Charset.availableCharsets(); for (String s : charsets.keySet()) { Charset charset = charsets.get(s); @@ -56,9 +61,9 @@ public class Utf8Helper { int minLen = Integer.MAX_VALUE; try { for (int i = 0; i < 65536; i++) { - ByteBuffer buf = charset.encode("" + (char) i); int len = buf.remaining(); + if (len > maxLen) { maxLen = len; } @@ -68,12 +73,13 @@ public class Utf8Helper { } if ((minLen == maxLen && minLen == 1) || allowedVariableLengthCharsets.contains(s)) { allowedCharsets.add(s); - } + } } catch (UnsupportedOperationException ex) { //System.out.println(s + " ... ERROR"); } - } + + return allowedCharsets; } public static String urlDecode(String s) { diff --git a/src/com/jpexs/decompiler/flash/gui/tagtree/TagTreeContextMenu.java b/src/com/jpexs/decompiler/flash/gui/tagtree/TagTreeContextMenu.java index 26dda4086..1a4abd368 100644 --- a/src/com/jpexs/decompiler/flash/gui/tagtree/TagTreeContextMenu.java +++ b/src/com/jpexs/decompiler/flash/gui/tagtree/TagTreeContextMenu.java @@ -1,16 +1,16 @@ /* * Copyright (C) 2010-2023 JPEXS - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * 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. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -282,7 +282,7 @@ public class TagTreeContextMenu extends JPopupMenu { changeCharsetMenu = new JMenu(); JMenu currentCharsetMenu = changeCharsetMenu; int charsetCnt = 0; - for (String charsetStr : Utf8Helper.allowedCharsets) { + for (String charsetStr : Utf8Helper.getAllowedCharsets()) { if (charsetCnt == 30) { JMenu moreMenu = new JMenu(mainPanel.translate("contextmenu.more")); currentCharsetMenu.add(moreMenu); @@ -2249,8 +2249,8 @@ public class TagTreeContextMenu extends JPopupMenu { TreePath swfPath = mainPanel.tagTree.getFullModel().getTreePath(swf); FolderItem scriptsNode = (FolderItem) mainPanel.tagTree.getFullModel().getScriptsNode(swf); TreePath scriptsPath = swfPath.pathByAddingChild(scriptsNode); - - + + TreePath doinitPath = scriptsPath.pathByAddingChild(doinit); mainPanel.tagTree.setSelectionPath(doinitPath); mainPanel.tagTree.scrollPathToVisible(doinitPath); @@ -2388,7 +2388,7 @@ public class TagTreeContextMenu extends JPopupMenu { if (tags.get(i) instanceof DoInitActionTag) { DoInitActionTag doinit = (DoInitActionTag) tags.get(i); if (!exportedIds.contains(doinit.spriteId)) { - //this is #initpragma, make sure class is inserted before it + //this is #initpragma, make sure class is inserted before it insertPos = i; break; } @@ -2831,7 +2831,7 @@ public class TagTreeContextMenu extends JPopupMenu { private void cloneActionPerformed(ActionEvent e) { List items = getSelectedItems(); /* Currently useless since all selected items must have the same parent - * but a better way to detect for parent/child selection + * but a better way to detect for parent/child selection * could remove that limitation */ Set swfs = new HashSet<>(); @@ -3313,7 +3313,7 @@ public class TagTreeContextMenu extends JPopupMenu { while (spriteTag.getTimelined() instanceof DefineSpriteTag) { spriteTag = (DefineSpriteTag) spriteTag.getTimelined(); } - realTargetTimelined = spriteTag.getTimelined(); //should be SWF + realTargetTimelined = spriteTag.getTimelined(); //should be SWF realPosition = spriteTag; } }