55import android .content .pm .PackageManager ;
66
77import org .apache .commons .io .FileUtils ;
8+ import org .apache .commons .io .FilenameUtils ;
89import org .apache .commons .io .IOUtils ;
910
1011import java .io .File ;
2021
2122public abstract class StringDownloader extends Downloader {
2223 private File storageFile = null ;
24+ private String assetPath ;
25+ private Context context ;
2326
2427 public StringDownloader (OkHttpClient okHttpClient ) {
2528 super (okHttpClient );
@@ -32,10 +35,11 @@ public StringDownloader(OkHttpClient okHttpClient) {
3235 * uses the app files data unless the app gets updated.
3336 */
3437 public void setBundledData (Context context , String assetPath , File storageFile ) {
38+ this .context = context ;
3539 this .storageFile = storageFile ;
40+ this .assetPath = assetPath ;
3641
3742 try {
38- InputStream bundledDataInputStream = context .getAssets ().open (assetPath );
3943 PackageInfo packageInfo = context .getPackageManager ().getPackageInfo (context .getPackageName (), 0 );
4044
4145 //you can not check for file modified time in the android assets folder,
@@ -46,28 +50,45 @@ public void setBundledData(Context context, String assetPath, File storageFile)
4650 loadFromSavedData ();
4751 } else {
4852 Logger .d ("Using initial seed: %s" , storageFile .getAbsolutePath ());
49- onDownloadFinished ( IOUtils . toString ( bundledDataInputStream , Charset . forName ( "UTF-8" )) );
53+ loadFromBundledData ( );
5054 }
5155 } catch (IOException | PackageManager .NameNotFoundException e ) {
5256 Logger .e ("Could not load saved data: %s" , storageFile .getAbsolutePath ());
57+ loadFromBundledData ();
5358 }
5459 }
5560
5661 private void loadFromSavedData () throws IOException {
5762 FileInputStream fis = new FileInputStream (storageFile );
5863 Logger .d ("Using saved data: %s" , storageFile .getAbsolutePath ());
59- onDownloadFinished (IOUtils .toString (fis , Charset .forName ("UTF-8" )));
64+ String savedData = IOUtils .toString (fis , Charset .forName ("UTF-8" ));
65+ if (savedData != null && savedData .length () > 0 ){
66+ onDownloadFinished (savedData );
67+ } else {
68+ Logger .d ("Data corrupted, using initial seed: %s" , storageFile .getAbsolutePath ());
69+ loadFromBundledData ();
70+ }
6071 fis .close ();
6172 }
6273
74+ private void loadFromBundledData () {
75+ try {
76+ InputStream bundledDataInputStream = context .getAssets ().open (assetPath );
77+ onDownloadFinished (IOUtils .toString (bundledDataInputStream , Charset .forName ("UTF-8" )));
78+ bundledDataInputStream .close ();
79+ } catch (IOException e ) {
80+ Logger .e ("Could not bundled data: %s" , e .toString ());
81+ }
82+ }
83+
6384 public void setStorageFile (File storageFile ) {
6485 this .storageFile = storageFile ;
6586
6687 if (storageFile .exists ()) {
6788 try {
6889 loadFromSavedData ();
6990 } catch (IOException e ) {
70- Logger .e ("Could not load saved data: %s" , storageFile . getAbsolutePath ());
91+ Logger .e ("Could not load saved data: %s" , e . toString ());
7192 }
7293 }
7394 }
@@ -78,17 +99,22 @@ public void setStorageFile(File storageFile) {
7899 * Note that app updates are causing the updated data written here to be overwritten again,
79100 * as it is assumed that new builds always have newer data.
80101 */
81- public void updateStorage (String content ) {
102+ public synchronized void updateStorage (String content ) {
82103 if (storageFile != null ) {
83- FileUtils .deleteQuietly (storageFile );
84-
85104 try {
86- FileOutputStream fos = new FileOutputStream (storageFile );
105+ File tempFile = new File (FilenameUtils .getFullPath (storageFile .getPath ())
106+ + FilenameUtils .getBaseName (storageFile .getName ())
107+ + "_temp." + FilenameUtils .getExtension (storageFile .getName ()));
108+
109+ FileOutputStream fos = new FileOutputStream (tempFile );
87110 IOUtils .write (content , fos , Charset .forName ("UTF-8" ));
88111 fos .close ();
112+
113+ FileUtils .deleteQuietly (storageFile );
114+ FileUtils .moveFile (tempFile , storageFile );
89115 Logger .d ("Updated saved data:%s" , storageFile .getAbsolutePath ());
90116 } catch (IOException e ) {
91- Logger .e ("Could not update saved data %s" , storageFile . getAbsolutePath ());
117+ Logger .e ("Could not update saved data %s" , e . toString ());
92118 }
93119 }
94120 }
0 commit comments