|
Sharkysoft home | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--lava.io.IoCloser
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... :-)
| 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 |
public static java.io.IOException close(java.io.Writer writer)
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.
writer - the Writer to closepublic static java.io.IOException close(java.io.Reader reader)
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.
reader - the Reader to closepublic static java.io.IOException close(java.io.InputStream in)
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.
in - the InputStream to closepublic static java.io.IOException close(java.io.OutputStream out)
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.
out - the OutputStream to closepublic static java.io.IOException close(java.io.RandomAccessFile raf)
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.
raf - the RandomAccessFile to closepublic static java.io.IOException close(HtmlParser parser)
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.
parser - the HtmlParser to closepublic static java.io.IOException close(java.net.Socket socket)
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.
socket - the Socket to closepublic static java.io.IOException close(java.net.ServerSocket socket)
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.
socket - the ServerSocket to close
|
Sharkysoft home | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||