简体   繁体   English

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

[英]Removing the opening and trailing characters from a stream

I have a low level caching mechanism which receives a json array from a server and caches it in a file. 我有一个低级缓存机制,该机制从服务器接收json数组并将其缓存在文件中。

The actual caching mechanism is just saving large streams to a file without awareness that it is json. 实际的缓存机制只是将大型流保存到文件中,而没有意识到它是json。 Therefore when I would like to append a stream to an existing file cache by aggregating streams into another file I end up with something like this: 因此,当我想通过将流聚合到另一个文件中来将流附加到现有文件高速缓存时,我得到的结果如下:

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

where obviously what I desire is something like this: 显然我想要的是这样的:

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

What is the most efficient/effective way of doing this? 什么是最有效的方法?

I have looked into FilterReader but seen as I know that all I actually need to do is remove the last char ] of the existing cache and first char of new content [ and add a , I thought there may be a better way than checking every char in these big streams. 我已经调查FilterReader ,但看到我所知,所有我真正需要做的是去除最后一个字符]现有的高速缓存和新内容的第一个char [和增加,我认为有可能比检查每个字符一个更好的办法在这些大溪流中。

For context my code does something like this: 对于上下文,我的代码执行以下操作:

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

UPDATE: 更新:

Having implemented a FilterReader which checks chars one at a time like so: 实现了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;
    }
}

the processing time is unacceptably slow so I am looking for another option. 处理时间慢得令人无法接受,因此我正在寻找另一种选择。 I was thinking about using the file size to determine the size of the file and removing the tail square bracket this way 我正在考虑使用文件大小来确定文件的大小,并以这种方式删除尾方括号

This method did the trick 这种方法成功了

/**
 * 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);
}

Would be very interested to here if this is bad practice, I am guessing there may be encoding issues. 如果这是一种不好的做法,将对这里非常感兴趣,我猜可能存在编码问题。

UPDATE 更新

/**
 * 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);
    }
}

Added ability to handle an empty json array in either stream 增加了处理任一流中的空json数组的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM