Example usage for java.lang Character MAX_LOW_SURROGATE

List of usage examples for java.lang Character MAX_LOW_SURROGATE

Introduction

In this page you can find the example usage for java.lang Character MAX_LOW_SURROGATE.

Prototype

char MAX_LOW_SURROGATE

To view the source code for java.lang Character MAX_LOW_SURROGATE.

Click Source Link

Document

The maximum value of a <a href="http://www.unicode.org/glossary/#low_surrogate_code_unit"> Unicode low-surrogate code unit</a> in the UTF-16 encoding, constant '\u005CuDFFF' .

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = String.format("\\u%04x", (int) Character.MAX_LOW_SURROGATE);
    System.out.println(s);//from  ww w .  j a v  a 2 s.  co m
}

From source file:se.sawano.java.security.otp.google.keyuri.UnicodeEscaper.java

/**
 * Returns the Unicode code point of the character at the given index.
 *
 * <p>Unlike {@link Character#codePointAt(CharSequence, int)} or {@link String#codePointAt(int)}
 * this method will never fail silently when encountering an invalid surrogate pair.
 *
 * <p>The behaviour of this method is as follows:
 * <ol>// w  ww .j  a va  2s  . c om
 * <li>If {@code index >= end}, {@link IndexOutOfBoundsException} is thrown.
 * <li><b>If the character at the specified index is not a surrogate, it is returned.</b>
 * <li>If the first character was a high surrogate value, then an attempt is made to read the next
 * character.
 * <ol>
 * <li><b>If the end of the sequence was reached, the negated value of the trailing high
 * surrogate is returned.</b>
 * <li><b>If the next character was a valid low surrogate, the code point value of the
 * high/low surrogate pair is returned.</b>
 * <li>If the next character was not a low surrogate value, then {@link
 * IllegalArgumentException} is thrown.
 * </ol>
 * <li>If the first character was a low surrogate value, {@link IllegalArgumentException} is
 * thrown.
 * </ol>
 *
 * @param seq
 *         the sequence of characters from which to decode the code point
 * @param index
 *         the index of the first character to decode
 * @param end
 *         the index beyond the last valid character to decode
 *
 * @return the Unicode code point for the given index or the negated value of the trailing high surrogate character at the end of the sequence
 */
protected static int codePointAt(CharSequence seq, int index, int end) {
    notNull(seq);
    if (index < end) {
        char c1 = seq.charAt(index++);
        if (c1 < Character.MIN_HIGH_SURROGATE || c1 > Character.MAX_LOW_SURROGATE) {
            // Fast path (first test is probably all we need to do)
            return c1;
        } else if (c1 <= Character.MAX_HIGH_SURROGATE) {
            // If the high surrogate was the last character, return its inverse
            if (index == end) {
                return -c1;
            }
            // Otherwise look for the low surrogate following it
            char c2 = seq.charAt(index);
            if (Character.isLowSurrogate(c2)) {
                return Character.toCodePoint(c1, c2);
            }
            throw new IllegalArgumentException("Expected low surrogate but got char '" + c2 + "' with value "
                    + (int) c2 + " at index " + index + " in '" + seq + "'");
        } else {
            throw new IllegalArgumentException("Unexpected low surrogate character '" + c1 + "' with value "
                    + (int) c1 + " at index " + (index - 1) + " in '" + seq + "'");
        }
    }
    throw new IndexOutOfBoundsException("Index exceeds specified range");
}