JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
DexHelper.java
Go to the documentation of this file.
1 
4 package jarmos.app.io;
5 
6 import jarmos.app.Const;
7 
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 
12 import android.content.Context;
13 import android.util.Log;
14 import dalvik.system.DexClassLoader;
15 
26 public class DexHelper {
27 
28  private Context c;
29 
30  private String lastFile = null;
31 
35  public DexHelper(Context c) {
36  this.c = c;
37  }
38 
44  public ClassLoader getDexClassLoader(InputStream in) {
45  // Create a local copy for dex optimization needs write access
46  String file = "tmpdex" + System.currentTimeMillis() + ".jar";
47  Log.d("DexHelper", "Using temporary dex-jarfile " + file);
48  try {
49  if (lastFile != null)
50  c.deleteFile(lastFile);
51 
52  String[] files = c.fileList();
53  System.out.println(files);
54  FileOutputStream f = c.openFileOutput(file, Context.MODE_WORLD_READABLE);
55 
56  byte[] buffer = new byte[1024];
57  int len1 = 0;
58  while ((len1 = in.read(buffer)) > 0) {
59  f.write(buffer, 0, len1);
60  }
61  in.close();
62  Log.d("DexHelper", "Finished preparing local copy of jar-file input stream" + file);
63  } catch (IOException e) {
64  Log.e("DexHelper", "I/O exception while preparing local copy of jar-file input stream", e);
65  }
66  lastFile = file;
67  return new DexClassLoader(Const.APP_DATA_DIRECTORY + "/" + file, Const.APP_DATA_DIRECTORY, null,
68  c.getClassLoader());
69  }
70 
74  @Override
75  protected void finalize() throws Throwable {
76  if (lastFile != null)
77  c.deleteFile(lastFile);
78  super.finalize();
79  }
80 
81 }
static String APP_DATA_DIRECTORY
The directory where the application may write data to and read from.
Definition: Const.java:42
ClassLoader getDexClassLoader(InputStream in)
Definition: DexHelper.java:44
Class that contains miscellaneous JaRMoS specific constants and static functions. ...
Definition: Const.java:26
Utility class for providing class loaders that load from dex-jarfiles.
Definition: DexHelper.java:26