📄 tcubaseitemstr.java
字号:
package com.jr81.source.stream;
//package dataTransPackage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.jr81.common.JrUtility;
public class TcuBaseItemSTR extends Object {
private int NameLength=0;
private int ValueLength=0;
private int Size=0;
private int ItemTag=0;
protected byte[] Name=null ;
protected byte[] Value=null;
public void Clear() {
Name = null ;
Value = null;
Size=0;
}
/**
* @param value
*/
public void Assign(TcuBaseItemSTR value) {
Clear();
setName( value.getName());
setValue ( value.getValue());
setItemTag(value.getItemTag());
}
/**
* @return
* @throws IOException
*/
private ByteArrayOutputStream toStream() throws IOException {
try{
ByteArrayOutputStream value=new ByteArrayOutputStream(12+NameLength+ValueLength);
byte[] inLength = new byte[4];
inLength = JrUtility.int2bytes(NameLength);
value.write(inLength,0,4);
inLength = JrUtility.int2bytes(ValueLength);
value.write(inLength,0,4);
inLength = JrUtility.int2bytes(ItemTag);
value.write(inLength,0,4);
value.write(Name,0,NameLength);
value.write(Value,0,ValueLength);
//value.write(Value,0,ValueLength);
return value;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public byte[] toByteArray() throws IOException{
ByteArrayOutputStream ou=(ByteArrayOutputStream)toStream();
try{
return ou.toByteArray();
}finally{
ou.close();
ou=null;
//System.gc();
}
//return ((ByteArrayOutputStream)toStream()).toByteArray();
}
private boolean fromStream(InputStream value) throws IOException{
Clear();
byte[] inLength = new byte[4];
value.read(inLength,0,4);
NameLength = JrUtility.bytes2int(inLength);
value.read(inLength,0,4);
ValueLength = JrUtility.bytes2int(inLength);
value.read(inLength,0,4);
ItemTag = JrUtility.bytes2int(inLength);
Name=new byte[NameLength];
value.read(Name,0,NameLength);
Value=new byte[ValueLength];
value.read(Value,0,ValueLength);
//System.out.println(new String(Value));
//写入Size
Size=12+ValueLength+NameLength;
return true;
}
public boolean fromByteArray(byte[] value) throws IOException{
ByteArrayInputStream in=new ByteArrayInputStream(value);
try{
return fromStream(in);
}finally{
in.close();
in=null;
//System.gc();
}
}
/**
* @return 返回 name。
*/
public byte[] getName() {
return Name;
}
/**
* @param name 要设置的 name。
*/
public void setName(byte[] name) {
NameLength=name.length;
Name=new byte[NameLength];
Size=12+ValueLength+NameLength;
//Name = name;
System.arraycopy(name,0,Name,0,NameLength);
}
/**
* @return 返回 nameLength。
*/
public int getNameLength() {
return NameLength;
}
/**
* @return 返回 value。
*/
public byte[] getValue() {
return Value;
}
/**
* @param value 要设置的 value。
*/
public void setValue(byte[] value) {
//Value = value;
//ValueLength=value.length;
ValueLength=value.length;
Value=new byte[ValueLength];
Size=12+ValueLength+NameLength;
//Name = name;
System.arraycopy(value,0,Value,0,ValueLength);
}
/**
* @return 返回 valueLength。
*/
public int getValueLength() {
return ValueLength;
}
/**
* @param valueLength 要设置的 valueLength。
*/
public void setValueLength(int valueLength) {
ValueLength = valueLength;
}
public static void main(String[] args) {
// TODO 自动生成方法存根
try{
TcuBaseItemSTR Test=new TcuBaseItemSTR();
Test.setName("Test".getBytes());
String value="My name is Sanlen";
Test.setValue(value.getBytes());
//Test.setValueLength(value.length());
ByteArrayOutputStream out;
out=(ByteArrayOutputStream) Test.toStream();
//Test.toStream(out);
OutputStream Out=new FileOutputStream("c:\\1.txt");
Out.write(out.toByteArray());
ByteArrayInputStream in=new ByteArrayInputStream(out.toByteArray());
TcuBaseItemSTR Test2=new TcuBaseItemSTR();
Test2.fromStream(in);
String ddd = new String(Test2.getValue());
System.out.println(ddd);
TcuBaseItemSTR Test3=new TcuBaseItemSTR();
Test3.Assign(Test);
String ccc = new String(Test3.getValue());
ccc+="YYY";
System.out.println(ccc);
System.out.print(Test3.getSize());
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param nameLength 要设置的 nameLength。
*/
public void setNameLength(int nameLength) {
NameLength = nameLength;
}
/**
* @return 返回 size。
*/
public int getSize() {
return Size;
}
/**
* @param size 要设置的 size。
*/
public void setSize(int size) {
Size = size;
}
/**
* @return 返回 itemLevel。
*/
public int getItemTag() {
return ItemTag;
}
/**
* @param itemLevel 要设置的 itemLevel。
*/
public void setItemTag(int itemTag) {
ItemTag = itemTag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -