X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FImporter.java;h=eec30b24b754b248c9ebf55af950cefa1d4c97cf;hb=3b4319db57971bce3afd9092a0f9db9809d47ca2;hp=5d7d8d05ee30e298b802125abc63128c748ccb88;hpb=1d20e650389b5aaaaa5de4213cefefade582da5d;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/Importer.java b/src/be/nikiroo/utils/serial/Importer.java index 5d7d8d0..eec30b2 100644 --- a/src/be/nikiroo/utils/serial/Importer.java +++ b/src/be/nikiroo/utils/serial/Importer.java @@ -7,9 +7,9 @@ import java.util.HashMap; import java.util.Map; import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.NextableInputStream; -import be.nikiroo.utils.NextableInputStreamStep; import be.nikiroo.utils.StringUtils; +import be.nikiroo.utils.streams.NextableInputStream; +import be.nikiroo.utils.streams.NextableInputStreamStep; /** * A simple class that can accept the output of {@link Exporter} to recreate @@ -46,7 +46,7 @@ public class Importer { * content, or a number of lines of it (any given line MUST be * complete though) and accumulate it with the already present data. * - * @param data + * @param in * the data to parse * * @return itself so it can be chained @@ -68,37 +68,36 @@ public class Importer { NoSuchMethodException, ClassNotFoundException, IOException, NullPointerException { - // TODO: fix NexInStream: next() MUST be called first time, too - // TODO: NexInStream: add getBytes() (size downloaded) - // TODO: public InputStrem open() (open/close do nothing) - // TODO: public boolean eof() - // TODO: public nextAll(): next, but disable separation of sub-streams - // TODO: close(alsoCloseIncludedField) - NextableInputStream stream = new NextableInputStream(in, new NextableInputStreamStep('\n')); - if (in == null || stream.eof()) { + try { if (in == null) { throw new NullPointerException("InputStream is null"); } - throw new NullPointerException("InputStream is empty"); - } - - while (stream.next()) { - boolean zip = stream.startsWiths("ZIP:"); - boolean b64 = stream.startsWiths("B64:"); + if (stream.eof()) { + throw new NullPointerException("InputStream is empty"); + } - if (zip || b64) { - InputStream decoded = StringUtils.unbase64(stream.open(), zip); - try { - read(decoded); - } finally { - decoded.close(); + while (stream.next()) { + boolean zip = stream.startsWiths("ZIP:"); + boolean b64 = stream.startsWiths("B64:"); + + if (zip || b64) { + stream.skip("XXX:".length()); + InputStream decoded = StringUtils.unbase64(stream.open(), + zip); + try { + read(decoded); + } finally { + decoded.close(); + } + } else { + processLine(stream); } - } else { - processLine(stream); } + } finally { + stream.close(false); } return this; @@ -108,7 +107,7 @@ public class Importer { * Read a single (whole) line of serialised data into this {@link Importer} * and accumulate it with the already present data. * - * @param line + * @param in * the line to parse * * @return TRUE if we are just done with one object or sub-object @@ -217,37 +216,6 @@ public class Importer { } } - /** - * Find the given needle in the data and return its position (or -1 if not - * found). - * - * @param data - * the data to look through - * @param offset - * the offset at wich to start searching - * @param needle - * the needle to find - * - * @return the position of the needle if found, -1 if not found - */ - private int find(byte[] data, int offset, byte[] needle) { - for (int i = offset; i + needle.length - 1 < data.length; i++) { - boolean same = true; - for (int j = 0; j < needle.length; j++) { - if (data[i + j] != needle[j]) { - same = false; - break; - } - } - - if (same) { - return i; - } - } - - return -1; - } - /** * Return the current deserialised value. *