收藏一下

package com.huawie.nastarone.north.umts.config;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class SerializeXMLHandler implements ISerializeHandler {

public OutputStream serializeXMLToStream(Serializable obj, OutputStream os)
throws JAXBException {
if (null == obj || null == os) {
return null;
}
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller m = jc.createMarshaller();
m.marshal(obj, os);
return os;
}

@SuppressWarnings("unchecked")
public <T extends Serializable> T deserializeXMLFromStream(Class<T> cls,
InputStream is) throws JAXBException {
if (null == cls || null == is) {
return null;
}
JAXBContext jc = JAXBContext.newInstance(cls);
Unmarshaller um = jc.createUnmarshaller();
return (T) um.unmarshal(is);
}

public String serializeXMLToString(Serializable obj) throws JAXBException,
UnsupportedEncodingException {
if (null == obj) {
return null;
}
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller m = jc.createMarshaller();
ByteArrayOutputStream os = new ByteArrayOutputStream();
m.marshal(obj, os);
return new String(os.toByteArray(), "utf-8");
}

@SuppressWarnings("unchecked")
public <T extends Serializable> T deserializeXMLFromString(Class<T> cls,
String obj) throws JAXBException, UnsupportedEncodingException {
if (null == cls || null == obj) {
return null;
}
JAXBContext jc = JAXBContext.newInstance(cls);
Unmarshaller um = jc.createUnmarshaller();
ByteArrayInputStream is = new ByteArrayInputStream(obj
.getBytes("utf-8"));
return (T) um.unmarshal(is);
}

public File serializeXMLToFile(Serializable obj, String filePath)
throws JAXBException, FileNotFoundException {
if (null == obj || null == filePath) {
return null;
}
return serializeXMLToFile(obj, new File(filePath));
}

public File serializeXMLToFile(Serializable obj, File file)
throws JAXBException, FileNotFoundException {
if (null == obj || null == file) {
return null;
}
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller m = jc.createMarshaller();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
m.marshal(obj, fileOutputStream);
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}
return file;
}

public <T extends Serializable> T deserializeXMLFromFile(Class<T> cls,
String filePath) throws JAXBException {
if (null == cls || null == filePath) {
return null;
}
return deserializeXMLFromFile(cls, new File(filePath));
}

@SuppressWarnings("unchecked")
public <T extends Serializable> T deserializeXMLFromFile(Class<T> cls,
File file) throws JAXBException {
if (null == cls || null == file) {
return null;
}
JAXBContext jc = JAXBContext.newInstance(cls);
Unmarshaller um = jc.createUnmarshaller();
return (T) um.unmarshal(file);
}

}

你可能感兴趣的:(收藏一下)