package UTF;
import java.io.DataOutput;
import java.io.IOException;
import Clazz.jq_ClassFileConstants;
import Run_Time.Debug;
import Util.Assert;
import Util.Collections.UnmodifiableIterator;
/**
* @author John Whaley <[email protected]>
* @version $Id: Utf8.java,v 1.17 2003/05/23 21:10:56 joewhaley Exp $
*/
public class Utf8 implements jq_ClassFileConstants {
public static /*final*/ boolean TRACE = false;
public static final int STARTING_TABLE_SIZE = 16384;
public static final int STARTING_HASH_SIZE = 9999;
public static final int STARTING_CHAIN_SIZE = 4;
public static Utf8[] table = new Utf8[STARTING_TABLE_SIZE];
public static int size = -1;
public static int[][] chains = new int[STARTING_HASH_SIZE][]; // for hashing
public static final Utf8 BYTE_DESC = Utf8.get((char)TC_BYTE+"");
public static final Utf8 CHAR_DESC = Utf8.get((char)TC_CHAR+"");
public static final Utf8 DOUBLE_DESC = Utf8.get((char)TC_DOUBLE+"");
public static final Utf8 FLOAT_DESC = Utf8.get((char)TC_FLOAT+"");
public static final Utf8 INT_DESC = Utf8.get((char)TC_INT+"");
public static final Utf8 LONG_DESC = Utf8.get((char)TC_LONG+"");
public static final Utf8 SHORT_DESC = Utf8.get((char)TC_SHORT+"");
public static final Utf8 BOOLEAN_DESC = Utf8.get((char)TC_BOOLEAN+"");
public static final Utf8 VOID_DESC = Utf8.get((char)TC_VOID+"");
public static Utf8 get(String s) {
return get(toUtf8(s));
}
public static Utf8 get(byte[] b) {
int id = getID(b);
return table[id];
}
public static Utf8 get(byte[] b, int startIndex, int endIndex) {
int id = getID(b, startIndex, endIndex);
return table[id];
}
public static int getID(byte[] b) {
int hash = hashCode(b);
int chain_index = Math.abs(hash) % chains.length;
int[] chain = chains[chain_index];
if (chain == null) {
chains[chain_index] = chain = new int[STARTING_CHAIN_SIZE];
return addToTable_helper(b, hash, chain, 0);
}
for (int i=0; i<chain.length; ++i) {
int id = chain[i]-1;
if (id == -1) { // end of chain
return addToTable_helper(b, hash, chain, i);
}
Utf8 utf8 = table[id];
// ??? check hash before memcmp ???
if (memcmp(utf8.data, b)) {
// ??? swap first one and this one ???
return id;
}
}
int[] newchain = new int[chain.length<<1];
System.arraycopy(chain, 0, newchain, 0, chain.length);
chains[chain_index] = newchain;
return addToTable_helper(b, hash, newchain, chain.length);
// free chain
// todo: rehash when the table gets too full...
}
public static int getID(byte[] b, int startIndex, int endIndex) {
int hash = hashCode(b, startIndex, endIndex);
int chain_index = Math.abs(hash) % chains.length;
int[] chain = chains[chain_index];
if (chain == null) {
chains[chain_index] = chain = new int[STARTING_CHAIN_SIZE];
byte[] b2 = new byte[endIndex-startIndex];
System.arraycopy(b, startIndex, b2, 0, endIndex-startIndex);
return addToTable_helper(b2, hash, chain, 0);
}
for (int i=0; i<chain.length; ++i) {
int id = chain[i]-1;
if (id == -1) { // end of chain
byte[] b2 = new byte[endIndex-startIndex];
System.arraycopy(b, startIndex, b2, 0, endIndex-startIndex);
return addToTable_helper(b2, hash, chain, i);
}
Utf8 utf8 = table[id];
// ??? check hash before memcmp ???
if (memcmp(utf8.data, b, startIndex, endIndex)) {
// ??? swap first one and this one ???
return id;
}
}
int[] newchain = new int[chain.length<<1];
System.arraycopy(chain, 0, newchain, 0, chain.length);
chains[chain_index] = newchain;
byte[] b2 = new byte[endIndex-startIndex];
System.arraycopy(b, startIndex, b2, 0, endIndex-startIndex);
return addToTable_helper(b2, hash, newchain, chain.length);
// free(chain)
// todo: rehash when the table gets too full...
}
public boolean isValidMethodDescriptor() {
if (data.length < 3)
return false;
if (data[0] != TC_PARAM)
return false;
int i=1;
while (data[i] != TC_PARAMEND) {
here:
switch (data[i]) {
case TC_BYTE:
case TC_CHAR:
case TC_DOUBLE:
case TC_FLOAT:
case TC_INT:
case TC_LONG:
case TC_SHORT:
case TC_BOOLEAN:
case TC_ARRAY:
break;
case TC_CLASS:
for (;;) {
if (++i == data.length)
return false;
if (data[i] == TC_CLASSEND)
break here;
}
default:
return false;
}
if (++i == data.length)
return false;
}
++i;
return ((data[i] == TC_VOID) && (i == data.length-1)) ||
isValidTypeDescriptor(i);
}
public boolean isValidTypeDescriptor() {
return isValidTypeDescriptor(0);
}
private boolean isValidTypeDescriptor(int i) {
for (;;) {
if (data.length == i)
return false;
switch (data[i]) {
case TC_BYTE:
case TC_CHAR:
case TC_DOUBLE:
case TC_FLOAT:
case TC_INT:
case TC_LONG:
case TC_SHORT:
case TC_BOOLEAN:
return i == data.length-1;
case TC_ARRAY:
++i; continue;
case TC_CLASS:
for (;;) {
if (++i == data.length)
return false;
if (data[i] == TC_CLASSEND)
return i == data.length-1;
}
default:
return false;
}
}
}
public boolean isDescriptor(byte desc) {
return (data.length > 0) && (data[0] == desc);
}
public Utf8 getArrayElementDescriptor() {
Assert._assert(isDescriptor(TC_ARRAY));
return get(data, 1, data.length);
}
public Utf8 getClassName() {
Assert._assert(isDescriptor(TC_CLASS));
return get(data, 1, data.length-1);
}
public Utf8 getAsArrayDescriptor() {
Assert._assert(isValidTypeDescriptor());
// todo: might need to reevaluate making a new array on every query.
byte[] b = new byte[data.length+1];
b[0] = TC_ARRAY;
System.arraycopy(data, 0, b, 1, data.length);
return get(b);
}
public Utf8 getAsClassDescriptor() {
Assert._assert(data[0] != TC_ARRAY);
// todo: might need to reevaluate making a new array on every query.
byte[] b = new byte[data.length+2];
b[0] = TC_CLASS;
System.arraycopy(data, 0, b, 1, data.length);
b[data.length+1] = TC_CLASSEND;
return get(b);
}
public MethodDescriptorIterator getParamDescriptors() {
return new MethodDescriptorIterator();
}
public class MethodDescriptorIterator extends UnmodifiableIterator {
int currentIndex;
MethodDescriptorIterator() {
Assert._assert(isDescriptor(TC_PARAM));
currentIndex = 0;
}
public boolean hasNext() {
return data[currentIndex+1] !
没有合适的资源?快使用搜索试试~ 我知道了~
Convert_Utf8.zip_UTF8
共2个文件
txt:1个
java:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 99 浏览量
2022-09-21
03:20:31
上传
评论
收藏 4KB ZIP 举报
温馨提示
一个用来测试是否UTF8文字, 以及把文字转为UTF8的程式源码
资源推荐
资源详情
资源评论
收起资源包目录
Convert_Utf8.zip (2个子文件)
www.pudn.com.txt 218B
Utf8.java 19KB
共 2 条
- 1
资源评论
- lichengtools20042023-06-26资源很实用,内容详细,值得借鉴的内容很多,感谢分享。
局外狗
- 粉丝: 66
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功