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 ; // スルーしてfalseを返す 100 } 101 102 return flgRtn; 103 } 104 105 /** 106 * ファイル移動 107 * 108 * ファイルを指定先に移動します。 109 * 110 * @param afPath 移動先 111 * @return 成否フラグ 112 */ 113 public boolean move(final String afPath) { 114 boolean flgRtn = false; 115 116 try { 117 // 指定パスのファイルを、指定先に移動 from:jdk7 118 Files.move(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING); 119 flgRtn = true; 120 } catch (IOException ie) { 121 ; // スルーしてfalseを返す 122 } 123 return flgRtn; 124 } 125 126 /** 127 * 保存先のローカル判定。 128 * 129 * 判定結果を返します。 130 * trueの場合は、ローカル保存。 131 * falseの場合は、クラウドストレージに保存です。 132 * 133 * @return ローカルフラグ 134 */ 135 public boolean isLocal() { 136 return true; 137 } 138 139 /** 140 * カノニカルファイル取得。 141 * 142 * カノニカルファイル情報を取得します。 143 * 144 * @throws IOException ファイル関連エラー情報 145 * @return カノニカルファイル情報 146 */ 147 @Override 148 public FileOperation getCanonicalFile() throws IOException { 149 final String canonPath = getCanonicalPath(); 150 return new FileOperation(canonPath); 151 } 152 153 /** 154 * バケット名取得。 155 * 156 * バケット名を取得します。 157 * 158 * @return バケット名 159 */ 160 public String getBucket() { 161 return this.mybucket; 162 } 163 164 /** 165 * プラグイン名取得。 166 * 167 * プラグイン名を取得します。 168 * 169 * @return プラグイン名 170 */ 171 public String getPlugin() { 172 return this.myplugin; 173 } 174 175 /** 176 * プラグイン名のセット。 177 * 178 * プラグイン名をセットします。 179 * 180 * @param plugin プラグイン名 181 */ 182 protected void setPlugin( final String plugin ) { 183 myplugin = plugin; 184 } 185 186 /** 187 * このオブジェクトと他のオブジェクトが等しいかどうかを示します。 188 * インタフェース Comparable の 実装に関連して、再定義しています。 189 * 190 * @og.rev 7.2.9.4 (2020/11/20) spotbugs:スーパークラスの equals メソッドをオーバーライドしていないクラス 191 * 192 * @param object 比較対象の参照オブジェクト 193 * 194 * @return 引数に指定されたオブジェクトとこのオブジェクトが等しい場合は true、そうでない場合は false 195 */ 196 @Override 197 public boolean equals( final Object object ) { 198 return object instanceof File && super.equals( object ); // myplugin とmybucket は無視して、Fileとして比較します。 199 } 200 201// /** 202// * オブジェクトのハッシュコード値を返します。 203// * このメソッドは、java.io.File のハッシュ値を返すことで、equals メソッドとの整合性を取っています。 204// * 205// * @og.rev 7.2.9.4 (2020/11/20) spotbugs:equals メソッドは定義していますが hashCode メソッドは定義していないクラス 206// * 207// * @return このオブジェクトのハッシュコード値 208// */ 209// @Override 210// public int hashCode() { 211// return super.hashCode() ; 212// } 213 214// /** テスト用メソッドです。*/ 215// public static void main(String[] args) { 216// System.out.println("start"); 217// 218// try { 219// test01(); 220// }catch(IOException ie) { 221// System.out.println(ie); 222// } 223// 224// System.out.println("end"); 225// } 226// 227// public static void test01() throws IOException{ 228// File file = new FileOperation("test.txt"); 229// File file2 = file.getCanonicalFile(); 230// 231// System.out.println(file2.getClass()); 232// 233// FileOperation fo = (FileOperation)file2; 234// System.out.println(fo.getPath()); 235// } 236// 237// public static void writeTest() { 238// File file = new FileOperation("test.txt"); 239// FileOperation fileOperation = (FileOperation) file; 240//// FileOperation_AWS aws = (FileOperation_AWS)file; 241// // file.delete(); 242// 243// try( ByteArrayInputStream bais = new ByteArrayInputStream("テスト".getBytes())) { 244// fileOperation.write(bais); 245// } catch (IOException ie) { 246// System.out.println(ie); 247// } 248// } 249}