en cours, 3
[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.NextableInputStream;
9 import be.nikiroo.utils.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 // TODO read and skip "custom^......^": next(), next(), nextAll() ?
48 NextableInputStream stream = new NextableInputStream(in,
49 new NextableInputStreamStep('^'));
50
51 try {
52 if (!stream.next()) {
53 throw new IOException("Cannot find the first custom^ element");
54 }
55
56 String custom = IOUtils.readSmallStream(stream);
57 if (!"custom".equals(custom)) {
58 throw new IOException(
59 "Cannot find the first custom^ element, it is: "
60 + custom + "^");
61 }
62
63 if (!stream.next()) {
64 throw new IOException("Cannot find the second custom^"
65 + getType() + " element");
66 }
67
68 String type = IOUtils.readSmallStream(stream);
69 if (!getType().equals(type)) {
70 throw new IOException("Cannot find the second custom^"
71 + getType() + " element, it is: custom^" + type + "^");
72 }
73
74 if (!stream.nextAll()) {
75 throw new IOException("Cannot find the third custom^"
76 + getType() + "^value element");
77 }
78
79 // TODO: manage ENTER
80 return fromStream(stream);
81 } finally {
82 stream.close(false);
83 }
84 }
85
86 public static boolean isCustom(String encodedValue) {
87 int pos1 = encodedValue.indexOf('^');
88 int pos2 = encodedValue.indexOf('^', pos1 + 1);
89
90 return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^");
91 }
92
93 public static String typeOf(String encodedValue) {
94 int pos1 = encodedValue.indexOf('^');
95 int pos2 = encodedValue.indexOf('^', pos1 + 1);
96 String type = encodedValue.substring(pos1 + 1, pos2);
97
98 return type;
99 }
100
101 public static String contentOf(String encodedValue) {
102 int pos1 = encodedValue.indexOf('^');
103 int pos2 = encodedValue.indexOf('^', pos1 + 1);
104 String encodedContent = encodedValue.substring(pos2 + 1);
105
106 return encodedContent;
107 }
108 }