Java File Append appendHexJavaScriptRepresentation(StringBuilder sb, char c)

Here you can find the source of appendHexJavaScriptRepresentation(StringBuilder sb, char c)

Description

append Hex Java Script Representation

License

Apache License

Declaration

public static void appendHexJavaScriptRepresentation(StringBuilder sb, char c) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w .j  av a  2  s  . c o  m*/
 * Copyright 2009 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' };

    /**
     */
    public static void appendHexJavaScriptRepresentation(StringBuilder sb, char c) {
        try {
            appendHexJavaScriptRepresentation(c, sb);
        } catch (IOException ex) {
            // StringBuilder does not throw IOException.
            throw new RuntimeException(ex);
        }
    }

    /**
     * Returns a JavaScript representation of the character in a hex escaped format.
     *
     * @param codePoint
     *            The code point to append.
     * @param out
     *            The buffer to which the hex representation should be appended.
     */
    private static void appendHexJavaScriptRepresentation(int codePoint, Appendable out) 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(surrogates[0], out);
            appendHexJavaScriptRepresentation(surrogates[1], out);
            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. appendFileContent(File file, String content, boolean append)
  2. appendFileLines(String fileName, String[] data)
  3. appendFilePart(File dstFile, byte[] bytes)
  4. appendHex(A sb, byte[] array, int offset, int len, char sep)
  5. appendHexJavaScriptRepresentation(Appendable out, int codePoint)
  6. appendTo(String fileName, String str)
  7. appendTo(StringBuffer sb, CharSequence s)
  8. appendToArray(final File[] original, final File[] toAppend)
  9. appendToCollection(final Vector original, final File[] toAppend)