Merge commit 'b063fe0a74fdf17f440a850bbb98a2de6233a679'
[nikiroo-utils.git] / serial / CustomSerializer.java
CommitLineData
db31c358
NR
1package be.nikiroo.utils.serial;
2
452f38c8 3import java.io.IOException;
08f80ac5
NR
4import java.io.InputStream;
5import java.io.OutputStream;
6
dce410fe 7import be.nikiroo.utils.streams.BufferedInputStream;
08f80ac5
NR
8import be.nikiroo.utils.streams.ReplaceInputStream;
9import be.nikiroo.utils.streams.ReplaceOutputStream;
452f38c8 10
bd86c221
NR
11/**
12 * A {@link CustomSerializer} supports and generates values in the form:
13 * <ul>
14 * <li><tt>custom^<i>TYPE</i>^<i>ENCODED_VALUE</i></tt></li>
15 * </ul>
16 * <p>
17 * In this scheme, the values are:
18 * <ul>
19 * <li><tt>custom</tt>: a fixed keyword</li>
20 * <li><tt>^</tt>: a fixed separator character (the
21 * <tt><i>ENCODED_VALUE</i></tt> can still use it inside its content, though</li>
22 * <li><tt><i>TYPE</i></tt>: the object type of this value</li>
23 * <li><tt><i>ENCODED_VALUE</i></tt>: the custom encoded value</li>
24 * </ul>
25 * <p>
26 * To create a new {@link CustomSerializer}, you are expected to implement the
27 * abstract methods of this class. The rest should be taken care of bythe
28 * system.
29 *
30 * @author niki
31 */
db31c358 32public abstract class CustomSerializer {
bd86c221
NR
33 /**
34 * Generate the custom <tt><i>ENCODED_VALUE</i></tt> from this
35 * <tt>value</tt>.
36 * <p>
37 * The <tt>value</tt> will always be of the supported type.
38 *
39 * @param out
40 * the {@link OutputStream} to write the value to
41 * @param value
42 * the value to serialize
43 *
44 * @throws IOException
45 * in case of I/O error
46 */
08f80ac5
NR
47 protected abstract void toStream(OutputStream out, Object value)
48 throws IOException;
db31c358 49
bd86c221
NR
50 /**
51 * Regenerate the value from the custom <tt><i>ENCODED_VALUE</i></tt>.
52 * <p>
53 * The value in the {@link InputStream} <tt>in</tt> will always be of the
54 * supported type.
55 *
56 * @param in
57 * the {@link InputStream} containing the
58 * <tt><i>ENCODED_VALUE</i></tt>
59 *
60 * @return the regenerated object
61 *
62 * @throws IOException
63 * in case of I/O error
64 */
08f80ac5 65 protected abstract Object fromStream(InputStream in) throws IOException;
db31c358 66
bd86c221
NR
67 /**
68 * Return the supported type name.
69 * <p>
70 * It <b>must</b> be the name returned by {@link Object#getClass()
71 * #getCanonicalName()}.
72 *
73 * @return the supported class name
74 */
db31c358
NR
75 protected abstract String getType();
76
5bc55b51 77 /**
d2219aa0
NR
78 * Encode the object into the given {@link OutputStream}, i.e., generate the
79 * <tt><i>ENCODED_VALUE</i></tt> part.
80 * <p>
81 * Use whatever scheme you wish, the system shall ensure that the content is
82 * correctly encoded and that you will receive the same content at decode
83 * time.
5bc55b51 84 *
08f80ac5 85 * @param out
5bc55b51
NR
86 * the builder to append to
87 * @param value
88 * the object to encode
08f80ac5
NR
89 *
90 * @throws IOException
91 * in case of I/O error
5bc55b51 92 */
08f80ac5
NR
93 public void encode(OutputStream out, Object value) throws IOException {
94 ReplaceOutputStream replace = new ReplaceOutputStream(out, //
95 new String[] { "\\", "\n" }, //
96 new String[] { "\\\\", "\\n" });
5bc55b51 97
08f80ac5
NR
98 try {
99 SerialUtils.write(replace, "custom^");
100 SerialUtils.write(replace, getType());
101 SerialUtils.write(replace, "^");
102 toStream(replace, value);
103 } finally {
104 replace.close(false);
105 }
db31c358
NR
106 }
107
bd86c221
NR
108 /**
109 * Decode the value back into the supported object type.
d2219aa0
NR
110 * <p>
111 * We do <b>not</b> expect the full content here but only:
112 * <ul>
113 * <li>ENCODED_VALUE
114 * <li>
115 * </ul>
116 * That is, we do not expect the "<tt>custom</tt>^<tt><i>TYPE</i></tt>^"
117 * part.
bd86c221
NR
118 *
119 * @param in
120 * the encoded value
121 *
122 * @return the object
123 *
124 * @throws IOException
125 * in case of I/O error
126 */
08f80ac5
NR
127 public Object decode(InputStream in) throws IOException {
128 ReplaceInputStream replace = new ReplaceInputStream(in, //
129 new String[] { "\\\\", "\\n" }, //
130 new String[] { "\\", "\n" });
131
132 try {
d2219aa0 133 return fromStream(replace);
08f80ac5
NR
134 } finally {
135 replace.close(false);
136 }
db31c358
NR
137 }
138
dce410fe 139 public static boolean isCustom(BufferedInputStream in) throws IOException {
d251f3dd 140 return in.startsWith("custom^");
dce410fe
NR
141 }
142
db31c358 143 public static String typeOf(String encodedValue) {
f4053377
NR
144 int pos1 = encodedValue.indexOf('^');
145 int pos2 = encodedValue.indexOf('^', pos1 + 1);
db31c358
NR
146 String type = encodedValue.substring(pos1 + 1, pos2);
147
148 return type;
149 }
db31c358 150}