Get BufferedImage File Size Before Writing Data

I am working on a Java program that should send an image (preferably in PNG format, although I could possibly use a different format if necessary) over the network. The logical way to do this, of course, is to first send the length of the PNG image data, and then the data itself. Problem: I don't see any method in ImageIOor anywhere else that will give me the length of the image data. Is this somewhere in the Java 1.6 standard library?

I know that I can just write it in ByteArrayOutputStreamand get the length of the resulting array, but before resorting to this, I wanted to make sure that I did not miss something inline. In addition, it makes an extra copy of the image data, which I would prefer not to do if it is not needed.

+3
source share
2 answers

This information is not available because the final length of the content is not predictable in advance. First you need to generate the whole image.


On the other hand, what network protocol are you using? You can check if this protocol supports sending data in pieces.

HTTP , Transfer-Encoding: chunked. byte[] . , , , CRLF, , CRLF , 0.

, , , , HTTP, API , java.net.URLConnection Apache HttpComponents, .

. :

+4

, SVG, .

0

Source: https://habr.com/ru/post/1750218/


All Articles