raii java... apparatus ordering always ugly?
i only played java record component api, came down following function, used duplicate binary files. uncanny source came web, nonetheless i total try/catch/finally clauses certain that, should something wrong happen, aegis streams closed (and thus, os ressources freed) before quiting function.
i embellished down duty arrangement pattern:
public stationary vacant copyfile(fileoutputstream odstream, fileinputstream osstream) pitch etc...
{
bufferedinputstream osbuffer = new bufferedinputstream(osstream, 4096);
bufferedoutputstream odbuffer = new bufferedoutputstream(odstream, 4096);
try
{
try
{
int c;
while((c = osbuffer.read()) != -1) // pitch ioexception
{
odbuffer.write(c); // pitch ioexception
}
}
finally
{
odbuffer.close(); // pitch ioexception
}
}
finally
{
osbuffer.close(); // pitch ioexception
}
}
as distant i know it, i can't put twin close()
finally apportionment since initial close()
good throw, then, second executed.
i know c# dispose settlement have burnished using
keyword.
i even know improved c++ formula have something (using java-like api):
void copyfile(fileoutputstream & odstream, fileinputstream & osstream)
{
bufferedinputstream osbuffer(osstream, 4096);
bufferedoutputstream odbuffer(odstream, 4096);
int c;
while((c = osbuffer.read()) != -1) // pitch ioexception
{
odbuffer.write(c); // pitch ioexception
}
// i don't caring resources, raii hoop me
}
i am blank something, i unequivocally have furnish dizzy judicial formula java only hoop exceptions close()
slight buffered stream?
(please, tell me i'm wrong somewhere...)
edit: me, updating page, i saw both doubt answers decreased indicate integrate minutes? someone enjoying himself too many while remaning anonymous?
edit 2: mcdowell offering unequivocally engaging couple i felt i plead here:
edit 3: following mcdowell's link, i tumbled on offer java 7 settlement identical c# controlling pattern: . problem definitely described. apparently, even java 7 do
, problems remain.
Comments
Post a Comment