Home | History | Annotate | Download | only in io
      1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      2 <!--NewPage-->
      3 <HTML>
      4 <HEAD>
      5 <!-- Generated by javadoc (build 1.6.0-google-internal) on Mon Jan 04 20:47:59 PST 2010 -->
      6 <TITLE>
      7 Closeables (Guava Libraries 2010.01.04)
      8 </TITLE>
      9 
     10 <META NAME="date" CONTENT="2010-01-04">
     11 
     12 <LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">
     13 
     14 <SCRIPT type="text/javascript">
     15 function windowTitle()
     16 {
     17     if (location.href.indexOf('is-external=true') == -1) {
     18         parent.document.title="Closeables (Guava Libraries 2010.01.04)";
     19     }
     20 }
     21 </SCRIPT>
     22 <NOSCRIPT>
     23 </NOSCRIPT>
     24 
     25 </HEAD>
     26 
     27 <BODY BGCOLOR="white" onload="windowTitle();">
     28 <HR>
     29 
     30 
     31 <!-- ========= START OF TOP NAVBAR ======= -->
     32 <A NAME="navbar_top"><!-- --></A>
     33 <A HREF="#skip-navbar_top" title="Skip navigation links"></A>
     34 <TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
     35 <TR>
     36 <TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
     37 <A NAME="navbar_top_firstrow"><!-- --></A>
     38 <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
     39   <TR ALIGN="center" VALIGN="top">
     40   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
     41   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
     42   <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
     43   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/Closeables.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
     44   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
     45   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
     46   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
     47   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
     48   </TR>
     49 </TABLE>
     50 </TD>
     51 <TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
     52 </EM>
     53 </TD>
     54 </TR>
     55 
     56 <TR>
     57 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
     58 &nbsp;<A HREF="../../../../com/google/common/io/CharStreams.html" title="class in com.google.common.io"><B>PREV CLASS</B></A>&nbsp;
     59 &nbsp;<A HREF="../../../../com/google/common/io/CountingInputStream.html" title="class in com.google.common.io"><B>NEXT CLASS</B></A></FONT></TD>
     60 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
     61   <A HREF="../../../../index.html?com/google/common/io/Closeables.html" target="_top"><B>FRAMES</B></A>  &nbsp;
     62 &nbsp;<A HREF="Closeables.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
     63 &nbsp;<SCRIPT type="text/javascript">
     64   <!--
     65   if(window==top) {
     66     document.writeln('<A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>');
     67   }
     68   //-->
     69 </SCRIPT>
     70 <NOSCRIPT>
     71   <A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>
     72 </NOSCRIPT>
     73 
     74 
     75 </FONT></TD>
     76 </TR>
     77 <TR>
     78 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
     79   SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
     80 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
     81 DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
     82 </TR>
     83 </TABLE>
     84 <A NAME="skip-navbar_top"></A>
     85 <!-- ========= END OF TOP NAVBAR ========= -->
     86 
     87 <HR>
     88 <!-- ======== START OF CLASS DATA ======== -->
     89 <H2>
     90 <FONT SIZE="-1">
     91 com.google.common.io</FONT>
     92 <BR>
     93 Class Closeables</H2>
     94 <PRE>
     95 <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">java.lang.Object</A>
     96   <IMG SRC="../../../../resources/inherit.gif" ALT="extended by "><B>com.google.common.io.Closeables</B>
     97 </PRE>
     98 <HR>
     99 <DL>
    100 <DT><PRE>public final class <B>Closeables</B><DT>extends <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">Object</A></DL>
    101 </PRE>
    102 
    103 <P>
    104 Utility methods for working with <A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io"><CODE>Closeable</CODE></A> objects.
    105 <P>
    106 
    107 <P>
    108 <DL>
    109 <DT><B>Since:</B></DT>
    110   <DD>2009.09.15 <b>tentative</b></DD>
    111 <DT><B>Author:</B></DT>
    112   <DD>Michael Lancaster</DD>
    113 </DL>
    114 <HR>
    115 
    116 <P>
    117 
    118 <!-- ========== METHOD SUMMARY =========== -->
    119 
    120 <A NAME="method_summary"><!-- --></A>
    121 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
    122 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
    123 <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
    124 <B>Method Summary</B></FONT></TH>
    125 </TR>
    126 <TR BGCOLOR="white" CLASS="TableRowColor">
    127 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
    128 <CODE>static&nbsp;void</CODE></FONT></TD>
    129 <TD><CODE><B><A HREF="../../../../com/google/common/io/Closeables.html#close(java.io.Closeable, boolean)">close</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io">Closeable</A>&nbsp;closeable,
    130       boolean&nbsp;swallowIOException)</CODE>
    131 
    132 <BR>
    133 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Closes a <A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io"><CODE>Closeable</CODE></A>, with control over whether an
    134  <code>IOException</code> may be thrown.</TD>
    135 </TR>
    136 <TR BGCOLOR="white" CLASS="TableRowColor">
    137 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
    138 <CODE>static&nbsp;void</CODE></FONT></TD>
    139 <TD><CODE><B><A HREF="../../../../com/google/common/io/Closeables.html#closeQuietly(java.io.Closeable)">closeQuietly</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io">Closeable</A>&nbsp;closeable)</CODE>
    140 
    141 <BR>
    142 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Equivalent to calling <code>close(closeable, true)</code>, but with no
    143  IOException in the signature.</TD>
    144 </TR>
    145 </TABLE>
    146 &nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
    147 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
    148 <TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
    149 <TH ALIGN="left"><B>Methods inherited from class java.lang.<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">Object</A></B></TH>
    150 </TR>
    151 <TR BGCOLOR="white" CLASS="TableRowColor">
    152 <TD><CODE><A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#clone()" title="class or interface in java.lang">clone</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#equals(java.lang.Object)" title="class or interface in java.lang">equals</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#finalize()" title="class or interface in java.lang">finalize</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#getClass()" title="class or interface in java.lang">getClass</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#hashCode()" title="class or interface in java.lang">hashCode</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#notify()" title="class or interface in java.lang">notify</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#notifyAll()" title="class or interface in java.lang">notifyAll</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#toString()" title="class or interface in java.lang">toString</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#wait()" title="class or interface in java.lang">wait</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#wait(long)" title="class or interface in java.lang">wait</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html?is-external=true#wait(long, int)" title="class or interface in java.lang">wait</A></CODE></TD>
    153 </TR>
    154 </TABLE>
    155 &nbsp;
    156 <P>
    157 
    158 <!-- ============ METHOD DETAIL ========== -->
    159 
    160 <A NAME="method_detail"><!-- --></A>
    161 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
    162 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
    163 <TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
    164 <B>Method Detail</B></FONT></TH>
    165 </TR>
    166 </TABLE>
    167 
    168 <A NAME="close(java.io.Closeable, boolean)"><!-- --></A><H3>
    169 close</H3>
    170 <PRE>
    171 public static void <B>close</B>(<FONT SIZE="-1"><A HREF="http://jsr-305.googlecode.com/svn/trunk/javadoc/javax/annotation/Nullable.html?is-external=true" title="class or interface in javax.annotation">@Nullable</A></FONT>
    172                          <A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io">Closeable</A>&nbsp;closeable,
    173                          boolean&nbsp;swallowIOException)
    174                   throws <A HREF="http://java.sun.com/javase/6/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</A></PRE>
    175 <DL>
    176 <DD>Closes a <A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io"><CODE>Closeable</CODE></A>, with control over whether an
    177  <code>IOException</code> may be thrown. This is primarily useful in a
    178  finally block, where a thrown exception needs to be logged but not
    179  propagated (otherwise the original exception will be lost).
    180 
    181  <p>If <code>swallowIOException</code> is true then we never throw
    182  <code>IOException</code> but merely log it.
    183 
    184  <p>Example:
    185 
    186  <p><pre>public void useStreamNicely() throws IOException {
    187  SomeStream stream = new SomeStream("foo");
    188  boolean threw = true;
    189  try {
    190    // Some code which does something with the Stream. May throw a
    191    // Throwable.
    192    threw = false; // No throwable thrown.
    193  } finally {
    194    // Close the stream.
    195    // If an exception occurs, only rethrow it if (threw==false).
    196    Closeables.close(stream, threw);
    197  }
    198  </pre>
    199 <P>
    200 <DD><DL>
    201 <DT><B>Parameters:</B><DD><CODE>closeable</CODE> - the <code>Closeable</code> object to be closed, or null,
    202      in which case this method does nothing<DD><CODE>swallowIOException</CODE> - if true, don't propagate IO exceptions
    203      thrown by the <code>close</code> methods
    204 <DT><B>Throws:</B>
    205 <DD><CODE><A HREF="http://java.sun.com/javase/6/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</A></CODE> - if <code>swallowIOException</code> is false and
    206      <code>close</code> throws an <code>IOException</code>.</DL>
    207 </DD>
    208 </DL>
    209 <HR>
    210 
    211 <A NAME="closeQuietly(java.io.Closeable)"><!-- --></A><H3>
    212 closeQuietly</H3>
    213 <PRE>
    214 public static void <B>closeQuietly</B>(<FONT SIZE="-1"><A HREF="http://jsr-305.googlecode.com/svn/trunk/javadoc/javax/annotation/Nullable.html?is-external=true" title="class or interface in javax.annotation">@Nullable</A></FONT>
    215                                 <A HREF="http://java.sun.com/javase/6/docs/api/java/io/Closeable.html?is-external=true" title="class or interface in java.io">Closeable</A>&nbsp;closeable)</PRE>
    216 <DL>
    217 <DD>Equivalent to calling <code>close(closeable, true)</code>, but with no
    218  IOException in the signature.
    219 <P>
    220 <DD><DL>
    221 <DT><B>Parameters:</B><DD><CODE>closeable</CODE> - the <code>Closeable</code> object to be closed, or null, in
    222       which case this method does nothing</DL>
    223 </DD>
    224 </DL>
    225 <!-- ========= END OF CLASS DATA ========= -->
    226 <HR>
    227 
    228 
    229 <!-- ======= START OF BOTTOM NAVBAR ====== -->
    230 <A NAME="navbar_bottom"><!-- --></A>
    231 <A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
    232 <TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
    233 <TR>
    234 <TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
    235 <A NAME="navbar_bottom_firstrow"><!-- --></A>
    236 <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
    237   <TR ALIGN="center" VALIGN="top">
    238   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
    239   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
    240   <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
    241   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/Closeables.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
    242   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
    243   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
    244   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
    245   <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
    246   </TR>
    247 </TABLE>
    248 </TD>
    249 <TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
    250 </EM>
    251 </TD>
    252 </TR>
    253 
    254 <TR>
    255 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
    256 &nbsp;<A HREF="../../../../com/google/common/io/CharStreams.html" title="class in com.google.common.io"><B>PREV CLASS</B></A>&nbsp;
    257 &nbsp;<A HREF="../../../../com/google/common/io/CountingInputStream.html" title="class in com.google.common.io"><B>NEXT CLASS</B></A></FONT></TD>
    258 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
    259   <A HREF="../../../../index.html?com/google/common/io/Closeables.html" target="_top"><B>FRAMES</B></A>  &nbsp;
    260 &nbsp;<A HREF="Closeables.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
    261 &nbsp;<SCRIPT type="text/javascript">
    262   <!--
    263   if(window==top) {
    264     document.writeln('<A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>');
    265   }
    266   //-->
    267 </SCRIPT>
    268 <NOSCRIPT>
    269   <A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>
    270 </NOSCRIPT>
    271 
    272 
    273 </FONT></TD>
    274 </TR>
    275 <TR>
    276 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
    277   SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
    278 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
    279 DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
    280 </TR>
    281 </TABLE>
    282 <A NAME="skip-navbar_bottom"></A>
    283 <!-- ======== END OF BOTTOM NAVBAR ======= -->
    284 
    285 <HR>
    286 
    287 </BODY>
    288 </HTML>
    289