KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > Converter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.internal;
12
13
14 import org.eclipse.swt.internal.gtk.OS;
15
16 /**
17  * This class implements the conversions between unicode characters
18  * and the <em>platform supported</em> representation for characters.
19  * <p>
20  * Note that, unicode characters which can not be found in the platform
21  * encoding will be converted to an arbitrary platform specific character.
22  * </p>
23  */

24 public final class Converter {
25     public static final byte [] NullByteArray = new byte [1];
26     public static final byte [] EmptyByteArray = new byte [0];
27     public static final char [] EmptyCharArray = new char [0];
28
29 /**
30  * Returns the default code page for the platform where the
31  * application is currently running.
32  *
33  * @return the default code page
34  */

35 public static String JavaDoc defaultCodePage () {
36     return "UTF8";
37 }
38
39 public static char [] mbcsToWcs (String JavaDoc codePage, byte [] buffer) {
40     int /*long*/ [] items_written = new int /*long*/ [1];
41     int /*long*/ ptr = OS.g_utf8_to_utf16 (buffer, buffer.length, null, items_written, null);
42     if (ptr == 0) return EmptyCharArray;
43     int length = (int)/*64*/items_written [0];
44     char [] chars = new char [length];
45     OS.memmove (chars, ptr, length * 2);
46     OS.g_free (ptr);
47     return chars;
48 }
49
50 public static byte [] wcsToMbcs (String JavaDoc codePage, String JavaDoc string, boolean terminate) {
51     int length = string.length ();
52     char [] buffer = new char [length];
53     string.getChars (0, length, buffer, 0);
54     return wcsToMbcs (codePage, buffer, terminate);
55 }
56
57 public static byte [] wcsToMbcs (String JavaDoc codePage, char [] buffer, boolean terminate) {
58     int /*long*/ [] items_read = new int /*long*/ [1], items_written = new int /*long*/ [1];
59     /*
60     * Note that g_utf16_to_utf8() stops converting
61     * when it finds the first NULL.
62     */

63     int /*long*/ ptr = OS.g_utf16_to_utf8 (buffer, buffer.length, items_read, items_written, null);
64     if (ptr == 0) return terminate ? NullByteArray : EmptyByteArray;
65     int written = (int)/*64*/items_written [0];
66     byte [] bytes = new byte [written + (terminate ? 1 : 0)];
67     OS.memmove (bytes, ptr, written);
68     OS.g_free (ptr);
69     return bytes;
70 }
71
72 }
73
Popular Tags