code clean up in TagWriter
This commit is contained in:
parent
2553895300
commit
cdc239b040
|
@ -14,114 +14,109 @@ import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
|
||||||
|
|
||||||
public class TagWriter {
|
public class TagWriter {
|
||||||
|
|
||||||
private OutputStreamWriter outputStream;
|
private OutputStreamWriter outputStream;
|
||||||
private boolean finished = false;
|
private boolean finished = false;
|
||||||
private final LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>();
|
private final LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>();
|
||||||
private CountDownLatch stanzaWriterCountDownLatch = null;
|
private CountDownLatch stanzaWriterCountDownLatch = null;
|
||||||
|
|
||||||
private final Thread asyncStanzaWriter = new Thread() {
|
private final Thread asyncStanzaWriter = new Thread() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
stanzaWriterCountDownLatch = new CountDownLatch(1);
|
stanzaWriterCountDownLatch = new CountDownLatch(1);
|
||||||
while (!isInterrupted()) {
|
while (!isInterrupted()) {
|
||||||
if (finished && writeQueue.size() == 0) {
|
if (finished && writeQueue.size() == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
AbstractStanza output = writeQueue.take();
|
AbstractStanza output = writeQueue.take();
|
||||||
outputStream.write(output.toString());
|
outputStream.write(output.toString());
|
||||||
if (writeQueue.size() == 0) {
|
if (writeQueue.size() == 0) {
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stanzaWriterCountDownLatch.countDown();
|
stanzaWriterCountDownLatch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public TagWriter() {
|
public TagWriter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void setOutputStream(OutputStream out) throws IOException {
|
public synchronized void setOutputStream(OutputStream out) throws IOException {
|
||||||
if (out == null) {
|
if (out == null) {
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
}
|
}
|
||||||
this.outputStream = new OutputStreamWriter(out);
|
this.outputStream = new OutputStreamWriter(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TagWriter beginDocument() throws IOException {
|
public void beginDocument() throws IOException {
|
||||||
if (outputStream == null) {
|
if (outputStream == null) {
|
||||||
throw new IOException("output stream was null");
|
throw new IOException("output stream was null");
|
||||||
}
|
}
|
||||||
outputStream.write("<?xml version='1.0'?>");
|
outputStream.write("<?xml version='1.0'?>");
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized TagWriter writeTag(Tag tag) throws IOException {
|
public synchronized void writeTag(Tag tag) throws IOException {
|
||||||
if (outputStream == null) {
|
if (outputStream == null) {
|
||||||
throw new IOException("output stream was null");
|
throw new IOException("output stream was null");
|
||||||
}
|
}
|
||||||
outputStream.write(tag.toString());
|
outputStream.write(tag.toString());
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized TagWriter writeElement(Element element) throws IOException {
|
public synchronized void writeElement(Element element) throws IOException {
|
||||||
if (outputStream == null) {
|
if (outputStream == null) {
|
||||||
throw new IOException("output stream was null");
|
throw new IOException("output stream was null");
|
||||||
}
|
}
|
||||||
outputStream.write(element.toString());
|
outputStream.write(element.toString());
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public TagWriter writeStanzaAsync(AbstractStanza stanza) {
|
public void writeStanzaAsync(AbstractStanza stanza) {
|
||||||
if (finished) {
|
if (finished) {
|
||||||
Log.d(Config.LOGTAG,"attempting to write stanza to finished TagWriter");
|
Log.d(Config.LOGTAG, "attempting to write stanza to finished TagWriter");
|
||||||
return this;
|
} else {
|
||||||
} else {
|
if (!asyncStanzaWriter.isAlive()) {
|
||||||
if (!asyncStanzaWriter.isAlive()) {
|
try {
|
||||||
try {
|
asyncStanzaWriter.start();
|
||||||
asyncStanzaWriter.start();
|
} catch (IllegalThreadStateException e) {
|
||||||
} catch (IllegalThreadStateException e) {
|
// already started
|
||||||
// already started
|
}
|
||||||
}
|
}
|
||||||
}
|
writeQueue.add(stanza);
|
||||||
writeQueue.add(stanza);
|
}
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void finish() {
|
public void finish() {
|
||||||
this.finished = true;
|
this.finished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean await(long timeout, TimeUnit timeunit) throws InterruptedException {
|
public boolean await(long timeout, TimeUnit timeunit) throws InterruptedException {
|
||||||
if (stanzaWriterCountDownLatch == null) {
|
if (stanzaWriterCountDownLatch == null) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return stanzaWriterCountDownLatch.await(timeout, timeunit);
|
return stanzaWriterCountDownLatch.await(timeout, timeunit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isActive() {
|
public boolean isActive() {
|
||||||
return outputStream != null;
|
return outputStream != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void forceClose() {
|
public synchronized void forceClose() {
|
||||||
asyncStanzaWriter.interrupt();
|
asyncStanzaWriter.interrupt();
|
||||||
if (outputStream != null) {
|
if (outputStream != null) {
|
||||||
try {
|
try {
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
//ignoring
|
//ignoring
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputStream = null;
|
outputStream = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue