OSDN Git Service

https://osdn.net/cvs/view/gokigen/MeMoMa/ から コピーしてくる。(+API27でビルドできるよう、少し調整。)
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / fileio / ExternalStorageFileUtility.java
1 package jp.sourceforge.gokigen.memoma.fileio;
2
3 import android.os.Environment;
4 import android.util.Log;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.text.SimpleDateFormat;
11 import java.util.Calendar;
12
13 import jp.sourceforge.gokigen.memoma.Main;
14
15 /**
16  *  外部ストレージにデータを記録するために使うユーティリティ
17  *  
18  * @author MRSa
19  */
20 public class ExternalStorageFileUtility 
21 {
22         private final int COPY_BUFFER_SIZE = 32768;
23         private final int BUFFER_MARGIN    = 4;
24
25         private String baseDirectory = "/";
26
27     /**
28      *   コンストラクタ
29      * 
30      */
31     public ExternalStorageFileUtility(String offsetDir)
32     {
33         /** ベースディレクトリの作成 (あれば何もしない) **/
34         prepareBaseDirectory(offsetDir);
35     }
36     
37     /**
38      *   ベースディレクトリの決定を行う
39      * 
40      */
41     private boolean prepareBaseDirectory(String offsetDir)
42     {
43         String gokigenDirectory = Environment.getExternalStorageDirectory().getPath() + "/Gokigen";
44         try
45         {
46             File baseDir = new File(gokigenDirectory);
47             if (baseDir.exists() == false)
48             {
49                 if (baseDir.mkdirs() == false)
50                 {
51                     // ベースディレクトリ作成失敗...終了する
52                     baseDirectory = Environment.getExternalStorageDirectory().getPath();
53                     baseDir = null;
54                     return (false);
55                 }
56             }
57             gokigenDirectory = gokigenDirectory + offsetDir;
58             baseDir = null;
59             baseDir = new File(gokigenDirectory);
60             if (baseDir.exists() == false)
61             {
62                 if (baseDir.mkdirs() == false)
63                 {
64                     // ベースディレクトリ作成失敗...終了する
65                     baseDirectory = Environment.getExternalStorageDirectory().getPath() + "/Gokigen";
66                     baseDir = null;
67                     return (false);                    
68                 }
69             }
70             baseDirectory = gokigenDirectory;
71             return (true);
72         }
73         catch (Exception ex)
74         {
75             Log.v(Main.APP_IDENTIFIER, "prepareBaseDirectory() : " + ex.getMessage());
76         }
77         baseDirectory = Environment.getExternalStorageDirectory().getPath();
78         return (false);
79     }
80
81     /**
82      *  ベースディレクトリを取得する
83      * @return  書き込みを行うアプリ用ベースディレクトリ
84      */
85     public String getGokigenDirectory()
86     {
87         return (baseDirectory);
88     }    
89     
90     /**
91      *  ディレクトリを作成する
92      * 
93      * @param dirName
94      * @return
95      */
96     public boolean makeDirectory(String dirName)
97     {
98         String makeDir = baseDirectory + "/" + dirName;
99         try
100         {
101             File dir = new File(makeDir);
102             if (dir.exists() == false)
103             {
104                 return (dir.mkdirs());
105             }
106             return (true);
107         }
108         catch (Exception ex)
109         {
110             Log.v(Main.APP_IDENTIFIER, "makeDirectory() : " + ex.getMessage());
111         }
112         return (false);
113     }
114
115     /**
116      *  記録ファイルをオープンする    
117      * @param fileName  ファイル名
118      * @param isAppend  追記モードでオープンするか?
119      * @return  ファイルストリーム
120      */
121     public FileOutputStream openFileStream(String fileName, boolean isAppend)
122     {
123         try
124         {
125             String targetName = baseDirectory + "/" + fileName;
126             FileOutputStream fileStream = new FileOutputStream(targetName, isAppend);
127             return (fileStream);
128         }
129         catch (Exception e)
130         {
131             Log.v(Main.APP_IDENTIFIER, "openFileStream() : " + e.getMessage());
132         }
133         return (null);
134     }    
135
136     /**
137      *   日単位でディレクトリを作成し、そこに書き込むためのファイル名を決定する
138      * @return ファイル名称
139      */
140     private String decideFileNameWithSpecifiedDate(String fileName, String year, String month, String date)
141     {
142         String directory = year;
143         
144         // 年のディレクトリを掘る
145         makeDirectory(year);
146
147         directory = directory + "/" + year + month;
148
149         // 年月のディレクトリを掘る
150         makeDirectory(directory);
151
152         directory = directory + "/" + year + month + date;
153
154         // 年月日のディレクトリを掘る
155         makeDirectory(directory);
156         
157         return (directory + "/" + year + month + date + "-" + fileName);
158     }
159
160     /**
161      *   日単位でディレクトリを作成し、そこに書き込むためのファイル名を決定する
162      * @return ファイル名称
163      */
164     public String decideFileNameWithDate(String fileName)
165     {
166         Calendar calendar = Calendar.getInstance();
167
168         SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
169         SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
170         SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
171
172         String year  = yearFormat.format(calendar.getTime());
173         String month = monthFormat.format(calendar.getTime());
174         String date  = dateFormat.format(calendar.getTime());
175
176         return (decideFileNameWithSpecifiedDate(fileName, year, month, date));
177     }
178
179     /**
180      *   日単位でディレクトリを作成し、そこに書き込むためのファイル名を決定する
181      * @return ファイル名称
182      */
183     public String decideFileNameWithSpecifiedDate(String fileName, int year, int month, int day)
184     {
185         String yearStr = year + "";
186         String monthStr = "";
187         if (month < 10)
188         {
189             monthStr = "0" + month;     
190         }
191         else
192         {
193                 monthStr = month + "";
194         }
195         String dayStr = "";
196         if (day < 10)
197         {
198             dayStr = "0" + day;
199         }
200         else
201         {
202                 dayStr = day + "";
203         }
204         return (decideFileNameWithSpecifiedDate(fileName, yearStr, monthStr, dayStr));
205     }
206
207     /**
208      *  日付ディレクトリ名を取得する
209      * @param year
210      * @param month
211      * @param date
212      * @return
213      */
214     public String decideDateDirectory(int year, int month, int date)
215     {
216         String addMonth = "";
217         String addDate  = "";
218         String directory = baseDirectory + "/" + year + "/";
219         if (month < 10)
220         {
221             addMonth =  "0";
222         }
223         if (date < 10)
224         {
225             addDate = "0";
226         }
227         directory = directory + year + addMonth + month + "/" + year + addMonth + month + addDate + date + "/";        
228         return (directory);
229     }
230
231     /**
232      * ファイルのコピー (kaniFilerから持ってきた...)
233      * 
234      * @param destFileName コピー先ファイル (full path)
235      * @param srcFileName  コピー元ファイル (full path)
236      */
237         public boolean copyFile(String destFileName, String srcFileName)
238         {
239                 File srcFile = null;
240                 File dstFile = null;
241                 
242                 boolean     ret = false;
243                 InputStream   is = null;
244                 OutputStream  os = null;
245
246                 if (destFileName == srcFileName)
247                 {
248                         // ファイル名が同じだった場合にはコピーを実行しない
249                         return (false);
250                 }
251                 
252                 try
253                 {
254                         srcFile = new File(srcFileName);
255                         if (srcFile.exists() != true)
256                         {
257                                 // ファイルが存在しなかった、、、終了する
258                                 return (false);
259                         }
260                         is = new FileInputStream(srcFile);
261
262                         long dataFileSize = srcFile.length();
263                         byte[] buffer = new byte[COPY_BUFFER_SIZE + BUFFER_MARGIN];
264
265                         dstFile = new File(destFileName);
266                         if (dstFile.exists() == true)
267                         {
268                                 // ファイルが存在した、、、削除して作り直す
269                                 dstFile.delete();
270                         }
271
272                         os = new FileOutputStream(dstFile);
273                         if ((is != null)&&(os != null))
274                         {
275                                 while (dataFileSize > 0)
276                                 {
277                                 int size = is.read(buffer, 0, COPY_BUFFER_SIZE);
278                                 if (size <= 0)
279                                 {
280                                         break;
281                                 }
282                                 os.write(buffer, 0, size);
283                                 }
284                         }
285                         os.flush();
286                         os.close();
287                         is.close();
288                         
289                         dstFile = null;
290                         srcFile = null;
291                         buffer = null;
292                         is = null;
293                         os = null;
294                         ret = true;
295                         System.gc();
296                 }
297                 catch (Exception e)
298                 {
299                         // 例外発生!!!
300                         try
301                         {
302                                 if (is != null)
303                                 {
304                                         is.close();
305                                 }
306                         }
307                         catch (Exception e2)
308                         {
309                                 //
310                         }
311                                 
312                         try
313                         {
314                                 if (os != null)
315                                 {
316                                         os.close();
317                                 }
318                         }
319                         catch (Exception e2)
320                         {
321                                 //
322                         }
323                         is = null;
324                         os = null;
325                         srcFile = null;
326                         dstFile = null;
327                         System.gc();
328
329                         return (false);
330                 }
331                 return (ret);
332         }
333 }