Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:43:21+00:00 2026-05-27T00:43:21+00:00

I have a low level caching mechanism which receives a json array from a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:43:22+00:00Added an answer on May 27, 2026 at 12:43 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just been getting into low level programming (reading/writing to memory that sort
I have a win 32 application written in c++ which sets the low level
We have some code that must access low level windows XP os calls which
I have just started learning how to program Windows GUI's using the low level
I have a class on low-level programming which requires a final project (syllabus at
I have no experience with low level programing and I need this piece of
I wish to manipulate data on a very low level. Therefore I have a
Hello we have an SQL server application running over a low bandwith connection. We
Somewhere I have read that modern Intel processors have low-level hardware for implementing exceptions
I have successfully gotten my low-level mouse hook code to work, but there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.