Using object stream from java

I have a image file that need to be exposed as stream. So I have written a fs which reads the image from File system and outputs stream as object.
We have a java client which calls this flow service.

Do anybody have any examples of java code in using the stream, convert this stream back to image?

You might want to use the javax.imageio package provided in versions above J2SE 1.4.

Check with your resources about the java version.


Couple of things:

  • “I have a image file that need to be exposed as stream.” I’m willing to bet that that is not true. Rather, you simply need to return the contents of the file, which happens to be an image file, correct?

  • Do not expose the “image” as a stream object. It won’t work across the network. Just return the bytes. The Java client will handle what it should do with the bytes.

  • A stream object is only valid in the context of the current JVM–you can’t hand it off to another JVM.

  • A stream isn’t a useful object in and of itself. It is always “backed” by something else (bytes in a file, bytes in an array, etc.). One doesn’t “convert” a stream to anything. One reads from the stream. In this case, the bytes of the image file–but the fact the bytes are an image is immaterial. It could be a Word document, a PDF file, etc.

  • javax.imageio is in no way or form needed for this sort of operation, given the info so far.

Thank you for your details.

What is the best way to transfer an image. I was able to transfer image in bytes and client was able to successfully read the image in our POC.

But We are expecting lots of load with image transfers.

So we want to handle the transfer of these images the best possible way in terms of cosuming less memory and best performance.

Can you provide any suggestions regarding this?

To keep memory usage to a minimum, use streams within IS when retrieving the image file from disk. Depending on how you are passing the bytes to the client, you may be able to avoid loading entire image files into memory.

I am unclear about -->avoid loading entire image files into memory

How can I do that?

In addition to:

  1. how to avoid loading entire image files into memory
  2. Can we use any compression mechanisms?

to improve memory usage and performance.

  1. Use streams where possible. You can learn more about those on the web. Depending on how your service is constructed, you may or may not be able to respond to the client without loading the entire file.
  2. Compression will decrease performance. And most image files are already compressed to the extent that they can be.