KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > Crc64


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * Crc64 hash
33  */

34 public class Crc64 {
35   private static final long POLY64REV = 0xd800000000000000L;
36   private static final long []CRC_TABLE;
37
38   /**
39    * Calculates CRC from a string.
40    */

41   public static long generate(String JavaDoc value)
42   {
43     int len = value.length();
44     long crc = 0;
45
46     for (int i = 0; i < len; i++)
47       crc = next(crc, value.charAt(i));
48
49     return crc;
50   }
51
52   /**
53    * Calculates CRC from a string.
54    */

55   public static long generate(long crc, String JavaDoc value)
56   {
57     int len = value.length();
58
59     for (int i = 0; i < len; i++) {
60       char ch = value.charAt(i);
61
62       if (ch > 0xff)
63         crc = next(crc, (ch >> 8));
64
65       crc = next(crc, ch);
66     }
67
68     return crc;
69   }
70
71   /**
72    * Calculates CRC from a char buffer
73    */

74   public static long generate(long crc, char []buffer, int offset, int len)
75   {
76     for (int i = 0; i < len; i++) {
77       char ch = buffer[offset + i];
78
79       if (ch > 0xff)
80         crc = next(crc, (ch >> 8));
81
82       crc = next(crc, ch);
83     }
84
85     return crc;
86   }
87
88   /**
89    * Calculates the next crc value.
90    */

91   public static long generate(long crc, byte ch)
92   {
93     return (crc >>> 8) ^ CRC_TABLE[((int) crc ^ ch) & 0xff];
94   }
95
96   /**
97    * Calculates the next crc value.
98    */

99   public static long next(long crc, int ch)
100   {
101     return (crc >>> 8) ^ CRC_TABLE[((int) crc ^ ch) & 0xff];
102   }
103
104   static {
105     CRC_TABLE = new long[256];
106
107     for (int i = 0; i < 256; i++) {
108       long v = i;
109
110       for (int j = 0; j < 8; j++) {
111         boolean flag = (v & 1) != 0;
112
113         long newV = v >>> 1;
114
115         if ((v & 0x100000000L) != 0)
116           newV |= 0x100000000L;
117
118         if ((v & 1) != 0)
119           newV ^= POLY64REV;
120
121         v = newV;
122       }
123
124       CRC_TABLE[i] = v;
125     }
126   }
127 }
128
Popular Tags