クラス jni.JNICharCode


package jni;

import java.io.*;
import java.util.ArrayList;

/**
* ネイティブで文字列を中国語文字列を表示する。
*/
public class JNICharCode {
static {
System.loadLibrary("jnicharcode");
}

public static void main(String[] args) {
JNICharCode jnicd = new JNICharCode();
String[] s = jnicd.read("test.txt");
jnicd.write("test_copy.tdf", s);
jnicd.printString(s);
}

JNICharCode() {
}

/**
* JavaとCの両方で文字を出力する
*/
private void printString(String[] s) {
try {
String utf16 = new String(s[0].getBytes(), "UTF16");
String utf8 = new String(s[0].getBytes(), "UTF8");
String eucjp = new String(s[0].getBytes(), "EUC-JP");
String sjis = new String(s[0].getBytes(), "SJIS");
String siftjis = new String(s[0].getBytes(), "Shift-JIS");
String utf16be = new String(s[0].getBytes(), "UTF-16BE");
String euc_cn = new String(s[0].getBytes(), "EUC-CN");
String gb = new String(s[0].getBytes(), "GB2312");
String ms = new String(s[0].getBytes(), "MS932");

System.out.println(s[0].equals(utf16));
System.out.println(s[0].equals(utf8));
System.out.println(s[0].equals(eucjp));
System.out.println(s[0].equals(sjis));
System.out.println(s[0].equals(siftjis));
System.out.println(s[0].equals(utf16be));
System.out.println(s[0].equals(euc_cn));
System.out.println(s[0].equals(gb));
System.out.println(s[0].equals(ms));

System.out.println(s[0]);
System.out.println(utf16);
System.out.println(utf8);
System.out.println(eucjp);
System.out.println(sjis);
System.out.println(siftjis);
System.out.println(utf16be);
System.out.println(euc_cn);
System.out.println(gb);
System.out.println(ms);

printStringAtNative(s[0]);
printStringAtNative(utf16);
printStringAtNative(utf8);
printStringAtNative(eucjp);
printStringAtNative(sjis);
printStringAtNative(siftjis);
printStringAtNative(utf16be);
printStringAtNative(euc_cn);
printStringAtNative(gb);
printStringAtNative(ms);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

/**
* 文字を渡します
*/
private native void printStringAtNative(String s);

/**
* 中国語ファイルを読み込む
*/
private String[] read(String filename) {
FileInputStream fis = null;
InputStreamReader isr = null;
ArrayList al = new ArrayList();

try {
fis = new FileInputStream(filename);
isr = new InputStreamReader(fis, "GB2312");

int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = isr.read()) != -1) {
if (ch == '\n') {
al.add(sb.toString());
sb = new StringBuffer();
} else if (ch == '\r') {
// nothinig to do
} else {
sb.append((char) ch);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null)
try {
isr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
if (fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return (String[]) al.toArray(new String[0]);
}

/**
* 中国語ファイルを書き込む
*/
private void write(String filename, String[] s) {
FileOutputStream fos = null;
OutputStreamWriter osw = null;

try {
fos = new FileOutputStream(filename);
osw = new OutputStreamWriter(fos, "GB2312");

for (int i = 0; i < s.length; i++) {
osw.write(s[i] + "\r\n");
}
osw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null)
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fos != null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}