Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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.io.ByteArrayOutputStream;
20 import java.nio.charset.StandardCharsets;
21
22 /**
23 * Decoder for labels in utf-8, note that I am not actually sure that any
24 * map uses utf-8.
25 *
26 * @author Steve Ratcliffe
27 */
28 public class Utf8Decoder implements CharacterDecoder {
29 private final ByteArrayOutputStream out = new ByteArrayOutputStream();
30 private boolean needreset;
31
32 /**
33 * Add a byte to this decoder. This will be saved until a complete
34 * label string has been detected.
35 *
36 * @param b The byte read from the lbl file.
37 * @return True if a label string is finished and is ready to be retrieved
38 * via the {@link #getText} method.
39 */
40 public boolean addByte(int b) {
41 if (b == 0) {
42 needreset = true;
43 return true;
44 }
45
46 if (needreset) {
47 out.reset();
48 needreset = false;
49 }
50
51 out.write(b);
52 return false;
53 }
54
55 /**
56 * Get the valid text. This is guaranteed to be encoded as utf-8.
57 *
58 * @return The byte array and length as an EncodedText struct.
59 */
60 public DecodedText getText() {
61 byte[] ba = out.toByteArray();
62 return new DecodedText(ba, StandardCharsets.UTF_8);
63 }
64
65 /**
66 * Resets the state. This should be called for example if the reader is
67 * jumping to a new place in the file and cannot guarantee that the previous
68 * label was fully read.
69 */
70 public void reset() {
71 needreset = false;
72 out.reset();
73 }
74 }