Code Search for Developers
 
 
  

PersistentClock.java from PeerWriter at Krugle


Show PersistentClock.java syntax highlighted

package fr.loria.ecoo.wooki.woot.clock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 
 * @author nabil
 */
public class PersistentClock implements Serializable, Clock {
	String clockFile; // file location

	int clock;

	/** Creates a new instance of PersistentClock */
	public PersistentClock(String clockFile) throws Exception {
		this.clockFile = clockFile;
		File fClock = new File(this.clockFile);
		if (!(fClock.exists())) {
			try {
				FileOutputStream fout = new FileOutputStream(fClock);
				ObjectOutputStream oos = new ObjectOutputStream(fout);
				oos.writeObject(new Integer(0));
				oos.flush();
				oos.close();

			} catch (FileNotFoundException e) {
				throw new RuntimeException(e);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public synchronized int load() throws Exception {
		try {
			FileInputStream fi = new FileInputStream(clockFile);
			ObjectInputStream si = new ObjectInputStream(fi);
			Integer value = (Integer) si.readObject();
			si.close();
			return value.intValue();

		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public synchronized void store() throws Exception {
		try {
			FileOutputStream fos = new FileOutputStream(clockFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(new Integer(clock));
			oos.flush();
			oos.close();
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public int getValue() {
		return clock;
	}

	public void setValue(int i) {
		clock = i;
	}
}




See more files for this project here

PeerWriter

PeerWriter is a collaborative text editor. Multiple peers can edit the same document while they see overall changes in real-time. PeerWriter is based on a decentralized infrastructure, using a non-locking concurrency protocol ensuring global consistency.

Project homepage: http://sourceforge.net/projects/peerwriter
Programming language(s): Java,XML
License: gpl2

  Clock.java
  PersistentClock.java