mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s..@apache.org
Subject svn commit: r1131350 [2/3] - in /mahout/trunk: core/src/main/java/org/apache/mahout/cf/taste/impl/common/jdbc/ core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/ core/src/main/java/org/apache/mahout/classifier/ core/src/main/java/org/apache...
Date Sat, 04 Jun 2011 08:43:01 GMT
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesClassifierSelfTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesClassifierSelfTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesClassifierSelfTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesClassifierSelfTest.java Sat Jun  4 08:42:57 2011
@@ -22,6 +22,7 @@ import java.io.File;
 import java.util.List;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -51,10 +52,13 @@ public final class BayesClassifierSelfTe
 
     File tempInputFile = getTestTempFile("bayesinput");
     BufferedWriter writer = Files.newWriter(tempInputFile, Charsets.UTF_8);
-    for (String[] entry : ClassifierData.DATA) {
-      writer.write(entry[0] + '\t' + entry[1] + '\n');
+    try {
+      for (String[] entry : ClassifierData.DATA) {
+        writer.write(entry[0] + '\t' + entry[1] + '\n');
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
 
     Path input = getTestTempFilePath("bayesinput");
     Configuration conf = new Configuration();

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesFileFormatterTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesFileFormatterTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesFileFormatterTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/classifier/bayes/BayesFileFormatterTest.java Sat Jun  4 08:42:57 2011
@@ -22,6 +22,7 @@ import java.io.Writer;
 import java.util.Iterator;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.WhitespaceAnalyzer;
@@ -48,8 +49,11 @@ public final class BayesFileFormatterTes
     for (String word : WORDS) {
       File file = new File(input, word);
       Writer writer = Files.newWriter(file, Charsets.UTF_8);
+      try {
       writer.write(word);
-      writer.close();
+      } finally {
+        Closeables.closeQuietly(writer);
+      }
     }
   }
 

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/ModelSerializerTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/ModelSerializerTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/ModelSerializerTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/ModelSerializerTest.java Sat Jun  4 08:42:57 2011
@@ -41,8 +41,11 @@ public final class ModelSerializerTest e
   private static <T extends Writable> T roundTrip(T m, Class<T> clazz) throws IOException {
     ByteArrayOutputStream buf = new ByteArrayOutputStream(1000);
     DataOutputStream dos = new DataOutputStream(buf);
-    PolymorphicWritable.write(dos, m);
-    dos.close();
+    try {
+      PolymorphicWritable.write(dos, m);
+    } finally {
+      dos.close();
+    }
 
     return PolymorphicWritable.read(new DataInputStream(new ByteArrayInputStream(buf.toByteArray())), clazz);
   }

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/ClusteringTestUtils.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/ClusteringTestUtils.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/ClusteringTestUtils.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/ClusteringTestUtils.java Sat Jun  4 08:42:57 2011
@@ -17,6 +17,7 @@
 
 package org.apache.mahout.clustering;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -50,10 +51,13 @@ public final class ClusteringTestUtils {
                                                          intWritable ? IntWritable.class : LongWritable.class,
                                                          VectorWritable.class);
     int recNum = 0;
-    for (VectorWritable point : points) {
-      writer.append(intWritable ? new IntWritable(recNum++) : new LongWritable(recNum++), point);
+    try {
+      for (VectorWritable point : points) {
+        writer.append(intWritable ? new IntWritable(recNum++) : new LongWritable(recNum++), point);
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
   }
 
 }

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/TestClusterClassifier.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/TestClusterClassifier.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/TestClusterClassifier.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/TestClusterClassifier.java Sat Jun  4 08:42:57 2011
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import com.google.common.io.Closeables;
 import org.apache.commons.lang.NotImplementedException;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -102,8 +103,11 @@ public final class TestClusterClassifier
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, config, path,
         Text.class, ClusterClassifier.class);
     Writable key = new Text("test");
-    writer.append(key, classifier);
-    writer.close();
+    try {
+      writer.append(key, classifier);
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
   }
   
   private static ClusterClassifier readClassifier(Configuration config,
@@ -112,8 +116,11 @@ public final class TestClusterClassifier
     SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, config);
     Writable key = new Text();
     ClusterClassifier classifierOut = new ClusterClassifier();
-    reader.next(key, classifierOut);
-    reader.close();
+    try {
+      reader.next(key, classifierOut);
+    } finally {
+      Closeables.closeQuietly(reader);
+    }
     return classifierOut;
   }
   

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/TestCanopyCreation.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/TestCanopyCreation.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/TestCanopyCreation.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/TestCanopyCreation.java Sat Jun  4 08:42:57 2011
@@ -24,6 +24,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Map.Entry;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -332,20 +333,23 @@ public final class TestCanopyCreation ex
     Path path = new Path(output, "clusters-0/part-r-00000");
     FileSystem fs = FileSystem.get(path.toUri(), config);
     SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, config);
-    Writable key = new Text();
-    Canopy canopy = new Canopy();
-    assertTrue("more to come", reader.next(key, canopy));
-    assertEquals("1st key", "C-0", key.toString());
-    assertEquals("1st x value", 1.5, canopy.getCenter().get(0), EPSILON);
-    assertEquals("1st y value", 1.5, canopy.getCenter().get(1), EPSILON);
-    assertTrue("more to come", reader.next(key, canopy));
-    assertEquals("2nd key", "C-1", key.toString());
-    assertEquals("2nd x value", 4.333333333333334, canopy.getCenter().get(0),
-        EPSILON);
-    assertEquals("2nd y value", 4.333333333333334, canopy.getCenter().get(1),
-        EPSILON);
-    assertFalse("more to come", reader.next(key, canopy));
-    reader.close();
+    try {
+      Writable key = new Text();
+      Canopy canopy = new Canopy();
+      assertTrue("more to come", reader.next(key, canopy));
+      assertEquals("1st key", "C-0", key.toString());
+      assertEquals("1st x value", 1.5, canopy.getCenter().get(0), EPSILON);
+      assertEquals("1st y value", 1.5, canopy.getCenter().get(1), EPSILON);
+      assertTrue("more to come", reader.next(key, canopy));
+      assertEquals("2nd key", "C-1", key.toString());
+      assertEquals("2nd x value", 4.333333333333334, canopy.getCenter().get(0),
+          EPSILON);
+      assertEquals("2nd y value", 4.333333333333334, canopy.getCenter().get(1),
+          EPSILON);
+      assertFalse("more to come", reader.next(key, canopy));
+    } finally {
+      Closeables.closeQuietly(reader);
+    }
   }
 
   /**
@@ -369,20 +373,23 @@ public final class TestCanopyCreation ex
     Path path = new Path(output, "clusters-0/part-r-00000");
     FileSystem fs = FileSystem.get(path.toUri(), config);
     SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, config);
-    Writable key = new Text();
-    Canopy value = new Canopy();
-    assertTrue("more to come", reader.next(key, value));
-    assertEquals("1st key", "C-0", key.toString());
-    assertEquals("1st x value", 1.8, value.getCenter().get(0), EPSILON);
-    assertEquals("1st y value", 1.8, value.getCenter().get(1), EPSILON);
-    assertTrue("more to come", reader.next(key, value));
-    assertEquals("2nd key", "C-1", key.toString());
-    assertEquals("2nd x value", 4.433333333333334, value.getCenter().get(0),
-        EPSILON);
-    assertEquals("2nd y value", 4.433333333333334, value.getCenter().get(1),
-        EPSILON);
-    assertFalse("more to come", reader.next(key, value));
-    reader.close();
+    try {
+      Writable key = new Text();
+      Canopy value = new Canopy();
+      assertTrue("more to come", reader.next(key, value));
+      assertEquals("1st key", "C-0", key.toString());
+      assertEquals("1st x value", 1.8, value.getCenter().get(0), EPSILON);
+      assertEquals("1st y value", 1.8, value.getCenter().get(1), EPSILON);
+      assertTrue("more to come", reader.next(key, value));
+      assertEquals("2nd key", "C-1", key.toString());
+      assertEquals("2nd x value", 4.433333333333334, value.getCenter().get(0),
+          EPSILON);
+      assertEquals("2nd y value", 4.433333333333334, value.getCenter().get(1),
+          EPSILON);
+      assertFalse("more to come", reader.next(key, value));
+    } finally {
+      Closeables.closeQuietly(reader);
+    }
   }
 
   /**
@@ -610,22 +617,25 @@ public final class TestCanopyCreation ex
     Path path = new Path(output, "clusters-0/part-r-00000");
     FileSystem fs = FileSystem.get(path.toUri(), job);
     SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, job);
-    Writable key = new Text();
-    Canopy value = new Canopy();
-    assertTrue("more to come", reader.next(key, value));
-    assertEquals("1st key", "C-0", key.toString());
-
-    assertEquals("1st x value", 1.5, value.getCenter().get(0), EPSILON);
-    assertEquals("1st y value", 1.5, value.getCenter().get(1), EPSILON);
-    assertTrue("more to come", reader.next(key, value));
-    assertEquals("2nd key", "C-1", key.toString());
-
-    assertEquals("1st x value", 4.333333333333334, value.getCenter().get(0),
-        EPSILON);
-    assertEquals("1st y value", 4.333333333333334, value.getCenter().get(1),
-        EPSILON);
-    assertFalse("more to come", reader.next(key, value));
-    reader.close();
+    try {
+      Writable key = new Text();
+      Canopy value = new Canopy();
+      assertTrue("more to come", reader.next(key, value));
+      assertEquals("1st key", "C-0", key.toString());
+
+      assertEquals("1st x value", 1.5, value.getCenter().get(0), EPSILON);
+      assertEquals("1st y value", 1.5, value.getCenter().get(1), EPSILON);
+      assertTrue("more to come", reader.next(key, value));
+      assertEquals("2nd key", "C-1", key.toString());
+
+      assertEquals("1st x value", 4.333333333333334, value.getCenter().get(0),
+          EPSILON);
+      assertEquals("1st y value", 4.333333333333334, value.getCenter().get(1),
+          EPSILON);
+      assertFalse("more to come", reader.next(key, value));
+    } finally {
+      Closeables.closeQuietly(reader);
+    }
   }
 
   /**

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java Sat Jun  4 08:42:57 2011
@@ -22,13 +22,11 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.io.DataInputBuffer;
-import org.apache.hadoop.io.DataOutputBuffer;
 import org.apache.hadoop.io.Text;
-import org.apache.hadoop.io.Writable;
 import org.apache.hadoop.io.WritableComparable;
 import org.apache.hadoop.mapreduce.Mapper;
 import org.apache.hadoop.mapreduce.RecordWriter;
@@ -40,7 +38,6 @@ import org.apache.mahout.clustering.Clus
 import org.apache.mahout.clustering.Model;
 import org.apache.mahout.clustering.dirichlet.models.DistanceMeasureClusterDistribution;
 import org.apache.mahout.clustering.dirichlet.models.DistributionDescription;
-import org.apache.mahout.clustering.dirichlet.models.GaussianCluster;
 import org.apache.mahout.clustering.dirichlet.models.GaussianClusterDistribution;
 import org.apache.mahout.common.DummyRecordWriter;
 import org.apache.mahout.common.MahoutTestCase;
@@ -380,7 +377,7 @@ public final class TestMapReduce extends
     try {
       inverseCovarianceMatrix.write(out);
     } finally {
-      out.close();
+      Closeables.closeQuietly(out);
     }
 
     Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
@@ -391,7 +388,7 @@ public final class TestMapReduce extends
     try {
       meanVectorWritable.write(out);
     } finally {
-      out.close();
+      Closeables.closeQuietly(out);
     }
 
     conf.set("MahalanobisDistanceMeasure.maxtrixClass", MatrixWritable.class.getName());
@@ -453,7 +450,7 @@ public final class TestMapReduce extends
     try {
       inverseCovarianceMatrix.write(out);
     } finally {
-      out.close();
+      Closeables.closeQuietly(out);
     }
 
     Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
@@ -464,7 +461,7 @@ public final class TestMapReduce extends
     try {
       meanVectorWritable.write(out);
     } finally {
-      out.close();
+      Closeables.closeQuietly(out);
     }
 
     conf.set("MahalanobisDistanceMeasure.maxtrixClass", MatrixWritable.class.getName());

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -193,19 +194,18 @@ public final class TestFuzzyKmeansCluste
                                                            new Path(clustersPath, "part-00000"),
                                                            Text.class,
                                                            SoftCluster.class);
-      for (int i = 0; i < k + 1; i++) {
-        Vector vec = tweakValue(points.get(i).get());
-
-        SoftCluster cluster = new SoftCluster(vec, i, measure);
-        // add the center so the centroid will be correct upon output
-        cluster.observe(cluster.getCenter(), 1);
-        /*
-         * writer.write(cluster.getIdentifier() + '\t' + SoftCluster.formatCluster(cluster) + '\n');
-         */
-        writer.append(new Text(cluster.getIdentifier()), cluster);
-
+      try {
+        for (int i = 0; i < k + 1; i++) {
+          Vector vec = tweakValue(points.get(i).get());
+          SoftCluster cluster = new SoftCluster(vec, i, measure);
+          /* add the center so the centroid will be correct upon output */
+          cluster.observe(cluster.getCenter(), 1);
+          // writer.write(cluster.getIdentifier() + '\t' + SoftCluster.formatCluster(cluster) + '\n');
+          writer.append(new Text(cluster.getIdentifier()), cluster);
+        }
+      } finally {
+        Closeables.closeQuietly(writer);
       }
-      writer.close();
 
       // now run the Job using the run() command line options.
       Path output = getTestTempDirPath("output");
@@ -265,19 +265,20 @@ public final class TestFuzzyKmeansCluste
                                                            new Path(clustersPath, "part-00000"),
                                                            Text.class,
                                                            SoftCluster.class);
-      for (int i = 0; i < k + 1; i++) {
-        Vector vec = tweakValue(points.get(i).get());
-
-        SoftCluster cluster = new SoftCluster(vec, i, measure);
-        // add the center so the centroid will be correct upon output
-        cluster.observe(cluster.getCenter(), 1);
-        /*
-         * writer.write(cluster.getIdentifier() + '\t' + SoftCluster.formatCluster(cluster) + '\n');
-         */
-        writer.append(new Text(cluster.getIdentifier()), cluster);
+      try {
+        for (int i = 0; i < k + 1; i++) {
+          Vector vec = tweakValue(points.get(i).get());
+
+          SoftCluster cluster = new SoftCluster(vec, i, measure);
+          /* add the center so the centroid will be correct upon output */
+          cluster.observe(cluster.getCenter(), 1);
+          // writer.write(cluster.getIdentifier() + '\t' + SoftCluster.formatCluster(cluster) + '\n');
+          writer.append(new Text(cluster.getIdentifier()), cluster);
 
+        }
+      } finally {
+        Closeables.closeQuietly(writer);
       }
-      writer.close();
 
       // now run the Job using the run() command line options.
       Path output = getTestTempDirPath("output");

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java Sat Jun  4 08:42:57 2011
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -375,16 +376,18 @@ public final class TestKmeansClustering 
       Path path = new Path(clustersPath, "part-00000");
       FileSystem fs = FileSystem.get(path.toUri(), conf);
       SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, Text.class, Cluster.class);
