Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

- added base64 ordering methods - added coding interface

orbiter10a095d6b5c1
diff --git a/source/de/anomic/kelondro/kelondroBase64Order.java b/source/de/anomic/kelondro/kelondroBase64Order.java
index 1452db7a7..16a7fac4d 100644
--- a/source/de/anomic/kelondro/kelondroBase64Order.java
+++ b/source/de/anomic/kelondro/kelondroBase64Order.java
@@ -48,7 +48,7 @@ package de.anomic.kelondro;
import java.util.Comparator;
-public class kelondroBase64Order extends kelondroAbstractOrder implements kelondroOrder, Comparator {
+public class kelondroBase64Order extends kelondroAbstractOrder implements kelondroOrder, kelondroCoding, Comparator {
public static final kelondroBase64Order standardCoder = new kelondroBase64Order(true);
public static final kelondroBase64Order enhancedCoder = new kelondroBase64Order(false);
@@ -84,9 +84,7 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond
if (c >= max(length)) {
StringBuffer s = new StringBuffer(length);
s.setLength(length);
- while (length > 0) {
- s.setCharAt(--length, alpha[0]);
- }
+ while (length > 0) s.setCharAt(--length, alpha[63]);
return s.toString();
} else {
return encodeLong(c, length);
@@ -107,10 +105,7 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond
while (s.endsWith("="))
s = s.substring(0, s.length() - 1);
long c = 0;
- for (int i = 0; i < s.length(); i++) {
- c <<= 6;
- c += ahpla[s.charAt(i)];
- }
+ for (int i = 0; i < s.length(); i++) c = (c << 6) | ahpla[s.charAt(i)];
return c;
}
@@ -118,10 +113,7 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond
// computes the maximum number that can be coded with a base64-encoded
// String of base len
long c = 0;
- for (int i = 0; i < len; i++) {
- c <<= 6;
- c += 63;
- }
+ for (int i = 0; i < len; i++) c = (c << 6) | 63;
return c;
}
@@ -203,12 +195,35 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond
}
public long cardinal(byte[] key) {
- // TODO Auto-generated method stub
- return 0;
+ // returns a cardinal number in the range of 0 .. Long.MAX_VALUE
+ long c = 0;
+ int p = 0;
+ while ((p < 10) && (p < key.length)) c = (c << 6) | ahpla[key[p++]];
+ while (p++ < 10) c = (c << 6);
+ c = c << 3;
+ return c;
}
public int compare(byte[] a, byte[] b) {
- // TODO Auto-generated method stub
+ int i = 0;
+ final int al = a.length;
+ final int bl = b.length;
+ final int len = (al > bl) ? bl : al;
+ while (i < len) {
+ if (ahpla[a[i]] > ahpla[b[i]])
+ return 1;
+ if (ahpla[a[i]] < ahpla[b[i]])
+ return -1;
+ // else the bytes are equal and it may go on yet undecided
+ i++;
+ }
+ // check if we have a zero-terminated equality
+ if ((i == al) && (i < bl) && (b[i] == 0)) return 0;
+ if ((i == bl) && (i < al) && (a[i] == 0)) return 0;
+ // no, decide by length
+ if (al > bl) return 1;
+ if (al < bl) return -1;
+ // no, they are equal
return 0;
}
diff --git a/source/de/anomic/kelondro/kelondroCoding.java b/source/de/anomic/kelondro/kelondroCoding.java
new file mode 100644
index 000000000..1968487c2
--- /dev/null
+++ b/source/de/anomic/kelondro/kelondroCoding.java
@@ -0,0 +1,63 @@
+// kelondroCoder.java
+// -----------------------
+// part of The Kelondro Database
+// (C) by Michael Peter Christen; [email protected]
+// first published on http://www.anomic.de
+// Frankfurt, Germany, 2005
+// created 04.01.2006
+//
+// $LastChangedDate: 2005-09-22 22:01:26 +0200 (Thu, 22 Sep 2005) $
+// $LastChangedRevision: 774 $
+// $LastChangedBy: orbiter $
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// Using this software in any meaning (reading, learning, copying, compiling,
+// running) means that you agree that the Author(s) is (are) not responsible
+// for cost, loss of data or any harm that may be caused directly or indirectly
+// by usage of this softare or this documentation. The usage of this software
+// is on your own risk. The installation and usage (starting/running) of this
+// software may allow other people or application to access your computer and
+// any attached devices and is highly dependent on the configuration of the
+// software which must be done by the user of the software; the author(s) is
+// (are) also not responsible for proper configuration and usage of the
+// software, even if provoked by documentation provided together with
+// the software.
+//
+// Any changes to this file according to the GPL as documented in the file
+// gpl.txt aside this file in the shipment you received can be done to the
+// lines that follows this copyright notice here, but changes must not be
+// done inside the copyright notive above. A re-distribution must contain
+// the intact and unchanged copyright notice.
+// Contributions and changes to the program code must be marked as such.
+
+package de.anomic.kelondro;
+
+public interface kelondroCoding {
+
+ public char encodeByte(byte b);
+ public byte decodeByte(char b);
+
+ public String encodeLongSmart(long c, int length);
+ public String encodeLong(long c, int length);
+ public long decodeLong(String s);
+
+ public String encodeString(String in);
+ public String decodeString(String in);
+
+ public String encode(byte[] in);
+ public byte[] decode(String in);
+
+}
diff --git a/source/de/anomic/server/serverCodings.java b/source/de/anomic/server/serverCodings.java
index 7572ebb9a..bb706bdd6 100644
--- a/source/de/anomic/server/serverCodings.java
+++ b/source/de/anomic/server/serverCodings.java
@@ -52,154 +52,6 @@ import java.util.StringTokenizer;
public final class serverCodings {
- // this provides encoding and decoding of long cardinals into a 6-bit - based number format
- // expressed by a string. This is probably the most compact form to encode numbers as strings.
- // the resulting string is filename-friendly, it contains no special character that is not
- // suitable for file names.
-
- public static final serverCodings standardCoder = new serverCodings(true);
- public static final serverCodings enhancedCoder = new serverCodings(false);
-
- final boolean rfc1113compliant;
- public final char[] alpha;
- public final byte[] ahpla;
-
- public serverCodings(boolean rfc1113compliant) {
- // if we choose not to be rfc1113compliant,
- // then we get shorter base64 results which are also filename-compatible
- this.rfc1113compliant = rfc1113compliant;
- alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
- if (!(rfc1113compliant)) {
- alpha[62] = '-';
- alpha[63] = '_';
- }
- ahpla = new byte[256];
- for (int i = 0; i < 256; i++) ahpla[i] = -1;
- for (int i = 0; i < alpha.length; i++) ahpla[alpha[i]] = (byte) i;
- }
-
- /*
- public char encodeBase64Byte(byte b) {
- return (char) alpha[b];
- }
-
- public byte decodeBase64Byte(char b) {
- return ahpla[b];
- }
-
- public String encodeBase64LongSmart(long c, int length) {
- if (c >= maxBase64(length)) {
- StringBuffer s = new StringBuffer(length);
- s.setLength(length);
- while (length > 0) {
- s.setCharAt(--length, alpha[0]);
- }
- return s.toString();
- } else {
- return encodeBase64Long(c, length);
- }
- }
-
- public String encodeBase64Long(long c, int length) {
- StringBuffer s = new StringBuffer(length);
- s.setLength(length);
- while (length > 0) {
- s.setCharAt(--length, alpha[(byte) (c & 0x3F)]);
- c >>= 6;
- }
- return s.toString();
- }
-
- public long decodeBase64Long(String s) {
- while (s.endsWith("=")) s = s.substring(0, s.length() - 1);
- long c = 0;
- for (int i = 0; i < s.length(); i++) {
- c <<= 6;
- c += ahpla[s.charAt(i)];
- }
- return c;
- }
-
- public static long maxBase64(int len) {
- // computes the maximum number that can be coded with a base64-encoded String of base len
- long c = 0;
- for (int i = 0; i < len; i++) {
- c <<= 6;
- c += 63;
- }
- return c;
- }
-
- public String encodeBase64String(String in) {
- return encodeBase64(in.getBytes());
- }
-
- // we will use this encoding to encode strings with 2^8 values to b64-Strings
- // we will do that by grouping each three input bytes to four output bytes.
- public String encodeBase64(byte[] in) {
- StringBuffer out = new StringBuffer(in.length / 3 * 4 + 3);
- int pos = 0;
- long l;
- while (in.length - pos >= 3) {
- l = ((((0XffL & (long) in[pos]) << 8) + (0XffL & (long) in[pos + 1])) << 8) + (0XffL & (long) in[pos + 2]);
- pos += 3;
- out = out.append(encodeBase64Long(l, 4));
- }
- // now there may be remaining bytes
- if (in.length % 3 != 0)
- out = out.append(
- (in.length % 3 == 2) ?
- encodeBase64Long((((0XffL & (long) in[pos]) << 8) + (0XffL & (long) in[pos + 1])) << 8, 4).substring(0,3) :
- encodeBase64Long((((0XffL & (long) in[pos])) << 8) << 8, 4).substring(0, 2));
- if (rfc1113compliant) while (out.length() % 4 > 0) out.append("=");
- // return result
- return out.toString();
- }
-
- public String decodeBase64String(String in) {
- try {
- return new String(decodeBase64(in), "ISO-8859-1");
- } catch (java.io.UnsupportedEncodingException e) {
- System.out.println("internal error in base64: " + e.getMessage());
- return null;
- }
- }
-
- public byte[] decodeBase64(String in) {
- try {
- int posIn = 0;
- int posOut = 0;
- if (rfc1113compliant) while (in.charAt(in.length() - 1) == '=') in = in.substring(0, in.length() - 1);
- byte[] out = new byte[in.length() / 4 * 3 + (((in.length() % 4) == 0) ? 0 : in.length() % 4 - 1)];
- long l;
- while (posIn + 3 < in.length()) {
- l = decodeBase64Long(in.substring(posIn, posIn + 4));
- out[posOut+2] = (byte) (l % 256); l = l / 256;
- out[posOut+1] = (byte) (l % 256); l = l / 256;
- out[posOut ] = (byte) (l % 256); l = l / 256;
- posIn += 4;
- posOut += 3;
- }
- if (posIn < in.length()) {
- if (in.length() - posIn == 3) {
- l = decodeBase64Long(in.substring(posIn) + "A");
- l = l / 256;
- out[posOut+1] = (byte) (l % 256); l = l / 256;
- out[posOut ] = (byte) (l % 256); l = l / 256;
- } else {
- l = decodeBase64Long(in.substring(posIn) + "AA");
- l = l / 256 / 256;
- out[posOut ] = (byte) (l % 256); l = l / 256;
- }
- }
- return out;
- } catch (ArrayIndexOutOfBoundsException e) {
- // maybe the input was not base64
- throw new RuntimeException("input probably not base64");
- }
- }
- */
-
public static String encodeHex(long in, int length) {
String s = Long.toHexString(in);
while (s.length() < length) s = "0" + s;
@@ -222,25 +74,6 @@ public final class serverCodings {
return result;
}
-
-/*
- public static String encodeMD5B64(String key, boolean enhanced) {
- if (enhanced)
- return enhancedCoder.encodeBase64(encodeMD5Raw(key));
- else
- return standardCoder.encodeBase64(encodeMD5Raw(key));
- }
-
- public static String encodeMD5B64(File file, boolean enhanced) {
- if (enhanced)
- return enhancedCoder.encodeBase64(encodeMD5Raw(file));
- else
- return standardCoder.encodeBase64(encodeMD5Raw(file));
- }
-*/
- // replace with "kelondroBase64Order.enhancedCoder.encode(serverCodings.encodeMD5Raw("
-
-
public static String encodeMD5Hex(String key) {
// generate a hex representation from the md5 of a string
return encodeHex(encodeMD5Raw(key));