project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
changeset 7326 1fae07363938
parent 7318 a446eafcddeb
child 7330 867e4fda496e
equal deleted inserted replaced
7324:fb6bfe8e30c8 7326:1fae07363938
     1 package org.hedgewars.hedgeroid.Downloader;
     1 package org.hedgewars.hedgeroid.Downloader;
     2 
     2 
     3 import java.io.File;
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileOutputStream;
       
     6 import java.io.IOException;
     5 import java.io.IOException;
     7 import java.io.InputStream;
       
     8 import java.io.OutputStream;
       
     9 
     6 
    10 import org.hedgewars.hedgeroid.MainActivity;
     7 import org.hedgewars.hedgeroid.MainActivity;
    11 import org.hedgewars.hedgeroid.R;
     8 import org.hedgewars.hedgeroid.R;
    12 import org.hedgewars.hedgeroid.Utils;
     9 import org.hedgewars.hedgeroid.Utils;
    13 import org.hedgewars.hedgeroid.Datastructures.Scheme;
    10 import org.hedgewars.hedgeroid.Datastructures.Scheme;
    25 		act = _act;
    22 		act = _act;
    26 	}
    23 	}
    27 	
    24 	
    28 	private static void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
    25 	private static void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
    29 		try {
    26 		try {
    30 			copyFile(assetManager, target, assetPath);
    27 			Utils.writeStreamToFile(assetManager.open(assetPath), target);
    31 		} catch(FileNotFoundException e) {
    28 		} catch(FileNotFoundException e) {
    32 			/*
    29 			/*
    33 			 * I can't find a better way to figure out whether an asset entry is
    30 			 * I can't find a better way to figure out whether an asset entry is
    34 			 * a file or a directory. Checking if assetManager.list(assetPath)
    31 			 * a file or a directory. Checking if assetManager.list(assetPath)
    35 			 * is empty is a bit cleaner, but SLOW.
    32 			 * is empty is a bit cleaner, but SLOW.
    38 				throw new IOException("Unable to create directory "+target);
    35 				throw new IOException("Unable to create directory "+target);
    39 			}
    36 			}
    40 			for (String asset : assetManager.list(assetPath)) {
    37 			for (String asset : assetManager.list(assetPath)) {
    41 				DownloadAssets.copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
    38 				DownloadAssets.copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
    42 			}
    39 			}
    43 		}
       
    44 	}
       
    45 	
       
    46 	private static void copyFile(AssetManager assetManager, File target, String assetPath) throws IOException {
       
    47 		InputStream is = null;
       
    48 		OutputStream os = null;
       
    49 		byte[] buffer = new byte[8192];
       
    50 		try {
       
    51 			is = assetManager.open(assetPath);
       
    52 			os = new FileOutputStream(target);
       
    53 			int size;
       
    54 			while((size=is.read(buffer)) != -1) {
       
    55 				os.write(buffer, 0, size);
       
    56 			}
       
    57 			os.close(); // Important to close this non-quietly, in case of exceptions when flushing
       
    58 		} finally {
       
    59 			Utils.closeQuietly(is);
       
    60 			Utils.closeQuietly(os);
       
    61 		}
    40 		}
    62 	}
    41 	}
    63 	
    42 	
    64 	@Override
    43 	@Override
    65 	protected Long doInBackground(Object... params) {
    44 	protected Long doInBackground(Object... params) {