001package org.opengion.fukurou.model;
002
003import java.io.File;
004import java.io.FileFilter;
005import java.io.FileNotFoundException;
006import java.io.IOException;
007import java.io.InputStream;
008
009/**
010 * CloudFileOperation用のファイル情報の格納クラス
011 * 
012 * listFilesで取得した、ディレクトリとファイル一覧情報を格納します。
013 * 
014 * パフォーマンスや分かりやすさを考慮してCloudFileOperationからは分離しています
015 * 
016 * @og.group ファイル操作
017 * 
018 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
019 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
020 * @author oota
021 * @since JDK7.0
022 */
023public class FileOperationInfo extends CloudFileOperation {
024        //* このプログラムのVERSION文字列を設定します。{@VALUE} */
025        private static final String VERSION = "7.0.2.1 (2019/03/04)" ;
026        private static final long serialVersionUID = 702120190304L ;
027
028        /** クラス変数 */
029        private final String plugin;
030
031        private long size;
032        private long lastModified;
033        private boolean isFile;
034        private boolean isDirectory;
035        private FileOperation file;
036        
037        /**
038         * コンストラクタ
039         * 
040         * 生成時の初期処理。
041         * 
042         * @param plugin プラグイン名
043         * @param bucket バケット名
044         * @param path ファイルパス
045         */
046        public FileOperationInfo(final String plugin, final String bucket, final String path) {
047                super(bucket, path);
048                this.plugin = plugin;
049                size = 0;
050                lastModified = 0;
051                isFile = false;
052                isDirectory = false;
053                file = null;
054        }
055
056        /**
057         * FileOperationクラスの生成
058         * 
059         * 呼び出し時に、FileOperationインスタンスが未生成の場合は、
060         * 生成を行います。
061         */
062        private void setFileOperation() {
063                if(file == null) {
064                        file = FileOperationFactory.newStorageOperation( plugin, conBucket, conPath );
065                }
066        }
067        
068        /** Method */
069        /**
070         * 書き込み処理
071         * 
072         * InputStreamのデータを書き込みます。
073         * 
074         * @param is 書き込みデータのInputStream
075         * @throws IOException ファイル関連エラー情報
076         */
077        @Override
078        public void write(final InputStream is) throws IOException {
079                setFileOperation();
080                file.write(is);
081        }
082
083        /**
084         * 読み込み処理
085         * 
086         * データを読み込み、InputStreamとして、返します。
087         * 
088         * @return 読み込みデータのInputStream
089         * @throws FileNotFoundException ファイル非存在エラー情報
090         */
091        @Override
092        public InputStream read() throws FileNotFoundException {
093                setFileOperation();
094                return file.read();
095        }
096
097        /**
098         * 削除処理
099         * 
100         * ファイルを削除します。
101         * 
102         * @return 成否フラグ
103         */
104        @Override
105        public boolean delete() {
106                setFileOperation();
107                return file.delete();
108        }
109        
110        /**
111         * コピー処理
112         * 
113         * ファイルを指定先に、コピーします。
114         * 
115         * @param afPath コピー先
116         * @return 成否フラグ
117         */
118        @Override
119        public boolean copy(final String afPath) {
120                setFileOperation();
121                return file.copy(afPath);
122        }
123
124        /**
125         * 一覧取得
126         * 
127         * 1つ下の、ディレクトリ・ファイル一覧を取得します。
128         * 
129         * @param filter フィルタ情報
130         * @return ファイル一覧
131         */
132        @Override
133        public File[] listFiles(final FileFilter filter) {
134                setFileOperation();
135                return file.listFiles(filter);
136        }
137
138        /**
139         * ファイルサイズ取得
140         * 
141         * ファイルサイズを取得します。
142         * 
143         * @return ファイルサイズ
144         */
145        @Override
146        public long length() {
147                return size;
148        }
149
150        /**
151         * ファイルサイズ設定
152         * 
153         * ファイルサイズを設定します。
154         * 
155         * @param size ファイルサイズ
156         */
157        public void setSize(final long size) {
158                this.size = size;
159        }
160
161        /**
162         * 最終更新時刻の取得
163         * 
164         * 最終更新時刻を取得します。
165         * 
166         * @return 最終更新時刻
167         */
168        @Override
169        public long lastModified() {
170                return lastModified;
171        }
172
173        /**
174         * 最終更新時刻の設定
175         * 
176         * 最終更新時刻を設定します。
177         * 
178         * @param lastModified 最終更新時刻
179         */
180        public void setLastModifiedValue(final long lastModified) {
181                this.lastModified = lastModified;
182        }
183
184        /**
185         * ファイル判定取得
186         * 
187         * ファイルであるかの判定を返します。
188         * 
189         * @return ファイル判定
190         */
191        @Override
192        public boolean isFile() {
193                return isFile;
194        }
195
196        /**
197         * ファイル判定設定
198         * 
199         * ファイルであるかの判定を設定します。
200         * 
201         * @param isFile ファイル判定
202         */
203        public void setFile(final boolean isFile) {
204                this.isFile = isFile;
205        }
206
207        /**
208         * ディレクトリ判定取得
209         * 
210         * ディレクトリであるかの判定を返します。
211         * 
212         * @return ディレクトリ判定
213         */
214        @Override
215        public boolean isDirectory() {
216                return isDirectory;
217        }
218
219        /**
220         * ディレクトリ判定設定
221         * 
222         * ディレクトリであるかの判定を設定します。
223         * 
224         * @param isDirectory ディレクトリ判定
225         */
226        public void setDirectory(final boolean isDirectory) {
227                this.isDirectory = isDirectory;
228        }
229
230        /**
231         * 親情報の取得
232         * 
233         * 親情報を返します。
234         * 
235         * @return 親情報
236         */
237        @Override
238        public File getParentFile() {
239                 return  FileOperationFactory.newStorageOperation( file , this.getParent() );
240        }
241
242//      // テスト用メソッドです
243//      public static void main(String[] args) {
244//              System.out.println("start");
245//              
246//              FileOperation file = new FileOperationInfo("aws", "otest20190205", "sample/test.txt");
247//              
248//              File parent = file.getParentFile();
249//              System.out.println(parent.getPath());
250//              System.out.println(parent.isDirectory());
251//              System.out.println(parent.isFile());
252//              
253//              System.out.println("end");
254//      }
255}