-
Notifications
You must be signed in to change notification settings - Fork 938
delete unneeded buffered streams #383
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
base: trunk
Are you sure you want to change the base?
delete unneeded buffered streams #383
Conversation
|
What research did you do? But yes I'm of course for reducing code but I don't want a check. |
I mean, for all the code sections that were changed here, I verified that the SomePdfBoxClass.class.getResourceAsStream method returns a buffered stream. |
|
ChatGPT disagrees, and I usually treat it as a mainstream opinion on a topic (and I'm aware that it is hallucinating sometimes) |
I don't care what the СhatGPT responds to. As I stated earlier, I checked all cases, and this method always returned a BufferedInputStream. This method returns an InputStream, which is the parent class of BufferedInputStream. Java doesn't really guarantee that it will always return a BufferedInputStream. Therefore, I suggested adding a check there. In my opinion, this is better than double synchronization. InputStream is = SomePdfBoxClass.class.getResourceAsStream(somePath); //another part of the code |
|
How could you be so sure that you checked all cases? I had a quick look and there are different branches which get confusing there one dig deeper into it. Many of them ended in java.net.URLConnection.getInputStream() which is implemented by several classes and some are JDK specific, so that there might be other implementations in other JDK you never saw. I'm afraid the assumption that all implementations are already returning a buffered stream is not correct. I agree with @THausherr and won't support a check. Did you have some cases where such a change introduces a real improvement? |
|
I think that most usages are in tests and in initialization. If there is a usage that happens regularly, e.g. with each page, only then it would make sense to look for optimization. |
Can I be responsible for all cases in the JDK? I only tested this on the trunk branch. I mean, I tested all the cases I'm requesting to change. My suggestion applies only to library resources. These are relatively small files. You don't want to add a check. Even if the getResourceAsStream method returns an unbuffered stream (I think it will always be buffered for new JDK versions), you don't lose anything significant. In my opinion, having a check will be a safer and faster option than your double synchronization. Although, I repeat, I am not insisting on type checking, I am only saying (as does chat gpt) that there is no guarantee that a buffered stream will always be returned. |
In my opinion, loading speed is also important. Especially for server computing, where there may be hundreds or thousands of library instances. |

I did a little research and realized that these streams are already buffered. It's possible that in some very old SDK versions they won't be buffered. In my opinion, it's best to only instantiate a BufferedInputStream if the current stream is unbuffered. I mean, if you'd like then I can add the check is this BufferedInputStream and wrap current stream if not. In the current implementation in the library, we're experiencing double synchronization due to the double wrapping of the BufferedInputStream.