1 11 package org.eclipse.swt.internal; 12 13 14 import org.eclipse.swt.internal.gtk.OS; 15 16 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 35 public static String defaultCodePage () { 36 return "UTF8"; 37 } 38 39 public static char [] mbcsToWcs (String codePage, byte [] buffer) { 40 int [] items_written = new int [1]; 41 int ptr = OS.g_utf8_to_utf16 (buffer, buffer.length, null, items_written, null); 42 if (ptr == 0) return EmptyCharArray; 43 int length = (int)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 codePage, String 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 codePage, char [] buffer, boolean terminate) { 58 int [] items_read = new int [1], items_written = new int [1]; 59 63 int 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)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 |