-
-      for (int i = 0; i < k + 1; i++) {
-        Vector vec = points.get(i).get();
-
-        Cluster cluster = new Cluster(vec, i, measure);
-        // add the center so the centroid will be correct upon output
-        cluster.observe(cluster.getCenter(), 1);
-        writer.append(new Text(cluster.getIdentifier()), cluster);
+      try {
+        for (int i = 0; i < k + 1; i++) {
+          Vector vec = points.get(i).get();
+
+          Cluster cluster = new Cluster(vec, i, measure);
+          // add the center so the centroid will be correct upon output
+          cluster.observe(cluster.getCenter(), 1);
+          writer.append(new Text(cluster.getIdentifier()), cluster);
+        }
+      } finally {
+        Closeables.closeQuietly(writer);
       }
-      writer.close();
       // now run the Job
       Path outputPath = getTestTempDirPath("output");
       //KMeansDriver.runJob(pointsPath, clustersPath, outputPath, EuclideanDistanceMeasure.class.getName(), 0.001, 10, k + 1, true);
@@ -429,15 +432,18 @@ public final class TestKmeansClustering 
       FileSystem fs = FileSystem.get(path.toUri(), conf);
       SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, Text.class, Cluster.class);
 
-      for (int i = 0; i < k + 1; i++) {
-        Vector vec = points.get(i).get();
-
-        Cluster cluster = new Cluster(vec, i, measure);
-        // add the center so the centroid will be correct upon output
-        cluster.observe(cluster.getCenter(), 1);
-        writer.append(new Text(cluster.getIdentifier()), cluster);
+      try {
+        for (int i = 0; i < k + 1; i++) {
+          Vector vec = points.get(i).get();
+
+          Cluster cluster = new Cluster(vec, i, measure);
+          // add the center so the centroid will be correct upon output
+          cluster.observe(cluster.getCenter(), 1);
+          writer.append(new Text(cluster.getIdentifier()), cluster);
+        }
+      } finally {
+        Closeables.closeQuietly(writer);
       }
-      writer.close();
       // now run the Job
       Path outputPath = getTestTempDirPath("output");
       //KMeansDriver.runJob(pointsPath, clustersPath, outputPath, EuclideanDistanceMeasure.class.getName(), 0.001, 10, k + 1, true);

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/minhash/TestMinHashClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/minhash/TestMinHashClustering.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/minhash/TestMinHashClustering.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/minhash/TestMinHashClustering.java Sat Jun  4 08:42:57 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.mahout.clustering.minhash;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -71,11 +72,14 @@ public class TestMinHashClustering exten
     output = new Path(getTestTempDirPath(), "output");
     Path pointFile = new Path(input, "file1");
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, pointFile, Text.class, VectorWritable.class);
-    int id = 0;
-    for (VectorWritable point : points) {
-      writer.append(new Text("Id-" + id++), point);
+    try {
+      int id = 0;
+      for (VectorWritable point : points) {
+        writer.append(new Text("Id-" + id++), point);
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
   }
   
   private String[] makeArguments(int minClusterSize,

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/spectral/common/TestVectorCache.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/spectral/common/TestVectorCache.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/spectral/common/TestVectorCache.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/spectral/common/TestVectorCache.java Sat Jun  4 08:42:57 2011
@@ -19,6 +19,7 @@ package org.apache.mahout.clustering.spe
 
 import java.net.URI;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.filecache.DistributedCache;
 import org.apache.hadoop.fs.FileSystem;
@@ -51,11 +52,13 @@ public class TestVectorCache extends Mah
     // can we read it from here?
     SequenceFileValueIterator<VectorWritable> iterator =
         new SequenceFileValueIterator<VectorWritable>(path, true, conf);
-    VectorWritable old = iterator.next();
-    iterator.close();
-    
-    // test if the values are identical
-    assertEquals("Saved vector is identical to original", old.get(), value);
+    try {
+      VectorWritable old = iterator.next();
+      // test if the values are identical
+      assertEquals("Saved vector is identical to original", old.get(), value);
+    } finally {
+      Closeables.closeQuietly(iterator);
+    }
   }
   
   @Test
@@ -73,9 +76,12 @@ public class TestVectorCache extends Mah
     HadoopUtil.delete(conf, path);
     DistributedCache.setCacheFiles(new URI[] {path.toUri()}, conf);
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, IntWritable.class, VectorWritable.class);
-    writer.append(key, new VectorWritable(value));
-    writer.close();
-    
+    try {
+      writer.append(key, new VectorWritable(value));
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
+
     // load it
     Vector result = VectorCache.load(conf);
     

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/df/data/Utils.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/df/data/Utils.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/df/data/Utils.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/df/data/Utils.java Sat Jun  4 08:42:57 2011
@@ -24,6 +24,7 @@ import java.util.Arrays;
 import java.util.Random;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.hadoop.conf.Configuration;
@@ -252,9 +253,8 @@ public final class Utils {
         output.write(line);
         output.write('\n');
       }
-      output.flush();
     } finally {
-      output.close();
+      Closeables.closeQuietly(output);
     }
   
   }
@@ -286,7 +286,7 @@ public final class Utils {
     try {
       dataset.write(out);
     } finally {
-      out.close();
+      Closeables.closeQuietly(out);
     }
   
     return datasetPath;

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/InterResultsTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/InterResultsTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/InterResultsTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/InterResultsTest.java Sat Jun  4 08:42:57 2011
@@ -18,6 +18,7 @@ package org.apache.mahout.df.mapreduce.p
 
 import java.util.Random;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FSDataInputStream;
 import org.apache.hadoop.fs.FileSystem;
@@ -155,7 +156,7 @@ public final class InterResultsTest exte
         assertEquals("index: " + index, trees[index], value);
       }
     } finally {
-      in.close();
+      Closeables.closeQuietly(in);
     }
   }
 

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/PartialBuilderTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/PartialBuilderTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/PartialBuilderTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/df/mapreduce/partial/PartialBuilderTest.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Random;
 
+import com.google.common.io.Closeables;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -70,10 +71,13 @@ public final class PartialBuilderTest ex
     Writer writer = SequenceFile.createWriter(fs, conf, outputFile,
         TreeID.class, MapredOutput.class);
 
