Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #760
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: TeeOutputStream? |
| References | <YVzGp.501$_I7.309@newsfe08.iad> <db77bbe4-6a43-4c1d-8929-8a637a599eb6@m10g2000yqd.googlegroups.com> |
| Message-ID | <CkTHp.40485$Vp.35212@newsfe14.iad> (permalink) |
| Organization | NewsDemon |
| Date | 2011-06-08 16:11 -0700 |
On 06/08/2011 03:31 AM, Henk van Voorthuijsen wrote:
> On Jun 5, 2:17 am, Knute Johnson<nos...@knutejohnson.com> wrote:
>> I'm working on a project where I need to send the same data to two
>> OutputStreams so I went snooping around on the net for some code to
>> snatch. Most of the examples I found had what I think is a significant
>> flaw. That is if for example the write() method looks like this;
>>
>> public void write(byte b) throws IOException {
>> os1.write(b);
>> os2.write(b);
>>
>> }
>>
>> if OutputStream os1 throws an IOException, no attempt to write the data
>> to os2 will happen. This probably isn't too big a concern for writes
>> but what about close()?
>>
>> public void close() throws IOException {
>> os1.close();
>> os2.close();
>>
>> }
>>
>> If os1 throws an IOException on the close() method, then os2's close()
>> method will never get executed possibly leaving the stream open and the
>> attached resources still around. What about;
>>
>> public void close() throws IOException {
>> try {
>> os1.close();
>> } finally {
>> os2.close();
>> }
>>
>> }
>>
>> This guarantees that both close() methods will get called. If both
>> methods throw IOExceptions the caller will never see the first one as it
>> will be hidden.
>>
>> What do you think? Have I completely over thought this?
>>
>
> You might want to wait for JDK 7's "try with resources" feature. All
> your concerns have been addressed there - im much more detail.
>
Java 7 is a long way off and I'm writing code today. That said, I do
like to use the latest stuff.
--
Knute Johnson
s/knute/nospam/
Back to comp.lang.java.help | Previous | Next — Previous in thread | Find similar
TeeOutputStream? Knute Johnson <nospam@knutejohnson.com> - 2011-06-04 17:17 -0700
Re: TeeOutputStream? Roedy Green <see_website@mindprod.com.invalid> - 2011-06-04 22:18 -0700
Re: TeeOutputStream? Stanimir Stamenkov <s7an10@netscape.net> - 2011-06-05 10:44 +0300
Re: TeeOutputStream? rossum <rossum48@coldmail.com> - 2011-06-05 12:23 +0100
Re: TeeOutputStream? Stanimir Stamenkov <s7an10@netscape.net> - 2011-06-06 01:23 +0300
Re: TeeOutputStream? Patricia Shanahan <pats@acm.org> - 2011-06-05 16:55 -0700
Re: TeeOutputStream? Stanimir Stamenkov <s7an10@netscape.net> - 2011-06-07 00:45 +0300
Re: TeeOutputStream? rossum <rossum48@coldmail.com> - 2011-06-06 15:16 +0100
Re: TeeOutputStream? markspace <-@.> - 2011-06-06 14:08 -0700
Re: TeeOutputStream? Henk van Voorthuijsen <voorth@gmail.com> - 2011-06-08 03:31 -0700
Re: TeeOutputStream? Knute Johnson <nospam@knutejohnson.com> - 2011-06-08 16:11 -0700
csiph-web