001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.db;
007:
008: import java.io.File;
009: import java.security.*;
010:
011: public class DbUtil {
012:
013: public static final int MD5_BYTES = 16;
014:
015: public static int hex2uint32(byte data[]) {
016: int v = 0;
017: int lim = Math.min(8, data.length);
018: for (int i = 0; i < lim; i++)
019: v |= ((int) (data[i] & 0xf)) << ((7 - i) * 4);
020: return v & 0x7fff;
021: }
022:
023: /** @return canonical, absolute form of fn (relative to cwd if fn is relative) */
024: public static String absolute_path(String fn) throws Exception {
025: return new File(fn).getCanonicalPath();
026: }
027:
028: /** @return the directory containing this path */
029: public static String path2directory(String path) throws Exception {
030: File fp = new File(path);
031: if (fp.isDirectory())
032: return fp.toString();
033: String parent = new File(path).getParent();
034: return parent;
035: }
036:
037: /** @return address of path relative to base */
038: public static String relative_path(String base, String path)
039: throws Exception {
040: File fp = new File(path);
041: if (fp.isAbsolute())
042: return fp.getCanonicalPath();
043: if (base.charAt(base.length() - 1) != File.separatorChar
044: && path.charAt(0) != File.separatorChar)
045: base += File.separator;
046: return base + path;
047: }
048:
049: /** @return address of path relative to base */
050: public static String absolute_path(String base, String path)
051: throws Exception {
052: File fp = new File(path);
053: if (fp.isAbsolute())
054: return fp.getCanonicalPath();
055: File fb = new File(base);
056: //return new File(fb.getCanonicalFile(), path).toString();
057: return new File(base, path).toString();
058: }
059:
060: /** @return a URL MD5 based db key */
061: public static Datum key_create(String url) throws Exception {
062: byte md5[] = new byte[MD5_BYTES + 1];
063: System
064: .arraycopy(md5bytes(url.getBytes()), 0, md5, 0,
065: MD5_BYTES);
066: md5[MD5_BYTES] = 0;
067: return new Datum(md5);
068: }
069:
070: static byte[] dotPer = { '.', 'p', 'e', 'r', };
071:
072: public static Datum persist_key_create(String url) throws Exception {
073: byte md5[] = new byte[MD5_BYTES + 5];
074: System
075: .arraycopy(md5bytes(url.getBytes()), 0, md5, 0,
076: MD5_BYTES);
077: System.arraycopy(dotPer, 0, md5, MD5_BYTES, 4);
078: md5[MD5_BYTES + 4] = 0;
079: return new Datum(md5);
080: }
081:
082: static MessageDigest md;
083: static byte[] hexchars = { '0', '1', '2', '3', '4', '5', '6', '7',
084: '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
085:
086: public static synchronized byte[] md5bytes(byte[] b)
087: throws Exception {
088: byte[] res = new byte[32];
089: if (md == null)
090: md = MessageDigest.getInstance("MD5");
091: md.update(b);
092: byte[] md5 = md.digest();
093: for (int i = 0; i < 16; ++i) {
094: res[2 * i + 1] = hexchars[md5[i] & 0xf]; // hi nibble
095: res[2 * i] = hexchars[(md5[i] >> 4) & 0xf]; // lo nibble
096: }
097: return res;
098: }
099:
100: }
|