Java File Append appendHexJavaScriptRepresentation(Appendable out, int codePoint)

Here you can find the source of appendHexJavaScriptRepresentation(Appendable out, int codePoint)

Description

Returns a javascript representation of the character in a hex escaped format.

License

Apache License

Parameter

Parameter Description
out The buffer to which the hex representation should be appended.
codePoint The codepoint to append.

Declaration

private static void appendHexJavaScriptRepresentation(Appendable out, int codePoint) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w w  w .j av  a 2  s  .c  o m*/
 * Copyright 2011 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

public class Main {
    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**
     * Returns a javascript representation of the character in a hex escaped
     * format.
     * @param out The buffer to which the hex representation should be appended.
     * @param codePoint The codepoint to append.
     */
    private static void appendHexJavaScriptRepresentation(Appendable out, int codePoint) throws IOException {
        if (Character.isSupplementaryCodePoint(codePoint)) {
            // Handle supplementary unicode values which are not representable in
            // javascript.  We deal with these by escaping them as two 4B sequences
            // so that they will round-trip properly when sent from java to javascript
            // and back.
            char[] surrogates = Character.toChars(codePoint);
            appendHexJavaScriptRepresentation(out, surrogates[0]);
            appendHexJavaScriptRepresentation(out, surrogates[1]);
            return;
        }
        out.append("\\u").append(HEX_CHARS[(codePoint >>> 12) & 0xf]).append(HEX_CHARS[(codePoint >>> 8) & 0xf])
                .append(HEX_CHARS[(codePoint >>> 4) & 0xf]).append(HEX_CHARS[codePoint & 0xf]);
    }
}

Related

  1. appendFileBytes(String srcFileName, String tarFileName)
  2. appendFileContent(File file, String content, boolean append)
  3. appendFileLines(String fileName, String[] data)
  4. appendFilePart(File dstFile, byte[] bytes)
  5. appendHex(A sb, byte[] array, int offset, int len, char sep)
  6. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  7. appendTo(String fileName, String str)
  8. appendTo(StringBuffer sb, CharSequence s)
  9. appendToArray(final File[] original, final File[] toAppend)