Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
452f38c8 | 3 | import java.io.IOException; |
68532958 NR |
4 | import java.io.InputStream; |
5 | import java.io.OutputStream; | |
452f38c8 | 6 | |
dc41a952 | 7 | import be.nikiroo.utils.IOUtils; |
52fb9a56 NR |
8 | import be.nikiroo.utils.streams.NextableInputStream; |
9 | import be.nikiroo.utils.streams.NextableInputStreamStep; | |
dc41a952 | 10 | |
db31c358 NR |
11 | public abstract class CustomSerializer { |
12 | ||
68532958 NR |
13 | protected abstract void toStream(OutputStream out, Object value) |
14 | throws IOException; | |
db31c358 | 15 | |
68532958 | 16 | protected abstract Object fromStream(InputStream in) throws IOException; |
db31c358 NR |
17 | |
18 | protected abstract String getType(); | |
19 | ||
5bc55b51 | 20 | /** |
143a64f4 | 21 | * Encode the object into the given {@link OutputStream} if supported. |
5bc55b51 | 22 | * |
68532958 | 23 | * @param out |
5bc55b51 NR |
24 | * the builder to append to |
25 | * @param value | |
26 | * the object to encode | |
68532958 | 27 | * |
143a64f4 NR |
28 | * @return FALSE if the value is not supported, TRUE if the operation was |
29 | * successful (if the value is supported by the operation was not | |
30 | * successful, you will get an {@link IOException}) | |
68532958 NR |
31 | * |
32 | * @throws IOException | |
33 | * in case of I/O error | |
5bc55b51 | 34 | */ |
68532958 | 35 | public boolean encode(OutputStream out, Object value) throws IOException { |
68532958 NR |
36 | SerialUtils.write(out, "custom^"); |
37 | SerialUtils.write(out, getType()); | |
38 | SerialUtils.write(out, "^"); | |
dc41a952 | 39 | // TODO: manage ENTER |
143a64f4 | 40 | toStream(out, value); |
5bc55b51 NR |
41 | |
42 | return true; | |
db31c358 NR |
43 | } |
44 | ||
dc41a952 NR |
45 | public Object decode(InputStream in) throws IOException { |
46 | // TODO: manage ENTER | |
dc41a952 NR |
47 | NextableInputStream stream = new NextableInputStream(in, |
48 | new NextableInputStreamStep('^')); | |
49 | ||
50 | try { | |
51 | if (!stream.next()) { | |
52 | throw new IOException("Cannot find the first custom^ element"); | |
53 | } | |
54 | ||
55 | String custom = IOUtils.readSmallStream(stream); | |
56 | if (!"custom".equals(custom)) { | |
57 | throw new IOException( | |
58 | "Cannot find the first custom^ element, it is: " | |
59 | + custom + "^"); | |
60 | } | |
61 | ||
62 | if (!stream.next()) { | |
63 | throw new IOException("Cannot find the second custom^" | |
64 | + getType() + " element"); | |
65 | } | |
66 | ||
67 | String type = IOUtils.readSmallStream(stream); | |
68 | if (!getType().equals(type)) { | |
69 | throw new IOException("Cannot find the second custom^" | |
70 | + getType() + " element, it is: custom^" + type + "^"); | |
71 | } | |
72 | ||
73 | if (!stream.nextAll()) { | |
74 | throw new IOException("Cannot find the third custom^" | |
75 | + getType() + "^value element"); | |
76 | } | |
77 | ||
78 | // TODO: manage ENTER | |
79 | return fromStream(stream); | |
80 | } finally { | |
81 | stream.close(false); | |
82 | } | |
db31c358 NR |
83 | } |
84 | ||
85 | public static boolean isCustom(String encodedValue) { | |
f4053377 NR |
86 | int pos1 = encodedValue.indexOf('^'); |
87 | int pos2 = encodedValue.indexOf('^', pos1 + 1); | |
db31c358 | 88 | |
f4053377 | 89 | return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^"); |
db31c358 NR |
90 | } |
91 | ||
92 | public static String typeOf(String encodedValue) { | |
f4053377 NR |
93 | int pos1 = encodedValue.indexOf('^'); |
94 | int pos2 = encodedValue.indexOf('^', pos1 + 1); | |
db31c358 NR |
95 | String type = encodedValue.substring(pos1 + 1, pos2); |
96 | ||
97 | return type; | |
98 | } | |
99 | ||
100 | public static String contentOf(String encodedValue) { | |
f4053377 NR |
101 | int pos1 = encodedValue.indexOf('^'); |
102 | int pos2 = encodedValue.indexOf('^', pos1 + 1); | |
db31c358 NR |
103 | String encodedContent = encodedValue.substring(pos2 + 1); |
104 | ||
105 | return encodedContent; | |
106 | } | |
107 | } |