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.FileNotFoundException; 021import java.io.IOException; 022import java.io.InputStream; 023import java.nio.file.Files; 024import java.nio.file.Paths; 025import java.nio.file.StandardCopyOption; 026 027/** 028 * ファイル操作のインタフェース 029 * 030 * ローカルサーバ、クラウドストレージ(AWS,AZURE,BLUEMIX,ORACLE)のファイル操作用です。 031 * FileOperationFactoryを通して、インスタンスを生成可能です。 032 * Fileクラスを継承しているため、通常のFileとしても扱えます。 033 * 034 * @og.group ファイル操作 035 * 036 * @og.rev 5.10.8.0 (2019/02/01) 新規作成 037 * @og.rev 5.10.9.0 (2019/03/01) 変更対応 038 * @author oota 039 * @since JDK7.0 040 */ 041public class FileOperation extends File{ 042 //* このプログラムのVERSION文字列を設定します。{@VALUE} */ 043 private static final String VERSION = "7.2.9.4 (2020/11/20)" ; 044 private static final long serialVersionUID = 729420201120L ; 045 046 private String myplugin; // プラグイン 047 private String mybucket; // バケット 048 049 /** 050 * コンストラクタ 051 * 052 * 初期化処理。 053 * 054 * @param path ファイルパス 055 */ 056 public FileOperation(final String path) { 057 super(path); 058 } 059 060 /** 061 * コンストラクタ 062 * 063 * FileOperationクラスでは、buketは使用しません。 064 * 065 * @param bucket バケット名 066 * @param path ファイルパス 067 */ 068 public FileOperation(final String bucket, final String path) { 069 this(path); 070 this.mybucket = bucket; 071 } 072 073 /** 074 * 書き込み処理 075 * 076 * InputStreamのデータを書き込みます。 077 * 078 * @param is 書き込みデータのInputStream 079 * @throws IOException ファイル関連エラー情報 080 */ 081 public void write(final InputStream is) throws IOException { 082 // InpustStreamを対象パスに出力 083 Files.copy(is, Paths.get(this.getPath()), StandardCopyOption.REPLACE_EXISTING); 084 } 085 086 /** 087 * 読み込み処理 088 * 089 * データを読み込み、InputStreamとして、返します。 090 * 091 * @return 読み込みデータのInputStream 092 * @throws FileNotFoundException ファイル非存在エラー情報 093 */ 094 public InputStream read() throws FileNotFoundException { 095 return new FileInputStream(this.getPath()); 096 } 097 098 /** 099 * コピー処理 100 * 101 * ファイルを指定先にコピーします。 102 * 103 * @param afPath コピー先 104 * @return 成否フラグ 105 */ 106 public boolean copy(final String afPath) { 107 boolean flgRtn = false; 108 109 try { 110 // 指定パスのファイルを、指定先にコピー from;jdk7 111 Files.copy(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING); 112 flgRtn = true; 113 } catch (IOException ie) { 114 System.err.println( ie.getMessage() ); // 8.0.0.0 (2021/07/31) 115// ; // スルーしてfalseを返す 116 } 117 118 return flgRtn; 119 } 120 121 /** 122 * ファイル移動 123 * 124 * ファイルを指定先に移動します。 125 * 126 * @param afPath 移動先 127 * @return 成否フラグ 128 */ 129 public boolean move(final String afPath) { 130 boolean flgRtn = false; 131 132 try { 133 // 指定パスのファイルを、指定先に移動 from:jdk7 134 Files.move(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING); 135 flgRtn = true; 136 } catch (IOException ie) { 137 System.err.println( ie.getMessage() ); // 8.0.0.0 (2021/07/31) 138// ; // スルーしてfalseを返す 139 } 140 return flgRtn; 141 } 142 143 /** 144 * 保存先のローカル判定。 145 * 146 * 判定結果を返します。 147 * trueの場合は、ローカル保存。 148 * falseの場合は、クラウドストレージに保存です。 149 * 150 * @return ローカルフラグ 151 */ 152 public boolean isLocal() { 153 return true; 154 } 155 156 /** 157 * カノニカルファイル取得。 158 * 159 * カノニカルファイル情報を取得します。 160 * 161 * @throws IOException ファイル関連エラー情報 162 * @return カノニカルファイル情報 163 */ 164 @Override 165 public FileOperation getCanonicalFile() throws IOException { 166 final String canonPath = getCanonicalPath(); 167 return new FileOperation(canonPath); 168 } 169 170 /** 171 * バケット名取得。 172 * 173 * バケット名を取得します。 174 * 175 * @return バケット名 176 */ 177 public String getBucket() { 178 return this.mybucket; 179 } 180 181 /** 182 * プラグイン名取得。 183 * 184 * プラグイン名を取得します。 185 * 186 * @return プラグイン名 187 */ 188 public String getPlugin() { 189 return this.myplugin; 190 } 191 192 /** 193 * プラグイン名のセット。 194 * 195 * プラグイン名をセットします。 196 * 197 * @param plugin プラグイン名 198 */ 199 protected void setPlugin( final String plugin ) { 200 myplugin = plugin; 201 } 202 203// /** 204// * このオブジェクトと他のオブジェクトが等しいかどうかを示します。 205// * インタフェース Comparable の 実装に関連して、再定義しています。 206// * 207// * @og.rev 7.2.9.4 (2020/11/20) spotbugs:スーパークラスの equals メソッドをオーバーライドしていないクラス 208// * 209// * @param object 比較対象の参照オブジェクト 210// * 211// * @return 引数に指定されたオブジェクトとこのオブジェクトが等しい場合は true、そうでない場合は false 212// */ 213// @Override 214// public boolean equals( final Object object ) { 215// return object instanceof File && super.equals( object ); // myplugin とmybucket は無視して、Fileとして比較します。 216// } 217 218// /** 219// * オブジェクトのハッシュコード値を返します。 220// * このメソッドは、java.io.File のハッシュ値を返すことで、equals メソッドとの整合性を取っています。 221// * 222// * @og.rev 7.2.9.4 (2020/11/20) spotbugs:equals メソッドは定義していますが hashCode メソッドは定義していないクラス 223// * @og.rev 8.0.0.0 (2021/07/31) Overriding method merely calls super 224// * 225// * @return このオブジェクトのハッシュコード値 226// */ 227// @Override 228// public int hashCode() { 229// return super.hashCode() ; // PMD:Overriding method merely calls super が出る。多分定義不要 230// } 231 232// /** テスト用メソッドです。*/ 233// public static void main(String[] args) { 234// System.out.println("start"); 235// 236// try { 237// test01(); 238// }catch(IOException ie) { 239// System.out.println(ie); 240// } 241// 242// System.out.println("end"); 243// } 244// 245// public static void test01() throws IOException{ 246// File file = new FileOperation("test.txt"); 247// File file2 = file.getCanonicalFile(); 248// 249// System.out.println(file2.getClass()); 250// 251// FileOperation fo = (FileOperation)file2; 252// System.out.println(fo.getPath()); 253// } 254// 255// public static void writeTest() { 256// File file = new FileOperation("test.txt"); 257// FileOperation fileOperation = (FileOperation) file; 258//// FileOperation_AWS aws = (FileOperation_AWS)file; 259// // file.delete(); 260// 261// try( ByteArrayInputStream bais = new ByteArrayInputStream("テスト".getBytes())) { 262// fileOperation.write(bais); 263// } catch (IOException ie) { 264// System.out.println(ie); 265// } 266// } 267}