Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
452f38c8 | 3 | import java.io.IOException; |
08f80ac5 NR |
4 | import java.io.InputStream; |
5 | import java.io.OutputStream; | |
6 | ||
7 | import be.nikiroo.utils.IOUtils; | |
8 | import be.nikiroo.utils.streams.NextableInputStream; | |
9 | import be.nikiroo.utils.streams.NextableInputStreamStep; | |
10 | import be.nikiroo.utils.streams.ReplaceInputStream; | |
11 | import be.nikiroo.utils.streams.ReplaceOutputStream; | |
452f38c8 | 12 | |
bd86c221 NR |
13 | /** |
14 | * A {@link CustomSerializer} supports and generates values in the form: | |
15 | * <ul> | |
16 | * <li><tt>custom^<i>TYPE</i>^<i>ENCODED_VALUE</i></tt></li> | |
17 | * </ul> | |
18 | * <p> | |
19 | * In this scheme, the values are: | |
20 | * <ul> | |
21 | * <li><tt>custom</tt>: a fixed keyword</li> | |
22 | * <li><tt>^</tt>: a fixed separator character (the | |
23 | * <tt><i>ENCODED_VALUE</i></tt> can still use it inside its content, though</li> | |
24 | * <li><tt><i>TYPE</i></tt>: the object type of this value</li> | |
25 | * <li><tt><i>ENCODED_VALUE</i></tt>: the custom encoded value</li> | |
26 | * </ul> | |
27 | * <p> | |
28 | * To create a new {@link CustomSerializer}, you are expected to implement the | |
29 | * abstract methods of this class. The rest should be taken care of bythe | |
30 | * system. | |
31 | * | |
32 | * @author niki | |
33 | */ | |
db31c358 | 34 | public abstract class CustomSerializer { |
bd86c221 NR |
35 | /** |
36 | * Generate the custom <tt><i>ENCODED_VALUE</i></tt> from this | |
37 | * <tt>value</tt>. | |
38 | * <p> | |
39 | * The <tt>value</tt> will always be of the supported type. | |
40 | * | |
41 | * @param out | |
42 | * the {@link OutputStream} to write the value to | |
43 | * @param value | |
44 | * the value to serialize | |
45 | * | |
46 | * @throws IOException | |
47 | * in case of I/O error | |
48 | */ | |
08f80ac5 NR |
49 | protected abstract void toStream(OutputStream out, Object value) |
50 | throws IOException; | |
db31c358 | 51 | |
bd86c221 NR |
52 | /** |
53 | * Regenerate the value from the custom <tt><i>ENCODED_VALUE</i></tt>. | |
54 | * <p> | |
55 | * The value in the {@link InputStream} <tt>in</tt> will always be of the | |
56 | * supported type. | |
57 | * | |
58 | * @param in | |
59 | * the {@link InputStream} containing the | |
60 | * <tt><i>ENCODED_VALUE</i></tt> | |
61 | * | |
62 | * @return the regenerated object | |
63 | * | |
64 | * @throws IOException | |
65 | * in case of I/O error | |
66 | */ | |
08f80ac5 | 67 | protected abstract Object fromStream(InputStream in) throws IOException; |
db31c358 | 68 | |
bd86c221 NR |
69 | /** |
70 | * Return the supported type name. | |
71 | * <p> | |
72 | * It <b>must</b> be the name returned by {@link Object#getClass() | |
73 | * #getCanonicalName()}. | |
74 | * | |
75 | * @return the supported class name | |
76 | */ | |
db31c358 NR |
77 | protected abstract String getType(); |
78 | ||
5bc55b51 | 79 | /** |
bd86c221 | 80 | * Encode the object into the given {@link OutputStream}. |
5bc55b51 | 81 | * |
08f80ac5 | 82 | * @param out |
5bc55b51 NR |
83 | * the builder to append to |
84 | * @param value | |
85 | * the object to encode | |
08f80ac5 NR |
86 | * |
87 | * @throws IOException | |
88 | * in case of I/O error | |
5bc55b51 | 89 | */ |
08f80ac5 NR |
90 | public void encode(OutputStream out, Object value) throws IOException { |
91 | ReplaceOutputStream replace = new ReplaceOutputStream(out, // | |
92 | new String[] { "\\", "\n" }, // | |
93 | new String[] { "\\\\", "\\n" }); | |
5bc55b51 | 94 | |
08f80ac5 NR |
95 | try { |
96 | SerialUtils.write(replace, "custom^"); | |
97 | SerialUtils.write(replace, getType()); | |
98 | SerialUtils.write(replace, "^"); | |
99 | toStream(replace, value); | |
100 | } finally { | |
101 | replace.close(false); | |
102 | } | |
db31c358 NR |
103 | } |
104 | ||
bd86c221 NR |
105 | /** |
106 | * Decode the value back into the supported object type. | |
107 | * | |
108 | * @param in | |
109 | * the encoded value | |
110 | * | |
111 | * @return the object | |
112 | * | |
113 | * @throws IOException | |
114 | * in case of I/O error | |
115 | */ | |
08f80ac5 NR |
116 | public Object decode(InputStream in) throws IOException { |
117 | ReplaceInputStream replace = new ReplaceInputStream(in, // | |
118 | new String[] { "\\\\", "\\n" }, // | |
119 | new String[] { "\\", "\n" }); | |
120 | ||
121 | try { | |
122 | NextableInputStream stream = new NextableInputStream( | |
123 | replace.open(), new NextableInputStreamStep('^')); | |
124 | try { | |
125 | if (!stream.next()) { | |
126 | throw new IOException( | |
127 | "Cannot find the first custom^ element"); | |
128 | } | |
129 | ||
130 | String custom = IOUtils.readSmallStream(stream); | |
131 | if (!"custom".equals(custom)) { | |
132 | throw new IOException( | |
133 | "Cannot find the first custom^ element, it is: " | |
134 | + custom + "^"); | |
135 | } | |
136 | ||
137 | if (!stream.next()) { | |
138 | throw new IOException("Cannot find the second custom^" | |
139 | + getType() + " element"); | |
140 | } | |
141 | ||
142 | String type = IOUtils.readSmallStream(stream); | |
143 | if (!getType().equals(type)) { | |
144 | throw new IOException("Cannot find the second custom^" | |
145 | + getType() + " element, it is: custom^" + type | |
146 | + "^"); | |
147 | } | |
148 | ||
149 | if (!stream.nextAll()) { | |
150 | throw new IOException("Cannot find the third custom^" | |
151 | + getType() + "^value element"); | |
152 | } | |
153 | ||
154 | return fromStream(stream); | |
155 | } finally { | |
156 | stream.close(); | |
157 | } | |
158 | } finally { | |
159 | replace.close(false); | |
160 | } | |
db31c358 NR |
161 | } |
162 | ||
163 | public static boolean isCustom(String encodedValue) { | |
f4053377 NR |
164 | int pos1 = encodedValue.indexOf('^'); |
165 | int pos2 = encodedValue.indexOf('^', pos1 + 1); | |
db31c358 | 166 | |
f4053377 | 167 | return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^"); |
db31c358 NR |
168 | } |
169 | ||
170 | public static String typeOf(String encodedValue) { | |
f4053377 NR |
171 | int pos1 = encodedValue.indexOf('^'); |
172 | int pos2 = encodedValue.indexOf('^', pos1 + 1); | |
db31c358 NR |
173 | String type = encodedValue.substring(pos1 + 1, pos2); |
174 | ||
175 | return type; | |
176 | } | |
177 | ||
178 | public static String contentOf(String encodedValue) { | |
f4053377 NR |
179 | int pos1 = encodedValue.indexOf('^'); |
180 | int pos2 = encodedValue.indexOf('^', pos1 + 1); | |
db31c358 NR |
181 | String encodedContent = encodedValue.substring(pos2 + 1); |
182 | ||
183 | return encodedContent; | |
184 | } | |
185 | } |