从流中删除开头和尾随字符

时间:2011-11-23 16:32:38

标签: java json file caching stream

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

实际的缓存机制只是将大型流保存到文件而不会意识到它是json。因此,当我想通过将流聚合到另一个文件来将流附加到现有文件缓存时,我最终得到如下内容:

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

显然我想要的是这样的:

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

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

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

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

    ... 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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

这种方法可以解决问题

/**
 * 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数组的功能