Skip to content

Commit

Permalink
Issue 10: Every new request gets IllegalStateException when calling
Browse files Browse the repository at this point in the history
EventsSerializeFactory.deserialize once previous request fails.
  • Loading branch information
abhayani committed Aug 22, 2012
1 parent ede0c11 commit 9831468
Showing 1 changed file with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,55 @@ public class EventsSerializeFactory {

private final XMLObjectWriter writer;
private final ByteArrayOutputStream baos;

private final XMLObjectReader reader;


public EventsSerializeFactory() throws XMLStreamException {
this.binding.setAlias(Dialog.class, "dialog");
this.binding.setClassAttribute("type");
this.baos = new ByteArrayOutputStream();

this.writer = new XMLObjectWriter();

this.reader = new XMLObjectReader();
}
private void resetWriter() throws XMLStreamException{

private void resetWriter() throws XMLStreamException {
this.writer.reset();
this.baos.reset();
this.writer.setBinding(binding);
this.writer.setIndentation("\t");
this.writer.setBinding(binding);
this.writer.setIndentation("\t");
this.writer.setOutput(this.baos);
}

public byte[] serialize(Dialog dialog) throws XMLStreamException {
this.resetWriter();

this.writer.write(dialog, "dialog", Dialog.class);
this.writer.flush();
byte[] data = baos.toByteArray();

return data;
}

public Dialog deserialize(byte[] data) throws XMLStreamException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
this.reader.setInput(bais);
Dialog dialog = this.reader.read("dialog", Dialog.class);
this.reader.reset();
return dialog;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
this.reader.setInput(bais);
Dialog dialog = this.reader.read("dialog", Dialog.class);
return dialog;
} finally {
this.reader.reset();
}
}

public Dialog deserialize(InputStream is) throws XMLStreamException {
this.reader.setInput(is);
Dialog dialog = this.reader.read("dialog", Dialog.class);
this.reader.reset();
return dialog;
try {
this.reader.setInput(is);
Dialog dialog = this.reader.read("dialog", Dialog.class);
return dialog;
} finally {
this.reader.reset();
}
}
}

0 comments on commit 9831468

Please sign in to comment.