blob: 229186784b5fcdb815d7cc852cc0942656b8c764 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package demo;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
/**
* @author oliver
* @since 18.11.2005
*/
public class Converter {
/**
* You may get problems using String.getBytes("UTF-8") on a non UTF-8
* environment. Here is an alternative for it from
* https://mindprod.com/jgloss/encoding.html.
*
* @param text
* @return
* @throws UnsupportedEncodingException
*/
public static byte[] utf8encode(String text) throws UnsupportedEncodingException {
//return text.getBytes("UTF-8");
Charset cs = Charset.forName("UTF8");
CharBuffer ss = CharBuffer.wrap(text);
ByteBuffer bb = cs.encode(ss);
int limit = bb.limit(); // how many chars in buffer
byte[] b = new byte[ limit ];
bb.get( b, 0 /* offset */, limit );
return b;
}
}
|