import java.io.IOException;
import java.io.NotSerializableException;
+import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
*/
public class Exporter {
private Map<Integer, Object> map;
- private StringBuilder builder;
+ private OutputStream out;
/**
* Create a new {@link Exporter}.
*/
- public Exporter() {
+ public Exporter(OutputStream out) {
+ if (out == null) {
+ throw new NullPointerException(
+ "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL");
+ }
+
+ this.out = out;
map = new HashMap<Integer, Object>();
- builder = new StringBuilder();
}
/**
* if the object cannot be serialised (in this case, the
* {@link Exporter} can contain bad, most probably not
* importable data)
+ * @throws IOException
+ * in case of I/O error
*/
- public Exporter append(Object o) throws NotSerializableException {
- SerialUtils.append(builder, o, map);
+ public Exporter append(Object o) throws NotSerializableException,
+ IOException {
+ SerialUtils.append(out, o, map);
return this;
}
- /**
- * Clear the current content.
- */
- public void clear() {
- builder.setLength(0);
- map.clear();
- }
-
/**
* Append the exported items in a serialised form into the given
- * {@link StringBuilder}.
+ * {@link OutputStream}.
*
- * @param toBuilder
- * the {@link StringBuilder}
+ * @param out
+ * the {@link OutputStream}
* @param b64
* TRUE to have BASE64-coded content, FALSE to have raw content,
* NULL to let the system decide
* TRUE to zip the BASE64 output if the output is indeed in
* BASE64 format, FALSE not to
*/
- public void appendTo(StringBuilder toBuilder, Boolean b64, boolean zip) {
- if (b64 == null && builder.length() < 128) {
+ public void appendTo(OutputStream out, Boolean b64, boolean zip) {
+ if (b64 == null && out.length() < 128) {
b64 = false;
}
if (b64 == null || b64) {
try {
- String zipped = StringUtils.base64(builder.toString(), zip);
- if (b64 != null || zipped.length() < builder.length() - 4) {
- toBuilder.append(zip ? "ZIP:" : "B64:");
- toBuilder.append(zipped);
+ String zipped = StringUtils.base64(out.toString(), zip);
+ if (b64 != null || zipped.length() < out.length() - 4) {
+ SerialUtils.write(out, zip ? "ZIP:" : "B64:");
+ SerialUtils.write(out, zipped);
return;
}
} catch (IOException e) {
}
}
- toBuilder.append(builder);
- }
-
- /**
- * The exported items in a serialised form.
- *
- * @deprecated use {@link Exporter#toString(Boolean, boolean)} instead
- *
- * @param zip
- * TRUE to have zipped (and BASE64-coded) content, FALSE to have
- * raw content, NULL to let the system decide
- *
- * @return the items currently in this {@link Exporter}
- */
- @Deprecated
- public String toString(Boolean zip) {
- return toString(zip, zip == null || zip);
- }
-
- /**
- * The exported items in a serialised form.
- *
- * @param b64
- * TRUE to have BASE64-coded content, FALSE to have raw content,
- * NULL to let the system decide
- * @param zip
- * TRUE to zip the BASE64 output if the output is indeed in
- * BASE64 format, FALSE not to
- *
- * @return the items currently in this {@link Exporter}
- */
- public String toString(Boolean b64, boolean zip) {
- StringBuilder toBuilder = new StringBuilder();
- appendTo(toBuilder, b64, zip);
- return toBuilder.toString();
- }
-
- /**
- * The exported items in a serialised form (possibly BASE64-coded, possibly
- * zipped).
- *
- * @return the items currently in this {@link Exporter}
- */
- @Override
- public String toString() {
- return toString(null, true);
+ out.append(out);
}
}
\ No newline at end of file