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
This method did the trick
Would be very interested to here if this is bad practice, I am guessing there may be encoding issues.
UPDATE
Added ability to handle an empty json array in either stream