Debian Codegraph @

src/uk/me/parabola/imgfmt/app/labelenc/Utf8Encoder.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007,2014 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: Feb 19, 2008
16 */
17 package uk.me.parabola.imgfmt.app.labelenc;
18
19 import java.util.Locale;
20 import java.nio.charset.StandardCharsets;
21 import java.text.Normalizer;
22
23 /**
24 * Encoder for labels in utf-8.
25 *
26 * @author Steve Ratcliffe
27 */
28 public class Utf8Encoder extends BaseEncoder implements CharacterEncoder {
29
30 public EncodedText encodeText(String text) {
31 if (text == null || text.isEmpty())
32 return NO_TEXT;
33
34 String uctext;
35 if (isUpperCase())
36 uctext = text.toUpperCase(Locale.ENGLISH);
37 else
38 uctext = text;
39 uctext = Normalizer.normalize(uctext, Normalizer.Form.NFC);
40
41 EncodedText et;
42 byte[] buf = uctext.getBytes(StandardCharsets.UTF_8);
43 byte[] res = new byte[buf.length + 1];
44 System.arraycopy(buf, 0, res, 0, buf.length);
45 res[buf.length] = 0;
46 et = new EncodedText(res, res.length, uctext.toCharArray());
47 return et;
48 }
49 }