minor enhancements

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3344 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 18 years ago
parent b4aa195c27
commit 7673f0869b

@ -67,47 +67,59 @@ public final class serverCodings {
return s; return s;
} }
public static String encodeOctal(byte[] in) {
if (in == null) return "";
StringBuffer result = new StringBuffer(in.length * 8 / 3);
for (int i = 0; i < in.length; i++) {
if ((0Xff & (int) in[i]) < 8) result.append('0');
result.append(Integer.toOctalString(0Xff & (int) in[i]));
}
return new String(result);
}
public static String encodeHex(byte[] in) { public static String encodeHex(byte[] in) {
if (in == null) return ""; if (in == null) return "";
String result = ""; StringBuffer result = new StringBuffer(in.length * 2);
for (int i = 0; i < in.length; i++) for (int i = 0; i < in.length; i++) {
result = result + (((0Xff & (int) in[i]) < 16) ? "0" : "") + Integer.toHexString(0Xff & (int) in[i]); if ((0Xff & (int) in[i]) < 16) result.append('0');
return result; result.append(Integer.toHexString(0Xff & (int) in[i]));
}
return new String(result);
} }
public static byte[] decodeHex(String hex) { public static byte[] decodeHex(String hex) {
byte[] result = new byte[hex.length() / 2]; byte[] result = new byte[hex.length() / 2];
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
result[i] = (byte) (16 * Integer.parseInt(hex.charAt(i * 2) + "", 16) + Integer.parseInt(hex.charAt(i * 2 + 1) + "", 16)); result[i] = (byte) (16 * Integer.parseInt(hex.charAt(i * 2) + "", 16) + Integer.parseInt(hex.charAt(i * 2 + 1) + "", 16));
} }
return result; return result;
} }
public static String encodeMD5Hex(String key) { public static String encodeMD5Hex(String key) {
// generate a hex representation from the md5 of a string // generate a hex representation from the md5 of a string
return encodeHex(encodeMD5Raw(key)); return encodeHex(encodeMD5Raw(key));
} }
public static String encodeMD5Hex(File file) { public static String encodeMD5Hex(File file) {
// generate a hex representation from the md5 of a file // generate a hex representation from the md5 of a file
return encodeHex(encodeMD5Raw(file)); return encodeHex(encodeMD5Raw(file));
} }
public static String encodeMD5Hex(byte[] b) { public static String encodeMD5Hex(byte[] b) {
// generate a hex representation from the md5 of a byte-array // generate a hex representation from the md5 of a byte-array
return encodeHex(encodeMD5Raw(b)); return encodeHex(encodeMD5Raw(b));
} }
public static byte[] encodeMD5Raw(String key) { public static byte[] encodeMD5Raw(String key) {
try { try {
MessageDigest digest = MessageDigest.getInstance("MD5"); MessageDigest digest = MessageDigest.getInstance("MD5");
digest.reset(); digest.reset();
digest.update(key.getBytes()); digest.update(key.getBytes());
return digest.digest(); return digest.digest();
} catch (java.security.NoSuchAlgorithmException e) { } catch (java.security.NoSuchAlgorithmException e) {
System.out.println("Internal Error at md5:" + e.getMessage()); System.out.println("Internal Error at md5:" + e.getMessage());
} }
return null; return null;
} }
public static byte[] encodeMD5Raw(File file) { public static byte[] encodeMD5Raw(File file) {
@ -181,7 +193,7 @@ public final class serverCodings {
} }
public static String map2string(Map m, String separator, boolean braces) { public static String map2string(Map m, String separator, boolean braces) {
final StringBuffer buf = new StringBuffer(512); final StringBuffer buf = new StringBuffer(20 * m.size());
if (braces) { buf.append("{"); } if (braces) { buf.append("{"); }
final Iterator i = m.entrySet().iterator(); final Iterator i = m.entrySet().iterator();
while (i.hasNext()) { while (i.hasNext()) {
@ -192,7 +204,7 @@ public final class serverCodings {
} }
if (buf.length() > 1) { buf.setLength(buf.length() - 1); } // remove last separator if (buf.length() > 1) { buf.setLength(buf.length() - 1); } // remove last separator
if (braces) { buf.append("}"); } if (braces) { buf.append("}"); }
return buf.toString(); return new String(buf);
} }
public static Set string2set(String string, String separator) { public static Set string2set(String string, String separator) {
@ -220,7 +232,7 @@ public final class serverCodings {
if (hasNext) buf.append(separator); if (hasNext) buf.append(separator);
} }
if (braces) buf.append("}"); if (braces) buf.append("}");
return buf.toString(); return new String(buf);
} }
public static void main(String[] s) { public static void main(String[] s) {

@ -312,6 +312,14 @@ public class yacySeed {
dna.put(yacySeed.URL_IN, Integer.toString(Integer.parseInt(v) + count)); dna.put(yacySeed.URL_IN, Integer.toString(Integer.parseInt(v) + count));
} }
// 12 * 6 bit = 72 bit = 24 characters octal-hash
// octal hashes are used for cache-dumps that are DHT-ready
// cause: the natural order of octal hashes are the same as the b64-order of b64Hashes
// a hexhash cannot be used in such cases, and b64Hashes are not appropriate for file names
public static String b64Hash2octalHash(String b64Hash) {
return serverCodings.encodeOctal(kelondroBase64Order.enhancedCoder.decode(b64Hash));
}
// 12 * 6 bit = 72 bit = 18 characters hex-hash // 12 * 6 bit = 72 bit = 18 characters hex-hash
public static String b64Hash2hexHash(String b64Hash) { public static String b64Hash2hexHash(String b64Hash) {
// the hash string represents 12 * 6 bit = 72 bits. This is too much for a long integer. // the hash string represents 12 * 6 bit = 72 bits. This is too much for a long integer.

Loading…
Cancel
Save