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 AuthTokenIO { 014 015 public byte[] serialise(AuthToken authToken) { 016 requireNonNull(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 requireNonNull(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}