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//      private final String myplugin;          // プラグイン
048//      private final String mybucket;          // バケット
049
050        /**
051         * コンストラクタ
052         *
053         * 初期化処理。
054         *
055         * @param path ファイルパス
056         */
057        public FileOperation(final String path) {
058                super(path);
059        }
060
061//      /**
062//       * コンストラクタ
063//       *
064//       * FileOperationクラスでは、buketは使用しません。
065//       *
066//       * @og.rev 8.0.0.1 (2021/10/08) 削除
067//       *
068//       * @param bucket バケット名
069//       * @param path ファイルパス
070//       */
071//      public FileOperation(final String bucket, final String path) {
072//              this(path);
073//              mybucket = bucket;
074//      }
075
076        /**
077         * 書き込み処理(評価用)
078         *
079         * Fileを書き込みます。
080         *
081         * @og.rev 8.0.0.1 (2021/10/08) 新規追加
082         * @param inFile 書き込みFile
083         * @throws IOException ファイル関連エラー情報
084         */
085        public void write(final File inFile) throws IOException {
086                Files.copy(inFile.toPath(), this.toPath(), StandardCopyOption.REPLACE_EXISTING);
087        }
088
089        /**
090         * 書き込み処理
091         *
092         * InputStreamのデータを書き込みます。
093         *
094         * @og.rev 8.0.1.0 (2021/10/29) Paths.get(this.getPath()) → this.toPath() に変更
095         *
096         * @param is 書き込みデータのInputStream
097         * @throws IOException ファイル関連エラー情報
098         */
099        public void write(final InputStream is) throws IOException {
100                // InpustStreamを対象パスに出力
101//              Files.copy(is, Paths.get(this.getPath()), StandardCopyOption.REPLACE_EXISTING);
102                Files.copy(is, this.toPath(), StandardCopyOption.REPLACE_EXISTING);
103        }
104
105        /**
106         * 読み込み処理
107         *
108         * データを読み込み、InputStreamとして、返します。
109         *
110         * @og.rev 8.0.1.0 (2021/10/29) FileInputStream → BufferedInputStream に変更
111         *
112         * @return 読み込みデータのInputStream
113         * @throws FileNotFoundException ファイル非存在エラー情報
114         */
115        public InputStream read() throws FileNotFoundException {
116//              return new FileInputStream(this.getPath());
117                return new BufferedInputStream( new FileInputStream(this));
118        }
119
120        /**
121         * コピー処理
122         *
123         * ファイルを指定先にコピーします。
124         *
125         * @og.rev 8.0.1.0 (2021/10/29) Paths.get(this.getPath()) → this.toPath() に変更
126         *
127         * @param afPath コピー先
128         * @return 成否フラグ
129         */
130        public boolean copy(final String afPath) {
131                boolean flgRtn = false;
132
133                try {
134                        // 指定パスのファイルを、指定先にコピー from;jdk7
135//                      Files.copy(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
136                        Files.copy(this.toPath(), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
137                        flgRtn = true;
138                } catch (IOException ie) {
139                        System.err.println( ie.getMessage() );  // 8.0.0.0 (2021/07/31)
140//                      ;       // スルーしてfalseを返す
141                }
142
143                return flgRtn;
144        }
145
146        /**
147         * ファイル移動
148         *
149         * ファイルを指定先に移動します。
150         *
151         * @og.rev 8.0.1.0 (2021/10/29) Paths.get(this.getPath()) → this.toPath() に変更
152         *
153         * @param afPath 移動先
154         * @return 成否フラグ
155         */
156        public boolean move(final String afPath) {
157                boolean flgRtn = false;
158
159                try {
160                        // 指定パスのファイルを、指定先に移動 from:jdk7
161//                      Files.move(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
162                        Files.move(this.toPath(), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
163                        flgRtn = true;
164                } catch (IOException ie) {
165                        System.err.println( ie.getMessage() );  // 8.0.0.0 (2021/07/31)
166//                      ;       // スルーしてfalseを返す
167                }
168                return flgRtn;
169        }
170
171//      /**
172//       * 保存先のローカル判定。
173//       *
174//       * 判定結果を返します。
175//       * trueの場合は、ローカル保存。
176//       * falseの場合は、クラウドストレージに保存です。
177//       *
178//       * @og.rev 8.0.0.1 (2021/10/08) 削除
179//       *
180//       * @return ローカルフラグ
181//       */
182//      public boolean isLocal() {
183//              return true;
184//      }
185
186        /**
187         * 保存先のクラウド判定。
188         *
189         * 判定結果を返します。
190         * trueの場合は、クラウドストレージ保存。
191         * falseの場合は、ローカルに保存です。
192         *
193         * @og.rev 8.0.0.1 (2021/10/08) クラウド修正
194         *
195         * @return クラウドならtrue
196         */
197        public boolean isCloud() {
198                return false;
199        }
200
201        /**
202         * カノニカルファイル取得。
203         *
204         * カノニカルファイル情報を取得します。
205         *
206         * @throws IOException ファイル関連エラー情報
207         * @return カノニカルファイル情報
208         */
209        @Override
210        public FileOperation getCanonicalFile() throws IOException {
211                final String canonPath = getCanonicalPath();
212                return new FileOperation(canonPath);
213        }
214
215        /**
216         * バケット名取得。
217         *
218         * バケット名を取得します。
219         * 生のFileOperationは、null を返します。
220         * 継承先で実際の値を設定してください。
221         *
222         * @return バケット名
223         */
224        public String getBucket() {
225//              return mybucket;
226                return null;
227        }
228
229        /**
230         * プラグイン名取得。
231         *
232         * プラグイン名を取得します。
233         * 生のFileOperationは、null を返します。
234         * 継承先で実際の値を設定してください。
235         *
236         * @return プラグイン名
237         */
238        public String getPlugin() {
239//              return this.myplugin;
240                return null;
241        }
242
243//      /**
244//       * プラグイン名のセット。
245//       *
246//       * プラグイン名をセットします。
247//       *
248//       * @og.rev 8.0.0.1 (2021/10/08) 削除
249//       *
250//       * @param plugin プラグイン名
251//       */
252//      protected void setPlugin( final String plugin ) {
253//              myplugin = plugin;
254//      }
255}