Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
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 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.labelenc;
14
15 import java.io.ByteArrayOutputStream;
16 import java.nio.charset.Charset;
17
18 /**
19 * Decodes strings from format 9 and a given character set to java strings.
20 */
21 public class AnyCharsetDecoder implements CharacterDecoder {
22 private final ByteArrayOutputStream out = new ByteArrayOutputStream();
23 private boolean needReset;
24 private final Charset charSet;
25
26 public AnyCharsetDecoder(String charsetName) {
27 charSet = Charset.forName(charsetName);
28 }
29
30 public boolean addByte(int b) {
31 if (b == 0) {
32 needReset = true;
33 return true;
34 }
35
36 if (needReset) {
37 needReset = false;
38 out.reset();
39 }
40
41 out.write(b);
42 return false;
43 }
44
45 public DecodedText getText() {
46 byte[] ba = out.toByteArray();
47 return new DecodedText(ba, charSet);
48 }
49
50 public void reset() {
51 needReset = false;
52 out.reset();
53 }
54 }