Adding IO.close(Closeable) and using it.

This commit is contained in:
Joakim Erdfelt 2015-05-14 07:41:46 -07:00
parent aba2e083d8
commit eb638777d0
2 changed files with 33 additions and 46 deletions

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.util; package org.eclipse.jetty.util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -348,7 +349,24 @@ public class IO
return file.delete(); return file.delete();
} }
/* ------------------------------------------------------------ */ /**
* Closes an arbitrary closable, and logs exceptions at ignore level
*
* @param closeable the closeable to close
*/
public static void close(Closeable closeable)
{
try
{
if (closeable != null)
closeable.close();
}
catch (IOException ignore)
{
LOG.ignore(ignore);
}
}
/** /**
* closes an input stream, and logs exceptions * closes an input stream, and logs exceptions
* *
@ -356,15 +374,17 @@ public class IO
*/ */
public static void close(InputStream is) public static void close(InputStream is)
{ {
try close((Closeable)is);
{ }
if (is != null)
is.close(); /**
} * closes an output stream, and logs exceptions
catch (IOException e) *
{ * @param os the output stream to close
LOG.ignore(e); */
} public static void close(OutputStream os)
{
close((Closeable)os);
} }
/** /**
@ -374,14 +394,7 @@ public class IO
*/ */
public static void close(Reader reader) public static void close(Reader reader)
{ {
try close((Closeable)reader);
{
if (reader != null)
reader.close();
} catch (IOException e)
{
LOG.ignore(e);
}
} }
/** /**
@ -391,14 +404,7 @@ public class IO
*/ */
public static void close(Writer writer) public static void close(Writer writer)
{ {
try close((Closeable)writer);
{
if (writer != null)
writer.close();
} catch (IOException e)
{
LOG.ignore(e);
}
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -410,25 +416,6 @@ public class IO
return bout.toByteArray(); return bout.toByteArray();
} }
/* ------------------------------------------------------------ */
/**
* closes an output stream, and logs exceptions
*
* @param os the output stream to close
*/
public static void close(OutputStream os)
{
try
{
if (os != null)
os.close();
}
catch (IOException e)
{
LOG.ignore(e);
}
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* @return An outputstream to nowhere * @return An outputstream to nowhere

View File

@ -29,7 +29,7 @@ import java.nio.file.Path;
import java.util.Calendar; import java.util.Calendar;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.toolchain.test.IO; import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;