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

发布于 2024-12-17 19:20:46 字数 1697 浏览 0 评论 0原文

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

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

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

显然我想要的是这样的:

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

执行此操作的最有效/最有效的方法是什么?

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

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

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

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

I have a low level caching mechanism which receives a json array from a server and caches it in a file.

The actual caching mechanism is just saving large streams to a file without awareness that it is 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.

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:

@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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-12-24 19:20:46

这个方法成功了,

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文