字节和字符的转换(二)
karte 2011-04-03 12:28:15 public String GetString() {
int len = GetSize();
if(len == 0) {
return EMPTY_STRING;
}
overflowCheck(len);
String str = null;
try{
if (IS_CONVERT_ENCODING) {
str = new String(m_strBuf, curpos, len , RECV_ENCODING);
}else{
str = new String(m_strBuf, curpos, len );
}
curpos += len;
}catch(UnsupportedEncodingException ex){
throw new EObArchive("UnsupportedEncodingException:"+ex.getMessage());
}
return str;
}
public void Save(String value) {
if (value == null || value.length() == 0) {
SetSize(0);
return;
}
try{
byte[] tmp;
if (IS_CONVERT_ENCODING && !Utils.isASCII(value)) {
tmp = value.getBytes(RECV_ENCODING);
} else {
tmp = value.getBytes();
}
Save(tmp);
}catch(UnsupportedEncodingException ex){
throw new EObArchive("UnsupportedEncodingException:"+ex.getMessage());
}
}
public String Load(String value) {
int ilen = GetSize();
overflowCheck(ilen);
if (ilen == 0) {
return EMPTY_STRING;
}
String str = null;
try{
if(IS_CONVERT_ENCODING){
str = new String(m_strBuf, curpos, ilen,RECV_ENCODING);
}else{
str = new String(m_strBuf, curpos, ilen);
}
curpos += ilen;
}catch(UnsupportedEncodingException ex){
throw new EObArchive("UnsupportedEncodingException:"+ex.getMessage());
}
return str;
}
public void Save(byte[] value) {
if (value == null || value.length == 0) {
SetSize(0);
return;
}
SetSize(value.length);
increaseBuf(value.length);
System.arraycopy(value, 0, m_strBuf, curpos, value.length);
curpos += value.length;
}