| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.imgfmt.app.labelenc; |
| 14 |
|
| 15 |
import java.io.ByteArrayOutputStream; |
| 16 |
import java.nio.charset.Charset; |
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
} |