Bundle: fix memory leak at init/reset
[fanfix.git] / serial / SerialUtils.java
1 package be.nikiroo.utils.serial;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.NotSerializableException;
6 import java.io.OutputStream;
7 import java.lang.reflect.Array;
8 import java.lang.reflect.Constructor;
9 import java.lang.reflect.Field;
10 import java.lang.reflect.Modifier;
11 import java.net.URL;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.UnknownFormatConversionException;
17
18 import be.nikiroo.utils.IOUtils;
19 import be.nikiroo.utils.Image;
20 import be.nikiroo.utils.StringUtils;
21 import be.nikiroo.utils.streams.Base64InputStream;
22 import be.nikiroo.utils.streams.Base64OutputStream;
23 import be.nikiroo.utils.streams.BufferedInputStream;
24 import be.nikiroo.utils.streams.NextableInputStream;
25 import be.nikiroo.utils.streams.NextableInputStreamStep;
26
27 /**
28 * Small class to help with serialisation.
29 * <p>
30 * Note that we do not support inner classes (but we do support nested classes)
31 * and all objects require an empty constructor to be deserialised.
32 * <p>
33 * It is possible to add support to custom types (both the encoder and the
34 * decoder will require the custom classes) -- see {@link CustomSerializer}.
35 * <p>
36 * Default supported types are:
37 * <ul>
38 * <li>NULL (as a null value)</li>
39 * <li>String</li>
40 * <li>Boolean</li>
41 * <li>Byte</li>
42 * <li>Character</li>
43 * <li>Short</li>
44 * <li>Long</li>
45 * <li>Float</li>
46 * <li>Double</li>
47 * <li>Integer</li>
48 * <li>Enum (any enum whose name and value is known by the caller)</li>
49 * <li>java.awt.image.BufferedImage (as a {@link CustomSerializer})</li>
50 * <li>An array of the above (as a {@link CustomSerializer})</li>
51 * <li>URL</li>
52 * </ul>
53 *
54 * @author niki
55 */
56 public class SerialUtils {
57 private static Map<String, CustomSerializer> customTypes;
58
59 static {
60 customTypes = new HashMap<String, CustomSerializer>();
61
62 // Array types:
63 customTypes.put("[]", new CustomSerializer() {
64 @Override
65 protected void toStream(OutputStream out, Object value)
66 throws IOException {
67
68 String type = value.getClass().getCanonicalName();
69 type = type.substring(0, type.length() - 2); // remove the []
70
71 write(out, type);
72 try {
73 for (int i = 0; true; i++) {
74 Object item = Array.get(value, i);
75
76 // encode it normally if direct value
77 write(out, "\r");
78 if (!SerialUtils.encode(out, item)) {
79 try {
80 write(out, "B64:");
81 OutputStream out64 = new Base64OutputStream(
82 out, true);
83 new Exporter(out64).append(item);
84 out64.flush();
85 } catch (NotSerializableException e) {
86 throw new UnknownFormatConversionException(e
87 .getMessage());
88 }
89 }
90 }
91 } catch (ArrayIndexOutOfBoundsException e) {
92 // Done.
93 }
94 }
95
96 @Override
97 protected Object fromStream(InputStream in) throws IOException {
98 NextableInputStream stream = new NextableInputStream(in,
99 new NextableInputStreamStep('\r'));
100
101 try {
102 List<Object> list = new ArrayList<Object>();
103 stream.next();
104 String type = IOUtils.readSmallStream(stream);
105
106 while (stream.next()) {
107 Object value = new Importer().read(stream).getValue();
108 list.add(value);
109 }
110
111 Object array = Array.newInstance(
112 SerialUtils.getClass(type), list.size());
113 for (int i = 0; i < list.size(); i++) {
114 Array.set(array, i, list.get(i));
115 }
116
117 return array;
118 } catch (Exception e) {
119 if (e instanceof IOException) {
120 throw (IOException) e;
121 }
122 throw new IOException(e.getMessage());
123 }
124 }
125
126 @Override
127 protected String getType() {
128 return "[]";
129 }
130 });
131
132 // URL:
133 customTypes.put("java.net.URL", new CustomSerializer() {
134 @Override
135 protected void toStream(OutputStream out, Object value)
136 throws IOException {
137 String val = "";
138 if (value != null) {
139 val = ((URL) value).toString();
140 }
141
142 out.write(StringUtils.getBytes(val));
143 }
144
145 @Override
146 protected Object fromStream(InputStream in) throws IOException {
147 String val = IOUtils.readSmallStream(in);
148 if (!val.isEmpty()) {
149 return new URL(val);
150 }
151
152 return null;
153 }
154
155 @Override
156 protected String getType() {
157 return "java.net.URL";
158 }
159 });
160
161 // Images (this is currently the only supported image type by default)
162 customTypes.put("be.nikiroo.utils.Image", new CustomSerializer() {
163 @Override
164 protected void toStream(OutputStream out, Object value)
165 throws IOException {
166 Image img = (Image) value;
167 OutputStream encoded = new Base64OutputStream(out, true);
168 try {
169 InputStream in = img.newInputStream();
170 try {
171 IOUtils.write(in, encoded);
172 } finally {
173 in.close();
174 }
175 } finally {
176 encoded.flush();
177 // Cannot close!
178 }
179 }
180
181 @Override
182 protected String getType() {
183 return "be.nikiroo.utils.Image";
184 }
185
186 @Override
187 protected Object fromStream(InputStream in) throws IOException {
188 try {
189 // Cannot close it!
190 InputStream decoded = new Base64InputStream(in, false);
191 return new Image(decoded);
192 } catch (IOException e) {
193 throw new UnknownFormatConversionException(e.getMessage());
194 }
195 }
196 });
197 }
198
199 /**
200 * Create an empty object of the given type.
201 *
202 * @param type
203 * the object type (its class name)
204 *
205 * @return the new object
206 *
207 * @throws ClassNotFoundException
208 * if the class cannot be found
209 * @throws NoSuchMethodException
210 * if the given class is not compatible with this code
211 */
212 public static Object createObject(String type)
213 throws ClassNotFoundException, NoSuchMethodException {
214
215 String desc = null;
216 try {
217 Class<?> clazz = getClass(type);
218 String className = clazz.getName();
219 List<Object> args = new ArrayList<Object>();
220 List<Class<?>> classes = new ArrayList<Class<?>>();
221 Constructor<?> ctor = null;
222 if (className.contains("$")) {
223 for (String parentName = className.substring(0,
224 className.lastIndexOf('$'));; parentName = parentName
225 .substring(0, parentName.lastIndexOf('$'))) {
226 Object parent = createObject(parentName);
227 args.add(parent);
228 classes.add(parent.getClass());
229
230 if (!parentName.contains("$")) {
231 break;
232 }
233 }
234
235 // Better error description in case there is no empty
236 // constructor:
237 desc = "";
238 String end = "";
239 for (Class<?> parent = clazz; parent != null
240 && !parent.equals(Object.class); parent = parent
241 .getSuperclass()) {
242 if (!desc.isEmpty()) {
243 desc += " [:";
244 end += "]";
245 }
246 desc += parent;
247 }
248 desc += end;
249 //
250
251 try {
252 ctor = clazz.getDeclaredConstructor(classes
253 .toArray(new Class[] {}));
254 } catch (NoSuchMethodException nsme) {
255 // TODO: it seems we do not always need a parameter for each
256 // level, so we currently try "ALL" levels or "FIRST" level
257 // only -> we should check the actual rule and use it
258 ctor = clazz.getDeclaredConstructor(classes.get(0));
259 Object firstParent = args.get(0);
260 args.clear();
261 args.add(firstParent);
262 }
263 desc = null;
264 } else {
265 ctor = clazz.getDeclaredConstructor();
266 }
267
268 ctor.setAccessible(true);
269 return ctor.newInstance(args.toArray());
270 } catch (ClassNotFoundException e) {
271 throw e;
272 } catch (NoSuchMethodException e) {
273 if (desc != null) {
274 throw new NoSuchMethodException("Empty constructor not found: "
275 + desc);
276 }
277 throw e;
278 } catch (Exception e) {
279 throw new NoSuchMethodException("Cannot instantiate: " + type);
280 }
281 }
282
283 /**
284 * Insert a custom serialiser that will take precedence over the default one
285 * or the target class.
286 *
287 * @param serializer
288 * the custom serialiser
289 */
290 static public void addCustomSerializer(CustomSerializer serializer) {
291 customTypes.put(serializer.getType(), serializer);
292 }
293
294 /**
295 * Serialise the given object into this {@link OutputStream}.
296 * <p>
297 * <b>Important: </b>If the operation fails (with a
298 * {@link NotSerializableException}), the {@link StringBuilder} will be
299 * corrupted (will contain bad, most probably not importable data).
300 *
301 * @param out
302 * the output {@link OutputStream} to serialise to
303 * @param o
304 * the object to serialise
305 * @param map
306 * the map of already serialised objects (if the given object or
307 * one of its descendant is already present in it, only an ID
308 * will be serialised)
309 *
310 * @throws NotSerializableException
311 * if the object cannot be serialised (in this case, the
312 * {@link StringBuilder} can contain bad, most probably not
313 * importable data)
314 * @throws IOException
315 * in case of I/O errors
316 */
317 static void append(OutputStream out, Object o, Map<Integer, Object> map)
318 throws NotSerializableException, IOException {
319
320 Field[] fields = new Field[] {};
321 String type = "";
322 String id = "NULL";
323
324 if (o != null) {
325 int hash = System.identityHashCode(o);
326 fields = o.getClass().getDeclaredFields();
327 type = o.getClass().getCanonicalName();
328 if (type == null) {
329 // Anonymous inner classes support
330 type = o.getClass().getName();
331 }
332 id = Integer.toString(hash);
333 if (map.containsKey(hash)) {
334 fields = new Field[] {};
335 } else {
336 map.put(hash, o);
337 }
338 }
339
340 write(out, "{\nREF ");
341 write(out, type);
342 write(out, "@");
343 write(out, id);
344 write(out, ":");
345
346 if (!encode(out, o)) { // check if direct value
347 try {
348 for (Field field : fields) {
349 field.setAccessible(true);
350
351 if (field.getName().startsWith("this$")
352 || field.isSynthetic()
353 || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
354 // Do not keep this links of nested classes
355 // Do not keep synthetic fields
356 // Do not keep final fields
357 continue;
358 }
359
360 write(out, "\n^");
361 write(out, field.getName());
362 write(out, ":");
363
364 Object value = field.get(o);
365
366 if (!encode(out, value)) {
367 write(out, "\n");
368 append(out, value, map);
369 }
370 }
371 } catch (IllegalArgumentException e) {
372 e.printStackTrace(); // should not happen (see
373 // setAccessible)
374 } catch (IllegalAccessException e) {
375 e.printStackTrace(); // should not happen (see
376 // setAccessible)
377 }
378
379 write(out, "\n}");
380 }
381 }
382
383 /**
384 * Encode the object into the given {@link OutputStream} if possible and if
385 * supported.
386 * <p>
387 * A supported object in this context means an object we can directly
388 * encode, like an Integer or a String. Custom objects and arrays are also
389 * considered supported, but <b>compound objects are not supported here</b>.
390 * <p>
391 * For compound objects, you should use {@link Exporter}.
392 *
393 * @param out
394 * the {@link OutputStream} to append to
395 * @param value
396 * the object to encode (can be NULL, which will be encoded)
397 *
398 * @return TRUE if success, FALSE if not (the content of the
399 * {@link OutputStream} won't be changed in case of failure)
400 *
401 * @throws IOException
402 * in case of I/O error
403 */
404 static boolean encode(OutputStream out, Object value) throws IOException {
405 if (value == null) {
406 write(out, "NULL");
407 } else if (value.getClass().getSimpleName().endsWith("[]")) {
408 // Simple name does support [] suffix and do not return NULL for
409 // inner anonymous classes
410 customTypes.get("[]").encode(out, value);
411 } else if (customTypes.containsKey(value.getClass().getCanonicalName())) {
412 customTypes.get(value.getClass().getCanonicalName())//
413 .encode(out, value);
414 } else if (value instanceof String) {
415 encodeString(out, (String) value);
416 } else if (value instanceof Boolean) {
417 write(out, value);
418 } else if (value instanceof Byte) {
419 write(out, "b");
420 write(out, value);
421 } else if (value instanceof Character) {
422 write(out, "c");
423 encodeString(out, "" + value);
424 } else if (value instanceof Short) {
425 write(out, "s");
426 write(out, value);
427 } else if (value instanceof Integer) {
428 write(out, "i");
429 write(out, value);
430 } else if (value instanceof Long) {
431 write(out, "l");
432 write(out, value);
433 } else if (value instanceof Float) {
434 write(out, "f");
435 write(out, value);
436 } else if (value instanceof Double) {
437 write(out, "d");
438 write(out, value);
439 } else if (value instanceof Enum) {
440 write(out, "E:");
441 String type = value.getClass().getCanonicalName();
442 write(out, type);
443 write(out, ".");
444 write(out, ((Enum<?>) value).name());
445 write(out, ";");
446 } else {
447 return false;
448 }
449
450 return true;
451 }
452
453 static boolean isDirectValue(BufferedInputStream encodedValue)
454 throws IOException {
455 if (CustomSerializer.isCustom(encodedValue)) {
456 return false;
457 }
458
459 for (String fullValue : new String[] { "NULL", "null", "true", "false" }) {
460 if (encodedValue.is(fullValue)) {
461 return true;
462 }
463 }
464
465 for (String prefix : new String[] { "c\"", "\"", "b", "s", "i", "l",
466 "f", "d", "E:" }) {
467 if (encodedValue.startsWith(prefix)) {
468 return true;
469 }
470 }
471
472 return false;
473 }
474
475 /**
476 * Decode the data into an equivalent supported source object.
477 * <p>
478 * A supported object in this context means an object we can directly
479 * encode, like an Integer or a String (see
480 * {@link SerialUtils#decode(String)}.
481 * <p>
482 * Custom objects and arrays are also considered supported here, but
483 * <b>compound objects are not</b>.
484 * <p>
485 * For compound objects, you should use {@link Importer}.
486 *
487 * @param encodedValue
488 * the encoded data, cannot be NULL
489 *
490 * @return the object (can be NULL for NULL encoded values)
491 *
492 * @throws IOException
493 * if the content cannot be converted
494 */
495 static Object decode(BufferedInputStream encodedValue) throws IOException {
496 if (CustomSerializer.isCustom(encodedValue)) {
497 // custom^TYPE^ENCODED_VALUE
498 NextableInputStream content = new NextableInputStream(encodedValue,
499 new NextableInputStreamStep('^'));
500 try {
501 content.next();
502 @SuppressWarnings("unused")
503 String custom = IOUtils.readSmallStream(content);
504 content.next();
505 String type = IOUtils.readSmallStream(content);
506 content.nextAll();
507 if (customTypes.containsKey(type)) {
508 return customTypes.get(type).decode(content);
509 }
510 content.end();
511 throw new IOException("Unknown custom type: " + type);
512 } finally {
513 content.close(false);
514 encodedValue.end();
515 }
516 }
517
518 String encodedString = IOUtils.readSmallStream(encodedValue);
519 return decode(encodedString);
520 }
521
522 /**
523 * Decode the data into an equivalent supported source object.
524 * <p>
525 * A supported object in this context means an object we can directly
526 * encode, like an Integer or a String.
527 * <p>
528 * For custom objects and arrays, you should use
529 * {@link SerialUtils#decode(InputStream)} or directly {@link Importer}.
530 * <p>
531 * For compound objects, you should use {@link Importer}.
532 *
533 * @param encodedValue
534 * the encoded data, cannot be NULL
535 *
536 * @return the object (can be NULL for NULL encoded values)
537 *
538 * @throws IOException
539 * if the content cannot be converted
540 */
541 static Object decode(String encodedValue) throws IOException {
542 try {
543 String cut = "";
544 if (encodedValue.length() > 1) {
545 cut = encodedValue.substring(1);
546 }
547
548 if (encodedValue.equals("NULL") || encodedValue.equals("null")) {
549 return null;
550 } else if (encodedValue.startsWith("\"")) {
551 return decodeString(encodedValue);
552 } else if (encodedValue.equals("true")) {
553 return true;
554 } else if (encodedValue.equals("false")) {
555 return false;
556 } else if (encodedValue.startsWith("b")) {
557 return Byte.parseByte(cut);
558 } else if (encodedValue.startsWith("c")) {
559 return decodeString(cut).charAt(0);
560 } else if (encodedValue.startsWith("s")) {
561 return Short.parseShort(cut);
562 } else if (encodedValue.startsWith("l")) {
563 return Long.parseLong(cut);
564 } else if (encodedValue.startsWith("f")) {
565 return Float.parseFloat(cut);
566 } else if (encodedValue.startsWith("d")) {
567 return Double.parseDouble(cut);
568 } else if (encodedValue.startsWith("i")) {
569 return Integer.parseInt(cut);
570 } else if (encodedValue.startsWith("E:")) {
571 cut = cut.substring(1);
572 return decodeEnum(cut);
573 } else {
574 throw new IOException("Unrecognized value: " + encodedValue);
575 }
576 } catch (Exception e) {
577 if (e instanceof IOException) {
578 throw (IOException) e;
579 }
580 throw new IOException(e.getMessage(), e);
581 }
582 }
583
584 /**
585 * Write the given {@link String} into the given {@link OutputStream} in
586 * UTF-8.
587 *
588 * @param out
589 * the {@link OutputStream}
590 * @param data
591 * the data to write, cannot be NULL
592 *
593 * @throws IOException
594 * in case of I/O error
595 */
596 static void write(OutputStream out, Object data) throws IOException {
597 out.write(StringUtils.getBytes(data.toString()));
598 }
599
600 /**
601 * Return the corresponding class or throw an {@link Exception} if it
602 * cannot.
603 *
604 * @param type
605 * the class name to look for
606 *
607 * @return the class (will never be NULL)
608 *
609 * @throws ClassNotFoundException
610 * if the class cannot be found
611 * @throws NoSuchMethodException
612 * if the class cannot be created (usually because it or its
613 * enclosing class doesn't have an empty constructor)
614 */
615 static private Class<?> getClass(String type)
616 throws ClassNotFoundException, NoSuchMethodException {
617 Class<?> clazz = null;
618 try {
619 clazz = Class.forName(type);
620 } catch (ClassNotFoundException e) {
621 int pos = type.length();
622 pos = type.lastIndexOf(".", pos);
623 if (pos >= 0) {
624 String parentType = type.substring(0, pos);
625 String nestedType = type.substring(pos + 1);
626 Class<?> javaParent = null;
627 try {
628 javaParent = getClass(parentType);
629 parentType = javaParent.getName();
630 clazz = Class.forName(parentType + "$" + nestedType);
631 } catch (Exception ee) {
632 }
633
634 if (javaParent == null) {
635 throw new NoSuchMethodException(
636 "Class not found: "
637 + type
638 + " (the enclosing class cannot be created: maybe it doesn't have an empty constructor?)");
639 }
640 }
641 }
642
643 if (clazz == null) {
644 throw new ClassNotFoundException("Class not found: " + type);
645 }
646
647 return clazz;
648 }
649
650 @SuppressWarnings({ "unchecked", "rawtypes" })
651 static private Enum<?> decodeEnum(String escaped) {
652 // escaped: be.xxx.EnumType.VALUE;
653 int pos = escaped.lastIndexOf(".");
654 String type = escaped.substring(0, pos);
655 String name = escaped.substring(pos + 1, escaped.length() - 1);
656
657 try {
658 return Enum.valueOf((Class<Enum>) getClass(type), name);
659 } catch (Exception e) {
660 throw new UnknownFormatConversionException("Unknown enum: <" + type
661 + "> " + name);
662 }
663 }
664
665 // aa bb -> "aa\tbb"
666 static void encodeString(OutputStream out, String raw) throws IOException {
667 // TODO: not. efficient.
668 out.write('\"');
669 for (char car : raw.toCharArray()) {
670 encodeString(out, car);
671 }
672 out.write('\"');
673 }
674
675 // for encoding string, NOT to encode a char by itself!
676 static void encodeString(OutputStream out, char raw) throws IOException {
677 switch (raw) {
678 case '\\':
679 out.write('\\');
680 out.write('\\');
681 break;
682 case '\r':
683 out.write('\\');
684 out.write('r');
685 break;
686 case '\n':
687 out.write('\\');
688 out.write('n');
689 break;
690 case '"':
691 out.write('\\');
692 out.write('\"');
693 break;
694 default:
695 out.write(raw);
696 break;
697 }
698 }
699
700 // "aa\tbb" -> aa bb
701 static String decodeString(String escaped) {
702 StringBuilder builder = new StringBuilder();
703
704 boolean escaping = false;
705 for (char car : escaped.toCharArray()) {
706 if (!escaping) {
707 if (car == '\\') {
708 escaping = true;
709 } else {
710 builder.append(car);
711 }
712 } else {
713 switch (car) {
714 case '\\':
715 builder.append('\\');
716 break;
717 case 'r':
718 builder.append('\r');
719 break;
720 case 'n':
721 builder.append('\n');
722 break;
723 case '"':
724 builder.append('"');
725 break;
726 }
727 escaping = false;
728 }
729 }
730
731 return builder.substring(1, builder.length() - 1);
732 }
733 }