Mercurial > hg > Database > jungle-network
view src/main/java/alice/jungle/persistent/PersistentJournal.java @ 118:f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
author | one |
---|---|
date | Wed, 25 Dec 2013 20:02:26 +0900 |
parents | 895ab2907db3 |
children | 2e8034524259 |
line wrap: on
line source
package alice.jungle.persistent; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.msgpack.MessagePack; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal; public class PersistentJournal implements Journal { private ChangeListWriter WRITER; private ChangeListReader READER; private OutputStream out; private InputStream in; private static MessagePack msgpack; static { msgpack = new MessagePack(); } public PersistentJournal() { } public PersistentJournal(File file) throws FileNotFoundException { out = new FileOutputStream(file,true); in = new FileInputStream(file); WRITER = new PersistentChangeListWriter(out); READER = new PersistentChangeListReader(in); } @Override public ChangeListReader getReader() { return READER; } @Override public ChangeListWriter getWriter() { String timeStamp = Long.toString(System.currentTimeMillis()); String logFileName = timeStamp + ".log"; OutputStream outStream = null; try { outStream = new FileOutputStream(new File("./log/"+logFileName)); } catch (FileNotFoundException e) { e.printStackTrace(); } PersistentChangeListWriter writer = new PersistentChangeListWriter(outStream); return writer; } public void setOutputFile(File file) throws FileNotFoundException { setOutputStream(new FileOutputStream(file, true)); } public void setInputFile(File file) throws FileNotFoundException { setInputStream(new FileInputStream(file)); } public void setOutputStream(OutputStream _out) { out = _out; WRITER = new PersistentChangeListWriter(out); } public OutputStream getOutputStream() { return out; } public void setInputStream(InputStream _in) { in = _in; READER = new PersistentChangeListReader(in); } public InputStream getInputStream() { return in; } public static MessagePack getMsgPackInstance() { return msgpack; } public void close() throws IOException { out.close(); in.close(); } }