src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/UUIDEncodingAlgorithm.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/UUIDEncodingAlgorithm.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,118 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.algorithm;
    1.32 +
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.List;
    1.35 +import java.nio.CharBuffer;
    1.36 +import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
    1.37 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    1.38 +
    1.39 +public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm {
    1.40 +
    1.41 +    public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
    1.42 +        if (octetLength % (LONG_SIZE * 2) != 0) {
    1.43 +            throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
    1.44 +                    getString("message.lengthNotMultipleOfUUID",new Object[]{Integer.valueOf(LONG_SIZE * 2)}));
    1.45 +        }
    1.46 +
    1.47 +        return octetLength / LONG_SIZE;
    1.48 +    }
    1.49 +
    1.50 +    public final Object convertFromCharacters(char[] ch, int start, int length) {
    1.51 +        final CharBuffer cb = CharBuffer.wrap(ch, start, length);
    1.52 +        final List longList = new ArrayList();
    1.53 +
    1.54 +        matchWhiteSpaceDelimnatedWords(cb,
    1.55 +                new WordListener() {
    1.56 +            public void word(int start, int end) {
    1.57 +                String uuidValue = cb.subSequence(start, end).toString();
    1.58 +                fromUUIDString(uuidValue);
    1.59 +                longList.add(Long.valueOf(_msb));
    1.60 +                longList.add(Long.valueOf(_lsb));
    1.61 +            }
    1.62 +        }
    1.63 +        );
    1.64 +
    1.65 +        return generateArrayFromList(longList);
    1.66 +    }
    1.67 +
    1.68 +    public final void convertToCharacters(Object data, StringBuffer s) {
    1.69 +        if (!(data instanceof long[])) {
    1.70 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray"));
    1.71 +        }
    1.72 +
    1.73 +        final long[] ldata = (long[])data;
    1.74 +
    1.75 +        final int end = ldata.length - 2;
    1.76 +        for (int i = 0; i <= end; i += 2) {
    1.77 +            s.append(toUUIDString(ldata[i], ldata[i + 1]));
    1.78 +            if (i != end) {
    1.79 +                s.append(' ');
    1.80 +            }
    1.81 +        }
    1.82 +    }
    1.83 +
    1.84 +
    1.85 +    private long _msb;
    1.86 +    private long _lsb;
    1.87 +
    1.88 +    final void fromUUIDString(String name) {
    1.89 +        String[] components = name.split("-");
    1.90 +        if (components.length != 5)
    1.91 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().
    1.92 +                    getString("message.invalidUUID", new Object[]{name}));
    1.93 +
    1.94 +        for (int i=0; i<5; i++)
    1.95 +            components[i] = "0x"+components[i];
    1.96 +
    1.97 +        _msb = Long.parseLong(components[0], 16);
    1.98 +        _msb <<= 16;
    1.99 +        _msb |= Long.parseLong(components[1], 16);
   1.100 +        _msb <<= 16;
   1.101 +        _msb |= Long.parseLong(components[2], 16);
   1.102 +
   1.103 +        _lsb = Long.parseLong(components[3], 16);
   1.104 +        _lsb <<= 48;
   1.105 +        _lsb |= Long.parseLong(components[4], 16);
   1.106 +    }
   1.107 +
   1.108 +    final String toUUIDString(long msb, long lsb) {
   1.109 +        return (digits(msb >> 32, 8) + "-" +
   1.110 +                digits(msb >> 16, 4) + "-" +
   1.111 +                digits(msb, 4) + "-" +
   1.112 +                digits(lsb >> 48, 4) + "-" +
   1.113 +                digits(lsb, 12));
   1.114 +    }
   1.115 +
   1.116 +    final String digits(long val, int digits) {
   1.117 +        long hi = 1L << (digits * 4);
   1.118 +        return Long.toHexString(hi | (val & (hi - 1))).substring(1);
   1.119 +    }
   1.120 +
   1.121 +}

mercurial