-    for (int index = 0; index < NUM_TREES; index++) {
-      writer.append(keys[index], values[index]);
+    try {
+      for (int index = 0; index < NUM_TREES; index++) {
+        writer.append(keys[index], values[index]);
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
 
     // load the output and make sure its valid
     TreeID[] newKeys = new TreeID[NUM_TREES];

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/FPGrowthTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/FPGrowthTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/FPGrowthTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/FPGrowthTest.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -61,6 +62,7 @@ public final class FPGrowthTest extends 
 
     SequenceFile.Writer writer =
         new SequenceFile.Writer(fs, conf, path, Text.class, TopKStringPatterns.class);
+    try {
     fp.generateTopKFrequentPatterns(
         transactions.iterator(),
         fp.generateFList(transactions.iterator(), 3),
@@ -69,7 +71,9 @@ public final class FPGrowthTest extends 
         new HashSet<String>(),
         new StringOutputConverter(new SequenceFileOutputCollector<Text,TopKStringPatterns>(writer)),
         new ContextStatusUpdater(null));
-    writer.close();
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
 
     List<Pair<String, TopKStringPatterns>> frequentPatterns = FPGrowth.readFrequentPattern(conf, path);
     assertEquals(
@@ -101,15 +105,18 @@ public final class FPGrowthTest extends 
     System.out.println(fp.generateFList(transactions.iterator(), 2));
     SequenceFile.Writer writer =
         new SequenceFile.Writer(fs, conf, path, Text.class, TopKStringPatterns.class);
-    fp.generateTopKFrequentPatterns(
-        transactions.iterator(),
-        fp.generateFList(transactions.iterator(), 2),
-        2,
-        100,
-        new HashSet<String>(),
-        new StringOutputConverter(new SequenceFileOutputCollector<Text,TopKStringPatterns>(writer)),
-        new ContextStatusUpdater(null));
-    writer.close();
+    try {
+      fp.generateTopKFrequentPatterns(
+          transactions.iterator(),
+          fp.generateFList(transactions.iterator(), 2),
+          2,
+          100,
+          new HashSet<String>(),
+          new StringOutputConverter(new SequenceFileOutputCollector<Text,TopKStringPatterns>(writer)),
+          new ContextStatusUpdater(null));
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
 
     List<Pair<String, TopKStringPatterns>> frequentPatterns = FPGrowth.readFrequentPattern(conf, path);
     assertEquals(
@@ -136,15 +143,18 @@ public final class FPGrowthTest extends 
     System.out.println(fp.generateFList(transactions.iterator(), 2));
     SequenceFile.Writer writer =
         new SequenceFile.Writer(fs, conf, path, Text.class, TopKStringPatterns.class);
-    fp.generateTopKFrequentPatterns(
-        transactions.iterator(),
-        fp.generateFList(transactions.iterator(), 2),
-        2,
-        100,
-        new HashSet<String>(),
-        new StringOutputConverter(new SequenceFileOutputCollector<Text,TopKStringPatterns>(writer)),
-        new ContextStatusUpdater(null));
-    writer.close();
+    try {
+      fp.generateTopKFrequentPatterns(
+          transactions.iterator(),
+          fp.generateFList(transactions.iterator(), 2),
+          2,
+          100,
+          new HashSet<String>(),
+          new StringOutputConverter(new SequenceFileOutputCollector<Text,TopKStringPatterns>(writer)),
+          new ContextStatusUpdater(null));
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
 
     List<Pair<String, TopKStringPatterns>> frequentPatterns = FPGrowth.readFrequentPattern(conf, path);
     assertEquals(

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthRetailDataTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthRetailDataTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthRetailDataTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthRetailDataTest.java Sat Jun  4 08:42:57 2011
@@ -30,6 +30,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.mahout.common.MahoutTestCase;
 import org.apache.mahout.common.Pair;
@@ -81,7 +82,7 @@ public class PFPGrowthRetailDataTest ext
       }
       
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
   }
   

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/fpm/pfpgrowth/PFPGrowthTest.java Sat Jun  4 08:42:57 2011
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Map;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.mahout.common.MahoutTestCase;
@@ -74,7 +75,7 @@ public final class PFPGrowthTest extends
         writer.write("\n");
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
     
   }

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/math/VectorWritableTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/math/VectorWritableTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/math/VectorWritableTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/math/VectorWritableTest.java Sat Jun  4 08:42:57 2011
@@ -17,6 +17,7 @@
 
 package org.apache.mahout.math;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.io.Writable;
 import org.junit.Test;
 
@@ -66,7 +67,7 @@ public final class VectorWritableTest ex
     try {
       toWrite.write(dos);
     } finally {
-      dos.close();
+      Closeables.closeQuietly(dos);
     }
 
     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
@@ -74,7 +75,7 @@ public final class VectorWritableTest ex
     try {
       toRead.readFields(dis);
     } finally {
-      dis.close();
+      Closeables.closeQuietly(dis);
     }
   }
 

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverDenseTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverDenseTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverDenseTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverDenseTest.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.util.Deque;
 import java.util.LinkedList;
 import java.util.Random;
 
+import com.google.common.io.Closeables;
 import junit.framework.Assert;
 
 import org.apache.hadoop.conf.Configuration;
@@ -95,7 +96,7 @@ public class LocalSSVDSolverDenseTest ex
       w.append(roww, vw);
     }
     closeables.remove(w);
-    w.close();
+    Closeables.closeQuietly(w);
 
     FileSystem fs = FileSystem.get(conf);
 

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverSparseSequentialTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverSparseSequentialTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverSparseSequentialTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/math/hadoop/stochasticsvd/LocalSSVDSolverSparseSequentialTest.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.util.Deque;
 import java.util.LinkedList;
 import java.util.Random;
 
+import com.google.common.io.Closeables;
 import junit.framework.Assert;
 
 import org.apache.hadoop.conf.Configuration;
@@ -93,7 +94,7 @@ public class LocalSSVDSolverSparseSequen
       w.append(roww, vw);
     }
     closeables.remove(w);
-    w.close();
+    Closeables.closeQuietly(w);
 
     FileSystem fs = FileSystem.get(conf);
 

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DictionaryVectorizerTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DictionaryVectorizerTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DictionaryVectorizerTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DictionaryVectorizerTest.java Sat Jun  4 08:42:57 2011
@@ -19,6 +19,7 @@ package org.apache.mahout.vectorizer;
 
 import java.io.IOException;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -57,12 +58,15 @@ public final class DictionaryVectorizerT
     inputPath = getTestTempFilePath("documents/docs.file");
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, inputPath, Text.class, Text.class);
 
-    RandomDocumentGenerator gen = new RandomDocumentGenerator();
-    
-    for (int i = 0; i < NUM_DOCS; i++) {
-      writer.append(new Text("Document::ID::" + i), new Text(gen.getRandomDocument()));
+    try {
+      RandomDocumentGenerator gen = new RandomDocumentGenerator();
+
+      for (int i = 0; i < NUM_DOCS; i++) {
+        writer.append(new Text("Document::ID::" + i), new Text(gen.getRandomDocument()));
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
   }
   
   @Test

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DocumentProcessorTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DocumentProcessorTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DocumentProcessorTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/DocumentProcessorTest.java Sat Jun  4 08:42:57 2011
@@ -17,6 +17,7 @@
 
 package org.apache.mahout.vectorizer;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
@@ -44,13 +45,16 @@ public class DocumentProcessorTest exten
 
     String documentId1 = "123";
     String text1 = "A test for the document processor";
-
-    SequenceFile.Writer writer = new SequenceFile.Writer(fs, configuration, input, Text.class, Text.class);
-    writer.append(new Text(documentId1), new Text(text1));
     String documentId2 = "456";
     String text2 = "and another one";
-    writer.append(new Text(documentId2), new Text(text2));
-    writer.close();
+
+    SequenceFile.Writer writer = new SequenceFile.Writer(fs, configuration, input, Text.class, Text.class);
+    try {
+      writer.append(new Text(documentId1), new Text(text1));
+      writer.append(new Text(documentId2), new Text(text2));
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
 
     DocumentProcessor.tokenizeDocuments(input, DefaultAnalyzer.class, output, configuration);
 

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/SparseVectorsFromSequenceFilesTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/SparseVectorsFromSequenceFilesTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/SparseVectorsFromSequenceFilesTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/SparseVectorsFromSequenceFilesTest.java Sat Jun  4 08:42:57 2011
@@ -20,6 +20,7 @@ package org.apache.mahout.vectorizer;
 import java.util.LinkedList;
 import java.util.List;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -47,11 +48,14 @@ public class SparseVectorsFromSequenceFi
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, inputPath, Text.class, Text.class);
 
     RandomDocumentGenerator gen = new RandomDocumentGenerator();
-    
-    for (int i = 0; i < NUM_DOCS; i++) {
-      writer.append(new Text("Document::ID::" + i), new Text(gen.getRandomDocument()));
+
+    try {
+      for (int i = 0; i < NUM_DOCS; i++) {
+        writer.append(new Text("Document::ID::" + i), new Text(gen.getRandomDocument()));
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
   }
   
   

Modified: mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/collocations/llr/GramKeyTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/collocations/llr/GramKeyTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/collocations/llr/GramKeyTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/vectorizer/collocations/llr/GramKeyTest.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.util.Arrays;
 
+import com.google.common.io.Closeables;
 import org.apache.mahout.common.MahoutTestCase;
 import org.junit.Test;
 
@@ -80,17 +81,23 @@ public final class GramKeyTest extends M
     
     ByteArrayOutputStream bout = new ByteArrayOutputStream();
     DataOutputStream out = new DataOutputStream(bout);
-    
-    two.write(out);
-    out.close();
+
+    try {
+      two.write(out);
+    } finally {
+      Closeables.closeQuietly(out);
+    }
     
     byte[] b = bout.toByteArray();
     
     ByteArrayInputStream bin = new ByteArrayInputStream(b);
     DataInputStream din = new DataInputStream(bin);
-    
-    one.readFields(din);
-    din.close();
+
+    try {
+      one.readFields(din);
+    } finally {
+      Closeables.closeQuietly(din);
+    }
 
     assertTrue(Arrays.equals(two.getBytes(), one.getBytes()));
     assertEquals(Gram.Type.UNIGRAM, one.getType());

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/DataFileIterator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/DataFileIterator.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/DataFileIterator.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/DataFileIterator.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.util.regex.Pattern;
 
 import com.google.common.collect.AbstractIterator;
+import com.google.common.io.Closeables;
 import org.apache.mahout.cf.taste.impl.common.SkippingIterator;
 import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
 import org.apache.mahout.cf.taste.model.PreferenceArray;
@@ -128,7 +129,7 @@ public final class DataFileIterator
   @Override
   public void close() {
     endOfData();
-    lineIterator.close();
+    Closeables.closeQuietly(lineIterator);
   }
 
   /**

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/ToCSV.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/ToCSV.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/ToCSV.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/ToCSV.java Sat Jun  4 08:42:57 2011
@@ -27,6 +27,7 @@ import java.util.zip.GZIPOutputStream;
 
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import org.apache.mahout.cf.taste.model.PreferenceArray;
 import org.apache.mahout.common.Pair;
 
@@ -54,26 +55,28 @@ public final class ToCSV {
     OutputStream outStream = new GZIPOutputStream(new FileOutputStream(outputFile));
     Writer outWriter = new BufferedWriter(new OutputStreamWriter(outStream, Charsets.UTF_8));
 
-    for (Pair<PreferenceArray,long[]> user : new DataFileIterable(inputFile)) {
-      PreferenceArray prefs = user.getFirst();
-      long[] timestamps = user.getSecond();
-      for (int i = 0; i < prefs.length(); i++) {
-        outWriter.write(String.valueOf(prefs.getUserID(i)));
-        outWriter.write(',');
-        outWriter.write(String.valueOf(prefs.getItemID(i)));
-        if (columnsToOutput > 2) {
+    try {
+      for (Pair<PreferenceArray,long[]> user : new DataFileIterable(inputFile)) {
+        PreferenceArray prefs = user.getFirst();
+        long[] timestamps = user.getSecond();
+        for (int i = 0; i < prefs.length(); i++) {
+          outWriter.write(String.valueOf(prefs.getUserID(i)));
           outWriter.write(',');
-          outWriter.write(String.valueOf(prefs.getValue(i)));
+          outWriter.write(String.valueOf(prefs.getItemID(i)));
+          if (columnsToOutput > 2) {
+            outWriter.write(',');
+            outWriter.write(String.valueOf(prefs.getValue(i)));
+          }
+          if (columnsToOutput > 3) {
+            outWriter.write(',');
+            outWriter.write(String.valueOf(timestamps[i]));
+          }
+          outWriter.write('\n');
         }
-        if (columnsToOutput > 3) {
-          outWriter.write(',');
-          outWriter.write(String.valueOf(timestamps[i]));
-        }
-        outWriter.write('\n');
       }
+    } finally {
+      Closeables.closeQuietly(outWriter);
     }
-    outWriter.flush();
-    outWriter.close();
   }
 
 }

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/Track1Runner.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/Track1Runner.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/Track1Runner.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/Track1Runner.java Sat Jun  4 08:42:57 2011
@@ -28,6 +28,7 @@ import java.util.concurrent.ExecutorServ
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 
+import com.google.common.io.Closeables;
 import org.apache.mahout.cf.taste.example.kddcup.DataFileIterable;
 import org.apache.mahout.cf.taste.example.kddcup.KDDCupDataModel;
 import org.apache.mahout.cf.taste.model.PreferenceArray;
@@ -81,13 +82,15 @@ public final class Track1Runner {
     start = end;
 
     OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(args[1])));
-    for (Future<byte[]> result : results) {
-      for (byte estimate : result.get()) {
-        out.write(estimate);
+    try {
+      for (Future<byte[]> result : results) {
+        for (byte estimate : result.get()) {
+          out.write(estimate);
+        }
       }
+    } finally {
+      Closeables.closeQuietly(out);
     }
-    out.flush();
-    out.close();
 
     end = System.currentTimeMillis();
     log.info("Wrote output in {}s", (end - start) / 1000);

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/svd/Track1SVDRunner.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/svd/Track1SVDRunner.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/svd/Track1SVDRunner.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track1/svd/Track1SVDRunner.java Sat Jun  4 08:42:57 2011
@@ -17,6 +17,7 @@
 
 package org.apache.mahout.cf.taste.example.kddcup.track1.svd;
 
+import com.google.common.io.Closeables;
 import org.apache.mahout.cf.taste.common.NoSuchItemException;
 import org.apache.mahout.cf.taste.common.NoSuchUserException;
 import org.apache.mahout.cf.taste.example.kddcup.DataFileIterable;
@@ -116,8 +117,7 @@ public class Track1SVDRunner {
         }
       }
     } finally {
-      out.flush();
-      out.close();
+      Closeables.closeQuietly(out);
     }
     log.info("wrote estimates to {}, done.", resultFile.getAbsolutePath());
   }

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track2/Track2Runner.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track2/Track2Runner.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track2/Track2Runner.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/track2/Track2Runner.java Sat Jun  4 08:42:57 2011
@@ -28,6 +28,7 @@ import java.util.concurrent.ExecutorServ
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 
+import com.google.common.io.Closeables;
 import org.apache.mahout.cf.taste.example.kddcup.DataFileIterable;
 import org.apache.mahout.cf.taste.example.kddcup.KDDCupDataModel;
 import org.apache.mahout.cf.taste.model.PreferenceArray;
@@ -81,18 +82,20 @@ public final class Track2Runner {
     start = end;
 
     OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(args[1])));
-    long lastUserID = Long.MIN_VALUE;
-    for (Future<UserResult> future : futures) {
-      UserResult result = future.get();
-      long userID = result.getUserID();
-      if (userID <= lastUserID) {
-        throw new IllegalStateException();
+    try {
+      long lastUserID = Long.MIN_VALUE;
+      for (Future<UserResult> future : futures) {
+        UserResult result = future.get();
+        long userID = result.getUserID();
+        if (userID <= lastUserID) {
+          throw new IllegalStateException();
+        }
+        lastUserID = userID;
+        out.write(result.getResultBytes());
       }
-      lastUserID = userID;
-      out.write(result.getResultBytes());
+    } finally {
+      Closeables.closeQuietly(out);
     }
-    out.flush();
-    out.close();
 
     end = System.currentTimeMillis();
     log.info("Wrote output in {}s", (end - start) / 1000);

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/WikipediaXmlSplitter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/WikipediaXmlSplitter.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/WikipediaXmlSplitter.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/WikipediaXmlSplitter.java Sat Jun  4 08:42:57 2011
@@ -26,6 +26,7 @@ import java.net.URI;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
 
+import com.google.common.io.Closeables;
 import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.Option;
@@ -195,7 +196,7 @@ public final class WikipediaXmlSplitter 
           try {
             chunkWriter.write(content.toString(), 0, content.length());
           } finally {
-            chunkWriter.close();
+            Closeables.closeQuietly(chunkWriter);
           }
           if (filenumber >= numChunks) {
             break;

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/XmlInputFormat.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/XmlInputFormat.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/XmlInputFormat.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/XmlInputFormat.java Sat Jun  4 08:42:57 2011
@@ -20,6 +20,7 @@ package org.apache.mahout.classifier.bay
 import java.io.IOException;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FSDataInputStream;
 import org.apache.hadoop.fs.FileSystem;
@@ -102,7 +103,7 @@ public class XmlInputFormat extends Text
 
     @Override
     public void close() throws IOException {
-      fsin.close();
+      Closeables.closeQuietly(fsin);
     }
 
     @Override

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/LogisticModelParameters.java Sat Jun  4 08:42:57 2011
@@ -19,6 +19,7 @@ package org.apache.mahout.classifier.sgd
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
+import com.google.common.io.Closeables;
 import org.apache.hadoop.io.Writable;
 
 import java.io.DataInput;
@@ -117,7 +118,7 @@ public class LogisticModelParameters imp
     try {
       return loadFrom(input);
     } finally {
-      input.close();
+      Closeables.closeQuietly(input);
     }
   }
 

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/PrintResourceOrFile.java Sat Jun  4 08:42:57 2011
@@ -18,6 +18,7 @@
 package org.apache.mahout.classifier.sgd;
 
 import com.google.common.base.Preconditions;
+import com.google.common.io.Closeables;
 
 import java.io.BufferedReader;
 
@@ -39,7 +40,7 @@ public final class PrintResourceOrFile {
         System.out.println(line);
       }
     } finally {
-      in.close();
+      Closeables.closeQuietly(in);
     }
   }
 }

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/SimpleCsvExamples.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/SimpleCsvExamples.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/SimpleCsvExamples.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/SimpleCsvExamples.java Sat Jun  4 08:42:57 2011
@@ -21,6 +21,7 @@ import com.google.common.base.Charsets;
 import com.google.common.base.Joiner;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Lists;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.mahout.common.IOUtils;
 import org.apache.mahout.common.RandomUtils;
@@ -275,7 +276,7 @@ public final class SimpleCsvExamples {
 
     @Override
     public void close() throws IOException {
-      in.close();
+      Closeables.closeQuietly(in);
     }
   }
 }

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainLogistic.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainLogistic.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainLogistic.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainLogistic.java Sat Jun  4 08:42:57 2011
@@ -19,6 +19,7 @@ package org.apache.mahout.classifier.sgd
 
 import com.google.common.base.Charsets;
 import com.google.common.collect.Lists;
+import com.google.common.io.Closeables;
 import com.google.common.io.Resources;
 import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.Group;
@@ -70,45 +71,47 @@ public final class TrainLogistic {
       OnlineLogisticRegression lr = lmp.createRegression();
       for (int pass = 0; pass < passes; pass++) {
         BufferedReader in = open(inputFile);
-
-        // read variable names
-        csv.firstLine(in.readLine());
-
-        String line = in.readLine();
-        while (line != null) {
-          // for each new line, get target and predictors
-          Vector input = new RandomAccessSparseVector(lmp.getNumFeatures());
-          int targetValue = csv.processLine(line, input);
-
-          // check performance while this is still news
-          double logP = lr.logLikelihood(targetValue, input);
-          if (!Double.isInfinite(logP)) {
-            if (samples < 20) {
-              logPEstimate = (samples * logPEstimate + logP) / (samples + 1);
-            } else {
-              logPEstimate = 0.95 * logPEstimate + 0.05 * logP;
+        try {
+          // read variable names
+          csv.firstLine(in.readLine());
+
+          String line = in.readLine();
+          while (line != null) {
+            // for each new line, get target and predictors
+            Vector input = new RandomAccessSparseVector(lmp.getNumFeatures());
+            int targetValue = csv.processLine(line, input);
+
+            // check performance while this is still news
+            double logP = lr.logLikelihood(targetValue, input);
+            if (!Double.isInfinite(logP)) {
+              if (samples < 20) {
+                logPEstimate = (samples * logPEstimate + logP) / (samples + 1);
+              } else {
+                logPEstimate = 0.95 * logPEstimate + 0.05 * logP;
+              }
+              samples++;
+            }
+            double p = lr.classifyScalar(input);
+            if (scores) {
+              output.printf(Locale.ENGLISH, "%10d %2d %10.2f %2.4f %10.4f %10.4f\n",
+                samples, targetValue, lr.currentLearningRate(), p, logP, logPEstimate);
             }
-            samples++;
-          }
-          double p = lr.classifyScalar(input);
-          if (scores) {
-            output.printf(Locale.ENGLISH, "%10d %2d %10.2f %2.4f %10.4f %10.4f\n",
-              samples, targetValue, lr.currentLearningRate(), p, logP, logPEstimate);
-          }
 
-          // now update model
-          lr.train(targetValue, input);
+            // now update model
+            lr.train(targetValue, input);
 
-          line = in.readLine();
+            line = in.readLine();
+          }
+        } finally {
+          Closeables.closeQuietly(in);
         }
-        in.close();
       }
 
       OutputStream modelOutput = new FileOutputStream(outputFile);
       try {
         lmp.saveTo(modelOutput);
       } finally {
-        modelOutput.close();
+        Closeables.closeQuietly(modelOutput);
       }
       
       output.printf(Locale.ENGLISH, "%d\n", lmp.getNumFeatures());

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainNewsGroups.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainNewsGroups.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainNewsGroups.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/sgd/TrainNewsGroups.java Sat Jun  4 08:42:57 2011
@@ -24,6 +24,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multiset;
 import com.google.common.collect.Ordering;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.TokenStream;
@@ -306,7 +307,7 @@ public final class TrainNewsGroups {
         countWords(analyzer, words, reader);
       }
     } finally {
-      reader.close();
+      Closeables.closeQuietly(reader);
     }
 
     Vector v = new RandomAccessSparseVector(FEATURES);

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplayClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplayClustering.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplayClustering.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplayClustering.java Sat Jun  4 08:42:57 2011
@@ -33,6 +33,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
@@ -243,7 +244,7 @@ public class DisplayClustering extends F
         writer.append(new Text(), vw);
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
   }
 
@@ -302,19 +303,25 @@ public class DisplayClustering extends F
 
   protected static ClusterClassifier readClassifier(Configuration config, Path path) throws IOException {
     SequenceFile.Reader reader = new SequenceFile.Reader(FileSystem.get(config), path, config);
-    Writable key = new Text();
-    ClusterClassifier classifierOut = new ClusterClassifier();
-    reader.next(key, classifierOut);
-    reader.close();
-    return classifierOut;
+    try {
+      Writable key = new Text();
+      ClusterClassifier classifierOut = new ClusterClassifier();
+      reader.next(key, classifierOut);
+      return classifierOut;
+    } finally {
+      Closeables.closeQuietly(reader);
+    }
   }
 
   protected static void writeClassifier(ClusterClassifier classifier, Configuration config, Path path)
     throws IOException {
     SequenceFile.Writer writer =
         new SequenceFile.Writer(FileSystem.get(config), config, path, Text.class, ClusterClassifier.class);
-    Writable key = new Text("test");
-    writer.append(key, classifier);
-    writer.close();
+    try {
+      Writable key = new Text("test");
+      writer.append(key, classifier);
+    } finally {
+      Closeables.closeQuietly(writer);
+    }
   }
 }

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplaySpectralKMeans.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplaySpectralKMeans.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplaySpectralKMeans.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/display/DisplaySpectralKMeans.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.Writer;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -64,7 +65,7 @@ public class DisplaySpectralKMeans exten
         }
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
     int maxIter = 10;
     double convergenceDelta = 0.001;

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/minhash/LastfmDataConverter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/minhash/LastfmDataConverter.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/minhash/LastfmDataConverter.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/minhash/LastfmDataConverter.java Sat Jun  4 08:42:57 2011
@@ -18,6 +18,7 @@
 package org.apache.mahout.clustering.minhash;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Files;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -135,7 +136,7 @@ public final class LastfmDataConverter {
       msg = usedMemory() + "Converting data to internal vector format: ";
       System.out.print('\r' + msg + percentDone + "% Completed\n");
     } finally {
-      br.close();
+      Closeables.closeQuietly(br);
     }
     return itemFeaturesMap;
   }
@@ -179,7 +180,7 @@ public final class LastfmDataConverter {
         }
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
     return true;
   }

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/df/mapreduce/TestForest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/df/mapreduce/TestForest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/df/mapreduce/TestForest.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/df/mapreduce/TestForest.java Sat Jun  4 08:42:57 2011
@@ -22,6 +22,7 @@ import java.util.Random;
 import java.util.Scanner;
 import java.util.Arrays;
 
+import com.google.common.io.Closeables;
 import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.Option;
@@ -245,30 +246,33 @@ public class TestForest extends Configur
     }
 
     FSDataInputStream input = dataFS.open(inPath);
-    Scanner scanner = new Scanner(input);
-
-    while (scanner.hasNextLine()) {
-      String line = scanner.nextLine();
-      if (line.isEmpty()) {
-        continue; // skip empty lines
-      }
-
-      Instance instance = converter.convert(0, line);
-      int prediction = forest.classify(rng, instance);
+    try {
+      Scanner scanner = new Scanner(input);
 
-      if (outputPath != null) {
-        ofile.writeChars(Integer.toString(prediction)); // write the prediction
-        ofile.writeChar('\n');
+      while (scanner.hasNextLine()) {
+        String line = scanner.nextLine();
+        if (line.isEmpty()) {
+          continue; // skip empty lines
+        }
+
+        Instance instance = converter.convert(0, line);
+        int prediction = forest.classify(rng, instance);
+
+        if (outputPath != null) {
+          ofile.writeChars(Integer.toString(prediction)); // write the prediction
+          ofile.writeChar('\n');
+        }
+
+        if (analyzer != null) {
+          analyzer.addInstance(dataset.getLabel(instance.getLabel()),
+                               new ClassifierResult(dataset.getLabel(prediction), 1.0));
+        }
       }
 
-      if (analyzer != null) {
-        analyzer.addInstance(dataset.getLabel(instance.getLabel()),
-                             new ClassifierResult(dataset.getLabel(prediction), 1.0));
-      }
+      scanner.close();
+    } finally {
+      Closeables.closeQuietly(input);
     }
-
-    scanner.close();
-    input.close();
   }
 
   public static void main(String[] args) throws Exception {

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/hadoop/DatasetSplit.java Sat Jun  4 08:42:57 2011
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.util.Random;
 
 import com.google.common.base.Preconditions;
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.io.LongWritable;
 import org.apache.hadoop.io.Text;
@@ -138,7 +139,7 @@ public final class DatasetSplit {
 
     @Override
     public void close() throws IOException {
-      reader.close();
+      Closeables.closeQuietly(reader);
     }
 
     @Override

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosTool.java Sat Jun  4 08:42:57 2011
@@ -27,6 +27,7 @@ import java.util.Locale;
 import java.util.Scanner;
 
 import com.google.common.base.Preconditions;
+import com.google.common.io.Closeables;
 import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.Option;
@@ -201,7 +202,7 @@ public final class CDInfosTool {
         writer.newLine();
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
   }
 

Modified: mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/bayes/SplitBayesInputTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/bayes/SplitBayesInputTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/bayes/SplitBayesInputTest.java (original)
+++ mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/bayes/SplitBayesInputTest.java Sat Jun  4 08:42:57 2011
@@ -24,6 +24,7 @@ import java.io.Writer;
 import java.nio.charset.Charset;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -89,12 +90,14 @@ public final class SplitBayesInputTest e
   }
 
   private void writeSingleInputFile() throws IOException {
-    BufferedWriter writer = new BufferedWriter(
-        new OutputStreamWriter(fs.create(tempInputFile), Charsets.UTF_8));
-    for (String[] entry : ClassifierData.DATA) {
-      writer.write(entry[0] + '\t' + entry[1] + '\n');
+    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fs.create(tempInputFile), Charsets.UTF_8));
+    try {
+      for (String[] entry : ClassifierData.DATA) {
+        writer.write(entry[0] + '\t' + entry[1] + '\n');
+      }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
   }
 
   @Test

Modified: mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/sgd/TrainLogisticTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/sgd/TrainLogisticTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/sgd/TrainLogisticTest.java (original)
+++ mahout/trunk/examples/src/test/java/org/apache/mahout/classifier/sgd/TrainLogisticTest.java Sat Jun  4 08:42:57 2011
@@ -20,6 +20,7 @@ package org.apache.mahout.classifier.sgd
 import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
+import com.google.common.io.Closeables;
 import com.google.common.io.Resources;
 import org.apache.mahout.classifier.AbstractVectorClassifier;
 import org.apache.mahout.examples.MahoutTestCase;
@@ -76,12 +77,15 @@ public class TrainLogisticTest extends M
 
     // test saved model
     InputStream in = new FileInputStream(new File(outputFile));
-    LogisticModelParameters lmpOut = LogisticModelParameters.loadFrom(in);
-    in.close();
-    CsvRecordFactory csvOut = lmpOut.getCsvRecordFactory();
-    csvOut.firstLine(data.get(0));
-    OnlineLogisticRegression lrOut = lmpOut.createRegression();
-    verifyModel(lmpOut, csvOut, data, lrOut, expectedValues);
+    try {
+      LogisticModelParameters lmpOut = LogisticModelParameters.loadFrom(in);
+      CsvRecordFactory csvOut = lmpOut.getCsvRecordFactory();
+      csvOut.firstLine(data.get(0));
+      OnlineLogisticRegression lrOut = lmpOut.createRegression();
+      verifyModel(lmpOut, csvOut, data, lrOut, expectedValues);
+    } finally {
+      Closeables.closeQuietly(in);
+    }
 
     String output = runMain(RunLogistic.class, new String[]{
         "--input", "donut.csv",
@@ -148,15 +152,17 @@ public class TrainLogisticTest extends M
     ByteArrayOutputStream trainOutput = new ByteArrayOutputStream();
     PrintStream printStream = new PrintStream(trainOutput);
 
-    Field outputField = clazz.getDeclaredField("output");
-    Method main = clazz.getMethod("main", args.getClass());
-
-    outputField.set(null, printStream);
-    Object[] argList = {args};
-    main.invoke(null, argList);
-    printStream.close();
-
-    return new String(trainOutput.toByteArray(), Charsets.UTF_8);
+    try {
+      Field outputField = clazz.getDeclaredField("output");
+      Method main = clazz.getMethod("main", args.getClass());
+
+      outputField.set(null, printStream);
+      Object[] argList = {args};
+      main.invoke(null, argList);
+      return new String(trainOutput.toByteArray(), Charsets.UTF_8);
+    } finally {
+      Closeables.closeQuietly(printStream);
+    }
   }
 
   private static void verifyModel(LogisticModelParameters lmp,

Modified: mahout/trunk/examples/src/test/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosToolTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/test/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosToolTest.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/examples/src/test/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosToolTest.java (original)
+++ mahout/trunk/examples/src/test/java/org/apache/mahout/ga/watchmaker/cd/tool/CDInfosToolTest.java Sat Jun  4 08:42:57 2011
@@ -17,6 +17,7 @@
 
 package org.apache.mahout.ga.watchmaker.cd.tool;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FSDataOutputStream;
 import org.apache.hadoop.fs.FileSystem;
@@ -117,15 +118,17 @@ public final class CDInfosToolTest exten
       FSDataOutputStream out = fs.create(new Path(input, "file." + floop));
       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
 
-      // make sure we have enough room to allow all nominal values to appear in the data
-      int nblines = rng.nextInt(200) + MAX_NOMINAL_VALUES;
-
-      for (int line = 0; line < nblines; line++) {
-        writer.write(randomLine(descriptors, descriptions, appeared));
-        writer.newLine();
+      try {
+        // make sure we have enough room to allow all nominal values to appear in the data
+        int nblines = rng.nextInt(200) + MAX_NOMINAL_VALUES;
+
+        for (int line = 0; line < nblines; line++) {
+          writer.write(randomLine(descriptors, descriptions, appeared));
+          writer.newLine();
+        }
+      } finally {
+        Closeables.closeQuietly(writer);
       }
-
-      writer.close();
     }
   }
 

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/benchmark/VectorBenchmarks.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/benchmark/VectorBenchmarks.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/benchmark/VectorBenchmarks.java (original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/benchmark/VectorBenchmarks.java Sat Jun  4 08:42:57 2011
@@ -29,6 +29,7 @@ import java.util.Random;
 import java.util.Map.Entry;
 import java.util.regex.Pattern;
 
+import com.google.common.io.Closeables;
 import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.Option;
@@ -288,45 +289,54 @@ public class VectorBenchmarks {
 
     Writable one = new IntWritable(0);
     VectorWritable vec = new VectorWritable();
-    
     TimingStatistics stats = new TimingStatistics();
-    for (int l = 0; l < loop; l++) {
-      for (int i = 0; i < numVectors; i++) {
-        TimingStatistics.Call call = stats.newCall();
-        vec.set(vectors[0][i]);
-        writer.append(one, vec);
-        call.end();
+
+    try {
+      for (int l = 0; l < loop; l++) {
+        for (int i = 0; i < numVectors; i++) {
+          TimingStatistics.Call call = stats.newCall();
+          vec.set(vectors[0][i]);
+          writer.append(one, vec);
+          call.end();
+        }
       }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
     printStats(stats, "Serialize", "DenseVector");
     
     writer = new SequenceFile.Writer(fs, conf,
       new Path("/tmp/randsparse-vector"), IntWritable.class, VectorWritable.class);
     stats = new TimingStatistics();
-    for (int l = 0; l < loop; l++) {
-      for (int i = 0; i < numVectors; i++) {
-        TimingStatistics.Call call = stats.newCall();
-        vec.set(vectors[1][i]);
-        writer.append(one, vec);
-        call.end();
+    try {
+      for (int l = 0; l < loop; l++) {
+        for (int i = 0; i < numVectors; i++) {
+          TimingStatistics.Call call = stats.newCall();
+          vec.set(vectors[1][i]);
+          writer.append(one, vec);
+          call.end();
+        }
       }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
     printStats(stats, "Serialize", "RandSparseVector");
     
     writer = new SequenceFile.Writer(fs, conf,
       new Path("/tmp/seqsparse-vector"), IntWritable.class, VectorWritable.class);
     stats = new TimingStatistics();
-    for (int l = 0; l < loop; l++) {
-      for (int i = 0; i < numVectors; i++) {
-        TimingStatistics.Call call = stats.newCall();
-        vec.set(vectors[2][i]);
-        writer.append(one, vec);
-        call.end();
+    try {
+      for (int l = 0; l < loop; l++) {
+        for (int i = 0; i < numVectors; i++) {
+          TimingStatistics.Call call = stats.newCall();
+          vec.set(vectors[2][i]);
+          writer.append(one, vec);
+          call.end();
+        }
       }
+    } finally {
+      Closeables.closeQuietly(writer);
     }
-    writer.close();
     printStats(stats, "Serialize", "SeqSparseVector");
     
   }

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java (original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java Sat Jun  4 08:42:57 2011
@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import com.google.common.io.Closeables;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
@@ -121,11 +122,14 @@ public final class RepresentativePointsD
       Path inPart = part.getPath();
       Path path = new Path(output, inPart.getName());
       SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, IntWritable.class, VectorWritable.class);
-      for (Cluster value : new SequenceFileValueIterable<Cluster>(inPart, true, conf)) {
-        log.debug("C-{}: {}", value.getId(), AbstractCluster.formatVector(value.getCenter(), null));
-        writer.append(new IntWritable(value.getId()), new VectorWritable(value.getCenter()));
+      try {
+        for (Cluster value : new SequenceFileValueIterable<Cluster>(inPart, true, conf)) {
+          log.debug("C-{}: {}", value.getId(), AbstractCluster.formatVector(value.getCenter(), null));
+          writer.append(new IntWritable(value.getId()), new VectorWritable(value.getCenter()));
+        }
+      } finally {
+        Closeables.closeQuietly(writer);
       }
-      writer.close();
     }
   }
 
@@ -184,7 +188,7 @@ public final class RepresentativePointsD
         }
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
     writer = new SequenceFile.Writer(fs, conf,
         new Path(stateOut, "part-m-" + part++), IntWritable.class, VectorWritable.class);
@@ -193,7 +197,7 @@ public final class RepresentativePointsD
         writer.append(new IntWritable(entry.getKey()), new VectorWritable(entry.getValue().getVector()));
       }
     } finally {
-      writer.close();
+      Closeables.closeQuietly(writer);
     }
   }
 

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/lda/LDAPrintTopics.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/lda/LDAPrintTopics.java?rev=1131350&r1=1131349&r2=1131350&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/lda/LDAPrintTopics.java (original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/lda/LDAPrintTopics.java Sat Jun  4 08:42:57 2011
@@ -17,6 +17,7 @@
 
 package org.apache.mahout.clustering.lda;
 
+import com.google.common.io.Closeables;
 import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.Option;
@@ -183,24 +184,31 @@ public final class LDAPrintTopics {
     throws IOException {
     for (int i = 0; i < topWords.size(); ++i) {
       PriorityQueue<StringDoublePair> topK = topWords.get(i);
-      PrintWriter out;
-      if(outputDir != null) {
-        out = new PrintWriter(new File(outputDir, "topic_" + i));
-      } else {
-        out = new PrintWriter(System.out);
-        out.println("Topic " + i);
-        out.println("===========");
-      }
-      List<StringDoublePair> topKasList = new ArrayList<StringDoublePair>(topK.size());
-      for(StringDoublePair wordWithScore : topK) {
-        topKasList.add(wordWithScore);
-      }
-      Collections.sort(topKasList, Collections.reverseOrder());
-      for(StringDoublePair wordWithScore : topKasList) {
-        out.println(wordWithScore.word + " [p(" + wordWithScore.word + "|topic_" + i +") = "
-         + wordWithScore.score);
+      PrintWriter out = null;
+      boolean printingToSystemOut = false;
+      try {
+        if (outputDir != null) {
+          out = new PrintWriter(new File(outputDir, "topic_" + i));
+        } else {
+          out = new PrintWriter(System.out);
+          printingToSystemOut = true;
+          out.println("Topic " + i);
+          out.println("===========");
+        }
+        List<StringDoublePair> topKasList = new ArrayList<StringDoublePair>(topK.size());
+        for(StringDoublePair wordWithScore : topK) {
+          topKasList.add(wordWithScore);
+        }
+        Collections.sort(topKasList, Collections.reverseOrder());
+        for(StringDoublePair wordWithScore : topKasList) {
+          out.println(wordWithScore.word + " [p(" + wordWithScore.word + "|topic_" + i +") = "
+           + wordWithScore.score);
+        }
+      } finally {
+        if (!printingToSystemOut) {
+          Closeables.closeQuietly(out);
+        }
       }
-      out.close();
     }
   }
   



Mime
View raw message