jdk/test/java/nio/charset/coders/Surrogate.java
changeset 6035 2f471c697ba2
parent 6034 755ea7201562
parent 6026 013d21180ccf
child 6036 88db80c8e49c
equal deleted inserted replaced
6034:755ea7201562 6035:2f471c697ba2
     1 /*
       
     2  * Copyright (c) 2010, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 public class Surrogate {
       
    25 
       
    26     public static final int UCS4_SURROGATE_MIN = 0x10000;
       
    27     public static final int UCS4_MAX = (1 << 20) + UCS4_SURROGATE_MIN - 1;
       
    28 
       
    29     // UTF-16 surrogate-character ranges
       
    30     //
       
    31     public static final char MIN_HIGH = '\uD800';
       
    32     public static final char MAX_HIGH = '\uDBFF';
       
    33     public static final char MIN_LOW  = '\uDC00';
       
    34     public static final char MAX_LOW  = '\uDFFF';
       
    35     public static final char MIN = MIN_HIGH;
       
    36     public static final char MAX = MAX_LOW;
       
    37 
       
    38     public static boolean neededFor(int uc) {
       
    39         return (uc >= UCS4_SURROGATE_MIN) && (uc <= UCS4_MAX);
       
    40     }
       
    41 
       
    42     public static boolean isHigh(int c) {
       
    43         return (MIN_HIGH <= c) && (c <= MAX_HIGH);
       
    44     }
       
    45 
       
    46     static char high(int uc) {
       
    47         return (char)(0xd800 | (((uc - UCS4_SURROGATE_MIN) >> 10) & 0x3ff));
       
    48     }
       
    49 
       
    50     public static boolean isLow(int c) {
       
    51         return (MIN_LOW <= c) && (c <= MAX_LOW);
       
    52     }
       
    53 
       
    54     static char low(int uc) {
       
    55         return (char)(0xdc00 | ((uc - UCS4_SURROGATE_MIN) & 0x3ff));
       
    56     }
       
    57 
       
    58     public static boolean is(int c) {
       
    59         return (MIN <= c) && (c <= MAX);
       
    60     }
       
    61 
       
    62     static int toUCS4(char c, char d) {
       
    63         return (((c & 0x3ff) << 10) | (d & 0x3ff)) + 0x10000;
       
    64     }
       
    65 
       
    66 }