Re: [io] copyAndClose

2005-02-07 Thread Stephen Colebourne
From: B. K. Oxley (binkley) [EMAIL PROTECTED]
I do not understand.  What is being copied in the finally block?  Should 
that have been:

finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
Oops ;-) Yes
I do not want to swallow exceptions thrown by closing in or out as they 
might indicate to the caller some kind of important failure (say a network 
error).  That is why my original has nexted try/finally blocks:

finally {
try {
out.close();
} finally {
in.close();
}
}
Oh. This is quite dangerous code as if both the closing of out and in throw 
an exception only the exception from in is actually thrown.

The question then becomes whether the method to add to IOUtils is 
copyAndClose() or copyAndCloseQuietly()

I wonder if any other committer has a view.
Were you interested in writing the patch and tests for IOUtils?
Do I just pull IO from SVN?  What form of patch: unified diff ok?
Yes x2, but wait until we see if someone else comments on the thread
Stephen
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [io] copyAndClose

2005-02-06 Thread Stephen Colebourne
Well, for a start your example could be simplified.
public static void copyAndClose(final InputStream in,
final OutputStream out)
throws IOException {
try {
CopyUtils.copy(in, out);
out.flush();
} finally {
  IOUtils.copyAndClose(in);
  IOUtils.copyAndClose(out);
}
}
Even so, this is quite a good suggestion. The main downside is that it 
doubles the number of methods. (CopyUtils is being deprecated, with the 
methods moving to IOUtils with better semantics and names to simplify the 
overall API)

Were you interested in writing the patch and tests for IOUtils?
Stephen
- Original Message - 
From: B. K. Oxley (binkley) [EMAIL PROTECTED]
It would be nice to include utility methods such as this in the io library 
to compliment the good set of primitives already there.

The method in question with Javadoc added:
/**
 * Copies varin/var to varout/var, flushing varout/var
 * and closing both streams even in the face of exceptions.  This is
 * usually sufficient to ensure proper copying for arbitrary streams
 * and avoid data lose (citee.g./cite, file streams can lose
 * data if you do not flush them before closing).
 * p/
 * If more than one operation throws an exception, propagates only
 * the most recent exception.  Java swallows earlier exceptions.
 *
 * @param in the input stream data source
 * @param out the output stream data sink
 *
 * @throws IOException if any I/O operation fails
 */
public static void copyAndClose(final InputStream in,
final OutputStream out)
throws IOException {
try {
CopyUtils.copy(in, out);
out.flush();
} finally {
try {
out.close();
} finally {
in.close();
}
}
}
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [io] copyAndClose

2005-02-06 Thread B. K. Oxley (binkley)
Stephen Colebourne wrote:
public static void copyAndClose(final InputStream in,
final OutputStream out)
throws IOException {
try {
CopyUtils.copy(in, out);
out.flush();
} finally {
  IOUtils.copyAndClose(in);
  IOUtils.copyAndClose(out);
}
}
I do not understand.  What is being copied in the finally block?  Should 
that have been:

finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
I do not want to swallow exceptions thrown by closing in or out as they 
might indicate to the caller some kind of important failure (say a 
network error).  That is why my original has nexted try/finally blocks:

finally {
try {
out.close();
} finally {
in.close();
}
}
Even so, this is quite a good suggestion. The main downside is that it 
doubles the number of methods. (CopyUtils is being deprecated, with the 
methods moving to IOUtils with better semantics and names to simplify 
the overall API)

Were you interested in writing the patch and tests for IOUtils?
Do I just pull IO from SVN?  What form of patch: unified diff ok?
Cheers,
--binkley
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


[io] copyAndClose

2005-02-04 Thread B. K. Oxley (binkley)
I note in my blog:
http://binkley.blogspot.com/2005/02/methods-i-cannot-do-without.html
It would be nice to include utility methods such as this in the io 
library to compliment the good set of primitives already there.

The method in question with Javadoc added:
/**
 * Copies varin/var to varout/var, flushing varout/var
 * and closing both streams even in the face of exceptions.  This is
 * usually sufficient to ensure proper copying for arbitrary streams
 * and avoid data lose (citee.g./cite, file streams can lose
 * data if you do not flush them before closing).
 * p/
 * If more than one operation throws an exception, propagates only
 * the most recent exception.  Java swallows earlier exceptions.
 *
 * @param in the input stream data source
 * @param out the output stream data sink
 *
 * @throws IOException if any I/O operation fails
 */
public static void copyAndClose(final InputStream in,
final OutputStream out)
throws IOException {
try {
CopyUtils.copy(in, out);
out.flush();
} finally {
try {
out.close();
} finally {
in.close();
}
}
}
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]