下面列出了怎么用org.json.Kim的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Write the name of an object property. Names have their own Keep and
* Huffman encoder because they are expected to be a more restricted set.
*
* @param name The name string.
* @throws JSONException
*/
private void writeName(String name) throws JSONException {
// If this name has already been registered, then emit its integer and
// increment its usage count.
Kim kim = new Kim(name);
int integer = this.namekeep.find(kim);
if (integer != none) {
one();
write(integer, this.namekeep);
} else {
// Otherwise, emit the string with Huffman encoding, and register it.
zero();
write(kim, this.namehuff, this.namehuffext);
write(end, namehuff);
this.namekeep.register(kim);
}
}
private String readName() throws JSONException {
byte[] bytes = new byte[65536];
int length = 0;
if (!bit()) {
while (true) {
int c = this.namehuff.read(this.bitreader);
if (c == end) {
break;
}
bytes[length] = (byte) c;
length += 1;
}
if (length == 0) {
return "";
}
Kim kim = new Kim(bytes, length);
this.namekeep.register(kim);
return kim.toString();
}
return getAndTick(this.namekeep, this.bitreader).toString();
}
/**
* Write the name of an object property. Names have their own Keep and
* Huffman encoder because they are expected to be a more restricted set.
*
* @param name
* @throws JSONException
*/
private void writeName(String name) throws JSONException {
// If this name has already been registered, then emit its integer and
// increment its usage count.
Kim kim = new Kim(name);
int integer = this.namekeep.find(kim);
if (integer != none) {
one();
writeAndTick(integer, this.namekeep);
} else {
// Otherwise, emit the string with Huffman encoding, and register
// it.
zero();
write(kim, this.namehuff);
write(end, namehuff);
this.namekeep.register(kim);
}
}
/**
* Find the integer value associated with this key, or nothing if this key
* is not in the keep.
*
* @param key
* An object.
* @return An integer
*/
public int match(Kim kim, int from, int thru) {
Node node = this.root;
int best = none;
for (int at = from; at < thru; at += 1) {
node = node.get(kim.get(at));
if (node == null) {
break;
}
if (node.integer != none) {
best = node.integer;
}
from += 1;
}
return best;
}
public boolean postMortem(PostMortem pm) {
boolean result = true;
TrieKeep that = (TrieKeep) pm;
if (this.length != that.length) {
JSONzip.log("\nLength " + this.length + " <> " + that.length);
return false;
}
if (this.capacity != that.capacity) {
JSONzip.log("\nCapacity " + this.capacity + " <> " + that.capacity);
return false;
}
for (int i = 0; i < this.length; i += 1) {
Kim thiskim = this.kim(i);
Kim thatkim = that.kim(i);
if (!thiskim.equals(thatkim)) {
JSONzip.log("\n[" + i + "] " + thiskim + " <> " + thatkim);
result = false;
}
}
return result && this.root.postMortem(that.root);
}
/**
* Write the name of an object property. Names have their own Keep and
* Huffman encoder because they are expected to be a more restricted set.
*
* @param name
* The name string.
* @throws JSONException
*/
private void writeName(final String name) throws JSONException {
// If this name has already been registered, then emit its integer and
// increment its usage count.
final Kim kim = new Kim(name);
final int integer = this.namekeep.find(kim);
if (integer != none) {
one();
write(integer, this.namekeep);
} else {
// Otherwise, emit the string with Huffman encoding, and register
// it.
zero();
write(kim, this.namehuff, this.namehuffext);
write(end, this.namehuff);
this.namekeep.register(kim);
}
}
public boolean postMortem(PostMortem pm) {
Keep that = (Keep) pm;
if (this.length != that.length) {
JSONzip.log(this.length + " <> " + that.length);
return false;
}
for (int i = 0; i < this.length; i += 1) {
boolean b;
if (this.list[i] instanceof Kim) {
b = this.list[i].equals(that.list[i]);
} else {
Object o = this.list[i];
Object q = that.list[i];
if (o instanceof Number) {
o = o.toString();
}
if (q instanceof Number) {
q = q.toString();
}
b = o.equals(q);
}
if (!b) {
JSONzip.log("\n[" + i + "]\n " + this.list[i] + "\n "
+ that.list[i] + "\n " + this.ticks[i] + "\n "
+ that.ticks[i]);
return false;
}
}
return true;
}
/**
* Write each of the bytes in a kim with Huffman encoding.
*
* @param kim
* A kim containing the bytes to be written.
* @param huff
* The Huffman encoder.
* @param ext
* The Huffman encoder for the extended bytes.
* @throws JSONException
*/
private void write(Kim kim, Huff huff, Huff ext) throws JSONException {
for (int at = 0; at < kim.length; at += 1) {
int c = kim.get(at);
write(c, huff);
while ((c & 128) == 128) {
at += 1;
c = kim.get(at);
write(c, ext);
}
}
}
/**
* Write a string.
*
* @param string The string to write.
* @throws JSONException
*/
private void writeString(String string) throws JSONException {
// Special case for empty strings.
if (string.length() == 0) {
zero();
write(end, this.stringhuff);
} else {
Kim kim = new Kim(string);
// Look for the string in the strings keep. If it is found, emit its
// integer and count that as a use.
int integer = this.stringkeep.find(kim);
if (integer != none) {
one();
write(integer, this.stringkeep);
} else {
// But if it is not found, emit the string's characters. Register the string
// so that a later lookup can succeed.
zero();
write(kim, this.stringhuff, this.stringhuffext);
write(end, this.stringhuff);
this.stringkeep.register(kim);
}
}
}
public boolean postMortem(PostMortem pm) {
MapKeep that = (MapKeep) pm;
if (this.length != that.length) {
JSONzip.log(this.length + " <> " + that.length);
return false;
}
for (int i = 0; i < this.length; i += 1) {
boolean b;
if (this.list[i] instanceof Kim) {
b = ((Kim) this.list[i]).equals(that.list[i]);
} else {
Object o = this.list[i];
Object q = that.list[i];
if (o instanceof Number) {
o = o.toString();
}
if (q instanceof Number) {
q = q.toString();
}
b = o.equals(q);
}
if (!b) {
JSONzip.log("\n[" + i + "]\n " + this.list[i] + "\n "
+ that.list[i] + "\n " + this.uses[i] + "\n "
+ that.uses[i]);
return false;
}
}
return true;
}
/**
* Write a string.
*
* @param string
* @throws JSONException
*/
private void writeString(String string) throws JSONException {
// Special case for empty strings.
if (string.length() == 0) {
zero();
zero();
write(end, this.substringhuff);
zero();
} else {
Kim kim = new Kim(string);
// Look for the string in the strings keep. If it is found, emit its
// integer and count that as a use.
int integer = this.stringkeep.find(kim);
if (integer != none) {
one();
writeAndTick(integer, this.stringkeep);
} else {
// But if it is not found, emit the string's substrings.
// Register the string
// so that the next lookup will succeed.
writeSubstring(kim);
this.stringkeep.register(kim);
}
}
}
/**
* Create a new Keep of kims.
*
* @param bits
* The log2 of the capacity of the Keep. For example, if bits is
* 12, then the keep's capacity will be 4096.
*/
public TrieKeep(int bits) {
super(bits);
this.froms = new int[this.capacity];
this.thrus = new int[this.capacity];
this.kims = new Kim[this.capacity];
this.root = new Node();
}
public void registerMany(Kim kim) {
int length = kim.length;
int limit = this.capacity - this.length;
if (limit > JSONzip.substringLimit) {
limit = JSONzip.substringLimit;
}
int until = length - (JSONzip.minSubstringLength - 1);
for (int from = 0; from < until; from += 1) {
int len = length - from;
if (len > JSONzip.maxSubstringLength) {
len = JSONzip.maxSubstringLength;
}
len += from;
Node node = this.root;
for (int at = from; at < len; at += 1) {
Node next = node.vet(kim.get(at));
if (next.integer == none
&& at - from >= (JSONzip.minSubstringLength - 1)) {
next.integer = this.length;
this.uses[this.length] = 1;
this.kims[this.length] = kim;
this.froms[this.length] = from;
this.thrus[this.length] = at + 1;
if (JSONzip.probe) {
try {
JSONzip.log("<<" + this.length + " "
+ new Kim(kim, from, at + 1) + ">> ");
} catch (Throwable ignore) {
}
}
this.length += 1;
limit -= 1;
if (limit <= 0) {
return;
}
}
node = next;
}
}
}
@Override
public boolean postMortem(final PostMortem pm) {
final Keep that = (Keep) pm;
if (this.length != that.length) {
JSONzip.log(this.length + " <> " + that.length);
return false;
}
for (int i = 0; i < this.length; i += 1) {
boolean b;
if (this.list[i] instanceof Kim) {
b = this.list[i].equals(that.list[i]);
} else {
Object o = this.list[i];
Object q = that.list[i];
if (o instanceof Number) {
o = o.toString();
}
if (q instanceof Number) {
q = q.toString();
}
b = o.equals(q);
}
if (!b) {
JSONzip.log("\n[" + i + "]\n " + this.list[i] + "\n "
+ that.list[i] + "\n " + this.ticks[i] + "\n "
+ that.ticks[i]);
return false;
}
}
return true;
}
/**
* Write each of the bytes in a kim with Huffman encoding.
*
* @param kim
* A kim containing the bytes to be written.
* @param huff
* The Huffman encoder.
* @param ext
* The Huffman encoder for the extended bytes.
* @throws JSONException
*/
private void write(final Kim kim, final Huff huff, final Huff ext)
throws JSONException {
for (int at = 0; at < kim.length; at += 1) {
int c = kim.get(at);
write(c, huff);
while ((c & 128) == 128) {
at += 1;
c = kim.get(at);
write(c, ext);
}
}
}
/**
* Write a string.
*
* @param string
* The string to write.
* @throws JSONException
*/
private void writeString(final String string) throws JSONException {
// Special case for empty strings.
if (string.length() == 0) {
zero();
write(end, this.stringhuff);
} else {
final Kim kim = new Kim(string);
// Look for the string in the strings keep. If it is found, emit its
// integer and count that as a use.
final int integer = this.stringkeep.find(kim);
if (integer != none) {
one();
write(integer, this.stringkeep);
} else {
// But if it is not found, emit the string's characters.
// Register the string
// so that a later lookup can succeed.
zero();
write(kim, this.stringhuff, this.stringhuffext);
write(end, this.stringhuff);
this.stringkeep.register(kim);
}
}
}
/**
* Write a string, attempting to match registered substrings.
*
* @param kim
* @throws JSONException
*/
private void writeSubstring(Kim kim) throws JSONException {
this.substringkeep.reserve();
zero();
int from = 0;
int thru = kim.length;
int until = thru - JSONzip.minSubstringLength;
int previousFrom = none;
int previousThru = 0;
// Find a substring from the substring keep.
while (true) {
int at;
int integer = none;
for (at = from; at <= until; at += 1) {
integer = this.substringkeep.match(kim, at, thru);
if (integer != none) {
break;
}
}
if (integer == none) {
break;
}
// If a substring is found, emit any characters that were before the
// matched
// substring. Then emit the substring's integer and loop back to
// match the
// remainder with another substring.
if (from != at) {
zero();
write(kim, from, at, this.substringhuff);
write(end, this.substringhuff);
if (previousFrom != none) {
this.substringkeep.registerOne(kim, previousFrom,
previousThru);
previousFrom = none;
}
}
one();
writeAndTick(integer, this.substringkeep);
from = at + this.substringkeep.length(integer);
if (previousFrom != none) {
this.substringkeep.registerOne(kim, previousFrom, previousThru);
previousFrom = none;
}
previousFrom = at;
previousThru = from + 1;
}
// If a substring is not found, then emit the remaining characters.
zero();
if (from < thru) {
write(kim, from, thru, this.substringhuff);
if (previousFrom != none) {
this.substringkeep.registerOne(kim, previousFrom, previousThru);
}
}
write(end, this.substringhuff);
zero();
// Register the string's substrings in the trie in hopes of future
// substring
// matching.
substringkeep.registerMany(kim);
}
public void registerOne(Kim kim) {
int integer = registerOne(kim, 0, kim.length);
if (integer != none) {
this.kims[integer] = kim;
}
}
/**
* Reserve space in the keep, compacting if necessary. A keep may contain at
* most -capacity- elements. The keep contents can be reduced by deleting
* all elements with low use counts, rebuilding the trie with the survivors.
*/
public void reserve() {
if (this.capacity - this.length < JSONzip.substringLimit) {
int from = 0;
int to = 0;
this.root = new Node();
while (from < this.capacity) {
if (this.uses[from] > 1) {
Kim kim = this.kims[from];
int thru = this.thrus[from];
Node node = this.root;
for (int at = this.froms[from]; at < thru; at += 1) {
Node next = node.vet(kim.get(at));
node = next;
}
node.integer = to;
this.uses[to] = age(this.uses[from]);
this.froms[to] = this.froms[from];
this.thrus[to] = thru;
this.kims[to] = kim;
to += 1;
}
from += 1;
}
// It is possible, but highly unlikely, that too many items survive.
// If that happens, clear the keep.
if (this.capacity - to < JSONzip.substringLimit) {
this.power = 0;
this.root = new Node();
to = 0;
}
this.length = to;
while (to < this.capacity) {
this.uses[to] = 0;
this.kims[to] = null;
this.froms[to] = 0;
this.thrus[to] = 0;
to += 1;
}
}
}
/**
* Write a range of bytes from a Kim with Huffman encoding.
*
* @param kim
* A Kim containing the bytes to be written.
* @param from
* The index of the first byte to write.
* @param thru
* The index after the last byte to write.
* @param huff
* The Huffman encoder.
* @throws JSONException
*/
private void write(Kim kim, int from, int thru, Huff huff)
throws JSONException {
for (int at = from; at < thru; at += 1) {
write(kim.get(at), huff);
}
}
/**
* Write each of the bytes in a kim with Huffman encoding.
*
* @param kim
* A kim containing the bytes to be written.
* @param huff
* The Huffman encoder.
* @throws JSONException
*/
private void write(Kim kim, Huff huff) throws JSONException {
write(kim, 0, kim.length, huff);
}