fix namespace from master merge
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / CustomSerializer.java
1 package be.nikiroo.utils.serial;
2
3 import java.io.IOException;
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
11 public abstract class CustomSerializer {
12
13 protected abstract void toStream(OutputStream out, Object value)
14 throws IOException;
15
16 protected abstract Object fromStream(InputStream in) throws IOException;
17
18 protected abstract String getType();
19
20 /**
21 * Encode the object into the given {@link OutputStream} if supported.
22 *
23 * @param out
24 * the builder to append to
25 * @param value
26 * the object to encode
27 *
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})
31 *
32 * @throws IOException
33 * in case of I/O error
34 */
35 public boolean encode(OutputStream out, Object value) throws IOException {
36 SerialUtils.write(out, "custom^");
37 SerialUtils.write(out, getType());
38 SerialUtils.write(out, "^");
39 // TODO: manage ENTER
40 toStream(out, value);
41
42 return true;
43 }
44
45 public Object decode(InputStream in) throws IOException {
46 // TODO: manage ENTER
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 }
83 }
84
85 public static boolean isCustom(String encodedValue) {
86 int pos1 = encodedValue.indexOf('^');
87 int pos2 = encodedValue.indexOf('^', pos1 + 1);
88
89 return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^");
90 }
91
92 public static String typeOf(String encodedValue) {
93 int pos1 = encodedValue.indexOf('^');
94 int pos2 = encodedValue.indexOf('^', pos1 + 1);
95 String type = encodedValue.substring(pos1 + 1, pos2);
96
97 return type;
98 }
99
100 public static String contentOf(String encodedValue) {
101 int pos1 = encodedValue.indexOf('^');
102 int pos2 = encodedValue.indexOf('^', pos1 + 1);
103 String encodedContent = encodedValue.substring(pos2 + 1);
104
105 return encodedContent;
106 }
107 }