OSDN Git Service

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