【问题标题】:Removing the opening and trailing characters from a stream从流中删除开头和结尾的字符
【发布时间】:2012-01-04 22:55:38
【问题描述】:

我有一个低级缓存机制,它从服务器接收一个 json 数组并将其缓存在一个文件中。

实际的缓存机制只是将 large 流保存到文件中,而不知道它是 json。因此,当我想通过将流聚合到另一个文件中来将流附加到现有文件缓存时,我最终会得到这样的结果:

[{"id":3144,"created_at":"1322064201"}][{"id":3144,"created_at":"1322064201"}] 

显然我想要的是这样的:

[{"id":3144,"created_at":"1322064201"},{"id":3144,"created_at":"1322064201"}]

最有效/最有效的方法是什么?

我查看了FilterReader,但据我所知,我真正需要做的就是删除现有缓存的最后一个字符] 和新内容[ 的第一个字符并添加一个, I认为可能有比检查这些大流中的每个字符更好的方法。

对于上下文,我的代码执行如下操作:

    ... input stream passed with new content

    File newCache = new File("JamesBluntHatersClub")
    FileOutputStream tempFileOutputStream = new FileOutputStream(newCache);
    FileInputStream fileInputStream = new FileInputStream(existingCache);
    copyStream(fileInputStream, tempFileOutputStream);
    copyStream(inputStream, tempFileOutputStream);

    ... clean up

更新:

已经实现了一个FilterReader,它一次检查一个字符,如下所示:

@Override
public int read() throws IOException {
    int content = super.read();
    // replace open square brackets with comma
    switch (content) {
        case SQUARE_BRACKETS_OPEN:
            return super.read();
        case SQUARE_BRACKETS_CLOSE:
            return super.read();
        default:
            return content;
    }
}

处理时间慢得令人无法接受,所以我正在寻找另一种选择。我正在考虑使用文件大小来确定文件的大小并以这种方式删除尾部方括号

【问题讨论】:

    标签: java json file caching stream


    【解决方案1】:

    这个方法成功了

    /**
     * Copys the input streams in order to the output stream and retains json array
     * format
     * 
     * @param inputStreamA
     * @param inputStreamB
     * @param outputStream
     * @throws IOException
     */
    private void copyStreamsToOutput(InputStream inputStreamA, InputStream inputStreamB,
            FileOutputStream outputStream) throws IOException {
        copyStream(inputStreamA, outputStream);
        // truncate file to remove trailing ']'
        outputStream.getChannel().truncate(outputStream.getChannel().size() - 1);
        // add comma between json objects
        outputStream.write(COMMA);
        // skip '['
        inputStreamB.skip(1);
        // and copy rest of streamas normal
        copyStream(inputStreamB, outputStream);
    }
    

    如果这是不好的做法,我会很感兴趣,我猜可能存在编码问题。

    更新

    /**
     * Copys the input streams in order to output stream and retains json array
     * format
     * 
     * @param inputStreamA
     * @param inputStreamB
     * @param outputStream
     * @throws IOException
     */
    private void copyStreamsToOutput(InputStream inputStreamA, InputStream inputStreamB,
            FileOutputStream outputStream) throws IOException {
        copyStream(inputStreamA, outputStream);
        long channelSize = outputStream.getChannel().size();
        // truncate file to remove trailing ']'
        outputStream.getChannel().truncate(channelSize - 1);
        // check to see if array was empty (2 = [])
        if (channelSize > 2) {
            // add comma between json objects
            outputStream.write(COMMA);
        }
        // skip '['
        inputStreamB.skip(1);
        // and copy rest of streams normal
        copyStream(inputStreamB, outputStream);
        long newChannelSize = outputStream.getChannel().size();
        // check if we haven't just added a empty array
        if(newChannelSize - channelSize < 2){
            // if so truncate to remove comma 
            outputStream.getChannel().truncate(channelSize - 1);
            outputStream.write(CLOSE_SQUARE_BRACKET);
        }
    }
    

    增加了在任一流中处理空 json 数组的能力

    【讨论】:

    • 看来您需要从非空缓存开始( [] ?)。您可以将最后一个字符从 ] 替换为 ,.
    • 我不确定我是否完全理解,您是在建议我先将 '[]' 写入 outputstream,然后将最后一个 ']' 替换为 ','?
    • 正如您已经建议的那样,您的方法不适用于每种编码。在这种情况下,我会在代码中添加一些断言,并在 Javadoc 中提及该方法的局限性。添加一些单元测试后,这是质量代码,不错的做法。您的第一个解决方案的处理时间慢得令人无法接受?您是否将 FilterReader 的参数包装在 BufferedReader 中以提高性能?只是问...
    • @rwoo 它在移动设备上,文件中缓存的数据量可能非常大。我似乎记得我在某个阶段的第一个解决方案中使用了 BufferedReader。问题是目标文件增长了多大。在这种情况下,2 秒太多了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多