001package org.opengion.fukurou.model;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.IOException;
007import java.io.InputStream;
008import java.nio.file.Files;
009import java.nio.file.Paths;
010import java.nio.file.StandardCopyOption;
011
012/**
013 * ファイル操作のインタフェース
014 *
015 * ローカルサーバ、クラウドストレージ(AWS,AZURE,BLUEMIX,ORACLE)のファイル操作用です。
016 * FileOperationFactoryを通して、インスタンスを生成可能です。
017 * Fileクラスを継承しているため、通常のFileとしても扱えます。
018 *
019 * @og.group ファイル操作
020 *
021 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
022 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
023 * @author oota
024 * @since       JDK7.0
025 */
026public class FileOperation extends File{
027        //* このプログラムのVERSION文字列を設定します。{@VALUE} */
028        private static final String VERSION = "7.2.9.4 (2020/11/20)" ;
029        private static final long serialVersionUID = 729420201120L ;
030
031        private String myplugin;
032        private String mybucket;
033
034        /**
035         * コンストラクタ
036         *
037         * 初期化処理。
038         *
039         * @param path ファイルパス
040         */
041        public FileOperation(final String path) {
042                super(path);
043        }
044
045        /**
046         * コンストラクタ
047         *
048         * FileOperationクラスでは、buketは使用しません。
049         *
050         * @param bucket バケット名
051         * @param path ファイルパス
052         */
053        public FileOperation(final String bucket, final String path) {
054                this(path);
055                this.mybucket = bucket;
056        }
057
058        /**
059         * 書き込み処理
060         *
061         * InputStreamのデータを書き込みます。
062         *
063         * @param is 書き込みデータのInputStream
064         * @throws IOException ファイル関連エラー情報
065         */
066        public void write(final InputStream is) throws IOException {
067                // InpustStreamを対象パスに出力
068                Files.copy(is, Paths.get(this.getPath()), StandardCopyOption.REPLACE_EXISTING);
069        }
070
071        /**
072         * 読み込み処理
073         *
074         * データを読み込み、InputStreamとして、返します。
075         *
076         * @return 読み込みデータのInputStream
077         * @throws FileNotFoundException ファイル非存在エラー情報
078         */
079        public InputStream read() throws FileNotFoundException {
080                return new FileInputStream(this.getPath());
081        }
082
083        /**
084         * コピー処理
085         *
086         * ファイルを指定先にコピーします。
087         *
088         * @param afPath コピー先
089         * @return 成否フラグ
090         */
091        public boolean copy(final String afPath) {
092                boolean flgRtn = false;
093
094                try {
095                        // 指定パスのファイルを、指定先にコピー from;jdk7
096                        Files.copy(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
097                        flgRtn = true;
098                } catch (IOException ie) {
099                        System.err.println( ie.getMessage() );  // 8.0.0.0 (2021/07/31)
100//                      ;       // スルーしてfalseを返す
101                }
102
103                return flgRtn;
104        }
105
106        /**
107         * ファイル移動
108         *
109         * ファイルを指定先に移動します。
110         *
111         * @param afPath 移動先
112         * @return 成否フラグ
113         */
114        public boolean move(final String afPath) {
115                boolean flgRtn = false;
116
117                try {
118                        // 指定パスのファイルを、指定先に移動 from:jdk7
119                        Files.move(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
120                        flgRtn = true;
121                } catch (IOException ie) {
122                        System.err.println( ie.getMessage() );  // 8.0.0.0 (2021/07/31)
123//                      ;       // スルーしてfalseを返す
124                }
125                return flgRtn;
126        }
127
128        /**
129         * 保存先のローカル判定。
130         *
131         * 判定結果を返します。
132         * trueの場合は、ローカル保存。
133         * falseの場合は、クラウドストレージに保存です。
134         *
135         * @return ローカルフラグ
136         */
137        public boolean isLocal() {
138                return true;
139        }
140
141        /**
142         * カノニカルファイル取得。
143         *
144         * カノニカルファイル情報を取得します。
145         *
146         * @throws IOException ファイル関連エラー情報
147         * @return カノニカルファイル情報
148         */
149        @Override
150        public FileOperation getCanonicalFile() throws IOException {
151                final String canonPath = getCanonicalPath();
152                return new FileOperation(canonPath);
153        }
154
155        /**
156         * バケット名取得。
157         *
158         * バケット名を取得します。
159         *
160         * @return バケット名
161         */
162        public String getBucket() {
163                return this.mybucket;
164        }
165
166        /**
167         * プラグイン名取得。
168         *
169         * プラグイン名を取得します。
170         *
171         * @return プラグイン名
172         */
173        public String getPlugin() {
174                return this.myplugin;
175        }
176
177        /**
178         * プラグイン名のセット。
179         *
180         * プラグイン名をセットします。
181         *
182         * @param plugin プラグイン名
183         */
184        protected void setPlugin( final String plugin ) {
185                myplugin = plugin;
186        }
187
188//      /**
189//       * このオブジェクトと他のオブジェクトが等しいかどうかを示します。
190//       * インタフェース Comparable の 実装に関連して、再定義しています。
191//       *
192//       * @og.rev 7.2.9.4 (2020/11/20) spotbugs:スーパークラスの equals メソッドをオーバーライドしていないクラス
193//       *
194//       * @param   object 比較対象の参照オブジェクト
195//       *
196//       * @return      引数に指定されたオブジェクトとこのオブジェクトが等しい場合は true、そうでない場合は false
197//       */
198//      @Override
199//      public boolean equals( final Object object ) {
200//              return object instanceof File && super.equals( object );        // myplugin とmybucket は無視して、Fileとして比較します。
201//      }
202
203//      /**
204//       * オブジェクトのハッシュコード値を返します。
205//       * このメソッドは、java.io.File のハッシュ値を返すことで、equals メソッドとの整合性を取っています。
206//       *
207//       * @og.rev 7.2.9.4 (2020/11/20) spotbugs:equals メソッドは定義していますが hashCode メソッドは定義していないクラス
208//       * @og.rev 8.0.0.0 (2021/07/31) Overriding method merely calls super
209//       *
210//       * @return  このオブジェクトのハッシュコード値
211//       */
212//      @Override
213//      public int hashCode() {
214//              return super.hashCode() ;                               // PMD:Overriding method merely calls super が出る。多分定義不要
215//      }
216
217//      /** テスト用メソッドです。*/
218//      public static void main(String[] args) {
219//              System.out.println("start");
220//
221//              try {
222//                      test01();
223//              }catch(IOException ie) {
224//                      System.out.println(ie);
225//              }
226//
227//              System.out.println("end");
228//      }
229//
230//      public static void test01() throws IOException{
231//              File file = new FileOperation("test.txt");
232//              File file2 = file.getCanonicalFile();
233//
234//              System.out.println(file2.getClass());
235//
236//              FileOperation fo = (FileOperation)file2;
237//              System.out.println(fo.getPath());
238//      }
239//
240//      public static void writeTest() {
241//              File file = new FileOperation("test.txt");
242//              FileOperation fileOperation = (FileOperation) file;
243////            FileOperation_AWS aws = (FileOperation_AWS)file;
244//              //              file.delete();
245//
246//              try( ByteArrayInputStream bais = new ByteArrayInputStream("テスト".getBytes())) {
247//                      fileOperation.write(bais);
248//              } catch (IOException ie) {
249//                      System.out.println(ie);
250//              }
251//      }
252}