c5876ce401b622788f53ca073ced5e89d7ca56c2
[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 import be.nikiroo.utils.streams.ReplaceInputStream;
11 import be.nikiroo.utils.streams.ReplaceOutputStream;
12
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 */
34 public abstract class CustomSerializer {
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 */
49 protected abstract void toStream(OutputStream out, Object value)
50 throws IOException;
51
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 */
67 protected abstract Object fromStream(InputStream in) throws IOException;
68
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 */
77 protected abstract String getType();
78
79 /**
80 * Encode the object into the given {@link OutputStream}.
81 *
82 * @param out
83 * the builder to append to
84 * @param value
85 * the object to encode
86 *
87 * @throws IOException
88 * in case of I/O error
89 */
90 public void encode(OutputStream out, Object value) throws IOException {
91 ReplaceOutputStream replace = new ReplaceOutputStream(out, //
92 new String[] { "\\", "\n" }, //
93 new String[] { "\\\\", "\\n" });
94
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 }
103 }
104
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 */
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 }
161 }
162
163 public static boolean isCustom(String encodedValue) {
164 int pos1 = encodedValue.indexOf('^');
165 int pos2 = encodedValue.indexOf('^', pos1 + 1);
166
167 return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^");
168 }
169
170 public static String typeOf(String encodedValue) {
171 int pos1 = encodedValue.indexOf('^');
172 int pos2 = encodedValue.indexOf('^', pos1 + 1);
173 String type = encodedValue.substring(pos1 + 1, pos2);
174
175 return type;
176 }
177
178 public static String contentOf(String encodedValue) {
179 int pos1 = encodedValue.indexOf('^');
180 int pos2 = encodedValue.indexOf('^', pos1 + 1);
181 String encodedContent = encodedValue.substring(pos2 + 1);
182
183 return encodedContent;
184 }
185 }