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

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

     1 /*
     2  * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  *
    25  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    26  */
    28 package com.sun.xml.internal.fastinfoset.algorithm;
    30 import java.util.ArrayList;
    31 import java.util.List;
    32 import java.nio.CharBuffer;
    33 import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
    34 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    36 public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm {
    38     public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
    39         if (octetLength % (LONG_SIZE * 2) != 0) {
    40             throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
    41                     getString("message.lengthNotMultipleOfUUID",new Object[]{Integer.valueOf(LONG_SIZE * 2)}));
    42         }
    44         return octetLength / LONG_SIZE;
    45     }
    47     public final Object convertFromCharacters(char[] ch, int start, int length) {
    48         final CharBuffer cb = CharBuffer.wrap(ch, start, length);
    49         final List longList = new ArrayList();
    51         matchWhiteSpaceDelimnatedWords(cb,
    52                 new WordListener() {
    53             public void word(int start, int end) {
    54                 String uuidValue = cb.subSequence(start, end).toString();
    55                 fromUUIDString(uuidValue);
    56                 longList.add(Long.valueOf(_msb));
    57                 longList.add(Long.valueOf(_lsb));
    58             }
    59         }
    60         );
    62         return generateArrayFromList(longList);
    63     }
    65     public final void convertToCharacters(Object data, StringBuffer s) {
    66         if (!(data instanceof long[])) {
    67             throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray"));
    68         }
    70         final long[] ldata = (long[])data;
    72         final int end = ldata.length - 2;
    73         for (int i = 0; i <= end; i += 2) {
    74             s.append(toUUIDString(ldata[i], ldata[i + 1]));
    75             if (i != end) {
    76                 s.append(' ');
    77             }
    78         }
    79     }
    82     private long _msb;
    83     private long _lsb;
    85     final void fromUUIDString(String name) {
    86         String[] components = name.split("-");
    87         if (components.length != 5)
    88             throw new IllegalArgumentException(CommonResourceBundle.getInstance().
    89                     getString("message.invalidUUID", new Object[]{name}));
    91         for (int i=0; i<5; i++)
    92             components[i] = "0x"+components[i];
    94         _msb = Long.parseLong(components[0], 16);
    95         _msb <<= 16;
    96         _msb |= Long.parseLong(components[1], 16);
    97         _msb <<= 16;
    98         _msb |= Long.parseLong(components[2], 16);
   100         _lsb = Long.parseLong(components[3], 16);
   101         _lsb <<= 48;
   102         _lsb |= Long.parseLong(components[4], 16);
   103     }
   105     final String toUUIDString(long msb, long lsb) {
   106         return (digits(msb >> 32, 8) + "-" +
   107                 digits(msb >> 16, 4) + "-" +
   108                 digits(msb, 4) + "-" +
   109                 digits(lsb >> 48, 4) + "-" +
   110                 digits(lsb, 12));
   111     }
   113     final String digits(long val, int digits) {
   114         long hi = 1L << (digits * 4);
   115         return Long.toHexString(hi | (val & (hi - 1))).substring(1);
   116     }
   118 }

mercurial