OSDN Git Service

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