From 0d80d88736926d06e97f22f9ecc63d4fb696751c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 4 Feb 2014 15:09:50 +0100 Subject: reworked account managment. now status display actually works --- src/de/gultsch/chat/xml/TagWriter.java | 51 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'src/de/gultsch/chat/xml/TagWriter.java') diff --git a/src/de/gultsch/chat/xml/TagWriter.java b/src/de/gultsch/chat/xml/TagWriter.java index 35f27477..401a6eee 100644 --- a/src/de/gultsch/chat/xml/TagWriter.java +++ b/src/de/gultsch/chat/xml/TagWriter.java @@ -3,12 +3,37 @@ package de.gultsch.chat.xml; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.util.Collection; +import java.util.Iterator; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import android.util.Log; public class TagWriter { - OutputStreamWriter writer; + private OutputStreamWriter outputStream; + private LinkedBlockingQueue writeQueue = new LinkedBlockingQueue(); + private Thread writer = new Thread() { + public boolean shouldStop = false; + @Override + public void run() { + while(!shouldStop) { + try { + String output = writeQueue.take(); + outputStream.write(output); + outputStream.flush(); + } catch (IOException e) { + Log.d("xmppService", "error writing to stream"); + } catch (InterruptedException e) { + + } + } + } + }; + public TagWriter() { @@ -16,31 +41,29 @@ public class TagWriter { public TagWriter(OutputStream out) { this.setOutputStream(out); + writer.start(); } public void setOutputStream(OutputStream out) { - this.writer = new OutputStreamWriter(out); + this.outputStream = new OutputStreamWriter(out); + if (!writer.isAlive()) writer.start(); } - public TagWriter beginDocument() throws IOException { - writer.write(""); + public TagWriter beginDocument() { + writeQueue.add(""); return this; } - public TagWriter writeTag(Tag tag) throws IOException { - writer.write(tag.toString()); + public TagWriter writeTag(Tag tag) { + writeQueue.add(tag.toString()); return this; } - - public void flush() throws IOException { - writer.flush(); - } - public void writeString(String string) throws IOException { - writer.write(string); + public void writeString(String string) { + writeQueue.add(string); } - public void writeElement(Element element) throws IOException { - writer.write(element.toString()); + public void writeElement(Element element) { + writeQueue.add(element.toString()); } } -- cgit v1.2.3