aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/xml/TagWriter.java
diff options
context:
space:
mode:
authorSam Whited <sam@samwhited.com>2014-10-30 15:20:20 -0400
committerSam Whited <sam@samwhited.com>2014-10-30 15:33:13 -0400
commit46f147a82c6b161e071f717c7686f8b78a20aaf1 (patch)
tree38ee04006e38197fd8009697b8975007ec0f45c8 /src/main/java/eu/siacs/conversations/xml/TagWriter.java
parent142384e5805d93887d185c9a5b74d4850e4ec719 (diff)
parenta362bd10214b16f8939c12a1dd4376667fe0d49f (diff)
Merge branch 'gradle' into development
Conflicts: .gitignore CHANGELOG.md README.md libs/MemorizingTrustManager libs/minidns libs/openpgp-api-lib
Diffstat (limited to 'src/main/java/eu/siacs/conversations/xml/TagWriter.java')
-rw-r--r--src/main/java/eu/siacs/conversations/xml/TagWriter.java114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/main/java/eu/siacs/conversations/xml/TagWriter.java b/src/main/java/eu/siacs/conversations/xml/TagWriter.java
new file mode 100644
index 00000000..f11c1846
--- /dev/null
+++ b/src/main/java/eu/siacs/conversations/xml/TagWriter.java
@@ -0,0 +1,114 @@
+package eu.siacs.conversations.xml;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
+
+public class TagWriter {
+
+ private OutputStream plainOutputStream;
+ private OutputStreamWriter outputStream;
+ private boolean finshed = false;
+ private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>();
+ private Thread asyncStanzaWriter = new Thread() {
+ private boolean shouldStop = false;
+
+ @Override
+ public void run() {
+ while (!shouldStop) {
+ if ((finshed) && (writeQueue.size() == 0)) {
+ return;
+ }
+ try {
+ AbstractStanza output = writeQueue.take();
+ if (outputStream == null) {
+ shouldStop = true;
+ } else {
+ outputStream.write(output.toString());
+ outputStream.flush();
+ }
+ } catch (IOException e) {
+ shouldStop = true;
+ } catch (InterruptedException e) {
+ shouldStop = true;
+ }
+ }
+ }
+ };
+
+ public TagWriter() {
+ }
+
+ public void setOutputStream(OutputStream out) throws IOException {
+ if (out == null) {
+ throw new IOException();
+ }
+ this.plainOutputStream = out;
+ this.outputStream = new OutputStreamWriter(out);
+ }
+
+ public OutputStream getOutputStream() throws IOException {
+ if (this.plainOutputStream == null) {
+ throw new IOException();
+ }
+ return this.plainOutputStream;
+ }
+
+ public TagWriter beginDocument() throws IOException {
+ if (outputStream == null) {
+ throw new IOException("output stream was null");
+ }
+ outputStream.write("<?xml version='1.0'?>");
+ outputStream.flush();
+ return this;
+ }
+
+ public TagWriter writeTag(Tag tag) throws IOException {
+ if (outputStream == null) {
+ throw new IOException("output stream was null");
+ }
+ outputStream.write(tag.toString());
+ outputStream.flush();
+ return this;
+ }
+
+ public TagWriter writeElement(Element element) throws IOException {
+ if (outputStream == null) {
+ throw new IOException("output stream was null");
+ }
+ outputStream.write(element.toString());
+ outputStream.flush();
+ return this;
+ }
+
+ public TagWriter writeStanzaAsync(AbstractStanza stanza) {
+ if (finshed) {
+ return this;
+ } else {
+ if (!asyncStanzaWriter.isAlive()) {
+ try {
+ asyncStanzaWriter.start();
+ } catch (IllegalThreadStateException e) {
+ // already started
+ }
+ }
+ writeQueue.add(stanza);
+ return this;
+ }
+ }
+
+ public void finish() {
+ this.finshed = true;
+ }
+
+ public boolean finished() {
+ return (this.writeQueue.size() == 0);
+ }
+
+ public boolean isActive() {
+ return outputStream != null;
+ }
+}