📄 defaultrecordcreator.java
字号:
package com.pub.berkeleydb;
import org.apache.log4j.Logger;
import org.apache.mina.common.ByteBuffer;
import com.sleepycat.je.DatabaseEntry;
/**
*
*/
public abstract class DefaultRecordCreator implements IRecordCreator {
// int keyBuf = 64;
// int valueBuf = 512;
private static final Logger log = Logger.getLogger(DefaultRecordCreator.class);
// public DefaultRecordCreator(){
//
// }
// public DefaultRecordCreator(int keyBuf,int valueBuf){
// this.keyBuf = keyBuf;
// this.valueBuf = valueBuf;
// }
//key ByteBuffer
private static ThreadLocal tlKB = new ThreadLocal() {
protected Object initialValue() {
log.error("Create ByteBuffer Size : 64");
return ByteBuffer.allocate(64,false).setAutoExpand( true );
}
};
public static ByteBuffer tmpKB() {
return (ByteBuffer) tlKB.get();
}
//value ByteBuffer
private static ThreadLocal tlVB = new ThreadLocal() {
protected Object initialValue() {
log.error("Create ByteBuffer Size : 512");
return ByteBuffer.allocate(512,false).setAutoExpand( true );
}
};
public static ByteBuffer tmpVB() {
return (ByteBuffer) tlVB.get();
}
public int toKey(Object o, DatabaseEntry key) throws Exception {
ByteBuffer out = tmpKB();
// ByteBuffer out = ByteBuffer.allocate(this.keyBuf).setAutoExpand( true );
out.clear();
toKey(out, o);
out.flip();
byte [] data = new byte[out.remaining()];
int p = out.position();
int r = out.remaining();
out.get(data,out.position(),out.remaining());
key.setData(data, p, r);
out.clear();
out = null;
return 0;
}
public int fromKey(Object r, DatabaseEntry key) throws Exception{
ByteBuffer in = ByteBuffer.wrap(key.getData());
fromKey(in, r);
in = null;
return 0;
}
public int toValue(Object o, DatabaseEntry val) throws Exception{
ByteBuffer out = tmpVB();
// ByteBuffer out = ByteBuffer.allocate(this.valueBuf).setAutoExpand( true );
out.clear();
toValue(out, o);
out.flip();
byte [] data = new byte[out.remaining()];
int p = out.position();
int r = out.remaining();
out.get(data,out.position(),out.remaining());
val.setData(data, p, r);
out.clear();
out = null;
return 0;
}
public int fromValue(Object r, DatabaseEntry val) throws Exception{
ByteBuffer in = ByteBuffer.wrap(val.getData());
fromValue(in, r);
in = null;
return 0;
}
protected abstract void toValue(ByteBuffer out, Object r) throws Exception;
protected abstract void fromValue(ByteBuffer in, Object r) throws Exception;
protected abstract void toKey(ByteBuffer out, Object r) throws Exception;
protected abstract void fromKey(ByteBuffer in, Object r) throws Exception;
static protected void str2bytebuffer(ByteBuffer buf, String s, int len) {
if (buf.remaining() <= len - 1)
return;
if (s == null) {
s = "";
}
if (s.length() < len) {
for (int i = 0; i < s.length(); i++) {
buf.put((byte) s.charAt(i));
}
for (int i = s.length(); i < len; i++) {
buf.put((byte) 0);
}
} else {
for (int i = 0; i < len; i++) {
buf.put((byte) s.charAt(i));
}
}
}
static protected String bytebuffer2str(ByteBuffer buf, int len) {
if (buf.remaining() <= len - 1)
return null;
String r = new String();
boolean flag = true;
for (int i = 0; i < len; i++) {
byte temp;
if ((temp = buf.get()) != '\0' && flag) {
r += (char) temp;
} else {
flag = false;
}
}
return r;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -