X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FImporter.java;h=6718fb8ecf623b8bd66350be1d1f51685836b2cf;hb=d251f3dd38a8f9d369a7cf627185eacc4f66ece5;hp=f7fece064d6028d8fca8ff5a742cf997f52f52b2;hpb=08f80ac5fa60738d3ad74c4b5390a0b79ae313d4;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/Importer.java b/src/be/nikiroo/utils/serial/Importer.java index f7fece0..6718fb8 100644 --- a/src/be/nikiroo/utils/serial/Importer.java +++ b/src/be/nikiroo/utils/serial/Importer.java @@ -5,9 +5,11 @@ import java.io.InputStream; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; +import java.util.zip.GZIPInputStream; import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.StringUtils; +import be.nikiroo.utils.streams.Base64InputStream; +import be.nikiroo.utils.streams.BufferedInputStream; import be.nikiroo.utils.streams.NextableInputStream; import be.nikiroo.utils.streams.NextableInputStreamStep; @@ -87,13 +89,18 @@ public class Importer { } first = false; - boolean zip = stream.startsWiths("ZIP:"); - boolean b64 = stream.startsWiths("B64:"); + boolean zip = stream.startsWith("ZIP:"); + boolean b64 = stream.startsWith("B64:"); if (zip || b64) { stream.skip("XXX:".length()); - InputStream decoded = StringUtils.unbase64(stream.open(), - zip); + + InputStream decoded = stream.open(); + if (zip) { + decoded = new GZIPInputStream(decoded); + } + decoded = new Base64InputStream(decoded, false); + try { read(decoded); } finally { @@ -130,8 +137,9 @@ public class Importer { * @throws IOException * if the content cannot be read (for instance, corrupt data) */ - private boolean processLine(InputStream in) throws NoSuchFieldException, - NoSuchMethodException, ClassNotFoundException, IOException { + private boolean processLine(BufferedInputStream in) + throws NoSuchFieldException, NoSuchMethodException, + ClassNotFoundException, IOException { // Defer to latest child if any if (child != null) { @@ -146,20 +154,34 @@ public class Importer { return false; } - // TODO use the stream, Luke - String line = IOUtils.readSmallStream(in); - - if (line.isEmpty()) { - return false; - } - - if (line.equals("{")) { // START: new child if needed + // Start/Stop object + if (in.is("{")) { // START: new child if needed if (link != null) { child = new Importer(map); } - } else if (line.equals("}")) { // STOP: report self to parent + in.end(); + return false; + } else if (in.is("}")) { // STOP: report self to parent + in.end(); return true; - } else if (line.startsWith("REF ")) { // REF: create/link self + } + + // Custom objects + if (CustomSerializer.isCustom(in)) { + // not a field value but a direct value + String line = IOUtils.readSmallStream(in); + me = SerialUtils.decode(line); + return false; + } + + // TODO use the stream, Luke + // .. at least for REF + String line = IOUtils.readSmallStream(in); + + if (line.startsWith("REF ")) { // REF: create/link self + // TODO: here, line is REF type@999:xxx + // xxx is optional + // note: use .end() when containsKey(ref) String[] tab = line.substring("REF ".length()).split("@"); String type = tab[0]; tab = tab[1].split(":"); @@ -185,7 +207,7 @@ public class Importer { // field value is compound currentFieldName = line.substring(0, line.length() - 1); } else if (line.startsWith(":") || !line.contains(":") - || line.startsWith("\"") || CustomSerializer.isCustom(line)) { + || line.startsWith("\"")) { // not a field value but a direct value me = SerialUtils.decode(line); } else {