80bac595a8a13068b1b45b10ece585c1566d5bb9
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 supported.
20 * the builder to append to
22 * the object to encode
24 * @return FALSE if the value is not supported, TRUE if the operation was
25 * successful (if the value is supported by the operation was not
26 * successful, you will get an {@link IOException})
29 * in case of I/O error
31 public boolean encode(OutputStream out
, Object value
) throws IOException
{
32 if (!isSupported(value
)) {
36 SerialUtils
.write(out
, "custom^");
37 SerialUtils
.write(out
, getType());
38 SerialUtils
.write(out
, "^");
44 public Object
decode(String encodedValue
) throws IOException
{
45 return fromString((String
) SerialUtils
.decode(contentOf(encodedValue
)));
48 public static boolean isCustom(String encodedValue
) {
49 int pos1
= encodedValue
.indexOf('^');
50 int pos2
= encodedValue
.indexOf('^', pos1
+ 1);
52 return pos1
>= 0 && pos2
>= 0 && encodedValue
.startsWith("custom^");
55 public static String
typeOf(String encodedValue
) {
56 int pos1
= encodedValue
.indexOf('^');
57 int pos2
= encodedValue
.indexOf('^', pos1
+ 1);
58 String type
= encodedValue
.substring(pos1
+ 1, pos2
);
63 public static String
contentOf(String encodedValue
) {
64 int pos1
= encodedValue
.indexOf('^');
65 int pos2
= encodedValue
.indexOf('^', pos1
+ 1);
66 String encodedContent
= encodedValue
.substring(pos2
+ 1);
68 return encodedContent
;