Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerSend2 implements Runnable{
- public Thread thread;
- private ObjectOutputStream oos;
- private String user;
- private String dirpath;
- private Path dir;
- public ServerSend2(ObjectOutputStream oos, String user){
- this.oos = oos;
- this.user = user;
- dirpath = "/home/g0lem/Studium/Semester III/ProgPrak/system/db/"+user;
- dir = Paths.get(dirpath);
- thread = new Thread(this);
- }
- public void run(){
- start();
- }
- public void start(){
- try{
- WatchService watcher = FileSystems.getDefault().newWatchService();
- WatchKey key = dir.register(watcher,ENTRY_CREATE);
- while(oos != null){
- try {
- key = watcher.take();
- }
- catch (InterruptedException x) {
- return;
- }
- for (WatchEvent<?> event: key.pollEvents()) {
- WatchEvent.Kind<?> kind = event.kind();
- if (kind == OVERFLOW) {
- continue;
- }
- WatchEvent<Path> ev = (WatchEvent<Path>)event;
- Path filename = ev.context();
- String t = filename.toString();
- FileInputStream fileStream = new FileInputStream(dirpath+"/"+t);
- ObjectInputStream ois = new ObjectInputStream(fileStream);
- Object toSend = ois.readObject();
- oos.writeObject(toSend);
- }
- // Reset the key
- boolean valid = key.reset();
- if (!valid) {
- break;
- }
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement