Fix a minor problem with JsonView

diff --git a/src/mapper/java/org/codehaus/jackson/map/ObjectMapper.java b/src/mapper/java/org/codehaus/jackson/map/ObjectMapper.java
index d184a2c..2543784 100644
--- a/src/mapper/java/org/codehaus/jackson/map/ObjectMapper.java
+++ b/src/mapper/java/org/codehaus/jackson/map/ObjectMapper.java
@@ -1055,10 +1055,7 @@
     public void writeValueUsingView(JsonGenerator jgen, Object value, Class<?> viewClass)
         throws IOException, JsonGenerationException, JsonMappingException
     {
-        SerializationConfig cfg = copySerializationConfig();
-        cfg.setSerializationView(viewClass);
-        _serializerProvider.serializeValue(cfg, jgen, value, _serializerFactory);
-        jgen.flush();
+        _configAndWriteValue(jgen, value, viewClass);
     }
 
     /**
@@ -1075,7 +1072,7 @@
     public void writeValueUsingView(Writer w, Object value, Class<?> viewClass)
         throws IOException, JsonGenerationException, JsonMappingException
     {
-        writeValueUsingView(_jsonFactory.createJsonGenerator(w), value, viewClass);
+        _configAndWriteValue(_jsonFactory.createJsonGenerator(w), value, viewClass);
     }
     
     /**
@@ -1092,7 +1089,7 @@
     public void writeValueUsingView(OutputStream out, Object value, Class<?> viewClass)
         throws IOException, JsonGenerationException, JsonMappingException
     {
-        writeValueUsingView(_jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8), value, viewClass);
+        _configAndWriteValue(_jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8), value, viewClass);
     }
 
     /*
@@ -1219,6 +1216,28 @@
         }
     }
 
+    protected final void _configAndWriteValue(JsonGenerator jgen, Object value, Class<?> viewClass)
+        throws IOException, JsonGenerationException, JsonMappingException
+    {
+        SerializationConfig cfg = copySerializationConfig();
+        if (cfg.isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {
+            jgen.useDefaultPrettyPrinter();
+        }
+        cfg.setSerializationView(viewClass);
+        boolean closed = false;
+        try {
+            _serializerProvider.serializeValue(cfg, jgen, value, _serializerFactory);
+            closed = true;
+            jgen.close();
+        } finally {
+            if (!closed) {
+                try {
+                    jgen.close();
+                } catch (IOException ioe) { }
+            }
+        }
+    }
+
     /**
      * Actual implementation of value reading+binding operation.
      */
diff --git a/src/perf/TestSerPerf.java b/src/perf/TestSerPerf.java
index d08c1ac..44215d3 100644
--- a/src/perf/TestSerPerf.java
+++ b/src/perf/TestSerPerf.java
@@ -60,7 +60,7 @@
     final Object _finalFieldBean = new FinalFieldBean();
     final Object _nonFinalBean = new NonFinalBean();
 
-    public TestSerPerf()
+    private TestSerPerf()
         throws Exception
     {
         _mapper = new ObjectMapper();
diff --git a/src/perf/TestViewPerf.java b/src/perf/TestViewPerf.java
new file mode 100644
index 0000000..2f31ecc
--- /dev/null
+++ b/src/perf/TestViewPerf.java
@@ -0,0 +1,138 @@
+import java.io.*;
+
+import org.codehaus.jackson.*;
+import org.codehaus.jackson.map.*;
+import org.codehaus.jackson.map.annotate.JsonView;
+
+public final class TestViewPerf
+{
+    /*
+    /////////////////////////////////////////////////////
+    // Bean classes
+    /////////////////////////////////////////////////////
+     */
+
+    static class ViewA { }
+    static class ViewB { }
+
+    static class ViewBean
+    {
+        final Bean2 _bean = new Bean2();
+
+        @JsonView({ViewA.class, ViewB.class})
+        public Bean2 getBean() { return _bean; }
+
+        @JsonView({ViewA.class, ViewB.class})
+        public String getBean2() { return "abc-def"; }
+
+        @JsonView({ViewA.class, ViewB.class})
+        public int getBean3() { return 218985; }
+
+        @JsonView({ViewA.class, ViewB.class})
+        public Bean2 getBean4() { return _bean; }
+
+        @JsonView({ViewA.class})
+        public String getText2() { return "abc-def"; }
+
+        @JsonView({ViewB.class})
+        public String getText3() { return "abc-def"; }
+    }
+
+    static class NonViewBean
+    {
+        final Bean2 _bean = new Bean2();
+
+        public Bean2 getBean() { return _bean; }
+        public String getBean2() { return "abc-def"; }
+        public int getBean3() { return 218985; }
+        public Bean2 getBean4() { return _bean; }
+    }
+
+    static class Bean2 {
+        public int getX() { return 3; }
+        public String getName() { return "foobar"; }
+    }
+
+    private final int REPS;
+    private final ObjectMapper _mapper;
+
+    private TestViewPerf()
+        throws Exception
+    {
+        _mapper = new ObjectMapper();
+        REPS = 8000;
+    }
+
+    public void test()
+        throws Exception
+    {
+        int i = 0;
+        int sum = 0;
+
+        ByteArrayOutputStream result = new ByteArrayOutputStream();
+
+        final ViewBean viewBean = new ViewBean();
+        final NonViewBean nonViewBean = new NonViewBean();
+
+        while (true) {
+            try {  Thread.sleep(100L); } catch (InterruptedException ie) { }
+            int round = (i++ % 3);
+
+            long curr = System.currentTimeMillis();
+            String msg;
+            boolean lf = (round == 0);
+
+            switch (round) {
+
+            case 0:
+                msg = "With view";
+                sum += testViewSer(viewBean, REPS, result, ViewA.class);
+                break;
+            case 1:
+                msg = "WithOUT view";
+                sum += testViewSer(viewBean, REPS, result, null);
+                break;
+            case 2:
+                msg = "NO view";
+                sum += testSer(nonViewBean, REPS, result, null);
+                break;
+
+            default:
+                throw new Error("Internal error");
+            }
+
+            curr = System.currentTimeMillis() - curr;
+            if (lf) {
+                System.out.println();
+            }
+            System.out.println("Test '"+msg+"' -> "+curr+" msecs ("
+                               +(sum & 0xFF)+").");
+        }
+    }
+
+    protected int testViewSer(Object value, int reps, ByteArrayOutputStream result, Class<?> view)
+        throws Exception
+    {
+        for (int i = 0; i < reps; ++i) {
+            result.reset();
+            _mapper.writeValueUsingView(result, value, view);
+            //_mapper.writeValue(result, value);
+        }
+        return _mapper.hashCode(); // just to get some non-optimizable number
+    }
+
+    protected int testSer(Object value, int reps, ByteArrayOutputStream result, Class<?> dummyView)
+        throws Exception
+    {
+        for (int i = 0; i < reps; ++i) {
+            result.reset();
+            _mapper.writeValue(result, value);
+        }
+        return _mapper.hashCode(); // just to get some non-optimizable number
+    }
+
+    public static void main(String[] args) throws Exception
+    {
+        new TestViewPerf().test();
+    }
+}