OSDN Git Service

move getAppSetDir().
authorOlyutorskii <olyutorskii@users.osdn.me>
Sun, 3 May 2020 05:09:51 +0000 (14:09 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Sun, 3 May 2020 05:09:51 +0000 (14:09 +0900)
src/main/java/jp/sfjp/jindolf/config/ConfigDirUtils.java
src/main/java/jp/sfjp/jindolf/config/ConfigStore.java
src/main/java/jp/sfjp/jindolf/config/FileUtils.java

index 43b8de4..1b862c1 100644 (file)
@@ -7,11 +7,10 @@
 
 package jp.sfjp.jindolf.config;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -37,8 +36,6 @@ public final class ConfigDirUtils{
     private static final String RES_DIR = "resources";
     private static final String RES_README = RES_DIR + "/README.txt";
 
-    private static final Charset CHARSET_README = StandardCharsets.UTF_8;
-
     private static final String MSG_POST =
             "<ul>"
             + "<li><code>" + CmdOption.OPT_CONFDIR + "</code>"
@@ -108,7 +105,8 @@ public final class ConfigDirUtils{
      *
      * <p>閉じるまで待つ。
      *
-     * @param seq メッセージtxt*/
+     * @param seq メッセージtxt
+     */
     private static void showWarnMessage(String txt){
         JOptionPane pane;
         pane = new JOptionPane(txt, JOptionPane.WARNING_MESSAGE);
@@ -122,7 +120,8 @@ public final class ConfigDirUtils{
      *
      * <p>閉じるまで待つ。
      *
-     * @param seq メッセージtxt*/
+     * @param seq メッセージtxt
+     */
     private static void showInfoMessage(String txt){
         JOptionPane pane;
         pane = new JOptionPane(txt, JOptionPane.INFORMATION_MESSAGE);
@@ -300,6 +299,31 @@ public final class ConfigDirUtils{
     }
 
     /**
+     * アプリケーション設定ディレクトリを返す。
+     *
+     * <p>存在の有無、アクセスの可否は関知しない。
+     *
+     * <p>WindowsやLinuxではホームディレクトリ。
+     * Mac OS X ではさらにホームディレクトリの下の
+     * "Library/Application Support/"
+     *
+     * @return アプリケーション設定ディレクトリ
+     */
+    public static Path getAppSetDir(){
+        Path home = FileUtils.getHomeDirectory();
+        if(home == null) return null;
+
+        Path result = home;
+
+        if(FileUtils.isMacOSXFs()){
+            result = result.resolve("Library");
+            result = result.resolve("Application Support");
+        }
+
+        return result;
+    }
+
+    /**
      * 暗黙的な設定格納ディレクトリを返す。
      *
      * <ul>
@@ -322,8 +346,6 @@ public final class ConfigDirUtils{
      * @return 設定格納ディレクトリ
      */
     public static Path getImplicitConfigDirectory(){
-        Path result;
-
         Path jarParent = FileUtils.getJarDirectory();
         if(jarParent != null && FileUtils.isAccessibleDirectory(jarParent)){
             Path confPath = jarParent.resolve(JINCONF);
@@ -332,9 +354,10 @@ public final class ConfigDirUtils{
             }
         }
 
-        Path appset = FileUtils.getAppSetDir();
+        Path appset = getAppSetDir();
         if(appset == null) return null;
 
+        Path result;
         if(FileUtils.isMacOSXFs() || FileUtils.isWindowsOSFs()){
             result = appset.resolve(JINCONF);
         }else{
@@ -428,10 +451,6 @@ public final class ConfigDirUtils{
     public static void buildImageCacheDir(Path imgCacheDir){
         if(Files.exists(imgCacheDir)) return;
 
-        String jsonRes = "resources/image/avatarCache.json";
-        InputStream is = ResourceManager.getResourceAsStream(jsonRes);
-        if(is == null) return;
-
         try{
             Files.createDirectories(imgCacheDir);
         }catch(IOException e){
@@ -439,11 +458,16 @@ public final class ConfigDirUtils{
         }
         ConfigDirUtils.checkDirPerm(imgCacheDir);
 
+        String jsonRes = "resources/image/avatarCache.json";
+        InputStream is = ResourceManager.getResourceAsStream(jsonRes);
+        if(is == null) return;
+
         Path cachePath = imgCacheDir;
         Path jsonLeaf = Paths.get("avatarCache.json");
         Path path = cachePath.resolve(jsonLeaf);
-        try{
-            Files.copy(is, path);
+
+        try(InputStream bis = new BufferedInputStream(is)){
+            Files.copy(bis, path);
         }catch(IOException e){
             abortCantAccessConfigDir(path);
         }
@@ -478,14 +502,12 @@ public final class ConfigDirUtils{
 
         showDialog(pane);
 
-        Object result = pane.getValue();
-        if(result == null) return false;
-        else if( ! (result instanceof Integer) ) return false;
+        Object val = pane.getValue();
+        if( ! (val instanceof Integer) ) return false;
+        int ival = (Integer) val;
+        boolean result = ival == JOptionPane.YES_OPTION;
 
-        int ival = (Integer) result;
-        if(ival == JOptionPane.YES_OPTION) return true;
-
-        return false;
+        return result;
     }
 
     /**
@@ -587,11 +609,11 @@ public final class ConfigDirUtils{
     private static void touchReadme(Path path){
         Path readme = path.resolve(FILE_README);
 
-        InputStream resReadme;
-        resReadme = ResourceManager.getResourceAsStream(RES_README);
+        InputStream resReadme =
+                ResourceManager.getResourceAsStream(RES_README);
 
-        try{
-            Files.copy(resReadme, readme);
+        try(InputStream bis = new BufferedInputStream(resReadme)){
+            Files.copy(bis, readme);
         }catch(IOException e){
             abortCantWrite(readme);
         }
index 40b5c9e..b4745bf 100644 (file)
@@ -78,12 +78,12 @@ public class ConfigStore {
      * コンストラクタ。
      *
      * @param useStoreFile 設定ディレクトリ内への
-     * データセーブ機能を使うならtrue
+     *     データセーブ機能を使うならtrue
      * @param isImplicitPath 起動コマンドラインから指定された
-     * 設定ディレクトリを用いる場合false。
-     * trueならデフォルトの設定ディレクトリが用いられる。
+     *     設定ディレクトリを用いる場合false。
+     *     trueならデフォルトの設定ディレクトリが用いられる。
      * @param configDirPath 設定ディレクトリ名。
-     * 設定ディレクトリを使わない場合は無視される。
+     *     設定ディレクトリを使わない場合は無視される。
      */
     public ConfigStore(boolean useStoreFile,
                        boolean isImplicitPath,
index bf77b8a..d3ec293 100644 (file)
@@ -90,13 +90,12 @@ public final class FileUtils{
     }
 
     /**
-     * クラスがローカルファイルからロードされたのであれば
-     * そのファイルを返す。
+     * クラスのロード元のURLを返す。
      *
-     * @param klass 任意のクラス
-     * @return ロード元ファイル。見つからなければnull。
+     * @param klass クラス
+     * @return ロード元URL。不明ならnull
      */
-    public static Path getClassSourceFile(Class<?> klass){
+    public static URL getClassSourceUrl(Class<?> klass){
         ProtectionDomain domain = klass.getProtectionDomain();
         if(domain == null) return null;
 
@@ -104,6 +103,19 @@ public final class FileUtils{
         if(src == null) return null;
 
         URL location = src.getLocation();
+
+        return location;
+    }
+
+    /**
+     * クラスがローカルファイルからロードされたのであれば
+     * そのファイルを返す。
+     *
+     * @param klass 任意のクラス
+     * @return ロード元ファイル。見つからなければnull。
+     */
+    public static Path getClassSourcePath(Class<?> klass){
+        URL location = getClassSourceUrl(klass);
         if(location == null) return null;
 
         String scheme = location.getProtocol();
@@ -125,6 +137,9 @@ public final class FileUtils{
     /**
      * すでに存在するJARファイルか判定する。
      *
+     * <p>ファイルがすでに通常ファイルとしてローカルに存在し、
+     * ファイル名の拡張子が「.jar」であれば真と判定される。
+     *
      * @param path 任意のファイル
      * @return すでに存在するJARファイルであればtrue
      */
@@ -133,7 +148,9 @@ public final class FileUtils{
         if( ! Files.exists(path) ) return false;
         if( ! Files.isRegularFile(path) ) return false;
 
-        String leafName = path.getFileName().toString();
+        Path leaf = path.getFileName();
+        assert leaf != null;
+        String leafName = leaf.toString();
         boolean result = leafName.matches("^.+\\.[jJ][aA][rR]$");
 
         return result;
@@ -148,7 +165,7 @@ public final class FileUtils{
      *     JARが見つからない、もしくはロード元がJARファイルでなければnull。
      */
     public static Path getJarDirectory(Class<?> klass){
-        Path jarFile = getClassSourceFile(klass);
+        Path jarFile = getClassSourcePath(klass);
         if(jarFile == null) return null;
 
         if( ! isExistsJarFile(jarFile) ){
@@ -195,11 +212,8 @@ public final class FileUtils{
 
         osName = osName.toLowerCase(Locale.ROOT);
 
-        if(osName.startsWith("mac os x")){
-            return true;
-        }
-
-        return false;
+        boolean result = osName.startsWith("mac os x");
+        return result;
     }
 
     /**
@@ -215,35 +229,7 @@ public final class FileUtils{
 
         osName = osName.toLowerCase(Locale.ROOT);
 
-        if(osName.startsWith("windows")){
-            return true;
-        }
-
-        return false;
-    }
-
-    /**
-     * アプリケーション設定ディレクトリを返す。
-     *
-     * <p>存在の有無、アクセスの可否は関知しない。
-     *
-     * <p>WindowsやLinuxではホームディレクトリ。
-     * Mac OS X ではさらにホームディレクトリの下の
-     * "Library/Application Support/"
-     *
-     * @return アプリケーション設定ディレクトリ
-     */
-    public static Path getAppSetDir(){
-        Path home = getHomeDirectory();
-        if(home == null) return null;
-
-        Path result = home;
-
-        if(isMacOSXFs()){
-            result = result.resolve("Library");
-            result = result.resolve("Application Support");
-        }
-
+        boolean result = osName.startsWith("windows");
         return result;
     }