Eclipseplugins
CharsetConverter.java
1 package com.proalpha.pds.paconnector.utils;
2 
3 import java.io.UnsupportedEncodingException;
4 import java.nio.charset.Charset;
5 
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 
9 public class CharsetConverter {
10 
11  private static final Logger logger = LoggerFactory.getLogger(CharsetConverter.class);
12 
13  private CharsetConverter() {
14  super();
15  }
16 
22  public static String encode(final String codepage, final String str) {
23 
24  String retval = str;
25 
26  try {
27  // During the initialization of the bridge is the code page null
28  if (codepage != null) {
29 
30  if (Charset.isSupported(codepage)) {
31  retval = doEncoding(codepage, str);
32  } else if (Charset.isSupported("cp" + codepage)) {
33  retval = doEncoding("cp" + codepage, str);
34  } else {
35  throw new UnsupportedEncodingException(String.format("Codepage %s is not supported.", codepage));
36  }
37  }
38  } catch (UnsupportedEncodingException e) {
39  logger.error(e.getMessage(), e);
40  }
41  return retval;
42  }
43 
49  private static String doEncoding(final String codepage, final String str) {
50  return new String(Charset.forName(codepage).encode(str).array());
51  }
52 }
static String encode(final String codepage, final String str)