Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Created January 31, 2012 11:08
Show Gist options
  • Save azyobuzin/1709937 to your computer and use it in GitHub Desktop.
Save azyobuzin/1709937 to your computer and use it in GitHub Desktop.
package net.azyobuzi.fallfavo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import android.content.Context;
public class QueueManager {
private static HashMap<Long, Tweet> m_queue;
//141文字のセパレーター
private static final String separator = "separatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorseparatorsepara";
private static final String queueFile = "fallfavo_favqueue.txt";
public static HashMap<Long, Tweet> getQueue() {
if (m_queue == null) {
m_queue = new HashMap<Long, Tweet>();
try {
FileInputStream stream = FallFavoApplication.getInstance().openFileInput(queueFile);
byte[] bs = new byte[stream.available()];
stream.read(bs);
stream.close();
String content = new String(bs, "UTF-8");
String[] split = content.split("\n" + separator + "\n");
for (int i = 0; i < split.length - 1; i += 3) {
Tweet t = new Tweet();
t.id = Long.parseLong(split[i]);
t.screenName = split[i + 1];
t.text = split[i + 2];
m_queue.put(t.id, t);
}
} catch (Exception ex) { }
}
return m_queue;
}
private static String tweetToString(Tweet t) {
StringBuilder sb = new StringBuilder();
sb.append(t.id);
sb.append("\n");
sb.append(separator);
sb.append("\n");
sb.append(t.screenName);
sb.append("\n");
sb.append(separator);
sb.append("\n");
sb.append(t.text);
sb.append("\n");
sb.append(separator);
sb.append("\n");
return sb.toString();
}
public static void add(Tweet t) throws IOException {
FileOutputStream stream = FallFavoApplication.getInstance().openFileOutput(queueFile, Context.MODE_PRIVATE | Context.MODE_APPEND);
stream.write(tweetToString(t).getBytes("UTF-8"));
stream.close();
if (m_queue != null) {
m_queue.put(t.id, t);
}
}
public static void remove(Tweet t) throws IOException {
@SuppressWarnings("unchecked")
HashMap<Long, Tweet> copied = (HashMap<Long, Tweet>)m_queue.clone();
if (copied.remove(t.id) != null) {
FileOutputStream stream = FallFavoApplication.getInstance().openFileOutput(queueFile, Context.MODE_PRIVATE);
for (Tweet item : copied.values()) {
stream.write(tweetToString(item).getBytes("UTF-8"));
}
stream.close();
m_queue.remove(t.id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment