Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.json.JSONTokener should close or provide option to close java.io.Reader #836

Open
rikkarth opened this issue Dec 7, 2023 · 4 comments

Comments

@rikkarth
Copy link
Contributor

rikkarth commented Dec 7, 2023

Hi,

I spent a few days trying to fix an issue where I got the following error:

java.nio.file.FileSystemException: path/to/file: The process cannot access the file because it is being used by another process.

I started using FileLock to try to get a hold on the lock and wait until the lock was released, but unfortunately for this file, it would never be released.

That is because I was using a JSONTokener to tokenize a JSON file using an anonymous FileReader. This stream would never be closed, and therefor the error mentioned above.

image

Because JSONTokener doesn't provide an API to close the stream provided as parameter, I have to correct my implementation like this:

image

Obviously after reading the class it is clear how it should be used, since it is clearly written in the JavaDoc that the Reader must be closed by the caller.

image

I believe this implementation could be improved.

  • Implement Closeable/AutoClosable on JSONTokener and make JSONTokener throw some sort of exception to explicitly tell the user that the Reader must be closed. And then we could have a cleaner and more explicit implementation like this:

image

Let me know if my suggestion is outrageous. But I feel at least I should bring it here.

Thank you for your attention. :)

@stleary
Copy link
Owner

stleary commented Dec 7, 2023

@rikkarth Thanks for bringing this up. Unfortunately, at present, this project cannot accept Java 8 features like Closeable and AutoCloseable due to some Android developers being stuck on Java 7.

@rikkarth
Copy link
Contributor Author

rikkarth commented Dec 7, 2023

@stleary , thank you for the quick reply.

Not sure if is worth to keep or archive this ticket for the future then. But thank you for your attention to this.

Thank you for the feedback and have a great day.

@rikkarth
Copy link
Contributor Author

hi, since new version supports Closeable and AutoCloseable, I'd like just to suggest that the JSONTokener to be reviewed if possible. Thank you for your service.

@stleary
Copy link
Owner

stleary commented Feb 2, 2024

Hi @rikkarth Sorry, that is still not an option at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants