Example usage for java.lang Character MAX_HIGH_SURROGATE

List of usage examples for java.lang Character MAX_HIGH_SURROGATE

Introduction

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

Prototype

char MAX_HIGH_SURROGATE

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = String.format("\\u%04x", (int) Character.MAX_HIGH_SURROGATE);
    System.out.println(s);/*from  w  w  w. j av a  2  s .c o  m*/

}

From source file:org.zenoss.zep.dao.impl.DaoUtils.java

/**
 * Truncates the specified string to fit in the specified maximum number of
 * UTF-8 bytes. This method will not split strings in the middle of
 * surrogate pairs.//from w  w w  .j  a v a2  s  . c  om
 * 
 * @param original
 *            The original string.
 * @param maxBytes
 *            The maximum number of UTF-8 bytes available to store the
 *            string.
 * @return If the string doesn't overflow the number of specified bytes,
 *         then the original string is returned, otherwise the string is
 *         truncated to the number of bytes available to encode
 */
public static String truncateStringToUtf8(final String original, final int maxBytes) {
    final int length = original.length();
    int newLength = 0;
    int currentBytes = 0;
    while (newLength < length) {
        final char c = original.charAt(newLength);
        boolean isSurrogate = false;
        if (c <= 0x7f) {
            ++currentBytes;
        } else if (c <= 0x7FF) {
            currentBytes += 2;
        } else if (c <= Character.MAX_HIGH_SURROGATE) {
            currentBytes += 4;
            isSurrogate = true;
        } else if (c <= 0xFFFF) {
            currentBytes += 3;
        } else {
            currentBytes += 4;
        }
        if (currentBytes > maxBytes) {
            break;
        }
        if (isSurrogate) {
            newLength += 2;
        } else {
            ++newLength;
        }
    }
    return (newLength == length) ? original : original.substring(0, newLength);
}

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>/* ww  w. j a va 2 s  . c o  m*/
 * <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");
}