| 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.streams.BufferedInputStream; |
| 8 | import be.nikiroo.utils.streams.ReplaceInputStream; |
| 9 | import be.nikiroo.utils.streams.ReplaceOutputStream; |
| 10 | |
| 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 | */ |
| 32 | public abstract class CustomSerializer { |
| 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 | */ |
| 47 | protected abstract void toStream(OutputStream out, Object value) |
| 48 | throws IOException; |
| 49 | |
| 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 | */ |
| 65 | protected abstract Object fromStream(InputStream in) throws IOException; |
| 66 | |
| 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 | */ |
| 75 | protected abstract String getType(); |
| 76 | |
| 77 | /** |
| 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. |
| 84 | * |
| 85 | * @param out |
| 86 | * the builder to append to |
| 87 | * @param value |
| 88 | * the object to encode |
| 89 | * |
| 90 | * @throws IOException |
| 91 | * in case of I/O error |
| 92 | */ |
| 93 | public void encode(OutputStream out, Object value) throws IOException { |
| 94 | ReplaceOutputStream replace = new ReplaceOutputStream(out, // |
| 95 | new String[] { "\\", "\n" }, // |
| 96 | new String[] { "\\\\", "\\n" }); |
| 97 | |
| 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 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Decode the value back into the supported object type. |
| 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. |
| 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 | */ |
| 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 { |
| 133 | return fromStream(replace); |
| 134 | } finally { |
| 135 | replace.close(false); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public static boolean isCustom(BufferedInputStream in) throws IOException { |
| 140 | return in.startsWith("custom^"); |
| 141 | } |
| 142 | |
| 143 | public static String typeOf(String encodedValue) { |
| 144 | int pos1 = encodedValue.indexOf('^'); |
| 145 | int pos2 = encodedValue.indexOf('^', pos1 + 1); |
| 146 | String type = encodedValue.substring(pos1 + 1, pos2); |
| 147 | |
| 148 | return type; |
| 149 | } |
| 150 | } |