Sharkysoft home

lava.io
Class IoCloser

java.lang.Object
  |
  +--lava.io.IoCloser

public final class IoCloser
extends java.lang.Object

Clean stream/file closing.

Details: IoCloser contains static methods that allow your to close your streams without catching or throwing exceptions. To see why this is useful, consider the following code:

FileInputStream in = new FileInputStream (somefile);
perform operations on stream;
in . close ();

This code seems correct, unless the containing method is not allowed to throw IOExceptions. So we may have to enclose it in a try block:

try
{
	FileInputStream in = new FileInputStream (somefile);
	perform operations on stream;
	in . close ();
}
catch (IOException e)
{
	do something about it;
}

That's a little better. But now, what happens if an exception occurs while the stream is in use? in might never get closed! Can we just leave it open and wait for the garbage collector to deal with it? Probably, but that's not a great idea. Instead, we prefer that our code cleans up after itself, so we make the following modifications:

FileInputStream in = null;
try
{
	in = new FileInputStream (somefile);
	perform operations on stream;
}
catch (IOException e)
{
	do something about it;
}
finally
{
	try
	{
		if (in != null)
			in . close ();
	}
	catch (IOException ignored)
	{
		// We don't really care that we couldn't close it.
		// This exception is not very likely anyway.
	}
}

Whoa! What a mess! Fortunately, the close methods in IoCloser can spare us from some of this complexity (the red code):

FileInputStream in = null;
try
{
	in = new FileInputStream (somefile);
	perform operations on stream;
}
catch (IOException e)
{
	do something about it;
}
finally
{
	IoCloser.close (in);
}

Much cleaner! In this case, IoCloser allowed us to replace 8 lines of code with 1! (Assuming, of course, that you format your code the way we do -- the correct way... :-)

Since:
1998.10.11
Author:
Sharky

Method Summary
static java.io.IOException close(HtmlParser parser)
          Closes HtmlParser without throwing exception.
static java.io.IOException close(java.io.InputStream in)
          Closes InputStream without throwing exception.
static java.io.IOException close(java.io.OutputStream out)
          Closes OutputStream without throwing exception.
static java.io.IOException close(java.io.RandomAccessFile raf)
          Closes RandomAccessFile without throwing exception.
static java.io.IOException close(java.io.Reader reader)
          Closes Reader without throwing exception.
static java.io.IOException close(java.net.ServerSocket socket)
          Closes ServerSocket without throwing exception.
static java.io.IOException close(java.net.Socket socket)
          Closes Socket without throwing exception.
static java.io.IOException close(java.io.Writer writer)
          Closes Writer without throwing exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

close

public static java.io.IOException close(java.io.Writer writer)
Closes Writer without throwing exception.

Details: This method allows you to close a Writer without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if writer is null.

Parameters:
writer - the Writer to close
Returns:
the exception that occurred, if any
Since:
1998.11.05

close

public static java.io.IOException close(java.io.Reader reader)
Closes Reader without throwing exception.

Details: This method allows you to close a Reader without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if reader is null.

Parameters:
reader - the Reader to close
Returns:
the exception that occurred, if any
Since:
1998.10.11

close

public static java.io.IOException close(java.io.InputStream in)
Closes InputStream without throwing exception.

Details: This method allows you to close an InputStream without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if in is null.

Parameters:
in - the InputStream to close
Returns:
the exception that occurred, if any
Since:
1999.03.09

close

public static java.io.IOException close(java.io.OutputStream out)
Closes OutputStream without throwing exception.

Details: This method allows you to close an OutputStream without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if out is null.

Parameters:
out - the OutputStream to close
Returns:
the exception that occurred, if any
Since:
1999.12.09

close

public static java.io.IOException close(java.io.RandomAccessFile raf)
Closes RandomAccessFile without throwing exception.

Details: This method allows you to close a RandomAccessFile without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if raf is null.

Parameters:
raf - the RandomAccessFile to close
Returns:
the exception that occurred, if any
Since:
2000.01.29

close

public static java.io.IOException close(HtmlParser parser)
Closes HtmlParser without throwing exception.

Details: This method allows you to close an HtmlParser without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if parser is null.

Parameters:
parser - the HtmlParser to close
Returns:
the exception that occurred, if any
Since:
2000.07.08

close

public static java.io.IOException close(java.net.Socket socket)
Closes Socket without throwing exception.

Details: This method allows you to close a Socket without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if socket is null.

Parameters:
socket - the Socket to close
Returns:
the exception that occurred, if any
Since:
2000.11.05

close

public static java.io.IOException close(java.net.ServerSocket socket)
Closes ServerSocket without throwing exception.

Details: This method allows you to close a ServerSocket without worrying about trapping the possible exception. (This usually results in cleaner code.) If an exception occurs, it is returned instead of thrown. If no exception occurs, null is returned.

No action is taken and null is returned if socket is null.

Parameters:
socket - the ServerSocket to close
Returns:
the exception that occurred, if any
Since:
2000.11.05

Sharkysoft home