001package co.codewizards.cloudstore.core.auth;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.io.ByteArrayInputStream;
006import java.io.ByteArrayOutputStream;
007
008import javax.xml.bind.JAXBContext;
009import javax.xml.bind.JAXBException;
010import javax.xml.bind.Marshaller;
011import javax.xml.bind.Unmarshaller;
012
013public class AuthTokenIO {
014
015        public byte[] serialise(AuthToken authToken) {
016                assertNotNull("authToken", authToken);
017                try {
018                        JAXBContext context = createContext();
019                        Marshaller marshaller = context.createMarshaller();
020                        ByteArrayOutputStream os = new ByteArrayOutputStream();
021                        marshaller.marshal(authToken, os);
022                        return os.toByteArray();
023                } catch (JAXBException e) {
024                        throw new RuntimeException(e);
025                }
026        }
027
028        public AuthToken deserialise(byte[] authTokenData) {
029                assertNotNull("authTokenData", authTokenData);
030                try {
031                        JAXBContext context = createContext();
032                        Unmarshaller unmarshaller = context.createUnmarshaller();
033                        Object object = unmarshaller.unmarshal(new ByteArrayInputStream(authTokenData));
034                        return (AuthToken) object;
035                } catch (JAXBException e) {
036                        throw new RuntimeException(e);
037                }
038        }
039
040        private JAXBContext createContext() throws JAXBException {
041                return JAXBContext.newInstance(AuthToken.class);
042        }
043}