1 2 3 4 package net.nutch.io; 5 6 import java.io.*; 7 import java.util.*; 8 9 18 public class WritableComparator { 19 private DataInputBuffer buffer = new DataInputBuffer(); 20 21 private Class keyClass; 22 private WritableComparable key1; 23 private WritableComparable key2; 24 25 26 public WritableComparator(Class keyClass) { 27 this.keyClass = keyClass; 28 this.key1 = newKey(); 29 this.key2 = newKey(); 30 } 31 32 33 public Class getKeyClass() { return keyClass; } 34 35 36 public WritableComparable newKey() { 37 try { 38 return (WritableComparable)keyClass.newInstance(); 39 } catch (InstantiationException e) { 40 throw new RuntimeException (e); 41 } catch (IllegalAccessException e) { 42 throw new RuntimeException (e); 43 } 44 } 45 46 53 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { 54 try { 55 buffer.reset(b1, s1, l1); key1.readFields(buffer); 57 58 buffer.reset(b2, s2, l2); key2.readFields(buffer); 60 61 } catch (IOException e) { 62 throw new RuntimeException (e); 63 } 64 65 return compare(key1, key2); } 67 68 72 public int compare(WritableComparable a, WritableComparable b) { 73 return a.compareTo(b); 74 } 75 76 77 public static int compareBytes(byte[] b1, int s1, int l1, 78 byte[] b2, int s2, int l2) { 79 int end1 = s1 + l1; 80 int end2 = s2 + l2; 81 for (int i = s1, j = s2; i < end1 && j < end2; i++, j++) { 82 int a = (b1[i] & 0xff); 83 int b = (b2[j] & 0xff); 84 if (a != b) { 85 return a - b; 86 } 87 } 88 return l1 - l2; 89 } 90 91 92 public static int readUnsignedShort(byte[] bytes, int start) { 93 return (((bytes[start] & 0xff) << 8) + 94 ((bytes[start+1] & 0xff))); 95 } 96 97 98 public static int readInt(byte[] bytes, int start) { 99 return (((bytes[start ] & 0xff) << 24) + 100 ((bytes[start+1] & 0xff) << 16) + 101 ((bytes[start+2] & 0xff) << 8) + 102 ((bytes[start+3] & 0xff))); 103 104 } 105 106 107 public static float readFloat(byte[] bytes, int start) { 108 return Float.intBitsToFloat(readInt(bytes, start)); 109 } 110 111 112 public static long readLong(byte[] bytes, int start) { 113 return ((long)(readInt(bytes, start)) << 32) + 114 (readInt(bytes, start+4) & 0xFFFFFFFFL); 115 } 116 117 } 118 | Popular Tags |