en cours, 3
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / CustomSerializer.java
CommitLineData
db31c358
NR
1package be.nikiroo.utils.serial;
2
452f38c8 3import java.io.IOException;
68532958
NR
4import java.io.InputStream;
5import java.io.OutputStream;
452f38c8 6
dc41a952
NR
7import be.nikiroo.utils.IOUtils;
8import be.nikiroo.utils.NextableInputStream;
9import be.nikiroo.utils.NextableInputStreamStep;
10
db31c358
NR
11public 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
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 }
db31c358
NR
84 }
85
86 public static boolean isCustom(String encodedValue) {
f4053377
NR
87 int pos1 = encodedValue.indexOf('^');
88 int pos2 = encodedValue.indexOf('^', pos1 + 1);
db31c358 89
f4053377 90 return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^");
db31c358
NR
91 }
92
93 public static String typeOf(String encodedValue) {
f4053377
NR
94 int pos1 = encodedValue.indexOf('^');
95 int pos2 = encodedValue.indexOf('^', pos1 + 1);
db31c358
NR
96 String type = encodedValue.substring(pos1 + 1, pos2);
97
98 return type;
99 }
100
101 public static String contentOf(String encodedValue) {
f4053377
NR
102 int pos1 = encodedValue.indexOf('^');
103 int pos2 = encodedValue.indexOf('^', pos1 + 1);
db31c358
NR
104 String encodedContent = encodedValue.substring(pos2 + 1);
105
106 return encodedContent;
107 }
108}