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