1 package org.djutils.decoderdumper;
2
3 import java.io.IOException;
4
5
6
7
8
9
10
11
12
13
14 public class CharDecoder implements Decoder
15 {
16
17 private final String prototypeLine;
18
19
20 private final int fieldsPerLine;
21
22
23 private final int extraSpaceAfterEvery;
24
25
26
27
28
29
30 public CharDecoder(final int fieldsPerLine, final int extraSpaceAfterEvery)
31 {
32 this.fieldsPerLine = fieldsPerLine;
33 this.extraSpaceAfterEvery = extraSpaceAfterEvery > 0 ? extraSpaceAfterEvery : Integer.MAX_VALUE;
34 String format = String.format("%%%ds", fieldsPerLine + (fieldsPerLine - 1) / this.extraSpaceAfterEvery);
35 this.prototypeLine = String.format(format, "");
36 }
37
38
39 private StringBuilder buffer = new StringBuilder();
40
41
42 @Override
43 public String getResult()
44 {
45 String result = this.buffer.toString();
46 this.buffer.setLength(0);
47 return result;
48 }
49
50
51 @Override
52 public int getMaximumWidth()
53 {
54 return this.prototypeLine.length();
55 }
56
57
58 @Override
59 public boolean append(final int address, final byte theByte) throws IOException
60 {
61 if (this.buffer.length() == 0)
62 {
63 this.buffer.append(this.prototypeLine);
64 }
65 int lineByte = address % this.fieldsPerLine;
66 int index = lineByte + lineByte / this.extraSpaceAfterEvery;
67 this.buffer.replace(index, index + 1, theByte >= 32 && theByte < 127 ? "" + (char) theByte : ".");
68 return lineByte == this.fieldsPerLine - 1;
69 }
70
71
72 @Override
73 public boolean ignoreForIdenticalOutputCheck()
74 {
75 return false;
76 }
77
78
79 @Override
80 public String toString()
81 {
82 return "CharDecoder [buffer=" + this.buffer + "]";
83 }
84
85 }