From: Stefan Bund Date: Tue, 21 Sep 2010 16:00:05 +0000 (+0200) Subject: Implement Data utility to encapsulate resource management X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=5ac05364dc652686046f01849b810da6ffef1192;p=jpim.git Implement Data utility to encapsulate resource management --- diff --git a/build.xml b/build.xml index 99ba17a..ecc9433 100644 --- a/build.xml +++ b/build.xml @@ -35,7 +35,7 @@ - + diff --git a/src/de/j32/pimstuff/Data.java b/src/de/j32/pimstuff/Data.java new file mode 100644 index 0000000..5df5e94 --- /dev/null +++ b/src/de/j32/pimstuff/Data.java @@ -0,0 +1,51 @@ +package de.j32.pimstuff; + +import java.io.IOException; + +import de.j32.pimstuff.conduit.Conduit; +import de.j32.pimstuff.conduit.ConduitException; +import de.j32.pimstuff.conduit.Exporter; +import de.j32.pimstuff.conduit.Importer; +import de.j32.pimstuff.data.EntryConsumer; +import de.j32.pimstuff.data.EntryProducer; +import de.j32.util.Util; + +public class Data +{ + public static void transfer(EntryProducer producer, EntryConsumer consumer) + { + // TODO: It would be more 'beautoful' to have the EntryProducer be an + // Iterable, but this is simpler ... + producer.sendTo(consumer); + } + + public static void transfer(Conduit conduit, EntryConsumer consumer) throws ConduitException, + IOException + { + Importer importer = null; + try { + importer = conduit.importer(); + transfer(importer, consumer); + importer.close(); + importer = null; + } + finally { + Util.nothrowClose(importer); + } + } + + public static void transfer(EntryProducer producer, Conduit conduit) throws ConduitException, + IOException + { + Exporter exporter = null; + try { + exporter = conduit.exporter(); + transfer(producer, exporter); + exporter.close(); + exporter = null; + } + finally { + Util.nothrowClose(exporter); + } + } +} diff --git a/src/de/j32/pimstuff/Main.java b/src/de/j32/pimstuff/Main.java index 733b6f3..6214c7f 100644 --- a/src/de/j32/pimstuff/Main.java +++ b/src/de/j32/pimstuff/Main.java @@ -4,11 +4,8 @@ import java.io.IOException; import de.j32.pimstuff.conduit.Conduit; import de.j32.pimstuff.conduit.ConduitException; -import de.j32.pimstuff.conduit.Exporter; -import de.j32.pimstuff.conduit.Importer; import de.j32.pimstuff.conduit.Registry; import de.j32.pimstuff.data.Addressbook; -import de.j32.util.Util; public class Main { @@ -16,30 +13,11 @@ public class Main public static void main(String[] args) { try { - Addressbook ab = new Addressbook(); + Addressbook abook = new Addressbook(); Conduit conduit = Registry.get("fritzbox"); - Importer i = null; - try { - i = conduit.importer(); - i.sendTo(ab); - i.close(); - i = null; - } - finally { - Util.nothrowClose(i); - } - - Exporter e = null; - try { - e = conduit.exporter(); - ab.sendTo(e); - e.close(); - e = null; - } - finally { - Util.nothrowClose(e); - } + Data.transfer(conduit, abook); + Data.transfer(abook, conduit); } catch (ConduitException e) { e.printStackTrace();