从JSON流中提取和读取大型编码文件

时间:2018-05-05 22:09:53

标签: java json

我们在简单的JSON中有一些相当大的base64编码文件:

{
"name": "ox",
"file": "blah base64 very long file blahblah",
"file2"    :    "yet another blah base64 very long file blahblah"}
...
}

由于这些嵌入的编码文件的大小,我需要流式传输数据,因此我正在使用JsonParser。

但是,JsonParser似乎没有办法“查看”流足够接近的位置。它确实有getString(),但这会将大的嵌入式文件(~1GB)拉入内存。

我尝试了JsonParser.getLocation().getStreamOffset(),但是起始位置就在名称之后,而空白的变化使得难以准确地预测流中对象的位置。

所以这就是我所拥有的:

    public static void main(String[] args) throws Exception{
    InputStream is = Files.newInputStream(Paths.get("C:/Users/eclipse-workspace/jsonParseEmbeddedFile/src/main/resources/json01.json"));
    JsonParser jp = Json.createParser(is);
    while (jp.hasNext()) {
        Event e = jp.next();
        if(e == Event.KEY_NAME) {
            switch(jp.getString()) {
            case "name":
                System.out.print(jp.getString() + ": ");
                jp.next();
                System.out.println(jp.getString());
                break;
            case "file":
                System.out.print(jp.getString()+": ");
                jp.next();
                System.out.println(jp.getString());
            }
        }
    }
    is.close();
    jp.close();
    t1();
}

private static void t1() throws Exception {
    InputStream is = Files.newInputStream(Paths.get("C:/Users/eclipse-workspace/jsonParseEmbeddedFile/src/main/resources/json01.json"));
    JsonParser jp = Json.createParser(is);
    long start = 0L;
    long end = 0L;
    long start2 = 0L;
    long end2 = 0L;
    while (jp.hasNext()) {
        Event e = jp.next();
        if(e == Event.KEY_NAME) {
            switch(jp.getString()) {
            case "name":
                System.out.print(jp.getString() + ": ");
                jp.next();
                System.out.println(jp.getString());
                break;
            case "file":
                start=jp.getLocation().getStreamOffset();
                jp.next();
                end=jp.getLocation().getStreamOffset();
                break;
            case "file2":
                start2=jp.getLocation().getStreamOffset();
                jp.next();
                end2=jp.getLocation().getStreamOffset();
                break;
            }
        }
    }
    is.close();
    jp.close();
    System.out.println((char) 10 + "file results:");
    InputStream is2 = Files.newInputStream(Paths.get("C:/Users/eclipse-workspace/jsonParseEmbeddedFile/src/main/resources/json01.json"));
    is2.skip(start);
    for(int i = 0; i < (end - start);i++) {
        System.out.print((char) is2.read());
    }
    is2.close();
    System.out.println((char) 10 + "file2 results:");
    InputStream is3 = Files.newInputStream(Paths.get("C:/Users/eclipse-workspace/jsonParseEmbeddedFile/src/main/resources/json01.json"));
    is3.skip(start2);
    for(int i = 0; i < (end2 - start2);i++) {
        System.out.print((char) is3.read());
    }
    is3.close();
}

结果如下:

name: ox
file: blah base64 very long file blahblah
name: ox
file results:
: "blah base64 very long file blahblah"
file2 results:
:    "yet another blah base64 very long file blahblah"

如何可靠地找到嵌入对象的开头?

0 个答案:

没有答案