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