Guest User

Untitled

a guest
Jan 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. Exception java.lang.NoClassDefFoundError: org.quuux.sack.Sack
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.os.AsyncTask;
  5. import android.support.v4.util.AtomicFile;
  6. import android.util.Pair;
  7.  
  8. import com.google.gson.Gson;
  9.  
  10. import java.io.BufferedInputStream;
  11. import java.io.BufferedOutputStream;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.InputStreamReader;
  18. import java.io.Reader;
  19. import java.util.zip.GZIPInputStream;
  20. import java.util.zip.GZIPOutputStream;
  21.  
  22. public class Sack<T> {
  23.  
  24. public enum Status {
  25. SUCCESS,
  26. ERROR
  27. }
  28.  
  29. public interface Listener<T> {
  30. void onResult(Sack.Status status, T obj);
  31. }
  32.  
  33. private final Class<T> mClass;
  34. private final File mPath;
  35.  
  36. private final Gson mGson = new Gson();
  37.  
  38. Sack(final Class<T> klass, final File path) {
  39. mClass = klass;
  40. mPath = path;
  41. }
  42.  
  43. public Pair<Sack.Status, T> doLoad() {
  44.  
  45. final AtomicFile file = new AtomicFile(mPath);
  46. try {
  47. final FileInputStream in = file.openRead();
  48. final String s = slurp(new BufferedInputStream(new GZIPInputStream(in)), 4096);
  49. final Pair<Sack.Status, T> rv = new Pair<>(Sack.Status.SUCCESS, mGson.fromJson(s, mClass));
  50. in.close();
  51. return rv;
  52. } catch (IOException e) {
  53. return new Pair<Sack.Status, T>(Sack.Status.ERROR, null);
  54. } finally {
  55. }
  56. }
  57.  
  58. public static String slurp(final InputStream is, final int bufferSize) {
  59. final char[] buffer = new char[bufferSize];
  60. final StringBuilder out = new StringBuilder();
  61. try {
  62. Reader in = new InputStreamReader(is, "UTF-8");
  63. for (; ; ) {
  64. int rsz = in.read(buffer, 0, buffer.length);
  65. if (rsz < 0)
  66. break;
  67. out.append(buffer, 0, rsz);
  68. }
  69. } catch (IOException ex) {
  70. return null;
  71. }
  72. return out.toString();
  73. }
  74.  
  75. public AsyncTask<Void, Void, Pair<Sack.Status, T>> load(final Sack.Listener<T> listener) {
  76.  
  77. @SuppressLint("StaticFieldLeak")
  78. final AsyncTask<Void, Void, Pair<Sack.Status, T>> task = new AsyncTask<Void, Void, Pair<Sack.Status, T>>() {
  79.  
  80. @Override
  81. protected Pair<Sack.Status, T> doInBackground(final Void... params) {
  82. return doLoad();
  83. }
  84.  
  85. @Override
  86. protected void onPostExecute(final Pair<Sack.Status, T> t) {
  87. if (listener != null)
  88. listener.onResult(t.first, t.second);
  89. }
  90. };
  91.  
  92. task.execute();
  93.  
  94. return task;
  95. }
  96.  
  97. public AsyncTask<Void, Void, Pair<Sack.Status, T>> load() {
  98. return load(null);
  99. }
  100.  
  101. public Pair<Sack.Status, T> doCommit(final T obj) {
  102. final AtomicFile file = new AtomicFile(mPath);
  103. FileOutputStream str = null;
  104. try {
  105. str = file.startWrite();
  106. final BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(str));
  107. out.write(mGson.toJson(obj).getBytes());
  108. out.flush();
  109. out.close();
  110. str.flush();
  111. str.close();
  112. file.finishWrite(str);
  113. return new Pair<Sack.Status, T>(Sack.Status.SUCCESS, obj);
  114. } catch (IOException e) {
  115. if (str != null)
  116. file.failWrite(str);
  117. return new Pair<Sack.Status, T>(Sack.Status.ERROR, null);
  118. }
  119. }
  120.  
  121. public AsyncTask<T, Void, Pair<Sack.Status, T>> commit(final T obj, final Sack.Listener<T> listener) {
  122.  
  123. @SuppressLint("StaticFieldLeak") final AsyncTask<T, Void, Pair<Sack.Status, T>> task = new AsyncTask<T, Void, Pair<Sack.Status, T>>() {
  124.  
  125. @Override
  126. protected Pair<Sack.Status, T> doInBackground(final T... params) {
  127. return doCommit(params[0]);
  128. }
  129.  
  130. @Override
  131. protected void onPostExecute(final Pair<Sack.Status, T> t) {
  132. if (listener != null)
  133. listener.onResult(t.first, t.second);
  134. }
  135. };
  136.  
  137. task.execute(obj);
  138.  
  139. return task;
  140. }
  141.  
  142. public AsyncTask<T, Void, Pair<Sack.Status, T>> commit(final T obj) {
  143. return commit(obj, null);
  144. }
  145.  
  146. public static <T> Sack<T> open(final Class<T> entity, final File path) {
  147. return new Sack<T>(entity, path);
  148. }}
Add Comment
Please, Sign In to add comment