|
| 1 | +package org.ugp.serialx; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.io.OutputStream; |
| 11 | +import java.io.OutputStreamWriter; |
| 12 | +import java.io.Reader; |
| 13 | +import java.io.StringReader; |
| 14 | +import java.io.Writer; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +/** |
| 19 | + * Interface with common methods for serializing and deserializing/loading contents from various types of media ({@link File}, {@link InputStream}, {@link Appendable}, {@link Reader} etc...).<br> |
| 20 | + * Intended/recommended use is to implement this together with some sort of {@link Collection} or {@link Map} and/or something that can be treated as such. |
| 21 | + * |
| 22 | + * @param <T> Type of the object that is serialized and deserialized, you likely want this to be the class (or patent class) of an implementor of this interface. |
| 23 | + * |
| 24 | + * @author PETO |
| 25 | + * |
| 26 | + * @since 4.0.0 (Separated from {@link Serializer} in 4.0.0) |
| 27 | + */ |
| 28 | +public interface MultimediaSerializer<T> |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @param f | File to write in. This must be a text file. |
| 32 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 33 | + * |
| 34 | + * @throws IOException if file can't be opened or serialization fails! |
| 35 | + * |
| 36 | + * @since 1.1.5 |
| 37 | + */ |
| 38 | + default void serializeTo(File f, Object... args) throws IOException |
| 39 | + { |
| 40 | + serializeTo(false, f, args); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param append | When true, the new objects will be appended to files content (same objects will be also appended if there are some)! |
| 45 | + * @param f | File to write in. This must be a text file. |
| 46 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 47 | + * |
| 48 | + * @throws IOException if file can't be opened or serialization fails! |
| 49 | + * |
| 50 | + * @since 1.1.5 |
| 51 | + * |
| 52 | + * @see MultimediaSerializer#serializeTo(Appendable, Object...) |
| 53 | + */ |
| 54 | + default void serializeTo(boolean append, File f, Object... args) throws IOException |
| 55 | + { |
| 56 | + //double t0 = System.nanoTime(); |
| 57 | + try (Writer writer = new BufferedWriter(new FileWriter(f, append))) |
| 58 | + { |
| 59 | + serializeTo(writer, args); |
| 60 | + } |
| 61 | + |
| 62 | +// String serialized = stringify(args); |
| 63 | +// |
| 64 | +// RandomAccessFile fileOutputStream = new RandomAccessFile(f, "rw"); |
| 65 | +// FileChannel channel = fileOutputStream.getChannel(); |
| 66 | +// |
| 67 | +//// channel.write(ByteBuffer.wrap(serialized.getBytes())); |
| 68 | +// |
| 69 | +// ByteBuffer buff = channel.map(FileChannel.MapMode.READ_WRITE, append ? channel.size() : 0, serialized.length()); |
| 70 | +// buff.put(serialized.getBytes()); |
| 71 | +// |
| 72 | +// channel.force(true); |
| 73 | +// channel.close(); |
| 74 | +// fileOutputStream.close(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 79 | + * |
| 80 | + * @return String with the respective contents serialized in specific format. |
| 81 | + * |
| 82 | + * @throws RuntimeException IOException, but it should not occur in this case... |
| 83 | + * |
| 84 | + * @since 1.0.0 |
| 85 | + * |
| 86 | + * @see MultimediaSerializer#serializeTo(Appendable, Object...) |
| 87 | + */ |
| 88 | + default String stringify(Object... args) |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + return serializeTo(new StringBuilder(), args).toString(); |
| 93 | + } |
| 94 | + catch (IOException e) |
| 95 | + { |
| 96 | + throw new RuntimeException(e); // Should not occur... |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param outputStream | Source {@link OutputStream} to serialize the respective contents into! |
| 102 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 103 | + * |
| 104 | + * @return Source {@link OutputStream} with respective contents serialized in specific format (may or may not be flushed). |
| 105 | + * |
| 106 | + * @throws IOException When appending the {@link OutputStream} throws.... |
| 107 | + * |
| 108 | + * @since 1.3.2 |
| 109 | + * |
| 110 | + * @see MultimediaSerializer#serializeTo(Appendable, Object...) |
| 111 | + */ |
| 112 | + default OutputStream serializeTo(OutputStream outputStream, Object... args) throws IOException |
| 113 | + { |
| 114 | + serializeTo(new OutputStreamWriter(outputStream), args); |
| 115 | + return outputStream; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param source | Source {@link Appendable} to serialize respective contents into! |
| 120 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 121 | + * |
| 122 | + * @return Source {@link Appendable} with respective contents serialized in specific format (depends on the implementation). |
| 123 | + * |
| 124 | + * @throws IOException depends on the implementation (likely when the provided source throws). |
| 125 | + * |
| 126 | + * @since 1.3.2 |
| 127 | + */ |
| 128 | + <A extends Appendable> A serializeTo(A source, Object... args) throws IOException; |
| 129 | + |
| 130 | + /** |
| 131 | + * @param source | Source {@link Appendable} to serialize respective contents into! |
| 132 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 133 | + * |
| 134 | + * @return Serialized contents as the inner/nested sub-scope of this {@link MultimediaSerializer}! Wrapped inside of corresponding wrappingBrackets (default { and })! This is more usecase specific, mainly useful with treelike structures. |
| 135 | + * |
| 136 | + * @since 1.3.5 |
| 137 | + */ |
| 138 | + default <A extends Appendable> A serializeAsSubscope(A source, Object... args) throws IOException |
| 139 | + { |
| 140 | + return serializeAsSubscope(source, new char[] {'{', '}'}, args); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param source | Source {@link Appendable} to serialize respective contents into! |
| 145 | + * @param wrappingBrackets | Array of 2 characters to wrap content inside (opening and closing one). |
| 146 | + * @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)! |
| 147 | + * |
| 148 | + * @return Serialized contents as the inner/nested sub-scope of this {@link MultimediaSerializer}! Wrapped inside of corresponding wrappingBrackets (default { and })! This is more usecase specific, mainly useful with treelike structures. |
| 149 | + * |
| 150 | + * @since 1.3.5 |
| 151 | + */ |
| 152 | + <A extends Appendable> A serializeAsSubscope(A source, final char[] wrappingBrackets, Object... args) throws IOException; |
| 153 | + |
| 154 | + /** |
| 155 | + * @param file | Text file with serialized contents in specific format to load. |
| 156 | + * @param parserArgs | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (probably used for formating of incoming information)! |
| 157 | + * |
| 158 | + * @return Unserialized contents represented by object <code>T</code> (may or may not be "this")! |
| 159 | + * |
| 160 | + * @throws IOException if file does not exist or cannot be read! |
| 161 | + * |
| 162 | + * @since 1.0.0 |
| 163 | + * |
| 164 | + * @see MultimediaSerializer#loadFrom(Reader, Object...) |
| 165 | + */ |
| 166 | + default <S extends T> S loadFrom(File file, Object... parserArgs) throws IOException |
| 167 | + { |
| 168 | + return loadFrom(new FileReader(file), parserArgs); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @param str | {@link CharSequence} with serialized contents in specific format to load. |
| 173 | + * @param parserArgs | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (probably used for formating of incoming information)! |
| 174 | + * |
| 175 | + * @return Unserialized contents represented by object <code>T</code> (may or may not be "this")! |
| 176 | + * |
| 177 | + * @throws IOException When reading the char sequence fails... |
| 178 | + * |
| 179 | + * @since 1.2.5 |
| 180 | + * |
| 181 | + * @see MultimediaSerializer#loadFrom(Reader, Object...) |
| 182 | + */ |
| 183 | + default <S extends T> S loadFrom(CharSequence str, Object... parserArgs) throws IOException |
| 184 | + { |
| 185 | + return loadFrom(new StringReader(str.toString()), parserArgs); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @param stream | Any {@link InputStream} with serialized contents in specific format to load. |
| 190 | + * @param parserArgs | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (probably used for formating of incoming information)! |
| 191 | + * |
| 192 | + * @return Unserialized contents represented by object <code>T</code> (may or may not be "this")! |
| 193 | + * |
| 194 | + * @throws IOException When reading the input stream fails... |
| 195 | + * |
| 196 | + * @since 1.3.2 |
| 197 | + * |
| 198 | + * @see MultimediaSerializer#loadFrom(Reader, Object...) |
| 199 | + */ |
| 200 | + default <S extends T> S loadFrom(InputStream stream, Object... parserArgs) throws IOException |
| 201 | + { |
| 202 | + return loadFrom(new InputStreamReader(stream), parserArgs); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @param reader | Reader to read the with serialized contents from! |
| 207 | + * @param parserArgs | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (probably used for formating of incoming information)! |
| 208 | + * |
| 209 | + * @return Unserialized contents represented by object <code>T</code> (may or may not be "this". If <code>T</code> is instanceof this Class, you most likely want to return "this")! |
| 210 | + * |
| 211 | + * @throws IOException When reading the reader fails... |
| 212 | + * |
| 213 | + * @since 1.3.2 |
| 214 | + */ |
| 215 | + <S extends T> S loadFrom(Reader reader, Object... parserArgs) throws IOException; |
| 216 | +} |
0 commit comments