收藏一下

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);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值