1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
7 public abstract class CustomSerializer
{
9 protected abstract void toStream(OutputStream out
, Object value
)
12 protected abstract Object
fromStream(InputStream in
) throws IOException
;
14 protected abstract String
getType();
17 * Encode the object into the given {@link OutputStream} if possible (if
21 * the builder to append to
23 * the object to encode
25 * @return TRUE if success, FALSE if not (the content of the builder won't
26 * be changed in case of failure)
29 * in case of I/O error
31 public boolean encode(OutputStream out
, Object value
) throws IOException
{
32 InputStream customString
= toStream(out
, value
);
33 SerialUtils
.write(out
, "custom^");
34 SerialUtils
.write(out
, getType());
35 SerialUtils
.write(out
, "^");
36 if (!SerialUtils
.encode(out
, customString
)) {
43 public Object
decode(String encodedValue
) throws IOException
{
44 return fromString((String
) SerialUtils
.decode(contentOf(encodedValue
)));
47 public static boolean isCustom(String encodedValue
) {
48 int pos1
= encodedValue
.indexOf('^');
49 int pos2
= encodedValue
.indexOf('^', pos1
+ 1);
51 return pos1
>= 0 && pos2
>= 0 && encodedValue
.startsWith("custom^");
54 public static String
typeOf(String encodedValue
) {
55 int pos1
= encodedValue
.indexOf('^');
56 int pos2
= encodedValue
.indexOf('^', pos1
+ 1);
57 String type
= encodedValue
.substring(pos1
+ 1, pos2
);
62 public static String
contentOf(String encodedValue
) {
63 int pos1
= encodedValue
.indexOf('^');
64 int pos2
= encodedValue
.indexOf('^', pos1
+ 1);
65 String encodedContent
= encodedValue
.substring(pos2
+ 1);
67 return encodedContent
;