Merge "Fix usage DeflaterOutputStream constructor on android"
diff --git a/Android.mk b/Android.mk
index a24d43f..a942b67 100644
--- a/Android.mk
+++ b/Android.mk
@@ -26,6 +26,7 @@
 LOCAL_SRC_FILES := $(filter-out %/Libcore.java, $(LOCAL_SRC_FILES))
 
 LOCAL_SRC_FILES += $(call all-java-files-under, android/main/java)
+LOCAL_SDK_VERSION := 16
 
 LOCAL_JARJAR_RULES := ${LOCAL_PATH}/jarjar-rules.txt
 include $(BUILD_JAVA_LIBRARY)
diff --git a/android/main/java/libcore/util/Libcore.java b/android/main/java/libcore/util/Libcore.java
index e617b81..cd32c17 100644
--- a/android/main/java/libcore/util/Libcore.java
+++ b/android/main/java/libcore/util/Libcore.java
@@ -19,6 +19,8 @@
 import javax.net.ssl.SSLSocket;
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.Socket;
@@ -27,6 +29,8 @@
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.ByteOrder;
+import java.util.zip.Deflater;
+import java.util.zip.DeflaterOutputStream;
 
 /**
  * APIs for interacting with Android's core library. The main purpose of this
@@ -47,6 +51,7 @@
     private static final Method setHostname;
     private static final Method setNpnProtocols;
     private static final Method getNpnSelectedProtocol;
+    private static final Constructor<DeflaterOutputStream> deflaterOutputStreamConstructor;
 
     static {
         try {
@@ -59,6 +64,8 @@
             setHostname = openSslSocketClass.getMethod("setHostname", String.class);
             setNpnProtocols = openSslSocketClass.getMethod("setNpnProtocols", byte[].class);
             getNpnSelectedProtocol = openSslSocketClass.getMethod("getNpnSelectedProtocol");
+            deflaterOutputStreamConstructor = DeflaterOutputStream.class.getConstructor(
+                   new Class[] { OutputStream.class, Deflater.class, boolean.class });
         } catch (ClassNotFoundException cnfe) {
             throw new RuntimeException(cnfe);
         } catch (NoSuchMethodException nsme) {
@@ -66,6 +73,19 @@
         }
     }
 
+    public static DeflaterOutputStream newDeflaterOutputStream(
+            OutputStream os, Deflater deflater, boolean syncFlush) {
+        try {
+            return deflaterOutputStreamConstructor.newInstance(os, deflater, syncFlush);
+        } catch (InstantiationException e) {
+            throw new RuntimeException("Unknown DeflaterOutputStream implementation.");
+        } catch (IllegalAccessException e) {
+            throw new RuntimeException("Unknown DeflaterOutputStream implementation.");
+        } catch (InvocationTargetException e) {
+            throw new RuntimeException("Unknown DeflaterOutputStream implementation.");
+        }
+    }
+
     public static void makeTlsTolerant(SSLSocket socket, String socketHost, boolean tlsTolerant) {
         if (!tlsTolerant) {
             socket.setEnabledProtocols(new String[] {"SSLv3"});
diff --git a/src/main/java/libcore/net/spdy/SpdyWriter.java b/src/main/java/libcore/net/spdy/SpdyWriter.java
index 5bc4644..28490ac 100644
--- a/src/main/java/libcore/net/spdy/SpdyWriter.java
+++ b/src/main/java/libcore/net/spdy/SpdyWriter.java
@@ -24,6 +24,8 @@
 import java.util.zip.Deflater;
 import java.util.zip.DeflaterOutputStream;
 
+import static libcore.util.Libcore.newDeflaterOutputStream;
+
 /**
  * Write version 2 SPDY frames.
  */
@@ -46,7 +48,7 @@
         deflater.setDictionary(SpdyReader.DICTIONARY);
         nameValueBlockBuffer = new ByteArrayOutputStream();
         nameValueBlockOut = new DataOutputStream(
-                new DeflaterOutputStream(nameValueBlockBuffer, deflater, true));
+               newDeflaterOutputStream(nameValueBlockBuffer, deflater, true));
     }
 
     public void synStream() throws IOException {
diff --git a/src/main/java/libcore/util/Libcore.java b/src/main/java/libcore/util/Libcore.java
index 0d7db0d..4798004 100644
--- a/src/main/java/libcore/util/Libcore.java
+++ b/src/main/java/libcore/util/Libcore.java
@@ -18,6 +18,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -29,6 +30,8 @@
 import java.nio.ByteOrder;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.zip.Deflater;
+import java.util.zip.DeflaterOutputStream;
 import javax.net.ssl.SSLSocket;
 import org.eclipse.jetty.npn.NextProtoNego;
 
@@ -72,6 +75,11 @@
         }
     }
 
+    public static DeflaterOutputStream newDeflaterOutputStream(
+            OutputStream os, Deflater deflater, boolean syncFlush) {
+        return new DeflaterOutputStream(os, deflater, syncFlush);
+    }
+
     public static void makeTlsTolerant(SSLSocket socket, String socketHost, boolean tlsTolerant) {
         if (!tlsTolerant) {
             socket.setEnabledProtocols(new String[] {"SSLv3"});