📄 tcuxmllist.java
字号:
/**
*
*/
package com.jr81.source.xml.source;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* @author yanslat
*
*/
public class TcuXmlList {
private byte[] Name = null;
private int NameLength = 0;
private byte[] Value = null;
private int ValueLength = 0;
private String XmlHead = "<?xml version='1.0' encoding = 'GB2312'?> ";
private String Xml1 = "<List><Name><![CDATA[";
private String Xml2 = "]]></Name><Value><![CDATA[";
private String Xml3 = "]]></Value></List>";
public TcuXmlList () {
}
public void Clear() {
Name = null;
Value = null;
}
public String GetXmlHead() {
return XmlHead;
}
public int GetLength() {
return Xml1.length() + Xml2.length() + Xml3.length() + NameLength + ValueLength;
}
public void Assign(TcuXmlList value) {
Clear();
this.setName(value.getName());
this.setValue(value.getValue());
}
/*
public boolean FromXml(String value) {
Clear();
SAXBuilder sb = new SAXBuilder();
Document MyDocument = new Document();
ByteArrayInputStream InputStream = new ByteArrayInputStream((XmlHead +value).getBytes());
try {
MyDocument=sb.build(InputStream);
Element root = MyDocument.getRootElement();
setName(root.getChildTextTrim("Name").getBytes());
setValue(root.getChildTextTrim("Value").getBytes());
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
public String ToXml() {
String XmlText = "<List><Name><![CDATA[" + this.getName() + "]]></Name>"
+ "<Value><![CDATA[" + this.getValue() + "]]></Value></List>";
return XmlText;
}
*/
public boolean FromByteXml(byte[] value) {
Clear();
SAXBuilder sb = new SAXBuilder();
Document MyDocument = new Document();
ByteArrayOutputStream Output = new ByteArrayOutputStream(XmlHead.length() + value.length);
Output.write(XmlHead.getBytes(),0, XmlHead.length());
Output.write(value,0,value.length);
ByteArrayInputStream InputStream = new ByteArrayInputStream(Output.toByteArray());
try {
MyDocument=sb.build(InputStream);
Element root = MyDocument.getRootElement();
setName(root.getChildTextTrim("Name").getBytes());
setValue(root.getChildTextTrim("Value").getBytes());
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
public byte[] ToByteXml() throws IOException {
int NameLength1 = 0;
if (this.getName() != null) {
NameLength1 = this.getName().length;
}
int ValueLength1 = 0;
if (this.getValue() != null) {
ValueLength1 = this.getValue().length;
}
int length = Xml1.length() + NameLength1 + Xml2.length() + ValueLength1 + Xml3.length();
ByteArrayOutputStream value = new ByteArrayOutputStream(length);
value.write(Xml1.getBytes());
if (NameLength1 > 0) {
value.write(this.getName(),0,NameLength1);
}
value.write(Xml2.getBytes());
if (ValueLength1 > 0) {
value.write(this.getValue(),0,ValueLength1);
}
value.write(Xml3.getBytes());
return value.toByteArray();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TcuXmlList List = new TcuXmlList();
TcuXmlList List1 = new TcuXmlList();
String Name = "yanslat";
List.setName(Name.getBytes());
List.setValue("hha".getBytes());
String dd;
try {
dd = new String(List.ToByteXml());
System.out.println(dd);
List1.FromByteXml(List.ToByteXml());
dd = new String(List1.ToByteXml());
System.out.println(dd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return Returns the name.
*/
public byte[] getName() {
return Name;
}
/**
* @param name The name to set.
*/
public void setName(byte[] name) {
Name = null;
NameLength = name.length;
if (NameLength > 0) {
Name = new byte[NameLength];
System.arraycopy(name, 0, Name, 0, NameLength);
//NameLength = Length;
}
}
/**
* @return Returns the value.
*/
public byte[] getValue() {
return Value;
}
/**
* @param value The value to set.
*/
public void setValue(byte[] value) {
Value = null;
if (value != null) {
ValueLength = value.length;
} else {
ValueLength = 0;
}
if (ValueLength > 0) {
Value = new byte[ValueLength];
System.arraycopy(value, 0, Value, 0, ValueLength);
//ValueLength = Length;
}
}
public void setCompressValue(byte[] value) {
try
{
ByteArrayInputStream fin=new ByteArrayInputStream(value);
ByteArrayOutputStream fout= new ByteArrayOutputStream();
//建立压缩文件输出流
//建立gzip压缩输出流
GZIPOutputStream gzout=new GZIPOutputStream(fout);
byte[] buf=new byte[1024];//设定读入缓冲区尺寸
int num;
while ((num=fin.read(buf)) != -1)
{
gzout.write(buf,0,num);
}
gzout.close();//!!!关闭流,必须关闭所有输入输出流.保证输入输出完整和释放系统资源.
Value = null;
ValueLength = fout.size();
if (ValueLength > 0) {
Value = new byte[ValueLength];
System.arraycopy(fout.toByteArray(), 0, Value, 0, ValueLength);
}
fout.close();
fin.close();
}catch(IOException e)
{
System.out.println(e);
}
}
public byte[] getCompressValue() {
ByteArrayOutputStream fout= new ByteArrayOutputStream();
try
{
//建立gzip压缩文件输入流
ByteArrayInputStream fin=new ByteArrayInputStream(Value);
//建立gzip解压工作流
GZIPInputStream gzin=new GZIPInputStream(fin);
//建立解压文件输出流
byte[] buf=new byte[1024];
int num;
while ((num=gzin.read(buf,0,buf.length)) != -1)
{
fout.write(buf,0,num);
}
gzin.close();
//fout.close();
fin.close();
}catch(IOException e)
{
System.out.println(e);
}
return fout.toByteArray();
}
public void setCompressBase64Value(byte[] value) {
try
{
ByteArrayInputStream fin=new ByteArrayInputStream(value);
ByteArrayOutputStream fout= new ByteArrayOutputStream();
//建立压缩文件输出流
//建立gzip压缩输出流
GZIPOutputStream gzout=new GZIPOutputStream(fout);
byte[] buf=new byte[1024];//设定读入缓冲区尺寸
int num;
while ((num=fin.read(buf)) != -1)
{
gzout.write(buf,0,num);
}
gzout.close();//!!!关闭流,必须关闭所有输入输出流.保证输入输出完整和释放系统资源.
Value = null;
char[] tmpValue = TcuBase64.encode(fout.toByteArray());
ValueLength = tmpValue.length;
if (ValueLength > 0) {
Value = new byte[ValueLength];
System.arraycopy(TcuUnility.getBytes(tmpValue), 0, Value, 0, ValueLength);
}
fout.close();
fin.close();
}catch(IOException e)
{
System.out.println(e);
}
}
public byte[] getCompressBase64Value() {
byte[] ResultValue = null;
try
{
//建立gzip压缩文件输入流
ByteArrayInputStream fin=new ByteArrayInputStream(TcuBase64.decode(TcuUnility.getChars(Value)));
//建立gzip解压工作流
GZIPInputStream gzin=new GZIPInputStream(fin);
//建立解压文件输出流
ByteArrayOutputStream fout= new ByteArrayOutputStream();
byte[] buf=new byte[1024];
int num;
while ((num=gzin.read(buf,0,buf.length)) != -1)
{
fout.write(buf,0,num);
}
gzin.close();
ResultValue = fout.toByteArray();
fout.close();
fin.close();
}catch(IOException e)
{
System.out.println(e);
}
return ResultValue;
}
public void setBase64Value(byte[] value) {
Value = null;
char[] tmpValue = TcuBase64.encode(value);
ValueLength = tmpValue.length;
if (ValueLength > 0) {
Value = new byte[ValueLength];
System.arraycopy(tmpValue, 0, Value, 0, ValueLength);
//ValueLength = Length;
}
}
public byte[] getBase64Value() {
return (byte[])TcuBase64.decode(TcuUnility.getChars(Value));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -