[pgsql-jp: 25256] Re: Windows上JDBC でのエンコーディング

Eiji Tokuya e-tokuya @ sankyo-unyu.jp
2002年 3月 12日 (火) 15:43:05 JST


This is a multi-part message in MIME format.

------=_NextPart_000_0000_01C1C9DC.9A209820
Content-Type: text/plain;
	charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit

徳家です。

> 谷田です。
>
> いえ、私が言いたいのは、単にそれが本当ならJDBCドライバにとbackendにエン
> コーディングの不一致というバグがあるということで、実は以下のpatchが必要
> じゃないの?ということです。
>
> *** Encoding.java.orig	Tue Nov 20 07:33:37 2001
> --- Encoding.java	Mon Mar 11 19:52:18 2002
> ***************
> *** 41,47 ****
>   		encodings.put("EUC_CN", new String[] { "EUC_CN" });
>   		encodings.put("EUC_KR", new String[] { "EUC_KR" });
>   		encodings.put("EUC_TW", new String[] { "EUC_TW" });
> ! 		encodings.put("SJIS", new String[] { "SJIS" });
>   		encodings.put("BIG5", new String[] { "Big5" });
>   		encodings.put("WIN1250", new String[] { "Cp1250" });
>   		encodings.put("WIN", new String[] { "Cp1251" });
> --- 41,47 ----
>   		encodings.put("EUC_CN", new String[] { "EUC_CN" });
>   		encodings.put("EUC_KR", new String[] { "EUC_KR" });
>   		encodings.put("EUC_TW", new String[] { "EUC_TW" });
> ! 		encodings.put("SJIS", new String[] { "MS932","SJIS" });
>   		encodings.put("BIG5", new String[] { "Big5" });
>   		encodings.put("WIN1250", new String[] { "Cp1250" });
>   		encodings.put("WIN", new String[] { "Cp1251" });
>
>
> --
> Yutaka tanida <yutaka @ hi-net.zaq.ne.jp>
>
>

JDBCにこのような部分があったのですね・・・・・。
ということで、追加エンコーディング対応のEncoding.javaを添付してみます。

こんな感じで正しく動作しますかねぇ?

徳家

------=_NextPart_000_0000_01C1C9DC.9A209820
Content-Type: application/octet-stream;
	name="Encoding.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="Encoding.java"

package org.postgresql.core;=0A=
=0A=
import java.io.*;=0A=
import java.util.*;=0A=
import java.sql.SQLException;=0A=
import org.postgresql.util.*;=0A=
=0A=
/*=0A=
 * Converts to and from the character encoding used by the backend.=0A=
 *=0A=
 * $Id: Encoding.java,v 1.4 2001/11/19 22:33:37 momjian Exp $=0A=
 */=0A=
=0A=
public class Encoding=0A=
{=0A=
=0A=
	private static final Encoding DEFAULT_ENCODING =3D new Encoding(null);=0A=
=0A=
	/*=0A=
	 * Preferred JVM encodings for backend encodings.=0A=
	 */=0A=
	private static final Hashtable encodings =3D new Hashtable();=0A=
=0A=
	static {=0A=
		//Note: this list should match the set of supported server=0A=
		// encodings found in backend/util/mb/encnames.c=0A=
		encodings.put("SQL_ASCII", new String[] { "ASCII", "us-ascii" });=0A=
		encodings.put("UNICODE", new String[] { "UTF-8", "UTF8" });=0A=
		encodings.put("LATIN1", new String[] { "ISO8859_1" });=0A=
		encodings.put("LATIN2", new String[] { "ISO8859_2" });=0A=
		encodings.put("LATIN3", new String[] { "ISO8859_3" });=0A=
		encodings.put("LATIN4", new String[] { "ISO8859_4" });=0A=
		encodings.put("ISO_8859_5", new String[] { "ISO8859_5" });=0A=
		encodings.put("ISO_8859_6", new String[] { "ISO8859_6" });=0A=
		encodings.put("ISO_8859_7", new String[] { "ISO8859_7" });=0A=
		encodings.put("ISO_8859_8", new String[] { "ISO8859_8" });=0A=
		encodings.put("LATIN5", new String[] { "ISO8859_9" });=0A=
		encodings.put("LATIN7", new String[] { "ISO8859_13" });=0A=
		encodings.put("LATIN9", new String[] { "ISO8859_15_FDIS" });=0A=
		encodings.put("EUC_JP", new String[] { "EUC_JP" });=0A=
		encodings.put("EUC_CN", new String[] { "EUC_CN" });=0A=
		encodings.put("EUC_KR", new String[] { "EUC_KR" });=0A=
		encodings.put("JOHAB", new String[] { "Johab" });=0A=
		encodings.put("EUC_TW", new String[] { "EUC_TW" });=0A=
		encodings.put("SJIS", new String[] { "MS932","SJIS" });=0A=
		encodings.put("BIG5", new String[] { "Big5","MS950","Cp950" });=0A=
		encodings.put("GBK", new String[] { "GBK","MS936" });=0A=
		encodings.put("UHC", new String[] { "MS949","Cp949","Cp949C" });=0A=
		encodings.put("TCVN", new String[] { "Cp1258" });=0A=
		encodings.put("WIN1256", new String[] { "Cp1256" });=0A=
		encodings.put("WIN1250", new String[] { "Cp1250" });=0A=
		encodings.put("WIN874", new String[] { "MS874","Cp874" });=0A=
		encodings.put("WIN", new String[] { "Cp1251" });=0A=
		encodings.put("ALT", new String[] { "Cp866" });=0A=
		// We prefer KOI8-U, since it is a superset of KOI8-R.=0A=
		encodings.put("KOI8", new String[] { "KOI8_U", "KOI8_R" });=0A=
		// If the database isn't encoding-aware then we can't have=0A=
		// any preferred encodings.=0A=
		encodings.put("UNKNOWN", new String[0]);=0A=
		// The following encodings do not have a java equivalent=0A=
		encodings.put("MULE_INTERNAL", new String[0]);=0A=
		encodings.put("LATIN6", new String[0]);=0A=
		encodings.put("LATIN8", new String[0]);=0A=
		encodings.put("LATIN10", new String[0]);=0A=
	}=0A=
=0A=
	private final String encoding;=0A=
=0A=
	private Encoding(String encoding)=0A=
	{=0A=
		this.encoding =3D encoding;=0A=
	}=0A=
=0A=
	/*=0A=
	 * Get an Encoding for from the given database encoding and=0A=
	 * the encoding passed in by the user.=0A=
	 */=0A=
	public static Encoding getEncoding(String databaseEncoding,=0A=
									   String passedEncoding)=0A=
	{=0A=
		if (passedEncoding !=3D null)=0A=
		{=0A=
			if (isAvailable(passedEncoding))=0A=
			{=0A=
				return new Encoding(passedEncoding);=0A=
			}=0A=
			else=0A=
			{=0A=
				return defaultEncoding();=0A=
			}=0A=
		}=0A=
		else=0A=
		{=0A=
			return encodingForDatabaseEncoding(databaseEncoding);=0A=
		}=0A=
	}=0A=
=0A=
	/*=0A=
	 * Get an Encoding matching the given database encoding.=0A=
	 */=0A=
	private static Encoding encodingForDatabaseEncoding(String =
databaseEncoding)=0A=
	{=0A=
		// If the backend encoding is known and there is a suitable=0A=
		// encoding in the JVM we use that. Otherwise we fall back=0A=
		// to the default encoding of the JVM.=0A=
=0A=
		if (encodings.containsKey(databaseEncoding))=0A=
		{=0A=
			String[] candidates =3D (String[]) encodings.get(databaseEncoding);=0A=
			for (int i =3D 0; i < candidates.length; i++)=0A=
			{=0A=
				if (isAvailable(candidates[i]))=0A=
				{=0A=
					return new Encoding(candidates[i]);=0A=
				}=0A=
			}=0A=
		}=0A=
		return defaultEncoding();=0A=
	}=0A=
=0A=
	/*=0A=
	 * Name of the (JVM) encoding used.=0A=
	 */=0A=
	public String name()=0A=
	{=0A=
		return encoding;=0A=
	}=0A=
=0A=
	/*=0A=
	 * Encode a string to an array of bytes.=0A=
	 */=0A=
	public byte[] encode(String s) throws SQLException=0A=
	{=0A=
		try=0A=
		{=0A=
			if (encoding =3D=3D null)=0A=
			{=0A=
				return s.getBytes();=0A=
			}=0A=
			else=0A=
			{=0A=
				return s.getBytes(encoding);=0A=
			}=0A=
		}=0A=
		catch (UnsupportedEncodingException e)=0A=
		{=0A=
			throw new PSQLException("postgresql.stream.encoding", e);=0A=
		}=0A=
	}=0A=
=0A=
	/*=0A=
	 * Decode an array of bytes into a string.=0A=
	 */=0A=
	public String decode(byte[] encodedString, int offset, int length) =
throws SQLException=0A=
	{=0A=
		try=0A=
		{=0A=
			if (encoding =3D=3D null)=0A=
			{=0A=
				return new String(encodedString, offset, length);=0A=
			}=0A=
			else=0A=
			{=0A=
				return new String(encodedString, offset, length, encoding);=0A=
			}=0A=
		}=0A=
		catch (UnsupportedEncodingException e)=0A=
		{=0A=
			throw new PSQLException("postgresql.stream.encoding", e);=0A=
		}=0A=
	}=0A=
=0A=
	/*=0A=
	 * Decode an array of bytes into a string.=0A=
	 */=0A=
	public String decode(byte[] encodedString) throws SQLException=0A=
	{=0A=
		return decode(encodedString, 0, encodedString.length);=0A=
	}=0A=
=0A=
	/*=0A=
	 * Get a Reader that decodes the given InputStream.=0A=
	 */=0A=
	public Reader getDecodingReader(InputStream in) throws SQLException=0A=
	{=0A=
		try=0A=
		{=0A=
			if (encoding =3D=3D null)=0A=
			{=0A=
				return new InputStreamReader(in);=0A=
			}=0A=
			else=0A=
			{=0A=
				return new InputStreamReader(in, encoding);=0A=
			}=0A=
		}=0A=
		catch (UnsupportedEncodingException e)=0A=
		{=0A=
			throw new PSQLException("postgresql.res.encoding", e);=0A=
		}=0A=
	}=0A=
=0A=
	/*=0A=
	 * Get an Encoding using the default encoding for the JVM.=0A=
	 */=0A=
	public static Encoding defaultEncoding()=0A=
	{=0A=
		return DEFAULT_ENCODING;=0A=
	}=0A=
=0A=
	/*=0A=
	 * Test if an encoding is available in the JVM.=0A=
	 */=0A=
	private static boolean isAvailable(String encodingName)=0A=
	{=0A=
		try=0A=
		{=0A=
			"DUMMY".getBytes(encodingName);=0A=
			return true;=0A=
		}=0A=
		catch (UnsupportedEncodingException e)=0A=
		{=0A=
			return false;=0A=
		}=0A=
	}=0A=
}=0A=

------=_NextPart_000_0000_01C1C9DC.9A209820--




pgsql-jp メーリングリストの案内