aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/xml/TagWriter.java
blob: 0a663ce1b8a2fcfca482b86ff643c728a4ce62c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package de.pixart.messenger.xml;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.concurrent.LinkedBlockingQueue;

import de.pixart.messenger.xmpp.stanzas.AbstractStanza;

public class TagWriter {

    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();
                    outputStream.write(output.toString());
                    outputStream.flush();
                } catch (Exception e) {
                    shouldStop = true;
                }
            }
        }
    };

    public TagWriter() {
    }

    public void setOutputStream(OutputStream out) throws IOException {
        if (out == null) {
            throw new IOException();
        }
        this.outputStream = new OutputStreamWriter(out);
    }

    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;
    }

    public void forceClose() {
        finish();
        outputStream = null;
    }
}