1 29 30 package com.caucho.db.index; 31 32 35 public class StringKeyCompare extends KeyCompare { 36 39 public int compare(byte []keyBuffer, int keyOffset, 40 byte []block, int offset, int length) 41 { 42 int keyLen = keyBuffer[keyOffset]; 43 int blockLen = block[offset]; 44 45 int keySize = 2 * keyLen; 46 int blockSize = 2 * blockLen; 47 48 int end = keySize; 49 if (blockSize < end) 50 end = blockSize; 51 52 for (int i = 1; i <= end; i++) { 53 int ch1 = keyBuffer[keyOffset + i] & 0xff; 54 int ch2 = block[offset + i] & 0xff; 55 56 if (ch1 < ch2) 57 return -1; 58 else if (ch2 < ch1) 59 return 1; 60 } 61 62 if (keyLen < blockLen) 63 return -1; 64 else if (blockLen < keyLen) 65 return 1; 66 else 67 return 0; 68 } 69 70 public String toString(byte []buffer, int offset, int length) 71 { 72 StringBuilder sb = new StringBuilder (); 73 74 int keyLen = buffer[offset]; 75 76 for (int j = 0; j < keyLen; j++) { 77 int ch1 = buffer[offset + 1 + 2 * j] & 0xff; 78 int ch2 = buffer[offset + 1 + 2 * j + 1] & 0xff; 79 80 int ch = (ch1 << 8) + ch2; 81 82 if (ch == 0) 83 break; 84 85 sb.append((char) ch); 86 } 87 88 return sb.toString(); 89 } 90 } 91 | Popular Tags |