字节和字符的转换(二)

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;
}
...全文
33 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