Stream closed Exception


java.io.IOException: Stream closed
	at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
	at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2213)
	at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2226)
	at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2694)
	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:761)
	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
	at apk.GuardAI.directOpen(GuardAI.java:503)
	at apk.GuardAI.initialize(GuardAI.java:68)
	at apk.Agent00PK.<init>(Agent00PK.java:53)
	at apk.Agent00PK.main(Agent00PK.java:62)


/**
	 * Open an AI using the class path only.
	 * @param rec	The path of the resource.
	 */
	private static void directOpen(String res)
	{
		try
		{
			ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(res)));
			qValues = (float[][][][][][]) is.readObject();
			is.close();
		}
		catch(Exception e) {e.printStackTrace();}
	}

Makes no sense to me. How is the stream closed immediately after I open it? Is this maybe because I’m not running from a JNLP right now? Also, I literally copy and pasted the InputStream creation from other places in my code that work with no issues. Any ideas why this one is so picky?

Make sure your resource is correct. If your resource is not constructed correctly, then the stream will close.

The bottom line is you’ll never get rid of these completely. Networking is complicated and the networking
layers of your OS are replete with “punt” conditions when there is no way to go forward with the prescribed
next step. These kind of exceptions tend to happen in clusters, and more often when opening connections,
but they can happen at any time. The best you can do is have a recovery plan which you have tested by
simulating random failures.