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.0.2.1 (2019/03/04)" ; 029 private static final long serialVersionUID = 702120190304L ; 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// public static void main(String[] args) { 189// System.out.println("start"); 190// 191// try { 192// test01(); 193// }catch(IOException ie) { 194// System.out.println(ie); 195// } 196// 197// System.out.println("end"); 198// } 199// 200// public static void test01() throws IOException{ 201// File file = new FileOperation("test.txt"); 202// File file2 = file.getCanonicalFile(); 203// 204// System.out.println(file2.getClass()); 205// 206// FileOperation fo = (FileOperation)file2; 207// System.out.println(fo.getPath()); 208// } 209// 210// public static void writeTest() { 211// File file = new FileOperation("test.txt"); 212// FileOperation fileOperation = (FileOperation) file; 213//// FileOperation_AWS aws = (FileOperation_AWS)file; 214// // file.delete(); 215// 216// try (ByteArrayInputStream bais = new ByteArrayInputStream("テスト".getBytes())) { 217// fileOperation.write(bais); 218// } catch (IOException ie) { 219// System.out.println(ie); 220// } 221// } 222}