| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.labelenc; |
| 18 |
|
| 19 |
import java.nio.charset.Charset; |
| 20 |
|
| 21 |
import uk.me.parabola.log.Logger; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class BaseEncoder { |
| 31 |
private static final Logger log = Logger.getLogger(BaseEncoder.class); |
| 32 |
|
| 33 |
public static final EncodedText NO_TEXT = new EncodedText(null, 0, null); |
| 34 |
|
| 35 |
private boolean charsetSupported = true; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private boolean upperCase; |
| 40 |
|
| 41 |
protected boolean isCharsetSupported() { |
| 42 |
return charsetSupported; |
| 43 |
} |
| 44 |
|
| 45 |
protected void prepareForCharacterSet(String name) { |
| 46 |
if (Charset.isSupported(name)) { |
| 47 |
charsetSupported = true; |
| 48 |
} else { |
| 49 |
charsetSupported = false; |
| 50 |
log.warn("requested character set not found " + name); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
protected EncodedText simpleEncode(String text) { |
| 55 |
if (text == null) |
| 56 |
return NO_TEXT; |
| 57 |
|
| 58 |
char[] in = text.toCharArray(); |
| 59 |
byte[] out = new byte[in.length + 1]; |
| 60 |
|
| 61 |
int off = 0; |
| 62 |
for (char c : in) |
| 63 |
out[off++] = (byte) (c & 0xff); |
| 64 |
|
| 65 |
return new EncodedText(out, out.length, in); |
| 66 |
} |
| 67 |
|
| 68 |
public boolean isUpperCase() { |
| 69 |
return upperCase; |
| 70 |
} |
| 71 |
|
| 72 |
public void setUpperCase(boolean upperCase) { |
| 73 |
this.upperCase = upperCase; |
| 74 |
} |
| 75 |
} |