001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.fukurou.model;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.BufferedInputStream;                                                     // 8.0.1.0 (2021/10/29)
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.io.InputStream;
024import java.nio.file.Files;
025import java.nio.file.Paths;
026import java.nio.file.StandardCopyOption;
027
028/**
029 * ファイル操作のインタフェース
030 *
031 * ローカルサーバ、クラウドストレージ(AWS,AZURE,BLUEMIX,ORACLE)のファイル操作用です。
032 * FileOperationFactoryを通して、インスタンスを生成可能です。
033 * Fileクラスを継承しているため、通常のFileとしても扱えます。
034 *
035 * @og.group ファイル操作
036 *
037 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
038 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
039 * @author oota
040 * @since       JDK7.0
041 */
042public class FileOperation extends File{
043        /** このプログラムのVERSION文字列を設定します。{@VALUE} */
044        private static final String VERSION = "8.0.1.0 (2021/10/29)" ;
045        private static final long serialVersionUID = 801020211029L ;
046
047        /** AWSのバケットなどを使用しない場合の記号 */
048        public static final String LOCAL = "LOCAL" ;                    // 8.0.1.0 (2021/10/29)
049
050//      private final String myplugin;          // プラグイン
051//      private final String mybucket;          // バケット
052
053        /**
054         * コンストラクタ
055         *
056         * 初期化処理。
057         *
058         * @param path ファイルパス
059         */
060        public FileOperation(final String path) {
061                super(path);
062        }
063
064//      /**
065//       * コンストラクタ
066//       *
067//       * FileOperationクラスでは、buketは使用しません。
068//       *
069//       * @og.rev 8.0.0.1 (2021/10/08) 削除
070//       *
071//       * @param       bucket  バケット名
072//       * @param       path    ファイルパス
073//       */
074//      public FileOperation(final String bucket, final String path) {
075//              this(path);
076//              mybucket = bucket;
077//      }
078
079        /**
080         * 書き込み処理(評価用)
081         *
082         * Fileを書き込みます。
083         *
084         * @og.rev 8.0.0.1 (2021/10/08) 新規追加
085         *
086         * @param       inFile  書き込みFile
087         * @throws      IOException     ファイル関連エラー情報
088         */
089        public void write(final File inFile) throws IOException {
090                Files.copy(inFile.toPath(), this.toPath(), StandardCopyOption.REPLACE_EXISTING);
091        }
092
093        /**
094         * 書き込み処理
095         *
096         * InputStreamのデータを書き込みます。
097         *
098         * @og.rev 8.0.1.0 (2021/10/29) Paths.get(this.getPath()) → this.toPath() に変更
099         *
100         * @param       is      書き込みデータのInputStream
101         * @throws      IOException     ファイル関連エラー情報
102         */
103        public void write(final InputStream is) throws IOException {
104                // InpustStreamを対象パスに出力
105//              Files.copy(is, Paths.get(this.getPath()), StandardCopyOption.REPLACE_EXISTING);
106                Files.copy(is, this.toPath(), StandardCopyOption.REPLACE_EXISTING);
107        }
108
109        /**
110         * 読み込み処理
111         *
112         * データを読み込み、InputStreamとして、返します。
113         *
114         * @og.rev 8.0.1.0 (2021/10/29) FileInputStream → BufferedInputStream に変更
115         *
116         * @return      読み込みデータのInputStream
117         * @throws      FileNotFoundException   ファイル非存在エラー情報
118         */
119        public InputStream read() throws FileNotFoundException {
120//              return new FileInputStream(this.getPath());
121                return new BufferedInputStream( new FileInputStream(this));
122        }
123
124        /**
125         * コピー処理
126         *
127         * ファイルを指定先にコピーします。
128         *
129         * @og.rev 8.0.1.0 (2021/10/29) Paths.get(this.getPath()) → this.toPath() に変更
130         *
131         * @param       afPath  コピー先
132         * @return      成否フラグ
133         */
134        public boolean copy(final String afPath) {
135                boolean flgRtn = false;
136
137                try {
138                        // 指定パスのファイルを、指定先にコピー from;jdk7
139//                      Files.copy(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
140                        Files.copy(this.toPath(), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
141                        flgRtn = true;
142                } catch (IOException ie) {
143                        System.err.println( ie.getMessage() );  // 8.0.0.0 (2021/07/31)
144//                      ;       // スルーしてfalseを返す
145                }
146
147                return flgRtn;
148        }
149
150        /**
151         * ファイル移動
152         *
153         * ファイルを指定先に移動します。
154         *
155         * @og.rev 8.0.1.0 (2021/10/29) Paths.get(this.getPath()) → this.toPath() に変更
156         *
157         * @param       afPath  移動先
158         * @return      成否フラグ
159         */
160        public boolean move(final String afPath) {
161                boolean flgRtn = false;
162
163                try {
164                        // 指定パスのファイルを、指定先に移動 from:jdk7
165//                      Files.move(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
166                        Files.move(this.toPath(), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
167                        flgRtn = true;
168                } catch (IOException ie) {
169                        System.err.println( ie.getMessage() );  // 8.0.0.0 (2021/07/31)
170//                      ;       // スルーしてfalseを返す
171                }
172                return flgRtn;
173        }
174
175//      /**
176//       * 保存先のローカル判定。
177//       *
178//       * 判定結果を返します。
179//       * trueの場合は、ローカル保存。
180//       * falseの場合は、クラウドストレージに保存です。
181//       *
182//       * @og.rev 8.0.0.1 (2021/10/08) 削除
183//       *
184//       * @return      ローカルフラグ
185//       */
186//      public boolean isLocal() {
187//              return true;
188//      }
189
190        /**
191         * 保存先のクラウド判定。
192         *
193         * 判定結果を返します。
194         * trueの場合は、クラウドストレージ保存。
195         * falseの場合は、ローカルに保存です。
196         *
197         * @og.rev 8.0.0.1 (2021/10/08) クラウド修正
198         *
199         * @return      クラウドならtrue
200         */
201        public boolean isCloud() {
202                return false;
203        }
204
205        /**
206         * カノニカルファイル取得。
207         *
208         * カノニカルファイル情報を取得します。
209         *
210         * @throws      IOException     ファイル関連エラー情報
211         * @return      カノニカルファイル情報
212         */
213        @Override
214        public FileOperation getCanonicalFile() throws IOException {
215                final String canonPath = getCanonicalPath();
216                return new FileOperation(canonPath);
217        }
218
219        /**
220         * バケット名取得。
221         *
222         * バケット名を取得します。
223         * 生のFileOperationは、null を返します。
224         * 継承先で実際の値を設定してください。
225         *
226         * @return      バケット名
227         */
228        public String getBucket() {
229//              return mybucket;
230                return null;
231        }
232
233        /**
234         * プラグイン名取得。
235         *
236         * プラグイン名を取得します。
237         * 生のFileOperationは、null を返します。
238         * 継承先で実際の値を設定してください。
239         *
240         * @return      プラグイン名
241         */
242        public String getPlugin() {
243//              return this.myplugin;
244                return null;
245        }
246
247//      /**
248//       * プラグイン名のセット。
249//       *
250//       * プラグイン名をセットします。
251//       *
252//       * @og.rev 8.0.0.1 (2021/10/08) 削除
253//       *
254//       * @param       plugin  プラグイン名
255//       */
256//      protected void setPlugin( final String plugin ) {
257//              myplugin = plugin;
258//      }
259}