diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java index f04c77cc..13273da8 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.URL; @@ -40,6 +41,36 @@ public class IITC_FileManager { public static final String DOMAIN = ".iitcm.localhost"; + /** + * copies the contents of a stream into another stream and (optionally) closes the output stream afterwards + * + * @param inStream + * the stream to read from + * @param outStream + * the stream to write to + * @param closeOutput + * whether to close the output stream when finished + * + * @throws IOException + */ + public static void copyStream(InputStream inStream, OutputStream outStream, boolean closeOutput) throws IOException + { + + // in case Android includes Apache commons IO in the future, this function should be replaced by IOUtils.copy + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + int len = 0; + + try { + while ((len = inStream.read(buffer)) != -1) { + outStream.write(buffer, 0, len); + } + } finally { + if (outStream != null && closeOutput) + outStream.close(); + } + } + public static HashMap getScriptInfo(String js) { HashMap map = new HashMap(); String header = ""; @@ -78,7 +109,6 @@ public class IITC_FileManager { private AssetManager mAssetManager; private IITC_Mobile mIitc; private String mIitcPath; - private SharedPreferences mPrefs; public IITC_FileManager(IITC_Mobile iitc) { @@ -292,20 +322,15 @@ public class IITC_FileManager { new Base64OutputStream(mStreamOut, Base64.NO_CLOSE | Base64.NO_WRAP | Base64.DEFAULT); FileInputStream fileinput = new FileInputStream(file); - int c; - while ((c = fileinput.read()) != -1) - { - encoder.write(c); - } - encoder.close(); + copyStream(fileinput, encoder, true); + mStreamOut.write("');".getBytes()); } - mStreamOut.close(); } catch (IOException e) { Log.w(e); - + } finally { // try to close stream, but ignore errors try { mStreamOut.close(); diff --git a/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java b/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java index 18f68cf0..98ac6897 100644 --- a/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java +++ b/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java @@ -2,6 +2,7 @@ package com.cradle.iitc_mobile.async; import android.os.AsyncTask; +import com.cradle.iitc_mobile.IITC_FileManager; import com.cradle.iitc_mobile.Log; import java.io.File; @@ -43,13 +44,7 @@ public class DownloadTile extends AsyncTask { private void writeTileToFile(InputStream inStream, File file) throws IOException { file.getParentFile().mkdirs(); FileOutputStream outStream = new FileOutputStream(file); - int bufferSize = 1024; - byte[] buffer = new byte[bufferSize]; - int len = 0; - while ((len = inStream.read(buffer)) != -1) { - outStream.write(buffer, 0, len); - } - if (outStream != null) - outStream.close(); + + IITC_FileManager.copyStream(inStream, outStream, true); } }