1 38 39 package com.sun.xml.fastinfoset.algorithm; 40 41 import com.sun.xml.fastinfoset.algorithm.BuiltInEncodingAlgorithm.WordListener; 42 import java.util.ArrayList ; 43 import java.util.List ; 44 import java.io.EOFException ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 import java.nio.CharBuffer ; 49 import java.util.ArrayList ; 50 import org.jvnet.fastinfoset.EncodingAlgorithmException; 51 import com.sun.xml.fastinfoset.CommonResourceBundle; 52 53 public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm { 54 55 public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 56 if (octetLength % (LONG_SIZE * 2) != 0) { 57 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). 58 getString("message.lengthNotMultipleOfUUID",new Object []{new Integer (LONG_SIZE * 2)})); 59 } 60 61 return octetLength / LONG_SIZE; 62 } 63 64 public final Object convertFromCharacters(char[] ch, int start, int length) { 65 final CharBuffer cb = CharBuffer.wrap(ch, start, length); 66 final List longList = new ArrayList (); 67 68 matchWhiteSpaceDelimnatedWords(cb, 69 new WordListener() { 70 public void word(int start, int end) { 71 String uuidValue = cb.subSequence(start, end).toString(); 72 fromUUIDString(uuidValue); 73 longList.add(new Long (_msb)); 74 longList.add(new Long (_lsb)); 75 } 76 } 77 ); 78 79 return generateArrayFromList(longList); 80 } 81 82 public final void convertToCharacters(Object data, StringBuffer s) { 83 if (!(data instanceof long[])) { 84 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotLongArray")); 85 } 86 87 final long[] ldata = (long[])data; 88 89 final int end = ldata.length - 1; 90 for (int i = 0; i <= end; i += 2) { 91 s.append(toUUIDString(ldata[i], ldata[i + 1])); 92 if (i != end) { 93 s.append(' '); 94 } 95 } 96 } 97 98 99 private long _msb; 100 private long _lsb; 101 102 final void fromUUIDString(String name) { 103 String [] components = name.split("-"); 104 if (components.length != 5) 105 throw new IllegalArgumentException (CommonResourceBundle.getInstance(). 106 getString("message.invalidUUID", new Object []{name})); 107 108 for (int i=0; i<5; i++) 109 components[i] = "0x"+components[i]; 110 111 _msb = Long.parseLong(components[0], 16); 112 _msb <<= 16; 113 _msb |= Long.parseLong(components[1], 16); 114 _msb <<= 16; 115 _msb |= Long.parseLong(components[2], 16); 116 117 _lsb = Long.parseLong(components[3], 16); 118 _lsb <<= 48; 119 _lsb |= Long.parseLong(components[4], 16); 120 } 121 122 final String toUUIDString(long msb, long lsb) { 123 return (digits(msb >> 32, 8) + "-" + 124 digits(msb >> 16, 4) + "-" + 125 digits(msb, 4) + "-" + 126 digits(lsb >> 48, 4) + "-" + 127 digits(lsb, 12)); 128 } 129 130 final String digits(long val, int digits) { 131 long hi = 1L << (digits * 4); 132 return Long.toHexString(hi | (val & (hi - 1))).substring(1); 133 } 134 135 } 136 | Popular Tags |