我需要在java中读取一个cdb文件。我已经尝试过“strangegizmo”,但它没有工作,因为它给了我一个无效的cdb格式的错误,虽然另一个工具可以读取该文件。
我还尝试了另一个库“xbird.storage”,但徒劳无功。
以下是我尝试过的代码示例:
strangegizmo:来源:http://www.strangegizmo.com/products/sg-cdb/
String cdbFile = "C:\\workspace\\TestProject\\resources\\test.cdb";
/* Dump the CDB file. */
try {
Enumeration e = Cdb.elements(cdbFile);
while (e.hasMoreElements()) {
/* Get the element and its component parts. */
CdbElement element = (CdbElement)e.nextElement();
byte[] key = element.getKey();
byte[] data = element.getData();
/* Write the line directly to stdout to avoid any
* charset conversion that System.print() might want to
* perform. */
System.out.write(
("+" + key.length + "," + data.length + ":").getBytes());
System.out.write(key);
System.out.write('-');
System.out.write('>');
System.out.write(data);
System.out.write('\n');
}
System.out.write('\n');
} catch (IOException ioException) {
System.out.println("Couldn't dump CDB file: "
+ ioException);
}
还有 xbird api源码(svn链接):http://xbird.googlecode.com/svn/trunk/xbird-open:
/*
* @(#)$Id$
*
* Copyright 2006-2008 Makoto YUI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Makoto YUI - initial implementation
*/
package xbird.storage.index;
import java.io.Closeable;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
/**
*
* <DIV lang="en"></DIV>
* <DIV lang="ja"></DIV>
*
* @author Makoto YUI (yuin405+xbird@gmail.com)
* @link http://d.hatena.ne.jp/odz/20060623/1151107174
* @link http://www.corpit.ru/mjt/tinycdb.html
*/
public class ReadOnlyCDB implements Closeable {
private static final int HASHSTART = 5381;
/** map for CDB file */
private ByteBuffer map;
/** CDB File */
private RandomAccessFile file;
/** number of hash slots searched under this key */
private int loop;
/** initialized if loop is nonzero */
private int khash;
private int kpos;
private int hpos;
private int hslots;
public ReadOnlyCDB(String filename) throws IOException {
file = new RandomAccessFile(filename, "rw");
FileChannel channel = file.getChannel();
map = channel.map(MapMode.READ_WRITE, 0, file.length());
map.order(ByteOrder.LITTLE_ENDIAN);
// CharBuffer charBuffer = StandardCharsets.UTF_8.decode(map);
// String text = charBuffer.toString();
// System.out.println(text); // AB
Charset charset = Charset.forName( "us-ascii" );
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode( map );
String sb = decoder.decode( map ).toString();
// map.get(map.hasArray(), map.arrayOffset(), channel.read(map));
int noOfBytesRead = channel.read(map);
for (int i = 0; i < 25; i++) {
while (noOfBytesRead != -1) {
map.clear();
noOfBytesRead = channel.read(map);
}
map.clear();
channel.position(0);
noOfBytesRead = channel.read(map);
}
}
public void close() {
try {
file.close();
file = null;
map = null;
} catch (IOException ex) {
}
}
public void findstart() {
loop = 0;
}
public byte[] findnext(byte[] key, int offset, int len) {
int u;
int dlen;
if(loop == 0) {
u = hash(key, offset, len);
map.position((u << 3) & 2047);
hpos = map.getInt();
hslots = map.getInt();
if(hslots == 0) {
return null;
}
khash = u;
u >>>= 8;
u %= hslots;
u <<= 3;
kpos = hpos + u;
}
while(loop < hslots) {
int pos;
map.position(kpos);
u = map.getInt();
pos = map.getInt();
if(pos == 0) {
return null;
}
loop++;
kpos += 8;
if(kpos == hpos + (hslots << 3)) {
kpos = hpos;
}
if(u == khash) {
map.position(pos);
u = map.getInt();
if(u == len) {
dlen = map.getInt();
byte[] keyInDb = new byte[len];
map.get(keyInDb, 0, len);
if(!match(key, offset, len, keyInDb)) {
return null;
}
byte[] data = new byte[dlen];
map.get(data, 0, dlen);
return data;
}
}
}
return null;
}
public byte[] findnext(byte[] key) {
return findnext(key, 0, key.length);
}
public byte[] find(byte[] key) {
return find(key, 0, key.length);
}
public byte[] find(byte[] key, int offset, int len) {
findstart();
return findnext(key, offset, len);
}
private static int hash(byte[] buffer, int offset, int len) {
long h = HASHSTART;
final long mask = 0x00000000ffffffffL;
for(int i = 0; i < len; i++) {
h = (h + (h << 5) & mask) & mask;
h = h ^ ((buffer[offset + i] + 0x100) & 0xff);
}
return (int) (h & mask);
}
private static boolean match(byte[] key, int offset, int len, byte[] keyInDb) {
for(int i = 0; i < len; i++) {
if(key[offset + i] != keyInDb[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
try {
ReadOnlyCDB readOnlyCDB = new ReadOnlyCDB("C:\\workspace\\TestProject\\resources\\test.cdb");
// System.out.println("Hemant: " + readOnlyCDB.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
我已经能够在第二个代码示例中读取文件并将其放入CharBuffer中。但我需要密钥和数据对,我无法摆脱代码。
任何帮助都将深受赞赏。
谢谢, 与Hemant