001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     * @author Shuyang Zhou
023     */
024    public class UnicodeFormatter {
025    
026            public static String bytesToHex(byte[] bytes) {
027                    char[] array = new char[bytes.length * 2];
028    
029                    for (int i = 0; i < bytes.length; i++) {
030                            byte b = bytes[i];
031    
032                            array[(i * 2) + 0] = _HEX_DIGITS[(b >> 4) & 0x0f];
033                            array[(i * 2) + 1] = _HEX_DIGITS[b & 0x0f];
034                    }
035    
036                    return new String(array);
037            }
038    
039            public static String byteToHex(byte b) {
040                    char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]};
041    
042                    return new String(array);
043            }
044    
045            public static char[] byteToHex(byte b, char[] hexes) {
046                    return byteToHex(b, hexes, false);
047            }
048    
049            public static char[] byteToHex(byte b, char[] hexes, boolean upperCase) {
050                    if (upperCase) {
051                            return _byteToHex(b, hexes, _HEX_DIGITS_UPPER_CASE);
052                    }
053                    else {
054                            return _byteToHex(b, hexes, _HEX_DIGITS);
055                    }
056            }
057    
058            public static String charToHex(char c) {
059                    byte hi = (byte)(c >>> 8);
060                    byte lo = (byte)(c & 0xff);
061    
062                    char[] array = {
063                            _HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f],
064                            _HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f]
065                    };
066    
067                    return new String(array);
068            }
069    
070            public static String parseString(String hexString) {
071                    StringBuilder sb = new StringBuilder();
072    
073                    char[] array = hexString.toCharArray();
074    
075                    if ((array.length % 6) != 0) {
076                            _log.error("String is not in hex format");
077    
078                            return hexString;
079                    }
080    
081                    for (int i = 2; i < hexString.length(); i = i + 6) {
082                            String s = hexString.substring(i, i + 4);
083    
084                            try {
085                                    char c = (char)Integer.parseInt(s, 16);
086    
087                                    sb.append(c);
088                            }
089                            catch (Exception e) {
090                                    _log.error(e, e);
091    
092                                    return hexString;
093                            }
094                    }
095    
096                    return sb.toString();
097            }
098    
099            public static String toString(char[] array) {
100                    StringBuilder sb = new StringBuilder(array.length * 6);
101    
102                    char[] hexes = new char[4];
103    
104                    for (int i = 0; i < array.length; i++) {
105                            sb.append(_UNICODE_PREFIX);
106                            sb.append(_charToHex(array[i], hexes));
107                    }
108    
109                    return sb.toString();
110            }
111    
112            public static String toString(String s) {
113                    if (s == null) {
114                            return null;
115                    }
116    
117                    StringBuilder sb = new StringBuilder(s.length() * 6);
118    
119                    char[] hexes = new char[4];
120    
121                    for (int i = 0; i < s.length(); i++) {
122                            sb.append(_UNICODE_PREFIX);
123                            sb.append(_charToHex(s.charAt(i), hexes));
124                    }
125    
126                    return sb.toString();
127            }
128    
129            private static char[] _byteToHex(byte b, char[] hexes, char[] table) {
130                    hexes[0] = table[(b >> 4) & 0x0f];
131                    hexes[1] = table[b & 0x0f];
132    
133                    return hexes;
134            }
135    
136            private static char[] _charToHex(char c, char[] hexes) {
137                    byte hi = (byte)(c >>> 8);
138                    byte lo = (byte)(c & 0xff);
139    
140                    hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f];
141                    hexes[1] = _HEX_DIGITS[hi & 0x0f];
142                    hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f];
143                    hexes[3] = _HEX_DIGITS[lo & 0x0f];
144    
145                    return hexes;
146            }
147    
148            private static final char[] _HEX_DIGITS = {
149                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
150                    'e', 'f'
151            };
152    
153            private static final char[] _HEX_DIGITS_UPPER_CASE = {
154                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
155                    'E', 'F'
156            };
157    
158            private static final String _UNICODE_PREFIX = "\\u";
159    
160            private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class);
161    
162    }