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