Question: I write images and other data to binary file. When I read image via ImageIO.read(InputStream) from that file, it reads image, it is ok, but method closes given input stream and I cant proceed to read other data.

1. Why so it is made?
2. Then how read image without closing stream?

**EDIT**: It is simple code that writes image and string after into file:

```
File f = new File("test.bin");
if(f.exists())
f.delete();
f.createNewFile();
DataOutputStream os = new DataOutputStream(new FileOutputStream(f));
BufferedImage img = ImageIO.read(new File("test.jpg"));
ImageIO.write(img, "jpg", os);
os.writeUTF("test string after image");
os.close();

```

And code that reads all:

```
DataInputStream is = new DataInputStream(new FileInputStream(f));
BufferedImage img = ImageIO.read(is);
String s = is.readUTF(); // on this line EOFException occurs
System.out.println(s);

```

NetBeans output:

```
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at mediamanager.Main.test(Main.java:105)
at mediamanager.Main.main(Main.java:44)

```

May be I'm doing something wrong?

Answer: 
Quote from the documentation of [ImageIO.read(InputStream)](http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read%28java.io.InputStream%29)

> 
> This method *does not* close the provided InputStream after the read operation has completed; it is the responsibility of the caller to close the stream, if desired.
> 
> 
> 

Emphasis *not* mine.

The problem is elsewhere. Probably in your code.
I can see two possible causes of such behaviour:

1. Image reader use buffer to read data from the stream to improve performance. So it reads more data from the stream.
2. Also image reader could try to read EXIF for already parsed image. Such information usually appended at the end of file to avoid full file rewriting when you are just adding a couple of piece of information about the image.

Try `ImageIO.setUseCash(false)` it could help.