Skip to content

Commit

Permalink
Speed: serialization; deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ChakshuGautam committed Jul 14, 2021
1 parent bab1e0a commit 3112988
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<relativePath>pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.samagra</groupId>
<groupId>com.uci</groupId>
<artifactId>message-rosa</artifactId>
<name>message-rosa</name>
<version>0.0.1-SNAPSHOT</version>
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/messagerosa/core/model/XMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,24 @@ public enum MessageType {
private XMessageThread thread;
private XMessagePayload payload;

private static JAXBContext context;
private static Marshaller marshaller;

static {
try {
context = JAXBContext.newInstance(XMessage.class);
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (JAXBException e) {
e.printStackTrace();
}
}


public String toXML() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(XMessage.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(this, sw);
return sw.toString();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(this, stringWriter);
return stringWriter.toString();
}

public void completeTransform() {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/messagerosa/xml/XMessageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@
*/

public class XMessageParser {

public static JAXBContext context;
public static Unmarshaller jaxbUnmarshaller;

static {
try {
context = JAXBContext.newInstance(XMessage.class);
jaxbUnmarshaller = context.createUnmarshaller();
} catch (JAXBException e) {
e.printStackTrace();
}
}

public static XMessage parse(InputStream stream) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(XMessage.class);
Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
return (XMessage) jaxbUnmarshaller.unmarshal(stream);
}
}
Expand Down

0 comments on commit 3112988

Please sign in to comment.