Invms.Licence.LicenceModel Maven / Gradle / Ivy
The newest version!
package Invms.Licence;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 许可模型
*/
@XmlRootElement(name = "licenceModel")
public class LicenceModel {
/**
* 获取或设置开始使用时间
*/
@XmlAttribute(name = "startTime")
public String startTime;
/**
* 获取或设置截止使用时间
*/
@XmlAttribute(name = "stopTime")
public String stopTime;
/**
* 获取或设置剩余时间间隔
*/
@XmlAttribute(name = "remainSpan")
public String remainSpan;
/**
* 获取或设置剩余使用次数
*/
@XmlAttribute(name = "remainCount")
public String remainCount;
/**
* 获取或设置验证时间间隔
*/
@XmlAttribute(name = "verifySpan")
public String verifySpan;
/**
* 加载
*
* @param file
* 文件
* @throws Exception
* 异常
*/
public void load(String file) throws Exception {
File xmlFile = new File(file);
if (!xmlFile.exists()) {
throw new Exception("许可文件不存在");
}
int length = (int) xmlFile.length();
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(xmlFile));
byte[] buffer = new byte[length];
bufferedInputStream.read(buffer, 0, length);
bufferedInputStream.close();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length);
for (int i = length - 1; i >= 0; i--) {
byteArrayOutputStream.write((byte) (buffer[i] + length));
}
buffer = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
JAXBContext jaxbContext = JAXBContext.newInstance(LicenceModel.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
Object xmlObject = unmarshaller.unmarshal(byteArrayInputStream);
byteArrayInputStream.close();
if (xmlObject instanceof LicenceModel) {
LicenceModel licenceModel = (LicenceModel) xmlObject;
startTime = licenceModel.startTime;
stopTime = licenceModel.stopTime;
remainSpan = licenceModel.remainSpan;
remainCount = licenceModel.remainCount;
verifySpan = licenceModel.verifySpan;
} else {
throw new Exception("无效的许可文件");
}
}
/**
* 保存
*
* @param file
* 文件
* @throws Exception
* 异常
*/
public void save(String file) throws Exception {
JAXBContext context = JAXBContext.newInstance(LicenceModel.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
marshaller.marshal(this, byteArrayOutputStream);
byte[] buffer = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
int length = buffer.length;
byteArrayOutputStream = new ByteArrayOutputStream(length);
for (int i = length - 1; i >= 0; i--) {
byteArrayOutputStream.write((byte) (buffer[i] - length));
}
buffer = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
File xmlFile = new File(file);
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(xmlFile, false));
dataOutputStream.write(buffer);
dataOutputStream.close();
}
}