mysqllatin1utility.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 105 行

JAVA
105
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Nam Nguyen */package com.caucho.quercus.lib.db;import com.caucho.util.IntArray;import com.caucho.util.IntMap;public class MysqlLatin1Utility{  // Mysql's "latin1" is not strict ISO-8859-1 as it is more like  // ISO-8859-1 supplemented with some of Windows-1252 0x80-0x9F  // characters.  //  // from /usr/share/mysql/charsets/latin1.xml   //  private static char []C1_MAP    = { '\u20AC', '\u0081', '\u201A', '\u0192',        '\u201E', '\u2026', '\u2020', '\u2021',        '\u02C6', '\u2030', '\u0160', '\u2039',        '\u0152', '\u008D', '\u017D', '\u008F',        '\u0090', '\u2018', '\u2019', '\u201C',        '\u201D', '\u2022', '\u2013', '\u2014',        '\u02DC', '\u2122', '\u0161', '\u203A',        '\u0153', '\u009D', '\u017E', '\u0178'};    private static IntMap UNICODE_MAP = new IntMap();    static {    for (int i = 0; i < C1_MAP.length; i++) {      UNICODE_MAP.put(new Integer(C1_MAP[i]), i + 0x80);    }  }    public static String decode(byte []bytes)  {    StringBuilder sb = new StringBuilder();        for (int i = 0; i < bytes.length; i++) {      byte b = bytes[i];            if (0x80 <= b && b <= 0x9F)        sb.append(C1_MAP[b - 0x80]);      else        sb.append((char) b);    }        return sb.toString();  }    public static byte[] encode(String s)  {    int len = s.length();        byte []bytes = new byte[len];        for (int i = 0; i < len; i++) {      int ch = s.charAt(i);            // there was a previous error in converting to a Java String      if (ch == 0xfffd)        return null;            int value = UNICODE_MAP.get(ch);            if (value != IntMap.NULL) {        bytes[i] = (byte) value;      }      else {        bytes[i] = (byte) ch;                //System.err.println("MysqlLatinUtility->encode(): " + Integer.toHexString((byte) ch) + " . " + Integer.toHexString(ch) + " . " + ch);              }    }        return bytes;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?