Merge "Update java.io.File to 8u60"
diff --git a/NativeCode.mk b/NativeCode.mk
index cc31819..7c3efbc 100644
--- a/NativeCode.mk
+++ b/NativeCode.mk
@@ -229,7 +229,7 @@
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE := libjavacore
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/NativeCode.mk
-LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libexpat libicuuc-host libicui18n-host libcrypto libz-host libziparchive
+LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libexpat libicuuc libicui18n libcrypto libz-host libziparchive
 LOCAL_STATIC_LIBRARIES += $(core_static_libraries)
 LOCAL_MULTILIB := both
 LOCAL_CXX_STL := libc++
@@ -241,7 +241,7 @@
 LOCAL_C_INCLUDES := $(core_c_includes)
 LOCAL_CFLAGS := -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -DLINUX -D__GLIBC__ # Sigh.
 LOCAL_CFLAGS += $(openjdk_cflags)
-LOCAL_SHARED_LIBRARIES := $(core_shared_libraries) libicuuc-host libcrypto libz-host
+LOCAL_SHARED_LIBRARIES := $(core_shared_libraries) libicuuc libcrypto libz-host
 LOCAL_SHARED_LIBRARIES += libopenjdkjvmd libnativehelper
 LOCAL_STATIC_LIBRARIES := $(core_static_libraries) libfdlibm
 LOCAL_MODULE_TAGS := optional
@@ -256,7 +256,7 @@
 LOCAL_C_INCLUDES := $(core_c_includes)
 LOCAL_CFLAGS := -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -DLINUX -D__GLIBC__ # Sigh.
 LOCAL_CFLAGS += $(openjdk_cflags)
-LOCAL_SHARED_LIBRARIES := $(core_shared_libraries) libicuuc-host libcrypto libz-host
+LOCAL_SHARED_LIBRARIES := $(core_shared_libraries) libicuuc libcrypto libz-host
 LOCAL_SHARED_LIBRARIES += libopenjdkjvm libnativehelper
 LOCAL_STATIC_LIBRARIES := $(core_static_libraries) libfdlibm
 LOCAL_MODULE_TAGS := optional
diff --git a/benchmarks/src/benchmarks/StringDexCacheBenchmark.java b/benchmarks/src/benchmarks/StringDexCacheBenchmark.java
new file mode 100644
index 0000000..ce72b4d
--- /dev/null
+++ b/benchmarks/src/benchmarks/StringDexCacheBenchmark.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package benchmarks;
+
+/**
+ * How long does it take to access a string in the dex cache?
+ */
+public class StringDexCacheBenchmark {
+    public int timeStringDexCacheAccess(int reps) {
+        int v = 0;
+        for (int rep = 0; rep < reps; ++rep) {
+          // Deliberately obscured to make optimizations less likely.
+          String s = (rep >= 0) ? "hello, world!" : null;
+          v += s.length();
+        }
+        return v;
+    }
+}
diff --git a/dalvik/src/main/java/dalvik/annotation/optimization/CriticalNative.java b/dalvik/src/main/java/dalvik/annotation/optimization/CriticalNative.java
new file mode 100644
index 0000000..4564b18
--- /dev/null
+++ b/dalvik/src/main/java/dalvik/annotation/optimization/CriticalNative.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dalvik.annotation.optimization;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Applied to native methods to enable an ART runtime built-in optimization:
+ * methods that are annotated this way can speed up JNI transitions for methods that contain no
+ * objects (in parameters or return values, or as an implicit {@code this}).
+ *
+ * <p>
+ * The native implementation must exclude the {@code JNIEnv} and {@code jclass} parameters from its
+ * function signature. As an additional limitation, the method must be explicitly registered with
+ * {@code RegisterNatives} instead of relying on the built-in dynamic JNI linking.
+ * </p>
+ *
+ * <p>
+ * Performance of JNI transitions:
+ * <ul>
+ * <li>Regular JNI cost in nanoseconds: 115
+ * <li>Fast {@code (!)} JNI cost in nanoseconds: 60
+ * <li>{@literal @}{@link FastNative} cost in nanoseconds: 35
+ * <li>{@literal @}{@code CriticalNative} cost in nanoseconds: 25
+ * </ul>
+ * (Measured on angler-userdebug in 07/2016).
+ * </p>
+ *
+ * <p>
+ * A similar annotation, {@literal @}{@link FastNative}, exists with similar performance guarantees.
+ * However, unlike {@code @CriticalNative} it supports non-statics, object return values, and object
+ * parameters. If a method absolutely must have access to a {@code jobject}, then use
+ * {@literal @}{@link FastNative} instead of this.
+ * </p>
+ *
+ * <p>
+ * This has the side-effect of disabling all garbage collections while executing a critical native
+ * method. Use with extreme caution. Any long-running methods must not be marked with
+ * {@code @CriticalNative} (including usually-fast but generally unbounded methods)!
+ * </p>
+ *
+ * <p>
+ * <b>Deadlock Warning:</b> As a rule of thumb, do not acquire any locks during a critical native
+ * call if they aren't also locally released [before returning to managed code].
+ * </p>
+ *
+ * <p>
+ * Say some code does:
+ *
+ * <code>
+ * critical_native_call_to_grab_a_lock();
+ * does_some_java_work();
+ * critical_native_call_to_release_a_lock();
+ * </code>
+ *
+ * <p>
+ * This code can lead to deadlocks. Say thread 1 just finishes
+ * {@code critical_native_call_to_grab_a_lock()} and is in {@code does_some_java_work()}.
+ * GC kicks in and suspends thread 1. Thread 2 now is in
+ * {@code critical_native_call_to_grab_a_lock()} but is blocked on grabbing the
+ * native lock since it's held by thread 1. Now thread suspension can't finish
+ * since thread 2 can't be suspended since it's doing CriticalNative JNI.
+ * </p>
+ *
+ * <p>
+ * Normal natives don't have the issue since once it's executing in native code,
+ * it is considered suspended from the runtime's point of view.
+ * CriticalNative natives however don't do the state transition done by the normal natives.
+ * </p>
+ *
+ * <p>
+ * This annotation has no effect when used with non-native methods.
+ * The runtime must throw a {@code VerifierError} upon class loading if this is used with a native
+ * method that contains object parameters, an object return value, or a non-static.
+ * </p>
+ *
+ * @hide
+ */
+@Retention(RetentionPolicy.CLASS)  // Save memory, don't instantiate as an object at runtime.
+@Target(ElementType.METHOD)
+public @interface CriticalNative {}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/InvalidPropertiesFormatExceptionTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/InvalidPropertiesFormatExceptionTest.java
deleted file mode 100644
index 10bb50e..0000000
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/InvalidPropertiesFormatExceptionTest.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.harmony.tests.java.util;
-
-import java.io.NotSerializableException;
-import java.util.InvalidPropertiesFormatException;
-
-import org.apache.harmony.testframework.serialization.SerializationTest;
-
-public class InvalidPropertiesFormatExceptionTest extends
-        junit.framework.TestCase {
-
-    /**
-     * java.util.InvalidPropertiesFormatException#SerializationTest()
-     */
-    public void test_Serialization() throws Exception {
-        InvalidPropertiesFormatException ipfe = new InvalidPropertiesFormatException(
-                "Hey, this is InvalidPropertiesFormatException");
-        try {
-            SerializationTest.verifySelf(ipfe);
-        } catch (NotSerializableException e) {
-            // expected
-        }
-    }
-
-    /**
-     * {@link java.util.InvalidPropertiesFormatException#InvalidPropertiesFormatException(Throwable)}
-     */
-    public void test_Constructor_Ljava_lang_Throwable() {
-        Throwable throwable = new Throwable();
-        InvalidPropertiesFormatException exception = new InvalidPropertiesFormatException(
-                throwable);
-        assertEquals("the casue did not equals argument passed in constructor",
-                throwable, exception.getCause());
-    }
-
-}
diff --git a/include/LocalArray.h b/include/LocalArray.h
deleted file mode 100644
index 2ab708a..0000000
--- a/include/LocalArray.h
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef LOCAL_ARRAY_H_included
-#define LOCAL_ARRAY_H_included
-
-#include <cstddef>
-#include <new>
-
-/**
- * A fixed-size array with a size hint. That number of bytes will be allocated
- * on the stack, and used if possible, but if more bytes are requested at
- * construction time, a buffer will be allocated on the heap (and deallocated
- * by the destructor).
- *
- * The API is intended to be a compatible subset of C++0x's std::array.
- */
-template <size_t STACK_BYTE_COUNT>
-class LocalArray {
-public:
-    /**
-     * Allocates a new fixed-size array of the given size. If this size is
-     * less than or equal to the template parameter STACK_BYTE_COUNT, an
-     * internal on-stack buffer will be used. Otherwise a heap buffer will
-     * be allocated.
-     */
-    LocalArray(size_t desiredByteCount) : mSize(desiredByteCount) {
-        if (desiredByteCount > STACK_BYTE_COUNT) {
-            mPtr = new char[mSize];
-        } else {
-            mPtr = &mOnStackBuffer[0];
-        }
-    }
-
-    /**
-     * Frees the heap-allocated buffer, if there was one.
-     */
-    ~LocalArray() {
-        if (mPtr != &mOnStackBuffer[0]) {
-            delete[] mPtr;
-        }
-    }
-
-    // Capacity.
-    size_t size() { return mSize; }
-    bool empty() { return mSize == 0; }
-
-    // Element access.
-    char& operator[](size_t n) { return mPtr[n]; }
-    const char& operator[](size_t n) const { return mPtr[n]; }
-
-private:
-    char mOnStackBuffer[STACK_BYTE_COUNT];
-    char* mPtr;
-    size_t mSize;
-
-    // Disallow copy and assignment.
-    LocalArray(const LocalArray&);
-    void operator=(const LocalArray&);
-};
-
-#endif // LOCAL_ARRAY_H_included
diff --git a/libart/src/main/java/java/lang/reflect/AbstractMethod.java b/libart/src/main/java/java/lang/reflect/AbstractMethod.java
deleted file mode 100644
index 6e3e181..0000000
--- a/libart/src/main/java/java/lang/reflect/AbstractMethod.java
+++ /dev/null
@@ -1,306 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.lang.reflect;
-
-import com.android.dex.Dex;
-
-import java.lang.annotation.Annotation;
-import libcore.reflect.GenericSignatureParser;
-import libcore.reflect.ListOfTypes;
-import libcore.reflect.Types;
-import libcore.util.EmptyArray;
-
-/**
- * This class represents an abstract method. Abstract methods are either methods or constructors.
- * @hide
- */
-public abstract class AbstractMethod extends Executable {
-    /** Bits encoding access (e.g. public, private) as well as other runtime specific flags */
-    @SuppressWarnings("unused") // set by runtime
-    private int accessFlags;
-
-    /**
-     * The ArtMethod associated with this Method, required for dispatching due to entrypoints
-     * Classloader is held live by the declaring class.
-     * Hidden to workaround b/16828157.
-     * @hide
-     */
-    @SuppressWarnings("unused") // set by runtime
-    private long artMethod;
-
-    /** Method's declaring class */
-    @SuppressWarnings("unused") // set by runtime
-    private Class<?> declaringClass;
-
-    /** Overriden method's declaring class (same as declaringClass unless declaringClass
-     * is a proxy class)
-     */
-    @SuppressWarnings("unused") // set by runtime
-    private Class<?> declaringClassOfOverriddenMethod;
-
-    /** The method index of this method within its defining dex file */
-    @SuppressWarnings("unused") // set by runtime
-    private int dexMethodIndex;
-
-    /**
-     * Hidden to workaround b/16828157.
-     * @hide
-     */
-    protected AbstractMethod() {
-    }
-
-    /**
-     * We insert native method stubs for abstract methods so we don't have to
-     * check the access flags at the time of the method call.  This results in
-     * "native abstract" methods, which can't exist.  If we see the "abstract"
-     * flag set, clear the "native" flag.
-     *
-     * We also move the DECLARED_SYNCHRONIZED flag into the SYNCHRONIZED
-     * position, because the callers of this function are trying to convey
-     * the "traditional" meaning of the flags to their callers.
-     */
-    private static int fixMethodFlags(int flags) {
-        if ((flags & Modifier.ABSTRACT) != 0) {
-            flags &= ~Modifier.NATIVE;
-        }
-        flags &= ~Modifier.SYNCHRONIZED;
-        int ACC_DECLARED_SYNCHRONIZED = 0x00020000;
-        if ((flags & ACC_DECLARED_SYNCHRONIZED) != 0) {
-            flags |= Modifier.SYNCHRONIZED;
-        }
-        return flags & 0xffff;  // mask out bits not used by Java
-    }
-
-    // Overrides {@link Executable#getModifiers()} - for ART behavior see fixMethodFlags().
-    @Override
-    public int getModifiers() {
-        return fixMethodFlags(accessFlags);
-    }
-
-    // Overrides {@link Executable#isSynthetic()} - we can do it cheaply here.
-    @Override
-    public boolean isSynthetic() {
-        return (accessFlags & Modifier.SYNTHETIC) != 0;
-    }
-
-    // Overrides {@link Executable#isVarArgs()} - we can do it cheaply here.
-    @Override
-    public boolean isVarArgs() {
-        return (accessFlags & Modifier.VARARGS) != 0;
-    }
-
-    @Override
-    public Class<?> getDeclaringClass() {
-        return declaringClass;
-    }
-
-    /**
-     * Returns an array of {@code Class} objects associated with the parameter types of this
-     * abstract method. If the method was declared with no parameters, an
-     * empty array will be returned.
-     *
-     * @return the parameter types
-     */
-    @Override
-    public Class<?>[] getParameterTypes() {
-        Dex dex = declaringClassOfOverriddenMethod.getDex();
-        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
-        if (types.length == 0) {
-            return EmptyArray.CLASS;
-        }
-        Class<?>[] parametersArray = new Class[types.length];
-        for (int i = 0; i < types.length; i++) {
-            // Note, in the case of a Proxy the dex cache types are equal.
-            parametersArray[i] = declaringClassOfOverriddenMethod.getDexCacheType(dex, types[i]);
-        }
-        return parametersArray;
-    }
-
-    @Override
-    public int getParameterCount() {
-        Dex dex = declaringClassOfOverriddenMethod.getDex();
-        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
-        return types.length;
-    }
-
-    @Override
-    public Type[] getGenericParameterTypes() {
-        return Types.getTypeArray(
-                getMethodOrConstructorGenericInfoInternal().genericParameterTypes, false);
-    }
-
-    @Override
-    public Type[] getGenericExceptionTypes() {
-        return Types.getTypeArray(
-                getMethodOrConstructorGenericInfoInternal().genericExceptionTypes, false);
-    }
-
-    @Override public native Annotation[] getDeclaredAnnotations();
-
-    @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
-        if (annotationType == null) {
-            throw new NullPointerException("annotationType == null");
-        }
-        return isAnnotationPresentNative(annotationType);
-    }
-    private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType);
-
-    @Override
-    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
-        if (annotationClass == null) {
-            throw new NullPointerException("annotationClass == null");
-        }
-        return getAnnotationNative(annotationClass);
-    }
-    private native <T extends Annotation> T getAnnotationNative(Class<T> annotationClass);
-
-    @Override
-    public Annotation[][] getParameterAnnotations() {
-        Annotation[][] parameterAnnotations = getParameterAnnotationsNative();
-        if (parameterAnnotations == null) {
-            parameterAnnotations = new Annotation[getParameterTypes().length][0];
-        }
-        return parameterAnnotations;
-    }
-    private native Annotation[][] getParameterAnnotationsNative();
-
-    /**
-     * @hide
-     */
-    public final int getAccessFlags() {
-        return accessFlags;
-    }
-
-    static final class GenericInfo {
-        final ListOfTypes genericExceptionTypes;
-        final ListOfTypes genericParameterTypes;
-        final Type genericReturnType;
-        final TypeVariable<?>[] formalTypeParameters;
-
-        GenericInfo(ListOfTypes exceptions, ListOfTypes parameters, Type ret,
-                    TypeVariable<?>[] formal) {
-            genericExceptionTypes = exceptions;
-            genericParameterTypes = parameters;
-            genericReturnType = ret;
-            formalTypeParameters = formal;
-        }
-    }
-
-    /**
-     * Returns generic information associated with this method/constructor member.
-     */
-    final GenericInfo getMethodOrConstructorGenericInfoInternal() {
-        String signatureAttribute = getSignatureAttribute();
-        Class<?>[] exceptionTypes = this.getExceptionTypes();
-        GenericSignatureParser parser =
-            new GenericSignatureParser(this.getDeclaringClass().getClassLoader());
-        if (this instanceof Method) {
-            parser.parseForMethod(this, signatureAttribute, exceptionTypes);
-        } else {
-            parser.parseForConstructor(this, signatureAttribute, exceptionTypes);
-        }
-        return new GenericInfo(parser.exceptionTypes, parser.parameterTypes,
-                               parser.returnType, parser.formalTypeParameters);
-    }
-
-    private String getSignatureAttribute() {
-        String[] annotation = getSignatureAnnotation();
-        if (annotation == null) {
-            return null;
-        }
-        StringBuilder result = new StringBuilder();
-        for (String s : annotation) {
-            result.append(s);
-        }
-        return result.toString();
-    }
-    private native String[] getSignatureAnnotation();
-
-    final boolean equalNameAndParametersInternal(Method m) {
-        return getName().equals(m.getName()) && equalMethodParameters(m.getParameterTypes());
-    }
-
-    private boolean equalMethodParameters(Class<?>[] params) {
-        Dex dex = declaringClassOfOverriddenMethod.getDex();
-        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
-        if (types.length != params.length) {
-            return false;
-        }
-        for (int i = 0; i < types.length; i++) {
-            if (declaringClassOfOverriddenMethod.getDexCacheType(dex, types[i]) != params[i]) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    final int compareMethodParametersInternal(Class<?>[] params) {
-        Dex dex = declaringClassOfOverriddenMethod.getDex();
-        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
-        int length = Math.min(types.length, params.length);
-        for (int i = 0; i < length; i++) {
-            Class<?> aType = declaringClassOfOverriddenMethod.getDexCacheType(dex, types[i]);
-            Class<?> bType = params[i];
-            if (aType != bType) {
-                int comparison = aType.getName().compareTo(bType.getName());
-                if (comparison != 0) {
-                    return comparison;
-                }
-            }
-        }
-        return types.length - params.length;
-    }
-
-    final String getMethodNameInternal() {
-        Dex dex = declaringClassOfOverriddenMethod.getDex();
-        int nameIndex = dex.nameIndexFromMethodIndex(dexMethodIndex);
-        return declaringClassOfOverriddenMethod.getDexCacheString(dex, nameIndex);
-    }
-
-    final Class<?> getMethodReturnTypeInternal() {
-        Dex dex = declaringClassOfOverriddenMethod.getDex();
-        int returnTypeIndex = dex.returnTypeIndexFromMethodIndex(dexMethodIndex);
-        // Note, in the case of a Proxy the dex cache types are equal.
-        return declaringClassOfOverriddenMethod.getDexCacheType(dex, returnTypeIndex);
-    }
-
-    /** A cheap implementation for {@link Method#isDefault()}. */
-    final boolean isDefaultMethodInternal() {
-        return (accessFlags & Modifier.DEFAULT) != 0;
-    }
-
-    /** A cheap implementation for {@link Method#isBridge()}. */
-    final boolean isBridgeMethodInternal() {
-        return (accessFlags & Modifier.BRIDGE) != 0;
-    }
-}
diff --git a/luni/src/main/java/libcore/io/Base64.java b/luni/src/main/java/libcore/io/Base64.java
index 236c166..f22bef4 100644
--- a/luni/src/main/java/libcore/io/Base64.java
+++ b/luni/src/main/java/libcore/io/Base64.java
@@ -22,7 +22,10 @@
 /**
  * Perform encoding and decoding of Base64 byte arrays as described in
  * http://www.ietf.org/rfc/rfc2045.txt
+ *
+ * @deprecated use {@link java.util.Base64} instead
  */
+@Deprecated
 public final class Base64 {
     private static final byte[] BASE_64_ALPHABET = initializeBase64Alphabet();
 
diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp
index 5fd4a7d..52df40e 100644
--- a/luni/src/main/native/Register.cpp
+++ b/luni/src/main/native/Register.cpp
@@ -53,3 +53,20 @@
 
     return JNI_VERSION_1_6;
 }
+
+// DalvikVM calls this on shutdown, do any global cleanup here.
+// -- Very important if we restart multiple DalvikVMs in the same process to reset the state.
+void JNI_OnUnload(JavaVM* vm, void*) {
+    JNIEnv* env;
+    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
+        ALOGE("JavaVM::GetEnv() failed");
+        abort();
+    }
+    ALOGV("libjavacore JNI_OnUnload");
+
+    ScopedLocalFrame localFrame(env);
+
+#define UNREGISTER(FN) extern void FN(JNIEnv*); FN(env)
+    UNREGISTER(unregister_libcore_icu_ICU);
+#undef UNREGISTER
+}
diff --git a/luni/src/main/native/libcore_icu_ICU.cpp b/luni/src/main/native/libcore_icu_ICU.cpp
index 3eda923..e6f378e 100644
--- a/luni/src/main/native/libcore_icu_ICU.cpp
+++ b/luni/src/main/native/libcore_icu_ICU.cpp
@@ -838,56 +838,110 @@
     NATIVE_METHOD(ICU, toUpperCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
 };
 
+//
+// Global initialization & Teardown for ICU Setup
+//   - Contains handlers for JNI_OnLoad and JNI_OnUnload
+//
+
 #define FAIL_WITH_STRERROR(s) \
-    ALOGE("Couldn't " s " '%s': %s", path.c_str(), strerror(errno)); \
+    ALOGE("Couldn't " s " '%s': %s", path_.c_str(), strerror(errno)); \
     return FALSE;
 
 #define MAYBE_FAIL_WITH_ICU_ERROR(s) \
     if (status != U_ZERO_ERROR) {\
-        ALOGE("Couldn't initialize ICU (" s "): %s (%s)", u_errorName(status), path.c_str()); \
+        ALOGE("Couldn't initialize ICU (" s "): %s (%s)", u_errorName(status), path_.c_str()); \
         return FALSE; \
     }
 
-static bool mapIcuData(const std::string& path) {
+// Contain the memory map for ICU data files.
+// Automatically adds the data file to ICU's list of data files upon constructing.
+//
+// - Automatically unmaps in the destructor.
+struct IcuDataMap {
+  // Map in ICU data at the path, returning null if it failed (prints error to ALOGE).
+  static std::unique_ptr<IcuDataMap> Create(const std::string& path) {
+    std::unique_ptr<IcuDataMap> map(new IcuDataMap(path));
+
+    if (!map->TryMap()) {
+      // madvise or ICU could fail but mmap still succeeds.
+      // Destructor will take care of cleaning up a partial init.
+      return nullptr;
+    }
+
+    return map;
+  }
+
+  // Unmap the ICU data.
+  ~IcuDataMap() {
+    TryUnmap();
+  }
+
+ private:
+  IcuDataMap(const std::string& path)
+    : path_(path),
+      data_(MAP_FAILED),
+      data_length_(0)
+  {}
+
+  bool TryMap() {
     // Open the file and get its length.
-    android::base::unique_fd fd(open(path.c_str(), O_RDONLY));
+    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path_.c_str(), O_RDONLY)));
+
     if (fd.get() == -1) {
         FAIL_WITH_STRERROR("open");
     }
+
     struct stat sb;
     if (fstat(fd.get(), &sb) == -1) {
         FAIL_WITH_STRERROR("stat");
     }
 
+    data_length_ = sb.st_size;
+
     // Map it.
-    void* data = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd.get(), 0);
-    if (data == MAP_FAILED) {
+    data_ = mmap(NULL, data_length_, PROT_READ, MAP_SHARED, fd.get(), 0  /* offset */);
+    if (data_ == MAP_FAILED) {
         FAIL_WITH_STRERROR("mmap");
     }
 
     // Tell the kernel that accesses are likely to be random rather than sequential.
-    if (madvise(data, sb.st_size, MADV_RANDOM) == -1) {
+    if (madvise(data_, data_length_, MADV_RANDOM) == -1) {
         FAIL_WITH_STRERROR("madvise(MADV_RANDOM)");
     }
 
     UErrorCode status = U_ZERO_ERROR;
 
     // Tell ICU to use our memory-mapped data.
-    udata_setCommonData(data, &status);
+    udata_setCommonData(data_, &status);
     MAYBE_FAIL_WITH_ICU_ERROR("udata_setCommonData");
 
-    return TRUE;
-}
+    return true;
+  }
 
-void register_libcore_icu_ICU(JNIEnv* env) {
-    // Check the timezone override file exists. If it does, map it first so we use it in preference
-    // to the one that shipped with the device.
-    const char* dataPathPrefix = getenv("ANDROID_DATA");
-    if (dataPathPrefix == NULL) {
-        ALOGE("ANDROID_DATA environment variable not set"); \
-        abort();
+  bool TryUnmap() {
+    // Don't need to do opposite of udata_setCommonData,
+    // u_cleanup (performed in unregister_libcore_icu_ICU) takes care of it.
+
+    // Don't need to opposite of madvise, munmap will take care of it.
+
+    if (data_ != MAP_FAILED) {
+      if (munmap(data_, data_length_) == -1) {
+        FAIL_WITH_STRERROR("munmap");
+      }
     }
 
+    // Don't need to close the file, it was closed automatically during TryMap.
+    return true;
+  }
+
+  std::string path_;    // Save for error messages.
+  void* data_;          // Save for munmap.
+  size_t data_length_;  // Save for munmap.
+};
+
+struct ICURegistration {
+  // Init ICU, configuring it and loading the data files.
+  ICURegistration(JNIEnv* env) {
     UErrorCode status = U_ZERO_ERROR;
     // Tell ICU it can *only* use our memory-mapped data.
     udata_setFileAccess(UDATA_NO_FILES, &status);
@@ -896,15 +950,13 @@
         abort();
     }
 
-    // Map in optional TZ data files.
-    std::string dataPath;
-    dataPath = dataPathPrefix;
-    dataPath += "/misc/zoneinfo/current/icu/icu_tzdata.dat";
+    std::string dataPath = getTzDataOverridePath();
 
+    // Map in optional TZ data files.
     struct stat sb;
     if (stat(dataPath.c_str(), &sb) == 0) {
         ALOGD("Timezone override file found: %s", dataPath.c_str());
-        if (!mapIcuData(dataPath)) {
+        if ((icu_datamap_from_data_ = IcuDataMap::Create(dataPath)) == nullptr) {
             ALOGW("TZ override file %s exists but could not be loaded. Skipping.", dataPath.c_str());
         }
     } else {
@@ -912,18 +964,7 @@
     }
 
     // Use the ICU data files that shipped with the device for everything else.
-    const char* systemPathPrefix = getenv("ANDROID_ROOT");
-    if (systemPathPrefix == NULL) {
-        ALOGE("ANDROID_ROOT environment variable not set"); \
-        abort();
-    }
-    std::string systemPath;
-    systemPath = systemPathPrefix;
-    systemPath += "/usr/icu/";
-    systemPath += U_ICUDATA_NAME;
-    systemPath += ".dat";
-
-    if (!mapIcuData(systemPath)) {
+    if ((icu_datamap_from_system_ = IcuDataMap::Create(getSystemPath())) == nullptr) {
         abort();
     }
 
@@ -937,4 +978,69 @@
     }
 
     jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods));
+  }
+
+  // De-init ICU, unloading the data files. Do the opposite of the above function.
+  ~ICURegistration() {
+    // Skip unregistering JNI methods explicitly, class unloading takes care of it.
+
+    // Reset libicu state to before it was loaded.
+    u_cleanup();
+
+    // Unmap ICU data files that shipped with the device for everything else.
+    icu_datamap_from_system_.reset();
+
+    // Unmap optional TZ data files.
+    icu_datamap_from_data_.reset();
+
+    // We don't need to call udata_setFileAccess because u_cleanup takes care of it.
+  }
+
+  // Check the timezone override file exists. If it does, map it first so we use it in preference
+  // to the one that shipped with the device.
+  static std::string getTzDataOverridePath() {
+    const char* dataPathPrefix = getenv("ANDROID_DATA");
+    if (dataPathPrefix == NULL) {
+      ALOGE("ANDROID_DATA environment variable not set"); \
+      abort();
+    }
+    std::string dataPath;
+    dataPath = dataPathPrefix;
+    dataPath += "/misc/zoneinfo/current/icu/icu_tzdata.dat";
+
+    return dataPath;
+  }
+
+  static std::string getSystemPath() {
+    const char* systemPathPrefix = getenv("ANDROID_ROOT");
+    if (systemPathPrefix == NULL) {
+      ALOGE("ANDROID_ROOT environment variable not set"); \
+      abort();
+    }
+
+    std::string systemPath;
+    systemPath = systemPathPrefix;
+    systemPath += "/usr/icu/";
+    systemPath += U_ICUDATA_NAME;
+    systemPath += ".dat";
+    return systemPath;
+  }
+
+  std::unique_ptr<IcuDataMap> icu_datamap_from_data_;
+  std::unique_ptr<IcuDataMap> icu_datamap_from_system_;
+};
+
+// Use RAII-style initialization/teardown so that we can get unregistered
+// when dlclose is called (even if JNI_OnUnload is not).
+static std::unique_ptr<ICURegistration> sIcuRegistration;
+
+// Init ICU, configuring it and loading the data files.
+void register_libcore_icu_ICU(JNIEnv* env) {
+  sIcuRegistration.reset(new ICURegistration(env));
+}
+
+// De-init ICU, unloading the data files. Do the opposite of the above function.
+void unregister_libcore_icu_ICU(JNIEnv*) {
+  // Explicitly calling this is optional. Dlclose will take care of it as well.
+  sIcuRegistration.reset();
 }
diff --git a/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp b/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
index 820bda62..e27af69 100644
--- a/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
+++ b/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
@@ -19,7 +19,6 @@
 #include "JNIHelp.h"
 #include "JniConstants.h"
 #include "JniException.h"
-#include "LocalArray.h"
 #include "ScopedLocalRef.h"
 #include "ScopedPrimitiveArray.h"
 #include "ScopedStringChars.h"
@@ -33,6 +32,8 @@
 #include <string.h>
 #include <expat.h>
 
+#include <android-base/stringprintf.h>
+
 #define BUCKET_COUNT 128
 
 /**
@@ -519,9 +520,8 @@
         }
 
         // return prefix + ":" + localName
-        ::LocalArray<1024> qName(strlen(mPrefix) + 1 + strlen(mLocalName) + 1);
-        snprintf(&qName[0], qName.size(), "%s:%s", mPrefix, mLocalName);
-        return internString(mEnv, mParsingContext, &qName[0]);
+        auto qName = android::base::StringPrintf("%s:%s", mPrefix, mLocalName);
+        return internString(mEnv, mParsingContext, qName.c_str());
     }
 
     /**
diff --git a/luni/src/test/java/libcore/io/OsTest.java b/luni/src/test/java/libcore/io/OsTest.java
index 648ac2a..687d903 100644
--- a/luni/src/test/java/libcore/io/OsTest.java
+++ b/luni/src/test/java/libcore/io/OsTest.java
@@ -596,8 +596,6 @@
   }
 
   public void test_xattr_Errno() throws Exception {
-    File file = File.createTempFile("xattr", "test");
-    final String path = file.getAbsolutePath();
     final String NAME_TEST = "user.meow";
     final byte[] VALUE_CAKE = "cake cake cake".getBytes(StandardCharsets.UTF_8);
 
@@ -606,53 +604,54 @@
       Libcore.os.getxattr("", NAME_TEST);
       fail();
     } catch (ErrnoException e) {
-      assertEquals(OsConstants.ENOENT, e.errno);
+      assertEquals(ENOENT, e.errno);
     }
     try {
       Libcore.os.listxattr("");
       fail();
     } catch (ErrnoException e) {
-      assertEquals(OsConstants.ENOENT, e.errno);
+      assertEquals(ENOENT, e.errno);
     }
     try {
       Libcore.os.removexattr("", NAME_TEST);
       fail();
     } catch (ErrnoException e) {
-      assertEquals(OsConstants.ENOENT, e.errno);
+      assertEquals(ENOENT, e.errno);
     }
     try {
       Libcore.os.setxattr("", NAME_TEST, VALUE_CAKE, OsConstants.XATTR_CREATE);
       fail();
     } catch (ErrnoException e) {
-      assertEquals(OsConstants.ENOENT, e.errno);
+      assertEquals(ENOENT, e.errno);
     }
 
     // ENOTSUP, Extended attributes are not supported by the filesystem, or are disabled.
+    final boolean root = (Libcore.os.getuid() == 0);
+    final String path = "/proc/self/stat";
     try {
-      Libcore.os.getxattr("/proc/version", NAME_TEST);
+      Libcore.os.setxattr(path, NAME_TEST, VALUE_CAKE, OsConstants.XATTR_CREATE);
       fail();
     } catch (ErrnoException e) {
-      assertEquals(OsConstants.ENOTSUP, e.errno);
+      // setxattr(2) requires root permission for writing to this file, will get EACCES otherwise.
+      assertEquals(root ? ENOTSUP : EACCES, e.errno);
+    }
+    try {
+      Libcore.os.getxattr(path, NAME_TEST);
+      fail();
+    } catch (ErrnoException e) {
+      assertEquals(ENOTSUP, e.errno);
     }
     try {
       // Linux listxattr does not set errno.
-      Libcore.os.listxattr("/proc/version");
+      Libcore.os.listxattr(path);
     } catch (ErrnoException e) {
       fail();
     }
     try {
-      Libcore.os.removexattr("/proc/version", "security.selinux");
+      Libcore.os.removexattr(path, NAME_TEST);
       fail();
     } catch (ErrnoException e) {
-      assertEquals(OsConstants.ENOTSUP, e.errno);
-    }
-    try {
-      Libcore.os.setxattr("/proc/version", NAME_TEST, VALUE_CAKE, OsConstants.XATTR_CREATE);
-      fail();
-    } catch (ErrnoException e) {
-      // For setxattr, EACCES or ENOTSUP is set depending on whether running with root permission.
-      assertTrue(e.errno == OsConstants.EACCES ||
-                 e.errno == OsConstants.ENOTSUP);
+      assertEquals(ENOTSUP, e.errno);
     }
   }
 
diff --git a/luni/src/test/java/libcore/java/lang/OldSystemTest.java b/luni/src/test/java/libcore/java/lang/OldSystemTest.java
index f28c704..dc5741f 100644
--- a/luni/src/test/java/libcore/java/lang/OldSystemTest.java
+++ b/luni/src/test/java/libcore/java/lang/OldSystemTest.java
@@ -321,13 +321,14 @@
         } catch(NullPointerException expected) {
         }
 
-        // Trivial positive test for System.load: Attempt to load a libc++.so - it's guaranteed
-        // to exist and is whitelisted for use from applications.
+        // Trivial positive test for System.load: Attempt to load a liblog.so - it's guaranteed
+        // to exist and is whitelisted for use from applications. Also, it's in the library search
+        // path for host builds.
         final ClassLoader cl = getClass().getClassLoader();
         // ClassLoader.findLibrary has protected access, so it's guaranteed to exist.
         final Method m = ClassLoader.class.getDeclaredMethod("findLibrary", String.class);
         assertNotNull(m);
-        String libPath = (String) m.invoke(cl, "c++");
+        String libPath = (String) m.invoke(cl, "log");
         assertNotNull(libPath);
         System.load(new File(libPath).getAbsolutePath());
 
diff --git a/luni/src/test/java/libcore/java/lang/invoke/MethodHandlesTest.java b/luni/src/test/java/libcore/java/lang/invoke/MethodHandlesTest.java
new file mode 100644
index 0000000..33555f7
--- /dev/null
+++ b/luni/src/test/java/libcore/java/lang/invoke/MethodHandlesTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package libcore.java.lang.invoke;
+
+import junit.framework.TestCase;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.util.Vector;
+
+import static java.lang.invoke.MethodHandles.Lookup.*;
+
+public class MethodHandlesTest extends TestCase {
+    private static final int ALL_LOOKUP_MODES = (PUBLIC | PRIVATE | PACKAGE | PROTECTED);
+
+    public void test_publicLookupClassAndModes() {
+        MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
+        assertSame(Object.class, publicLookup.lookupClass());
+        assertEquals(PUBLIC, publicLookup.lookupModes());
+    }
+
+    public void test_defaultLookupClassAndModes() {
+        MethodHandles.Lookup defaultLookup = MethodHandles.lookup();
+        assertSame(MethodHandlesTest.class, defaultLookup.lookupClass());
+        assertEquals(ALL_LOOKUP_MODES, defaultLookup.lookupModes());
+    }
+
+    public void test_LookupIn() {
+        MethodHandles.Lookup defaultLookup = MethodHandles.lookup();
+
+        // A class in the same package loses the privilege to lookup protected and private
+        // members.
+        MethodHandles.Lookup siblingLookup = defaultLookup.in(PackageSibling.class);
+        assertEquals(ALL_LOOKUP_MODES & ~(PROTECTED | PRIVATE),  siblingLookup.lookupModes());
+
+        // The new lookup isn't in the same package, so it loses all its privileges except
+        // for public.
+        MethodHandles.Lookup nonSibling = defaultLookup.in(Vector.class);
+        assertEquals(PUBLIC, nonSibling.lookupModes());
+
+        // Special case, sibling inner classes in the same parent class
+        MethodHandles.Lookup inner2 = Inner1.lookup.in(Inner2.class);
+        assertEquals(PUBLIC | PRIVATE | PACKAGE, inner2.lookupModes());
+
+        try {
+            MethodHandles.lookup().in(null);
+            fail();
+        } catch (NullPointerException expected) {
+        }
+
+        // Callers cannot change the lookup context to anything within the java.lang.invoke package.
+        try {
+            MethodHandles.lookup().in(MethodHandle.class);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    public static class Inner1 {
+        public static MethodHandles.Lookup lookup = MethodHandles.lookup();
+    }
+
+    public static class Inner2 {
+    }
+}
+
+class PackageSibling {
+}
+
diff --git a/luni/src/test/java/libcore/java/lang/reflect/ConstructorTest.java b/luni/src/test/java/libcore/java/lang/reflect/ConstructorTest.java
index 3093bbd..8d7ed71 100644
--- a/luni/src/test/java/libcore/java/lang/reflect/ConstructorTest.java
+++ b/luni/src/test/java/libcore/java/lang/reflect/ConstructorTest.java
@@ -17,6 +17,8 @@
 package libcore.java.lang.reflect;
 
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Parameter;
+
 import junit.framework.TestCase;
 
 public final class ConstructorTest extends TestCase {
@@ -33,8 +35,12 @@
     }
 
     public void test_getParameterTypes() throws Exception {
-        Class[] expectedParameters = new Class[] { Object.class };
+        Class[] expectedParameters = new Class[0];
         Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
+        assertEquals(0, constructor.getParameterTypes().length);
+
+        expectedParameters = new Class[] { Object.class };
+        constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
         Class[] parameters = constructor.getParameterTypes();
         assertEquals(1, parameters.length);
         assertEquals(expectedParameters[0], parameters[0]);
@@ -46,12 +52,37 @@
     }
 
     public void test_getParameterCount() throws Exception {
-        Class[] expectedParameters = new Class[] { Object.class };
+        Class[] expectedParameters = new Class[0];
         Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
+        assertEquals(0, constructor.getParameterCount());
+
+        expectedParameters = new Class[] { Object.class };
+        constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
         int count = constructor.getParameterCount();
         assertEquals(1, count);
     }
 
+    public void test_getParameters() throws Exception {
+        Class[] expectedParameters = new Class[0];
+        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
+        assertEquals(0, constructor.getParameters().length);
+
+        expectedParameters = new Class[] { Object.class };
+        constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
+
+        // Test the information available via other Constructor methods. See ParameterTest and
+        // annotations.ParameterTest for more in-depth Parameter testing.
+        Parameter[] parameters = constructor.getParameters();
+        assertEquals(1, parameters.length);
+        assertEquals(Object.class, parameters[0].getType());
+
+        // Check that corrupting our array doesn't affect other callers.
+        parameters[0] = null;
+        parameters = constructor.getParameters();
+        assertEquals(1, parameters.length);
+        assertEquals(Object.class, parameters[0].getType());
+    }
+
     public void testGetConstructorWithNullArgumentsArray() throws Exception {
         Constructor<?> constructor = ConstructorTestHelper.class.getConstructor((Class[]) null);
         assertEquals(0, constructor.getParameterTypes().length);
diff --git a/luni/src/test/java/libcore/java/lang/reflect/MethodTest.java b/luni/src/test/java/libcore/java/lang/reflect/MethodTest.java
index 5cb88d6..315a885 100644
--- a/luni/src/test/java/libcore/java/lang/reflect/MethodTest.java
+++ b/luni/src/test/java/libcore/java/lang/reflect/MethodTest.java
@@ -19,6 +19,7 @@
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.lang.reflect.Parameter;
 import java.lang.reflect.Proxy;
 import java.util.Collections;
 import java.util.Comparator;
@@ -41,8 +42,12 @@
     }
 
     public void test_getParameterTypes() throws Exception {
-        Class[] expectedParameters = new Class[] { Object.class };
-        Method method = MethodTestHelper.class.getMethod("m2", expectedParameters);
+        Class[] expectedParameters = new Class[0];
+        Method method = MethodTestHelper.class.getMethod("m1", expectedParameters);
+        assertEquals(0, method.getParameterTypes().length);
+
+        expectedParameters = new Class[] { Object.class };
+        method = MethodTestHelper.class.getMethod("m2", expectedParameters);
         Class[] parameters = method.getParameterTypes();
         assertEquals(1, parameters.length);
         assertEquals(expectedParameters[0], parameters[0]);
@@ -54,12 +59,37 @@
     }
 
     public void test_getParameterCount() throws Exception {
-        Class[] expectedParameters = new Class[] { Object.class };
-        Method method = MethodTestHelper.class.getMethod("m2", expectedParameters);
+        Class[] expectedParameters = new Class[0];
+        Method method = MethodTestHelper.class.getMethod("m1", expectedParameters);
+        assertEquals(0, method.getParameterCount());
+
+        expectedParameters = new Class[] { Object.class };
+        method = MethodTestHelper.class.getMethod("m2", expectedParameters);
         int count = method.getParameterCount();
         assertEquals(1, count);
     }
 
+    public void test_getParameters() throws Exception {
+        Class[] expectedParameters = new Class[0];
+        Method method = MethodTestHelper.class.getMethod("m1", expectedParameters);
+        assertEquals(0, method.getParameters().length);
+
+        expectedParameters = new Class[] { Object.class };
+        method = MethodTestHelper.class.getMethod("m2", expectedParameters);
+
+        // Test the information available via other Method methods. See ParameterTest and
+        // annotations.ParameterTest for more in-depth Parameter testing.
+        Parameter[] parameters = method.getParameters();
+        assertEquals(1, parameters.length);
+        assertEquals(Object.class, parameters[0].getType());
+
+        // Check that corrupting our array doesn't affect other callers.
+        parameters[0] = null;
+        parameters = method.getParameters();
+        assertEquals(1, parameters.length);
+        assertEquals(Object.class, parameters[0].getType());
+    }
+
     public void testGetMethodWithPrivateMethodAndInterfaceMethod() throws Exception {
         assertEquals(InterfaceA.class, Sub.class.getMethod("a").getDeclaringClass());
     }
diff --git a/luni/src/test/java/libcore/java/lang/reflect/ParameterTest.java b/luni/src/test/java/libcore/java/lang/reflect/ParameterTest.java
new file mode 100644
index 0000000..d5ff5b5
--- /dev/null
+++ b/luni/src/test/java/libcore/java/lang/reflect/ParameterTest.java
@@ -0,0 +1,671 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.lang.reflect;
+
+import junit.framework.TestCase;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+import java.util.Arrays;
+import java.util.concurrent.Callable;
+import java.util.function.Function;
+
+/**
+ * Tests for {@link Parameter}. For annotation-related tests see
+ * {@link libcore.java.lang.reflect.annotations.AnnotatedElementParameterTest} and
+ * {@link libcore.java.lang.reflect.annotations.ExecutableParameterTest}.
+ */
+public class ParameterTest extends TestCase {
+
+    /**
+     * A source annotation used to mark tests below with behavior that is highly dependent on
+     * parameter metadata. It is intended to bring readers here for the following:
+     *
+     * <p>Unless the compiler supports (and is configured to enable) storage of metadata
+     * for parameters, the runtime does not have access to the parameter name from the source and
+     * some modifier information like "implicit" (AKA "mandated"), "synthetic" and "final".
+     * These tests are currently expected to be compiled without requesting the metadata and can
+     * only test the negative case without the metadata present (the expected, common case).
+     */
+    @Retention(RetentionPolicy.SOURCE)
+    @Target(ElementType.METHOD)
+    private @interface DependsOnParameterMetadata {}
+
+    private static class SingleParameter {
+        @SuppressWarnings("unused")
+        SingleParameter(String p0) {}
+
+        @SuppressWarnings("unused")
+        void oneParameter(String p0) {}
+    }
+
+    public void testSingleParameterConstructor() throws Exception {
+        Constructor<?> constructor = SingleParameter.class.getDeclaredConstructor(String.class);
+        checkSingleStringParameter(constructor);
+    }
+
+    public void testSingleParameterMethod() throws Exception {
+        Method method = SingleParameter.class.getDeclaredMethod("oneParameter", String.class);
+        checkSingleStringParameter(method);
+    }
+
+    private static void checkSingleStringParameter(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.String arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(String.class)
+                .checkGetParameterizedType("class java.lang.String");
+    }
+
+    private static class GenericParameter {
+        @SuppressWarnings("unused")
+        GenericParameter(Function<String, Integer> p0) {}
+
+        @SuppressWarnings("unused")
+        void genericParameter(Function<String, Integer> p0) {}
+    }
+
+    public void testGenericParameterConstructor() throws Exception {
+        Constructor<?> constructor = GenericParameter.class.getDeclaredConstructor(Function.class);
+        checkGenericParameter(constructor);
+    }
+
+    public void testGenericParameterMethod() throws Exception {
+        Method method = GenericParameter.class.getDeclaredMethod(
+                "genericParameter", Function.class);
+        checkGenericParameter(method);
+    }
+
+    private static void checkGenericParameter(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString(
+                        "[java.util.function.Function<java.lang.String, java.lang.Integer> arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(Function.class)
+                .checkGetParameterizedType(
+                        "java.util.function.Function<java.lang.String, java.lang.Integer>");
+    }
+
+    private static class TwoParameters {
+        @SuppressWarnings("unused")
+        TwoParameters(String p0, Integer p1) {}
+        @SuppressWarnings("unused")
+        void twoParameters(String p0, Integer p1) {}
+    }
+
+    public void testTwoParameterConstructor() throws Exception {
+        Constructor<?> constructor =
+                TwoParameters.class.getDeclaredConstructor(String.class, Integer.class);
+        checkTwoParameters(constructor);
+    }
+
+    public void testTwoParameterMethod() throws Exception {
+        Method method = TwoParameters.class.getDeclaredMethod(
+                "twoParameters", String.class, Integer.class);
+        checkTwoParameters(method);
+    }
+
+    private static void checkTwoParameters(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.String arg0, java.lang.Integer arg1]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(String.class)
+                .checkGetParameterizedType("class java.lang.String");
+
+        helper.getParameterTestHelper(1)
+                .checkGetType(Integer.class)
+                .checkGetParameterizedType("class java.lang.Integer");
+    }
+
+    private static class FinalParameter {
+        @SuppressWarnings("unused")
+        FinalParameter(final String p0) {}
+        @SuppressWarnings("unused")
+        void finalParameter(final String p0) {}
+    }
+
+    public void testFinalParameterConstructor() throws Exception {
+        Constructor<?> constructor = FinalParameter.class.getDeclaredConstructor(String.class);
+        checkFinalParameter(constructor);
+    }
+
+    public void testFinalParameterMethod() throws Exception {
+        Method method = FinalParameter.class.getDeclaredMethod("finalParameter", String.class);
+        checkFinalParameter(method);
+    }
+
+    private static void checkFinalParameter(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.String arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(String.class)
+                .checkGetParameterizedType("class java.lang.String");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0).checkModifiers(Modifier.FINAL);
+    }
+
+    /**
+     * An inner class, used for checking compiler-inserted parameters: The first parameter is an
+     * instance of the surrounding class.
+     */
+    private class InnerClass {
+        @SuppressWarnings("unused")
+        public InnerClass() {}
+        @SuppressWarnings("unused")
+        public InnerClass(String p1) {}
+        @SuppressWarnings("unused")
+        public InnerClass(Function<String, Integer> p1) {}
+    }
+
+    public void testInnerClassSingleParameter() throws Exception {
+        Constructor<?> constructor =
+                InnerClass.class.getDeclaredConstructor(ParameterTest.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[libcore.java.lang.reflect.ParameterTest arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(ParameterTest.class)
+                .checkGetParameterizedType("class libcore.java.lang.reflect.ParameterTest");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(32784)
+        //         .checkImplicitAndSynthetic(true, false);
+        // i.e. 32784 == Modifier.MANDATED & Modifier.FINAL
+    }
+
+    public void testInnerClassTwoParameters() throws Exception {
+        Constructor<?> constructor =
+                InnerClass.class.getDeclaredConstructor(ParameterTest.class, String.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString(
+                        "[libcore.java.lang.reflect.ParameterTest arg0, java.lang.String arg1]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(ParameterTest.class)
+                .checkGetParameterizedType("class libcore.java.lang.reflect.ParameterTest");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(32784)
+        //         .checkImplicitAndSynthetic(true, false);
+        // i.e. 32784 == Modifier.MANDATED & Modifier.FINAL
+
+
+        helper.getParameterTestHelper(1)
+                .checkGetType(String.class)
+                .checkGetParameterizedType("class java.lang.String");
+    }
+
+    public void testInnerClassGenericParameter() throws Exception {
+        Constructor<?> constructor =
+                InnerClass.class.getDeclaredConstructor(
+                        ParameterTest.class, Function.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString(
+                        "[libcore.java.lang.reflect.ParameterTest arg0, "
+                                + "java.util.function.Function arg1]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(ParameterTest.class)
+                .checkGetParameterizedType("class libcore.java.lang.reflect.ParameterTest");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(32784)
+        //         .checkImplicitAndSynthetic(true, false);
+        // i.e. 32784 == Modifier.MANDATED & Modifier.FINAL
+
+        helper.getParameterTestHelper(1)
+                .checkGetType(Function.class)
+                .checkGetParameterizedType("interface java.util.function.Function");
+
+        // The non-genericised string above is probably the result of a bug due to a mismatch
+        // between the generic signature for the constructor (which suggests a single parameter)
+        // and the actual parameters (which suggests two).
+    }
+
+    @SuppressWarnings("unused")
+    enum TestEnum { ONE, TWO }
+
+    /**
+     * Enums are a documented example of a type of class with synthetic constructor parameters and
+     * generated methods. This test may be brittle as it may rely on the compiler's implementation
+     * of enums.
+     */
+    @DependsOnParameterMetadata
+    public void testEnumConstructor() throws Exception {
+        Constructor<?> constructor = TestEnum.class.getDeclaredConstructor(String.class, int.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.String arg0, int arg1]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(String.class)
+                .checkGetParameterizedType("class java.lang.String");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(4096)
+        //         .checkImplicitAndSynthetic(false, true);
+        // i.e. 4096 == Modifier.SYNTHETIC
+
+        helper.getParameterTestHelper(1)
+                .checkGetType(int.class)
+                .checkGetParameterizedType("int");
+    }
+
+    public void testEnumValueOf() throws Exception {
+        Method method = TestEnum.class.getDeclaredMethod("valueOf", String.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(method);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.String arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(String.class)
+                .checkGetParameterizedType("class java.lang.String");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(32768)
+        //         .checkImplicitAndSynthetic(true, false);
+        // i.e. 32768 == Modifier.MANDATED
+    }
+
+    private static class SingleVarArgs {
+        @SuppressWarnings("unused")
+        SingleVarArgs(String... p0) {}
+
+        @SuppressWarnings("unused")
+        void varArgs(String... p0) {}
+    }
+
+    public void testSingleVarArgsConstructor() throws Exception {
+        Constructor<?> constructor = SingleVarArgs.class.getDeclaredConstructor(String[].class);
+        checkSingleVarArgsParameter(constructor);
+    }
+
+    public void testSingleVarArgsMethod() throws Exception {
+        Method method = SingleVarArgs.class.getDeclaredMethod("varArgs", String[].class);
+        checkSingleVarArgsParameter(method);
+    }
+
+    private static void checkSingleVarArgsParameter(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.String... arg0]")
+                .checkParametersMetadataNotAvailable();
+
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(String[].class)
+                .checkIsVarArg(true)
+                .checkGetParameterizedType("class [Ljava.lang.String;");
+    }
+
+    private static class MixedVarArgs {
+        @SuppressWarnings("unused")
+        MixedVarArgs(Integer[] p0, String... p1) {}
+        @SuppressWarnings("unused")
+        void both(Integer[] p0, String... p1) {}
+    }
+
+    public void testMixedVarArgsConstructor() throws Exception {
+        Constructor<?> constructor =
+                MixedVarArgs.class.getDeclaredConstructor(Integer[].class, String[].class);
+        checkMixedVarArgsParameter(constructor);
+    }
+
+    public void testMixedVarArgsMethod() throws Exception {
+        Method method = MixedVarArgs.class.getDeclaredMethod("both", Integer[].class, String[].class);
+        checkMixedVarArgsParameter(method);
+    }
+
+    private static void checkMixedVarArgsParameter(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.Integer[] arg0, java.lang.String... arg1]")
+                .checkParametersMetadataNotAvailable();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(Integer[].class)
+                .checkIsVarArg(false)
+                .checkGetParameterizedType("class [Ljava.lang.Integer;");
+
+        helper.getParameterTestHelper(1)
+                .checkGetType(String[].class)
+                .checkIsVarArg(true)
+                .checkGetParameterizedType("class [Ljava.lang.String;");
+    }
+
+    private static class NonVarArgs {
+        @SuppressWarnings("unused")
+        NonVarArgs(Integer[] p0) {}
+        @SuppressWarnings("unused")
+        void notVarArgs(Integer[] p0) {}
+    }
+
+    public void testNonVarsArgsConstructor() throws Exception {
+        Constructor<?> constructor = NonVarArgs.class.getDeclaredConstructor(Integer[].class);
+        checkNonVarsArgsParameter(constructor);
+    }
+
+    public void testNonVarsArgsMethod() throws Exception {
+        Method method = NonVarArgs.class.getDeclaredMethod("notVarArgs", Integer[].class);
+        checkNonVarsArgsParameter(method);
+    }
+
+    private static void checkNonVarsArgsParameter(Executable executable) {
+        ExecutableTestHelper helper = new ExecutableTestHelper(executable);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[java.lang.Integer[] arg0]")
+                .checkParametersMetadataNotAvailable();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(Integer[].class)
+                .checkIsVarArg(false)
+                .checkGetParameterizedType("class [Ljava.lang.Integer;");
+    }
+
+    public void testAnonymousClassConstructor() throws Exception {
+        Class<?> clazz = getAnonymousClassWith1ParameterConstructor();
+        Constructor<?> constructor = clazz.getDeclaredConstructor(ParameterTest.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[libcore.java.lang.reflect.ParameterTest arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(ParameterTest.class)
+                .checkGetParameterizedType("class libcore.java.lang.reflect.ParameterTest");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(32784)
+        //         .checkImplicitAndSynthetic(true, false);
+        // i.e. 32784 == Modifier.MANDATED & Modifier.FINAL
+    }
+
+    private Class<?> getAnonymousClassWith1ParameterConstructor() {
+        // Deliberately not implemented with a lambda. Do not refactor.
+        Callable<String> anonymousClassObject = new Callable<String>() {
+            @Override
+            public String call() throws Exception {
+                return ParameterTest.this.outerClassMethod();
+            }
+        };
+        return anonymousClassObject.getClass();
+    }
+
+    public void testMethodClassConstructor() throws Exception {
+        Class<?> clazz = getMethodClassWith1ImplicitParameterConstructor();
+        Constructor<?> constructor = clazz.getDeclaredConstructor(ParameterTest.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[libcore.java.lang.reflect.ParameterTest arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(ParameterTest.class)
+                .checkGetParameterizedType("class libcore.java.lang.reflect.ParameterTest");
+
+        // If parameter metadata were included, this would be the expected:
+        // helper.getParameterTestHelper(0)
+        //         .checkModifiers(32784)
+        //         .checkImplicitAndSynthetic(true, false);
+        // i.e. 32784 == Modifier.MANDATED & Modifier.FINAL
+    }
+
+    private Class<?> getMethodClassWith1ImplicitParameterConstructor() {
+        class MethodClass {
+            MethodClass() {
+                ParameterTest.this.outerClassMethod();
+            }
+        }
+        return MethodClass.class;
+    }
+
+    // This behavior is likely to be quite brittle and may not be specified.
+    public void testLambdaClassConstructor() throws Exception {
+        Class<?> anonymousClass = getLambdaClassWith1ParameterConstructor();
+        Constructor<?> constructor = anonymousClass.getDeclaredConstructor(ParameterTest.class);
+
+        ExecutableTestHelper helper = new ExecutableTestHelper(constructor);
+        helper.checkStandardParametersBehavior()
+                .checkParametersToString("[libcore.java.lang.reflect.ParameterTest arg0]")
+                .checkParametersMetadataNotAvailable()
+                .checkParametersNoVarArgs();
+
+        helper.getParameterTestHelper(0)
+                .checkGetType(ParameterTest.class)
+                .checkGetParameterizedType("class libcore.java.lang.reflect.ParameterTest");
+
+        // Unclear what the implicit / synthetic parameter behavior should be.
+    }
+
+    private Class<?> getLambdaClassWith1ParameterConstructor() {
+        return ((Callable<String>) ParameterTest.this::outerClassMethod).getClass();
+    }
+
+    private static class NonIdenticalParameters {
+        void method0(String p1) {}
+        void method1(String p1) {}
+    }
+
+    public void testEquals_checksExecutable() throws Exception {
+        Method method0 = NonIdenticalParameters.class.getDeclaredMethod("method0", String.class);
+        Method method1 = NonIdenticalParameters.class.getDeclaredMethod("method1", String.class);
+        Parameter method0P0 = method0.getParameters()[0];
+        Parameter method1P0 = method1.getParameters()[0];
+        assertFalse(method0P0.equals(method1P0));
+        assertFalse(method1P0.equals(method0P0));
+        assertTrue(method0P0.equals(method0P0));
+    }
+
+    /** A non-static method that exists to be called by inner classes, lambdas, etc. */
+    private String outerClassMethod() {
+        return "Howdy";
+    }
+
+    private static class ExecutableTestHelper {
+        private final Executable executable;
+
+        ExecutableTestHelper(Executable executable) {
+            this.executable = executable;
+        }
+
+        ExecutableTestHelper checkParametersToString(String expectedString) {
+            assertEquals(expectedString, Arrays.toString(executable.getParameters()));
+            return this;
+        }
+
+        /**
+         * Combines checks that should be true of any result from
+         * {@link Executable#getParameters()}
+         */
+        ExecutableTestHelper checkStandardParametersBehavior() {
+            return checkGetParametersClonesArray()
+                    .checkParametersGetDeclaringExecutable()
+                    .checkParametersEquals()
+                    .checkParametersHashcode();
+        }
+
+        ExecutableTestHelper checkParametersGetDeclaringExecutable() {
+            for (Parameter p : executable.getParameters()) {
+                assertSame(executable, p.getDeclaringExecutable());
+            }
+            return this;
+        }
+
+        ExecutableTestHelper checkGetParametersClonesArray() {
+            Parameter[] parameters1 = executable.getParameters();
+            Parameter[] parameters2 = executable.getParameters();
+            assertNotSame(parameters1, parameters2);
+
+            assertEquals(parameters1.length, parameters2.length);
+            for (int i = 0; i < parameters1.length; i++) {
+                assertSame(parameters1[i], parameters2[i]);
+            }
+            return this;
+        }
+
+        ExecutableTestHelper checkParametersEquals() {
+            Parameter[] parameters = executable.getParameters();
+            for (int i = 0; i < parameters.length; i++) {
+                assertEquals(parameters[i], parameters[i]);
+                if (i > 0) {
+                    assertFalse(parameters[0].equals(parameters[i]));
+                    assertFalse(parameters[i].equals(parameters[0]));
+                }
+            }
+            return this;
+        }
+
+        ExecutableTestHelper checkParametersHashcode() {
+            for (Parameter parameter : executable.getParameters()) {
+                // Not much to assert. Just call the method and check it is consistent.
+                assertEquals(parameter.hashCode(), parameter.hashCode());
+            }
+            return this;
+        }
+
+        @DependsOnParameterMetadata
+        ExecutableTestHelper checkParametersMetadataNotAvailable() {
+            ParameterTestHelper[] parameterTestHelpers = getParameterTestHelpers();
+            for (int i = 0; i < parameterTestHelpers.length; i++) {
+                ParameterTestHelper parameterTestHelper = parameterTestHelpers[i];
+                parameterTestHelper.checkName(false, "arg" + i)
+                        .checkImplicitAndSynthetic(false, false)
+                        .checkModifiers(0);
+            }
+            return this;
+        }
+
+        /**
+         * Checks that non of the parameters return {@code true} for {@link Parameter#isVarArgs()}.
+         */
+        ExecutableTestHelper checkParametersNoVarArgs() {
+            for (ParameterTestHelper parameterTestHelper : getParameterTestHelpers()) {
+                parameterTestHelper.checkIsVarArg(false);
+            }
+            return this;
+        }
+
+        ParameterTestHelper getParameterTestHelper(int index) {
+            return new ParameterTestHelper(executable.getParameters()[index]);
+        }
+
+        private ParameterTestHelper[] getParameterTestHelpers() {
+            final int parameterCount = executable.getParameterCount();
+            ParameterTestHelper[] parameterTestHelpers = new ParameterTestHelper[parameterCount];
+            for (int i = 0; i < parameterCount; i++) {
+                parameterTestHelpers[i] = getParameterTestHelper(i);
+            }
+            return parameterTestHelpers;
+        }
+
+        private static class ParameterTestHelper {
+            private final Parameter parameter;
+
+            ParameterTestHelper(Parameter parameter) {
+                this.parameter = parameter;
+            }
+
+            ParameterTestHelper checkGetType(Class<?> expectedType) {
+                assertEquals(expectedType, parameter.getType());
+                return this;
+            }
+
+            @DependsOnParameterMetadata
+            ParameterTestHelper checkName(boolean expectedIsNamePresent, String expectedName) {
+                assertEquals(expectedIsNamePresent, parameter.isNamePresent());
+                assertEquals(expectedName, parameter.getName());
+                return this;
+            }
+
+            @DependsOnParameterMetadata
+            ParameterTestHelper checkModifiers(int expectedModifiers) {
+                assertEquals(expectedModifiers, parameter.getModifiers());
+                return this;
+            }
+
+            ParameterTestHelper checkGetParameterizedType(String expectedParameterizedTypeString) {
+                assertEquals(
+                        expectedParameterizedTypeString,
+                        parameter.getParameterizedType().toString());
+                return this;
+            }
+
+            ParameterTestHelper checkImplicitAndSynthetic(
+                    boolean expectedIsImplicit, boolean expectedIsSynthetic) {
+                assertEquals(expectedIsImplicit, parameter.isImplicit());
+                assertEquals(expectedIsSynthetic, parameter.isSynthetic());
+                return this;
+            }
+
+            ParameterTestHelper checkIsVarArg(boolean expectedIsVarArg) {
+                assertEquals(expectedIsVarArg, parameter.isVarArgs());
+                return this;
+            }
+        }
+    }
+}
diff --git a/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementParameterTest.java b/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementParameterTest.java
new file mode 100644
index 0000000..aa14bd3
--- /dev/null
+++ b/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementParameterTest.java
@@ -0,0 +1,456 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.lang.reflect.annotations;
+
+import junit.framework.TestCase;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationB;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationC;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationD;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Container;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Repeated;
+
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.EXPECT_EMPTY;
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertGetDeclaredAnnotation;
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertIsAnnotationPresent;
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.checkAnnotatedElementPresentMethods;
+
+/**
+ * Tests for the {@link java.lang.reflect.AnnotatedElement} methods from the {@link Parameter}
+ * objects obtained from both {@link Constructor} and {@link Method}.
+ */
+public class AnnotatedElementParameterTest extends TestCase {
+
+    private static class MethodClass {
+        public void methodWithoutAnnotatedParameters(String parameter1, String parameter2) {}
+
+        public void methodWithAnnotatedParameters(@AnnotationB @AnnotationD String parameter1,
+                @AnnotationC @AnnotationD String parameter2) {}
+    }
+
+    public void testMethodParameterAnnotations() throws Exception {
+        Class<?> c = MethodClass.class;
+        {
+            Parameter[] parameters = c.getDeclaredMethod(
+                    "methodWithoutAnnotatedParameters", String.class, String.class).getParameters();
+            Parameter parameter0 = parameters[0];
+            checkAnnotatedElementPresentMethods(parameter0);
+
+            Parameter parameter1 = parameters[1];
+            checkAnnotatedElementPresentMethods(parameter1);
+        }
+        {
+            Parameter[] parameters = c.getDeclaredMethod(
+                    "methodWithAnnotatedParameters", String.class, String.class).getParameters();
+
+            Parameter parameter0 = parameters[0];
+            checkAnnotatedElementPresentMethods(parameter0, AnnotationB.class, AnnotationD.class);
+
+            Parameter parameter1 = parameters[1];
+            checkAnnotatedElementPresentMethods(parameter1, AnnotationC.class, AnnotationD.class);
+        }
+    }
+
+    private static class ConstructorClass {
+        // No annotations.
+        public ConstructorClass(Integer parameter1, Integer parameter2) {}
+
+        // Annotations.
+        public ConstructorClass(@AnnotationB @AnnotationD String parameter1,
+                @AnnotationC @AnnotationD String parameter2) {}
+    }
+
+    public void testConstructorParameterAnnotations() throws Exception {
+        Class<?> c = ConstructorClass.class;
+        {
+            Parameter[] parameters =
+                    c.getDeclaredConstructor(Integer.class, Integer.class).getParameters();
+            Parameter parameter0 = parameters[0];
+            checkAnnotatedElementPresentMethods(parameter0);
+
+            Parameter parameter1 = parameters[1];
+            checkAnnotatedElementPresentMethods(parameter1);
+        }
+        {
+            Parameter[] parameters =
+                    c.getDeclaredConstructor(String.class, String.class).getParameters();
+
+            Parameter parameter0 = parameters[0];
+            checkAnnotatedElementPresentMethods(parameter0, AnnotationB.class, AnnotationD.class);
+
+            Parameter parameter1 = parameters[1];
+            checkAnnotatedElementPresentMethods(parameter1, AnnotationC.class, AnnotationD.class);
+        }
+    }
+
+    private static class AnnotatedMethodClass {
+        void noAnnotation(String p0) {}
+
+        void multipleAnnotationOddity(
+                @Repeated(1) @Container({@Repeated(2), @Repeated(3)}) String p0) {}
+
+        void multipleAnnotationExplicitSingle(@Container({@Repeated(1)}) String p0) {}
+
+        void multipleAnnotation(@Repeated(1) @Repeated(2) String p0) {}
+
+        void singleAnnotation(@Repeated(1) String p0) {}
+
+        static Method getMethodWithoutAnnotations() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod("noAnnotation", String.class);
+        }
+
+        static Method getMethodMultipleAnnotationOddity() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod(
+                    "multipleAnnotationOddity", String.class);
+        }
+
+        static Method getMethodMultipleAnnotationExplicitSingle() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod(
+                    "multipleAnnotationExplicitSingle", String.class);
+        }
+
+        static Method getMethodMultipleAnnotation() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod("multipleAnnotation", String.class);
+        }
+
+        static Method getMethodSingleAnnotation() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod("singleAnnotation", String.class);
+        }
+    }
+
+    // Tests for isAnnotationPresent and getDeclaredAnnotation.
+    public void testMethodDeclaredAnnotation() throws Exception {
+        Class<? extends Annotation> repeated = Repeated.class;
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(),
+                repeated, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                repeated, "@Repeated(1)");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                repeated, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                repeated, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                repeated, "@Repeated(1)");
+
+        Class<? extends Annotation> container = Container.class;
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(),
+                container, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                container, "@Container({@Repeated(2), @Repeated(3)})");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                container, "@Container({@Repeated(1)})");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                container, "@Container({@Repeated(1), @Repeated(2)})");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                container, null);
+    }
+
+    private static class AnnotatedConstructorClass {
+        public AnnotatedConstructorClass(Boolean p0) {}
+
+        public AnnotatedConstructorClass(
+                @Repeated(1) @Container({@Repeated(2), @Repeated(3)}) Long p0) {}
+
+        public AnnotatedConstructorClass(@Container({@Repeated(1)}) Double p0) {}
+
+        public AnnotatedConstructorClass(@Repeated(1) @Repeated(2) Integer p0) {}
+
+        public AnnotatedConstructorClass(@Repeated(1) String p0) {}
+
+        static Constructor<?> getConstructorWithoutAnnotations() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Boolean.class);
+        }
+
+        static Constructor<?> getConstructorMultipleAnnotationOddity() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Long.class);
+        }
+
+        static Constructor<?> getConstructorMultipleAnnotationExplicitSingle()
+                throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Double.class);
+        }
+
+        static Constructor<?> getConstructorSingleAnnotation() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(String.class);
+        }
+
+        static Constructor<?> getConstructorMultipleAnnotation() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Integer.class);
+        }
+    }
+
+    // Tests for isAnnotationPresent and getDeclaredAnnotation.
+    public void testConstructorDeclaredAnnotation() throws Exception {
+        Class<? extends Annotation> repeated = Repeated.class;
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                repeated, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                repeated, "@Repeated(1)");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                repeated, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                repeated, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                repeated, "@Repeated(1)");
+
+        Class<? extends Annotation> container = Container.class;
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                container, null);
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                container, "@Container({@Repeated(2), @Repeated(3)})");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                container, "@Container({@Repeated(1)})");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                container, "@Container({@Repeated(1), @Repeated(2)})");
+        checkParameter0DeclaredAnnotation(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                container, null);
+    }
+
+    private static void checkParameter0DeclaredAnnotation(
+            Executable executable, Class<? extends Annotation> annotationType,
+            String expectedAnnotationString) throws Exception {
+        Parameter parameter = executable.getParameters()[0];
+
+        // isAnnotationPresent
+        assertIsAnnotationPresent(parameter, annotationType, expectedAnnotationString != null);
+
+        // getDeclaredAnnotation
+        assertGetDeclaredAnnotation(parameter, annotationType, expectedAnnotationString);
+    }
+
+    public void testMethodGetDeclaredAnnotationsByType() throws Exception {
+        Class<? extends Annotation> repeated = Repeated.class;
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(),
+                repeated, EXPECT_EMPTY);
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                repeated, "@Repeated(1)", "@Repeated(2)", "@Repeated(3)");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                repeated, "@Repeated(1)");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                repeated, "@Repeated(1)", "@Repeated(2)");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                repeated, "@Repeated(1)");
+
+        Class<? extends Annotation> container = Container.class;
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(),
+                container, EXPECT_EMPTY);
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                container, "@Container({@Repeated(2), @Repeated(3)})");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                container, "@Container({@Repeated(1)})");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                container, "@Container({@Repeated(1), @Repeated(2)})");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                container, EXPECT_EMPTY);
+    }
+
+    public void testConstructorGetDeclaredAnnotationsByType() throws Exception {
+        Class<? extends Annotation> repeated = Repeated.class;
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                repeated, EXPECT_EMPTY);
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                repeated, "@Repeated(1)", "@Repeated(2)", "@Repeated(3)");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                repeated, "@Repeated(1)");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                repeated, "@Repeated(1)", "@Repeated(2)");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                repeated, "@Repeated(1)");
+
+        Class<? extends Annotation> container = Container.class;
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                container, EXPECT_EMPTY);
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                container, "@Container({@Repeated(2), @Repeated(3)})");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                container, "@Container({@Repeated(1)})");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                container, "@Container({@Repeated(1), @Repeated(2)})");
+        checkParameter0GetDeclaredAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                container, EXPECT_EMPTY);
+    }
+
+    private static void checkParameter0GetDeclaredAnnotationsByType(
+            Executable executable, Class<? extends Annotation> annotationType,
+            String... expectedAnnotationStrings) throws Exception {
+        Parameter parameter = executable.getParameters()[0];
+        AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType(
+                parameter, annotationType, expectedAnnotationStrings);
+    }
+
+    public void testMethodGetAnnotationsByType() throws Exception {
+        Class<? extends Annotation> repeated = Repeated.class;
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(),
+                repeated, EXPECT_EMPTY);
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                repeated, "@Repeated(1)", "@Repeated(2)", "@Repeated(3)");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                repeated, "@Repeated(1)");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                repeated, "@Repeated(1)", "@Repeated(2)");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                repeated, "@Repeated(1)");
+
+        Class<? extends Annotation> container = Container.class;
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(),
+                container, EXPECT_EMPTY);
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                container, "@Container({@Repeated(2), @Repeated(3)})");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                container, "@Container({@Repeated(1)})");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                container, "@Container({@Repeated(1), @Repeated(2)})");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                container, EXPECT_EMPTY);
+    }
+
+    public void testConstructorGetAnnotationsByType() throws Exception {
+        Class<? extends Annotation> repeated = Repeated.class;
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                repeated, EXPECT_EMPTY);
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                repeated, "@Repeated(1)", "@Repeated(2)", "@Repeated(3)");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                repeated, "@Repeated(1)");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                repeated, "@Repeated(1)", "@Repeated(2)");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                repeated, "@Repeated(1)");
+
+        Class<? extends Annotation> container = Container.class;
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                container, EXPECT_EMPTY);
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                container, "@Container({@Repeated(2), @Repeated(3)})");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                container, "@Container({@Repeated(1)})");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                container, "@Container({@Repeated(1), @Repeated(2)})");
+        checkParameter0GetAnnotationsByType(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                container, EXPECT_EMPTY);
+    }
+
+    private static void checkParameter0GetAnnotationsByType(
+            Executable executable, Class<? extends Annotation> annotationType,
+            String... expectedAnnotationStrings) throws Exception {
+        Parameter parameter = executable.getParameters()[0];
+        AnnotatedElementTestSupport.assertGetAnnotationsByType(
+                parameter, annotationType, expectedAnnotationStrings);
+    }
+
+    /**
+     * As an inner class the constructor will actually have two parameters: the first, referencing
+     * the enclosing object, is inserted by the compiler.
+     */
+    class InnerClass {
+        InnerClass(@Repeated(1) String p1) {}
+    }
+
+    /** Special case testing for a compiler-generated constructor parameter. */
+    public void testImplicitConstructorParameters_singleAnnotation() throws Exception {
+        Constructor<InnerClass> constructor =
+                InnerClass.class.getDeclaredConstructor(
+                        AnnotatedElementParameterTest.class, String.class);
+        Parameter[] parameters = constructor.getParameters();
+
+        // The compiler-generated constructor should have no annotations.
+        Parameter parameter0 = parameters[0];
+        AnnotatedElementTestSupport.assertGetAnnotationsByType(
+                parameter0, Repeated.class, new String[0]);
+        AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType(
+                parameter0, Repeated.class, new String[0]);
+        AnnotatedElementTestSupport.assertGetDeclaredAnnotation(
+                parameter0, Repeated.class, null);
+        AnnotatedElementTestSupport.assertIsAnnotationPresent(parameter0, Repeated.class, false);
+
+        // The annotation should remain on the correct parameter.
+        Parameter parameter1 = parameters[1];
+        AnnotatedElementTestSupport.assertGetAnnotationsByType(
+                parameter1, Repeated.class, new String[] {"@Repeated(1)"});
+        AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType(
+                parameter1, Repeated.class, new String[] {"@Repeated(1)"});
+        AnnotatedElementTestSupport.assertGetDeclaredAnnotation(
+                parameter1, Repeated.class, "@Repeated(1)");
+        AnnotatedElementTestSupport.assertIsAnnotationPresent(
+                parameter1, Repeated.class, true);
+    }
+}
diff --git a/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementTestSupport.java b/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementTestSupport.java
index ec57469..b17fbff 100644
--- a/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementTestSupport.java
+++ b/luni/src/test/java/libcore/java/lang/reflect/annotations/AnnotatedElementTestSupport.java
@@ -95,8 +95,8 @@
      * returns annotations of the supplied expected classes.
      *
      * <p>Where the expected classes contains some subset from
-     * {@link AnnotationA}, {@link AnnotationB} and {@link AnnotationC}, this method also asserts
-     * that {@link AnnotatedElement#isAnnotationPresent(Class)} and
+     * {@link AnnotationA}, {@link AnnotationB}, {@link AnnotationC}, {@link AnnotationD} this
+     * method also asserts that {@link AnnotatedElement#isAnnotationPresent(Class)} and
      * {@link AnnotatedElement#getAnnotation(Class)} works as expected.
      *
      * <p>This method also confirms that {@link AnnotatedElement#isAnnotationPresent(Class)} and
@@ -112,6 +112,7 @@
         assertPresent(expectedTypes.contains(AnnotationA.class), element, AnnotationA.class);
         assertPresent(expectedTypes.contains(AnnotationB.class), element, AnnotationB.class);
         assertPresent(expectedTypes.contains(AnnotationC.class), element, AnnotationC.class);
+        assertPresent(expectedTypes.contains(AnnotationD.class), element, AnnotationD.class);
 
         try {
             element.isAnnotationPresent(null);
diff --git a/luni/src/test/java/libcore/java/lang/reflect/annotations/ExecutableParameterTest.java b/luni/src/test/java/libcore/java/lang/reflect/annotations/ExecutableParameterTest.java
new file mode 100644
index 0000000..a07f2b3
--- /dev/null
+++ b/luni/src/test/java/libcore/java/lang/reflect/annotations/ExecutableParameterTest.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.lang.reflect.annotations;
+
+import junit.framework.TestCase;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Method;
+
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationB;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationC;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationD;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Container;
+import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Repeated;
+
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.EXPECT_EMPTY;
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.annotationsToTypes;
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertAnnotationsMatch;
+import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.set;
+
+/**
+ * Tests for {@link Executable#getParameterAnnotations()} via the {@link Constructor} and
+ * {@link Method} classes. See {@link AnnotatedElementParameterTest} for testing of the
+ * {@link java.lang.reflect.AnnotatedElement} methods.
+ */
+public class ExecutableParameterTest extends TestCase {
+    private static class MethodClass {
+        public void methodWithoutAnnotatedParameters(String parameter1, String parameter2) {}
+
+        public void methodWithAnnotatedParameters(@AnnotationB @AnnotationD String parameter1,
+                @AnnotationC @AnnotationD String parameter2) {}
+    }
+
+    public void testMethodGetParameterAnnotations() throws Exception {
+        Method methodWithoutAnnotatedParameters = MethodClass.class.getMethod(
+                "methodWithoutAnnotatedParameters", String.class, String.class);
+        Annotation[][] noParameterAnnotations =
+                methodWithoutAnnotatedParameters.getParameterAnnotations();
+        assertEquals(2, noParameterAnnotations.length);
+        assertEquals(set(), annotationsToTypes(noParameterAnnotations[0]));
+        assertEquals(set(), annotationsToTypes(noParameterAnnotations[1]));
+
+        Method methodWithAnnotatedParameters = MethodClass.class.getMethod(
+                "methodWithAnnotatedParameters", String.class, String.class);
+        Annotation[][] parameterAnnotations =
+                methodWithAnnotatedParameters.getParameterAnnotations();
+        assertEquals(2, parameterAnnotations.length);
+        assertEquals(set(AnnotationB.class, AnnotationD.class),
+                annotationsToTypes(parameterAnnotations[0]));
+        assertEquals(set(AnnotationC.class, AnnotationD.class),
+                annotationsToTypes(parameterAnnotations[1]));
+    }
+
+    private static class ConstructorClass {
+        // No annotations.
+        public ConstructorClass(Integer parameter1, Integer parameter2) {}
+
+        // Annotations.
+        public ConstructorClass(@AnnotationB @AnnotationD String parameter1,
+                @AnnotationC @AnnotationD String parameter2) {}
+    }
+
+    public void testConstructorGetParameterAnnotations() throws Exception {
+        Constructor constructorWithoutAnnotatedParameters =
+                ConstructorClass.class.getDeclaredConstructor(Integer.class, Integer.class);
+        Annotation[][] noParameterAnnotations =
+                constructorWithoutAnnotatedParameters.getParameterAnnotations();
+        assertEquals(2, noParameterAnnotations.length);
+        assertEquals(set(), annotationsToTypes(noParameterAnnotations[0]));
+        assertEquals(set(), annotationsToTypes(noParameterAnnotations[1]));
+
+        Constructor constructorWithAnnotatedParameters =
+                ConstructorClass.class.getDeclaredConstructor(String.class, String.class);
+        Annotation[][] parameterAnnotations =
+                constructorWithAnnotatedParameters.getParameterAnnotations();
+        assertEquals(2, parameterAnnotations.length);
+        assertEquals(set(AnnotationB.class, AnnotationD.class),
+                annotationsToTypes(parameterAnnotations[0]));
+        assertEquals(set(AnnotationC.class, AnnotationD.class),
+                annotationsToTypes(parameterAnnotations[1]));
+    }
+
+    private static class AnnotatedMethodClass {
+        void noAnnotation(String p0) {}
+
+        void multipleAnnotationOddity(
+                @Repeated(1) @Container({@Repeated(2), @Repeated(3)}) String p0) {}
+
+        void multipleAnnotationExplicitSingle(@Container({@Repeated(1)}) String p0) {}
+
+        void multipleAnnotation(@Repeated(1) @Repeated(2) String p0) {}
+
+        void singleAnnotation(@Repeated(1) String p0) {}
+
+        static Method getMethodWithoutAnnotations() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod("noAnnotation", String.class);
+        }
+
+        static Method getMethodMultipleAnnotationOddity() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod(
+                    "multipleAnnotationOddity", String.class);
+        }
+
+        static Method getMethodMultipleAnnotationExplicitSingle() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod(
+                    "multipleAnnotationExplicitSingle", String.class);
+        }
+
+        static Method getMethodMultipleAnnotation() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod("multipleAnnotation", String.class);
+        }
+
+        static Method getMethodSingleAnnotation() throws Exception {
+            return AnnotatedMethodClass.class.getDeclaredMethod("singleAnnotation", String.class);
+        }
+    }
+
+    public void testMethodGetParameterAnnotations_repeated() throws Exception {
+        assertParameter0Annotations(
+                AnnotatedMethodClass.getMethodWithoutAnnotations(), EXPECT_EMPTY);
+        assertParameter0Annotations(
+                AnnotatedMethodClass.getMethodMultipleAnnotationOddity(),
+                "@Repeated(1)", "@Container({@Repeated(2), @Repeated(3)})");
+        assertParameter0Annotations(
+                AnnotatedMethodClass.getMethodMultipleAnnotationExplicitSingle(),
+                "@Container({@Repeated(1)})");
+        assertParameter0Annotations(
+                AnnotatedMethodClass.getMethodMultipleAnnotation(),
+                "@Container({@Repeated(1), @Repeated(2)})");
+        assertParameter0Annotations(
+                AnnotatedMethodClass.getMethodSingleAnnotation(),
+                "@Repeated(1)");
+    }
+
+    private static class AnnotatedConstructorClass {
+        public AnnotatedConstructorClass(Boolean p0) {}
+
+        public AnnotatedConstructorClass(
+                @Repeated(1) @Container({@Repeated(2), @Repeated(3)}) Long p0) {}
+
+        public AnnotatedConstructorClass(@Container({@Repeated(1)}) Double p0) {}
+
+        public AnnotatedConstructorClass(@Repeated(1) @Repeated(2) Integer p0) {}
+
+        public AnnotatedConstructorClass(@Repeated(1) String p0) {}
+
+        static Constructor<?> getConstructorWithoutAnnotations() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Boolean.class);
+        }
+
+        static Constructor<?> getConstructorMultipleAnnotationOddity() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Long.class);
+        }
+
+        static Constructor<?> getConstructorMultipleAnnotationExplicitSingle()
+                throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Double.class);
+        }
+
+        static Constructor<?> getConstructorMultipleAnnotation() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(Integer.class);
+        }
+
+        static Constructor<?> getConstructorSingleAnnotation() throws Exception {
+            return AnnotatedConstructorClass.class.getDeclaredConstructor(String.class);
+        }
+    }
+
+    public void testConstructorGetParameterAnnotations_repeated() throws Exception {
+        assertParameter0Annotations(
+                AnnotatedConstructorClass.getConstructorWithoutAnnotations(),
+                EXPECT_EMPTY);
+        assertParameter0Annotations(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationOddity(),
+                "@Repeated(1)", "@Container({@Repeated(2), @Repeated(3)})");
+        assertParameter0Annotations(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotationExplicitSingle(),
+                "@Container({@Repeated(1)})");
+        assertParameter0Annotations(
+                AnnotatedConstructorClass.getConstructorMultipleAnnotation(),
+                "@Container({@Repeated(1), @Repeated(2)})");
+        assertParameter0Annotations(
+                AnnotatedConstructorClass.getConstructorSingleAnnotation(),
+                "@Repeated(1)");
+    }
+
+    private static void assertParameter0Annotations(
+            Executable executable, String... expectedAnnotationStrings) throws Exception {
+        Annotation[][] allAnnotations = executable.getParameterAnnotations();
+        final int expectedParameterCount = 1;
+        assertEquals(expectedParameterCount, allAnnotations.length);
+
+        Annotation[] p0Annotations = allAnnotations[0];
+        assertAnnotationsMatch(p0Annotations, expectedAnnotationStrings);
+    }
+}
diff --git a/luni/src/test/java/libcore/java/lang/reflect/annotations/ParameterTest.java b/luni/src/test/java/libcore/java/lang/reflect/annotations/ParameterTest.java
deleted file mode 100644
index f40ac9f..0000000
--- a/luni/src/test/java/libcore/java/lang/reflect/annotations/ParameterTest.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package libcore.java.lang.reflect.annotations;
-
-import junit.framework.TestCase;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationB;
-import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationC;
-import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationD;
-import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Container;
-import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Repeated;
-
-import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.EXPECT_EMPTY;
-import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.annotationsToTypes;
-import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertAnnotationsMatch;
-import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.set;
-
-public class ParameterTest extends TestCase {
-    private static class Type {
-        @AnnotationB
-        @AnnotationC
-        public void method(String parameter1, String parameter2) {}
-
-        @AnnotationB
-        @AnnotationC
-        public void parameters(@AnnotationB @AnnotationD String parameter1,
-                @AnnotationC @AnnotationD String parameter2) {}
-    }
-
-    public void testParameterAnnotations() throws Exception {
-        Method method = Type.class.getMethod("method", String.class, String.class);
-        Annotation[][] noParameterAnnotations = method.getParameterAnnotations();
-        assertEquals(2, noParameterAnnotations.length);
-        assertEquals(set(), annotationsToTypes(noParameterAnnotations[0]));
-        assertEquals(set(), annotationsToTypes(noParameterAnnotations[1]));
-
-        Method parameters = Type.class.getMethod("parameters", String.class, String.class);
-        Annotation[][] parameterAnnotations = parameters.getParameterAnnotations();
-        assertEquals(2, parameterAnnotations.length);
-        assertEquals(set(AnnotationB.class, AnnotationD.class),
-                annotationsToTypes(parameterAnnotations[0]));
-        assertEquals(set(AnnotationC.class, AnnotationD.class),
-                annotationsToTypes(parameterAnnotations[1]));
-    }
-
-    private static class AnnotatedClass {
-        public void singleAnnotation(@Repeated(1) String p0) {}
-
-        public void multipleAnnotation(@Repeated(1) @Repeated(2) String p0) {}
-
-        public void multipleAnnotationExplicitSingle(@Container({@Repeated(1)}) String p0) {}
-
-        public void multipleAnnotationOddity(
-                @Repeated(1) @Container({@Repeated(2), @Repeated(3)}) String p0) {}
-
-        public void noAnnotation(String p0) {}
-    }
-
-    public void testGetParameterAnnotations() throws Exception {
-        Class<?> c = AnnotatedClass.class;
-
-        assertParameter0Annotations(c, "noAnnotation", EXPECT_EMPTY);
-        assertParameter0Annotations(c, "multipleAnnotationOddity",
-                "@Repeated(1)", "@Container({@Repeated(2), @Repeated(3)})");
-        assertParameter0Annotations(c, "multipleAnnotationExplicitSingle",
-                "@Container({@Repeated(1)})");
-        assertParameter0Annotations(c, "multipleAnnotation",
-                "@Container({@Repeated(1), @Repeated(2)})");
-        assertParameter0Annotations(c, "singleAnnotation",
-                "@Repeated(1)");
-    }
-
-    private static void assertParameter0Annotations(
-            Class<?> c, String methodName, String... expectedAnnotationStrings) throws Exception {
-        Annotation[][] allAnnotations =
-                c.getDeclaredMethod(methodName, String.class).getParameterAnnotations();
-        final int expectedParameterCount = 1;
-        assertEquals(expectedParameterCount, allAnnotations.length);
-
-        Annotation[] p0Annotations = allAnnotations[0];
-        assertAnnotationsMatch(p0Annotations, expectedAnnotationStrings);
-    }
-}
diff --git a/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java b/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java
index d4d650f..63e252c 100644
--- a/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java
+++ b/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java
@@ -521,18 +521,9 @@
         HttpCookie cookieA = createCookie("a", "android", ".android.com", "/source");
         HttpCookie cookieB = createCookie("b", "banana", "code.google.com", "/p/android");
 
-        try {
-            cookieStore.add(null, cookieA);
-        } catch (NullPointerException expected) {
-            // the RI crashes even though the cookie does get added to the store; sigh
-            expected.printStackTrace();
-        }
+        cookieStore.add(null, cookieA);
         assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
-        try {
-            cookieStore.add(null, cookieB);
-            fail();
-        } catch (NullPointerException expected) {
-        }
+        cookieStore.add(null, cookieB);
         assertEquals(Arrays.asList(cookieA, cookieB), cookieStore.getCookies());
 
         try {
diff --git a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
index ad5326b..3277385 100644
--- a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
@@ -21,8 +21,10 @@
 import java.lang.reflect.Field;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
+import java.net.DatagramSocketImpl;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.SocketAddress;
 import java.net.SocketException;
 
 public class DatagramSocketTest extends TestCase {
@@ -64,28 +66,90 @@
     final int port = 9999;
 
     try (DatagramSocket s = new DatagramSocket()) {
-      s.connect(InetAddress.getLocalHost(), port);
+      forceConnectToThrowSocketException(s);
 
-      // connect may set pendingConnectException on internal failure; since we have no reliable way
-      // to make connect fail, set pendingConnectException through reflection.
-      Field pendingConnectException = s.getClass().getDeclaredField("pendingConnectException");
-      pendingConnectException.setAccessible(true);
-      pendingConnectException.set(s, new SocketException());
+      s.connect(InetAddress.getLocalHost(), port);
 
       byte[] data = new byte[100];
       DatagramPacket p = new DatagramPacket(data, data.length);
 
+      // Confirm send() throws the pendingConnectException.
       try {
         s.send(p);
         fail();
       } catch (SocketException expected) {
+        assertTrue(expected.getMessage().contains("Pending connect failure"));
       }
 
+      // Confirm receive() throws the pendingConnectException.
       try {
         s.receive(p);
         fail();
       } catch (SocketException expected) {
+        assertTrue(expected.getMessage().contains("Pending connect failure"));
+      }
+
+      // Confirm that disconnect() doesn't throw a runtime exception.
+      s.disconnect();
+    }
+  }
+
+  public void test_setTrafficClass() throws Exception {
+    try (DatagramSocket s = new DatagramSocket()) {
+      for (int i = 0; i <= 255; ++i) {
+        s.setTrafficClass(i);
+        assertEquals(i, s.getTrafficClass());
       }
     }
   }
+
+  // DatagramSocket should "become connected" even when impl.connect() fails and throws an
+  // exception.
+  public void test_b31218085() throws Exception {
+    final int port = 9999;
+
+    try (DatagramSocket s = new DatagramSocket()) {
+      forceConnectToThrowSocketException(s);
+
+      s.connect(InetAddress.getLocalHost(), port);
+      assertTrue(s.isConnected());
+
+      // Confirm that disconnect() doesn't throw a runtime exception.
+      s.disconnect();
+    }
+  }
+
+  public void testForceConnectToThrowSocketException() throws Exception {
+    // Unlike connect(InetAddress, int), connect(SocketAddress) can (and should) throw an
+    // exception after a call to forceConnectToThrowSocketException(). The
+    // forceConnectToThrowSocketException() method is used in various tests for
+    // connect(InetAddress, int) and this test exists to confirm it stays working.
+
+    SocketAddress validAddress = new InetSocketAddress(InetAddress.getLocalHost(), 9999);
+
+    try (DatagramSocket s1 = new DatagramSocket()) {
+      s1.connect(validAddress);
+      s1.disconnect();
+    }
+
+    try (DatagramSocket s2 = new DatagramSocket()) {
+      forceConnectToThrowSocketException(s2);
+      try {
+        s2.connect(validAddress);
+      } catch (SocketException expected) {
+      }
+      s2.disconnect();
+    }
+  }
+
+  private static void forceConnectToThrowSocketException(DatagramSocket s) throws Exception {
+    // Set fd of DatagramSocketImpl to null, forcing impl.connect() to throw a SocketException
+    // (Socket closed).
+    Field f = DatagramSocket.class.getDeclaredField("impl");
+    f.setAccessible(true);
+    DatagramSocketImpl impl = (DatagramSocketImpl) f.get(s);
+    f = DatagramSocketImpl.class.getDeclaredField("fd");
+    f.setAccessible(true);
+    f.set(impl, null);
+  }
 }
diff --git a/luni/src/test/java/libcore/java/net/SocketTest.java b/luni/src/test/java/libcore/java/net/SocketTest.java
index 6f1ce29..1952286 100644
--- a/luni/src/test/java/libcore/java/net/SocketTest.java
+++ b/luni/src/test/java/libcore/java/net/SocketTest.java
@@ -16,6 +16,7 @@
 
 package libcore.java.net;
 
+import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -43,7 +44,6 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
-import java.io.FileDescriptor;
 
 
 public class SocketTest extends junit.framework.TestCase {
@@ -51,6 +51,9 @@
     // This hostname is required to resolve to 127.0.0.1 and ::1 for all tests to pass.
     private static final String ALL_LOOPBACK_HOSTNAME = "loopback46.unittest.grpc.io";
 
+    // From net/inet_ecn.h
+    private static final int INET_ECN_MASK = 0x3;
+
     // See http://b/2980559.
     public void test_close() throws Exception {
         Socket s = new Socket();
@@ -249,8 +252,14 @@
 
     public void test_setTrafficClass() throws Exception {
         Socket s = new Socket();
-        s.setTrafficClass(123);
-        assertEquals(123, s.getTrafficClass());
+
+        for (int i = 0; i <= 255; ++i) {
+            s.setTrafficClass(i);
+
+            // b/30909505
+            // Linux does not set ECN bits for STREAM sockets, so these bits should be zero.
+            assertEquals(i & ~INET_ECN_MASK, s.getTrafficClass());
+        }
     }
 
     public void testReadAfterClose() throws Exception {
@@ -370,33 +379,43 @@
 
     public void testCloseDuringConnect() throws Exception {
         final CountDownLatch signal = new CountDownLatch(1);
-
         final Socket s = new Socket();
-        new Thread() {
-            @Override
-            public void run() {
-                try {
-                    // This address is reserved for documentation: should never be reachable.
-                    InetSocketAddress unreachableIp = new InetSocketAddress("192.0.2.0", 80);
-                    // This should never return.
-                    s.connect(unreachableIp, 0 /* infinite */);
-                    fail("Connect returned unexpectedly for: " + unreachableIp);
-                } catch (SocketException expected) {
-                    assertTrue(expected.getMessage().contains("Socket closed"));
-                    signal.countDown();
-                } catch (IOException e) {
-                    fail("Unexpected exception: " + e);
-                }
-            }
-        }.start();
 
-        // Wait for the connect() thread to run and start connect()
+        // Executes a connect() that should block.
+        Callable<String> connectWorker = () -> {
+            try {
+                // This address is reserved for documentation: should never be reachable.
+                InetSocketAddress unreachableIp = new InetSocketAddress("192.0.2.0", 80);
+                // This should never return.
+                s.connect(unreachableIp, 0 /* infinite */);
+                return "Connect returned unexpectedly for: " + unreachableIp;
+            } catch (SocketException expected) {
+                signal.countDown();
+                return expected.getMessage().contains("Socket closed")
+                        ? null
+                        : "Unexpected SocketException message: " + expected.getMessage();
+            } catch (IOException e) {
+                return "Unexpected exception: " + e;
+            }
+        };
+        Future<String> connectResult =
+                Executors.newSingleThreadScheduledExecutor().submit(connectWorker);
+
+        // Wait sufficient time for the connectWorker thread to run and start connect().
         Thread.sleep(2000);
 
+        // Close the socket that connectWorker should currently be blocked in connect().
         s.close();
 
+        // connectWorker should have been unblocked so await() should return true.
         boolean connectUnblocked = signal.await(2000, TimeUnit.MILLISECONDS);
-        assertTrue(connectUnblocked);
+
+        // connectWorker should have returned null if everything went as expected.
+        String workerFailure = connectResult.get(2000, TimeUnit.MILLISECONDS);
+
+        assertTrue("connectUnblocked=[" + connectUnblocked
+                + "], workerFailure=[" + workerFailure + "]",
+                connectUnblocked && workerFailure == null);
     }
 
     // http://b/29092095
diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
index 1bc31d5..d1d26a8 100644
--- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
@@ -154,7 +154,7 @@
         // allow (but strip) trailing \n, \r and \r\n
         // assertForbiddenRequestHeaderValue("\r");
         // End of workaround
-        assertForbiddenRequestHeaderValue("\t");
+        assertEquals("a valid\tvalue", setAndReturnRequestHeaderValue("a valid\tvalue"));
         assertForbiddenRequestHeaderValue("\u001f");
         assertForbiddenRequestHeaderValue("\u007f");
 
diff --git a/luni/src/test/java/libcore/java/net/URLStreamHandlerFactoryTest.java b/luni/src/test/java/libcore/java/net/URLStreamHandlerFactoryTest.java
index de50e16..21c2971 100644
--- a/luni/src/test/java/libcore/java/net/URLStreamHandlerFactoryTest.java
+++ b/luni/src/test/java/libcore/java/net/URLStreamHandlerFactoryTest.java
@@ -21,6 +21,8 @@
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
 import java.net.URLStreamHandlerFactory;
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 import libcore.java.net.customstreamhandler.http.Handler;
 
@@ -61,13 +63,23 @@
         try {
             URL.setURLStreamHandlerFactory(shf);
             fail();
+        } catch (AssertionFailedError error) {
+            // Rethrow the error thrown by fail to avoid it being caught by the more general catch
+            // statement below.
+            throw error;
         } catch (Error expected) {
+            // The setURLStreamHandlerFactory is behaving correctly by throwing an Error.
         }
 
         try {
             URL.setURLStreamHandlerFactory(null);
             fail();
+        } catch (AssertionFailedError error) {
+            // Rethrow the error thrown by fail to avoid it being caught by the more general catch
+            // statement below.
+            throw error;
         } catch (Error expected) {
+            // The setURLStreamHandlerFactory is behaving correctly by throwing an Error.
         }
     }
 
diff --git a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
index 0951302..52146ee 100644
--- a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
@@ -490,6 +490,7 @@
         assertTrue(key.isValid());
         assertSame(networkInterface, key.networkInterface());
         assertNull(key.sourceAddress());
+        dc.close();
     }
 
     public void test_dropAnySource_twice_IPv4() throws Exception {
diff --git a/luni/src/test/java/libcore/java/security/PrivilegedActionExceptionTest.java b/luni/src/test/java/libcore/java/security/PrivilegedActionExceptionTest.java
new file mode 100644
index 0000000..6f74dd8
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/PrivilegedActionExceptionTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.security;
+
+import org.junit.Test;
+
+import java.security.PrivilegedActionException;
+
+import static org.junit.Assert.assertSame;
+
+public class PrivilegedActionExceptionTest {
+
+    /**
+     * PrivilegedActionException's constructor argument may be rethrown by getException() or
+     * getCause().
+     * b/31360928
+     */
+    @Test
+    public void testGetException() {
+        Exception e = new Exception();
+        PrivilegedActionException pae = new PrivilegedActionException(e);
+
+        assertSame(e, pae.getException());
+        assertSame(e, pae.getCause());
+    }
+}
diff --git a/luni/src/test/java/libcore/java/security/ProviderTest.java b/luni/src/test/java/libcore/java/security/ProviderTest.java
index d525cf0..d9d0d31 100644
--- a/luni/src/test/java/libcore/java/security/ProviderTest.java
+++ b/luni/src/test/java/libcore/java/security/ProviderTest.java
@@ -41,9 +41,12 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 import java.util.Set;
 import java.util.TreeMap;
+import java.util.function.BiFunction;
 import java.util.function.Consumer;
+import java.util.function.Function;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javax.crypto.Cipher;
@@ -554,6 +557,46 @@
     }
 
     @SuppressWarnings("serial")
+    public void testProviderService_newInstance_PrivateClass_throws()
+            throws Exception {
+        MockProvider provider = new MockProvider("MockProvider");
+
+        provider.putServiceForTest(new Provider.Service(provider, "CertStore", "FOO",
+                CertStoreSpiPrivateClass.class.getName(), null, null));
+
+        Security.addProvider(provider);
+        // The class for the service is private, it must fail with NoSuchAlgorithmException
+        try {
+            Provider.Service service = provider.getService("CertStore", "FOO");
+            service.newInstance(null);
+            fail();
+        } catch (NoSuchAlgorithmException expected) {
+        } finally {
+            Security.removeProvider(provider.getName());
+        }
+    }
+
+    @SuppressWarnings("serial")
+    public void testProviderService_newInstance_PrivateEmptyConstructor_throws()
+            throws Exception {
+        MockProvider provider = new MockProvider("MockProvider");
+
+        provider.putServiceForTest(new Provider.Service(provider, "CertStore", "FOO",
+                CertStoreSpiPrivateEmptyConstructor.class.getName(), null, null));
+
+        Security.addProvider(provider);
+        // The empty constructor is private, it must fail with NoSuchAlgorithmException
+        try {
+            Provider.Service service = provider.getService("CertStore", "FOO");
+            service.newInstance(null);
+            fail();
+        } catch (NoSuchAlgorithmException expected) {
+        } finally {
+            Security.removeProvider(provider.getName());
+        }
+    }
+
+    @SuppressWarnings("serial")
     public void testProviderService_AliasDoesNotEraseCanonical_Success()
             throws Exception {
         // Make sure we start with a "known good" alias for this OID.
@@ -632,6 +675,45 @@
         }
     }
 
+    private static class CertStoreSpiPrivateClass extends CertStoreSpi {
+        public CertStoreSpiPrivateClass()
+                throws InvalidAlgorithmParameterException {
+            super(null);
+        }
+
+        @Override
+        public Collection<? extends Certificate> engineGetCertificates(CertSelector selector)
+                throws CertStoreException {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public Collection<? extends CRL> engineGetCRLs(CRLSelector selector)
+                throws CertStoreException {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    private static class CertStoreSpiPrivateEmptyConstructor extends CertStoreSpi {
+        private CertStoreSpiPrivateEmptyConstructor(CertStoreParameters params)
+                throws InvalidAlgorithmParameterException {
+            super(null);
+        }
+
+        @Override
+        public Collection<? extends Certificate> engineGetCertificates(CertSelector selector)
+                throws CertStoreException {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public Collection<? extends CRL> engineGetCRLs(CRLSelector selector)
+                throws CertStoreException {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+
     public static class MyCertStoreParameters implements CertStoreParameters {
         public Object clone() {
             return new MyCertStoreParameters();
@@ -692,6 +774,118 @@
                 "class2.algorithm1",
                 mapOf("class1.algorithm1", "impl1"),
                 true /* mustChangeSecurityVersion */);
+        performHashMapOperationAndCheckResults(
+                COMPUTE,
+                mapOf("class1.algorithm1", "impl1"),
+                // It's really difficult to find an example of this that sounds realistic
+                // for a Provider...
+                new Pair("class1.algorithm1", CONCAT),
+                mapOf("class1.algorithm1", "class1.algorithm1impl1"),
+                true);
+        performHashMapOperationAndCheckResults(
+                PUT_IF_ABSENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class1.algorithm1", "impl2"),
+                // Don't put because key is absent.
+                mapOf("class1.algorithm1", "impl1"),
+                true);
+        performHashMapOperationAndCheckResults(
+                PUT_IF_ABSENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class2.algorithm2", "impl2"),
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                PUT_IF_ABSENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class2.algorithm2", "impl2"),
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                COMPUTE_IF_PRESENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class1.algorithm1", CONCAT),
+                mapOf("class1.algorithm1", "class1.algorithm1impl1"),
+                true);
+        performHashMapOperationAndCheckResults(
+                COMPUTE_IF_PRESENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class2.algorithm2", CONCAT),
+                // Don't compute because is not present.
+                mapOf("class1.algorithm1", "impl1"),
+                true);
+        performHashMapOperationAndCheckResults(
+                COMPUTE_IF_ABSENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class2.algorithm2", TO_UPPER_CASE),
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "CLASS2.ALGORITHM2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                COMPUTE_IF_ABSENT,
+                mapOf("class1.algorithm1", "impl1"),
+                new Pair("class1.algorithm1", TO_UPPER_CASE),
+                // Don't compute because if not absent.
+                mapOf("class1.algorithm1", "impl1"),
+                true);
+        performHashMapOperationAndCheckResults(
+                REPLACE_USING_KEY,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                new Pair("class1.algorithm1", "impl3"),
+                mapOf("class1.algorithm1", "impl3", "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                REPLACE_USING_KEY,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                new Pair("class1.algorithm3", "impl3"),
+                // Do not replace as the key is not present.
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                REPLACE_USING_KEY_AND_VALUE,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                new Pair(new Pair("class1.algorithm1", "impl1"), "impl3"),
+                mapOf("class1.algorithm1", "impl3", "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                REPLACE_USING_KEY_AND_VALUE,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                new Pair(new Pair("class1.algorithm1", "impl4"), "impl3"),
+                // Do not replace as the key/value pair is not present.
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                REPLACE_ALL,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                // Applying simply CONCAT will affect internal mappings of the provider (version,
+                // info, name, etc)
+                CONCAT_IF_STARTING_WITH_CLASS,
+                mapOf("class1.algorithm1", "class1.algorithm1impl1",
+                        "class2.algorithm2", "class2.algorithm2impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                MERGE,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                new Pair(new Pair("class1.algorithm1", "impl3"), CONCAT),
+                // The key is present, so the function is used.
+                mapOf("class1.algorithm1", "impl1impl3",
+                        "class2.algorithm2", "impl2"),
+                true);
+        performHashMapOperationAndCheckResults(
+                MERGE,
+                mapOf("class1.algorithm1", "impl1", "class2.algorithm2", "impl2"),
+                new Pair(new Pair("class3.algorithm3", "impl3"), CONCAT),
+                // The key is not present, so the value is used.
+                mapOf("class1.algorithm1", "impl1",
+                        "class2.algorithm2", "impl2",
+                        "class3.algorithm3", "impl3"),
+                true);
+    }
+
+    public void test_getOrDefault() {
+        Provider p = new MockProvider("MockProvider");
+        p.put("class1.algorithm1", "impl1");
+        assertEquals("impl1", p.getOrDefault("class1.algorithm1", "default"));
+        assertEquals("default", p.getOrDefault("thisIsNotInTheProvider", "default"));
     }
 
     private static class Pair<A, B> {
@@ -714,17 +908,77 @@
     }
 
     private static final Consumer<ProviderAndOperationParameter<Pair<String, String>>> PUT =
-            (ProviderAndOperationParameter<Pair<String, String>> provAndParam) ->
-                {
-                    provAndParam.provider.put(provAndParam.operationParameters.first,
+            provAndParam ->
+                    provAndParam.provider.put(
+                            provAndParam.operationParameters.first,
                             provAndParam.operationParameters.second);
-                };
+
     private static final Consumer<ProviderAndOperationParameter<Map<String, String>>> PUT_ALL =
-            (ProviderAndOperationParameter<Map<String, String>> provAndParam) ->
-                    provAndParam.provider.putAll(provAndParam.operationParameters);
+            provAndParam -> provAndParam.provider.putAll(provAndParam.operationParameters);
+
     private static final Consumer<ProviderAndOperationParameter<String>> REMOVE =
-            (ProviderAndOperationParameter<String> provAndParam) ->
-                    provAndParam.provider.remove(provAndParam.operationParameters);
+            provAndParam -> provAndParam.provider.remove(provAndParam.operationParameters);
+
+    private static final Consumer<ProviderAndOperationParameter<
+            Pair<String, BiFunction<Object, Object, Object>>>> COMPUTE =
+                    provAndParam -> provAndParam.provider.compute(
+                            provAndParam.operationParameters.first,
+                            provAndParam.operationParameters.second);
+
+    private static final BiFunction<Object, Object, Object> CONCAT =
+            (a, b) -> Objects.toString(a) + Objects.toString(b);
+
+    private static final Consumer<ProviderAndOperationParameter<Pair<String, String>>>
+            PUT_IF_ABSENT = provAndParam ->
+                    provAndParam.provider.putIfAbsent(
+                            provAndParam.operationParameters.first,
+                            provAndParam.operationParameters.second);
+
+    private static final Consumer<ProviderAndOperationParameter<
+            Pair<String, BiFunction<Object, Object, Object>>>> COMPUTE_IF_PRESENT =
+                    provAndParam -> provAndParam.provider.computeIfPresent(
+                            provAndParam.operationParameters.first,
+                            provAndParam.operationParameters.second);
+
+    private static final Consumer<ProviderAndOperationParameter<
+            Pair<String, Function<Object, Object>>>> COMPUTE_IF_ABSENT =
+                    provAndParam -> provAndParam.provider.computeIfAbsent(
+                            provAndParam.operationParameters.first,
+                            provAndParam.operationParameters.second);
+
+    private static final Function<Object, Object> TO_UPPER_CASE =
+            s -> Objects.toString(s).toUpperCase();
+
+    private static final Consumer<ProviderAndOperationParameter<Pair<String, String>>>
+            REPLACE_USING_KEY = provAndParam ->
+                    provAndParam.provider.replace(
+                            provAndParam.operationParameters.first,
+                            provAndParam.operationParameters.second);
+
+    private static final Consumer<ProviderAndOperationParameter<Pair<Pair<String, String>, String>>>
+            REPLACE_USING_KEY_AND_VALUE = provAndParam ->
+            provAndParam.provider.replace(
+                    provAndParam.operationParameters.first.first,
+                    provAndParam.operationParameters.first.second,
+                    provAndParam.operationParameters.second);
+
+    private static final Consumer<ProviderAndOperationParameter<
+                BiFunction<Object, Object, Object>>> REPLACE_ALL =
+                        provAndParam -> provAndParam.provider.replaceAll(
+                                provAndParam.operationParameters);
+
+    private static final BiFunction<Object, Object, Object> CONCAT_IF_STARTING_WITH_CLASS =
+            (a, b) -> (Objects.toString(a).startsWith("class"))
+                    ? Objects.toString(a) + Objects.toString(b)
+                    : b;
+
+    private static final Consumer<ProviderAndOperationParameter<
+                    Pair<Pair<String, String>, BiFunction<Object, Object, Object>>>>
+            MERGE = provAndParam -> provAndParam.provider.merge(
+                    provAndParam.operationParameters.first.first,
+                    provAndParam.operationParameters.first.second,
+                    provAndParam.operationParameters.second);
+
 
 
     private static Map<String, String> mapOf(String... elements) {
diff --git a/luni/src/test/java/libcore/java/util/Base64Test.java b/luni/src/test/java/libcore/java/util/Base64Test.java
new file mode 100644
index 0000000..30aa177
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/Base64Test.java
@@ -0,0 +1,1138 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package libcore.java.util;
+
+import junit.framework.TestCase;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.Base64.Decoder;
+import java.util.Base64.Encoder;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Random;
+import java.util.Set;
+
+import static java.nio.charset.StandardCharsets.US_ASCII;
+import static java.util.Arrays.copyOfRange;
+
+public class Base64Test extends TestCase {
+
+    /**
+     * The base 64 alphabet from RFC 4648 Table 1.
+     */
+    private static final Set<Character> TABLE_1 =
+            Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(
+                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+                    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+                    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+                    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
+            )));
+
+    /**
+     * The "URL and Filename safe" Base 64 Alphabet from RFC 4648 Table 2.
+     */
+    private static final Set<Character> TABLE_2 =
+            Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(
+                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+                    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+                    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+                    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
+            )));
+
+    public void testAlphabet_plain() {
+        checkAlphabet(TABLE_1, "", Base64.getEncoder());
+    }
+
+    public void testAlphabet_mime() {
+        checkAlphabet(TABLE_1, "\r\n", Base64.getMimeEncoder());
+    }
+
+    public void testAlphabet_url() {
+        checkAlphabet(TABLE_2, "", Base64.getUrlEncoder());
+    }
+
+    private static void checkAlphabet(Set<Character> expectedAlphabet, String lineSeparator,
+            Encoder encoder) {
+        assertEquals("Base64 alphabet size must be 64 characters", 64, expectedAlphabet.size());
+        byte[] bytes = new byte[256];
+        for (int i = 0; i < 256; i++) {
+            bytes[i] = (byte) i;
+        }
+        Set<Character> actualAlphabet = new HashSet<>();
+
+        byte[] encodedBytes = encoder.encode(bytes);
+        // ignore the padding
+        int endIndex = encodedBytes.length;
+        while (endIndex > 0 && encodedBytes[endIndex - 1] == '=') {
+            endIndex--;
+        }
+        for (byte b : Arrays.copyOfRange(encodedBytes, 0, endIndex)) {
+            char c = (char) b;
+            actualAlphabet.add(c);
+        }
+        for (char c : lineSeparator.toCharArray()) {
+            assertTrue(actualAlphabet.remove(c));
+        }
+        assertEquals(expectedAlphabet, actualAlphabet);
+    }
+
+    /**
+     * Checks decoding of bytes containing a value outside of the allowed
+     * {@link #TABLE_1 "basic" alphabet}.
+     */
+    public void testDecoder_extraChars_basic() throws Exception {
+        Decoder basicDecoder = Base64.getDecoder(); // uses Table 1
+        // Check failure cases common to both RFC4648 Table 1 and Table 2 decoding.
+        checkDecoder_extraChars_common(basicDecoder);
+
+        // Tests characters that are part of RFC4848 Table 2 but not Table 1.
+        assertDecodeThrowsIAe(basicDecoder, "_aGVsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(basicDecoder, "aGV_sbG8sIHdvcmx");
+        assertDecodeThrowsIAe(basicDecoder, "aGVsbG8sIHdvcmx_");
+    }
+
+    /**
+     * Checks decoding of bytes containing a value outside of the allowed
+     * {@link #TABLE_2 url alphabet}.
+     */
+    public void testDecoder_extraChars_url() throws Exception {
+        Decoder urlDecoder = Base64.getUrlDecoder(); // uses Table 2
+        // Check failure cases common to both RFC4648 table 1 and table 2 decoding.
+        checkDecoder_extraChars_common(urlDecoder);
+
+        // Tests characters that are part of RFC4848 Table 1 but not Table 2.
+        assertDecodeThrowsIAe(urlDecoder, "/aGVsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(urlDecoder, "aGV/sbG8sIHdvcmx");
+        assertDecodeThrowsIAe(urlDecoder, "aGVsbG8sIHdvcmx/");
+    }
+
+    /**
+     * Checks characters that are bad both in RFC4648 {@link #TABLE_1} and
+     * in {@link #TABLE_2} based decoding.
+     */
+    private static void checkDecoder_extraChars_common(Decoder decoder) throws Exception {
+        // Characters outside alphabet before padding.
+        assertDecodeThrowsIAe(decoder, " aGVsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGV sbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmx ");
+        assertDecodeThrowsIAe(decoder, "*aGVsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGV*sbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmx*");
+        assertDecodeThrowsIAe(decoder, "\r\naGVsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGV\r\nsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmx\r\n");
+        assertDecodeThrowsIAe(decoder, "\naGVsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGV\nsbG8sIHdvcmx");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmx\n");
+
+        // padding 0
+        assertEquals("hello, world", decodeToAscii(decoder, "aGVsbG8sIHdvcmxk"));
+        // Extra padding
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxk=");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxk==");
+        // Characters outside alphabet intermixed with (too much) padding.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxk =");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxk = = ");
+
+        // padding 1
+        assertEquals("hello, world?!", decodeToAscii(decoder, "aGVsbG8sIHdvcmxkPyE="));
+        // Missing padding
+        assertEquals("hello, world?!", decodeToAscii(decoder, "aGVsbG8sIHdvcmxkPyE"));
+        // Characters outside alphabet before padding.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE =");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE*=");
+        // Trailing characters, otherwise valid.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE= ");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=*");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=X");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=XY");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=XYZ");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=XYZA");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=\n");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE=\r\n");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE= ");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE==");
+        // Characters outside alphabet intermixed with (too much) padding.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE ==");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkPyE = = ");
+
+        // padding 2
+        assertEquals("hello, world.", decodeToAscii(decoder, "aGVsbG8sIHdvcmxkLg=="));
+        // Missing padding
+        assertEquals("hello, world.", decodeToAscii(decoder, "aGVsbG8sIHdvcmxkLg"));
+        // Partially missing padding
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg=");
+        // Characters outside alphabet before padding.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg ==");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg*==");
+        // Trailing characters, otherwise valid.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg== ");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==*");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==X");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==XY");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==XYZ");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==XYZA");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==\n");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg==\r\n");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg== ");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg===");
+        // Characters outside alphabet inside padding.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg= =");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg=*=");
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg=\r\n=");
+        // Characters inside alphabet inside padding.
+        assertDecodeThrowsIAe(decoder, "aGVsbG8sIHdvcmxkLg=X=");
+    }
+
+    public void testDecoder_extraChars_mime() throws Exception {
+        Decoder mimeDecoder = Base64.getMimeDecoder();
+
+        // Characters outside alphabet before padding.
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, " aGVsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV sbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk "));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "_aGVsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV_sbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk_"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "*aGVsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV*sbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk*"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "\r\naGVsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV\r\nsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk\r\n"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "\naGVsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV\nsbG8sIHdvcmxk"));
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk\n"));
+
+        // padding 0
+        assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk"));
+        // Extra padding
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk=");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk==");
+        // Characters outside alphabet intermixed with (too much) padding.
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk =");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk = = ");
+
+        // padding 1
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE="));
+        // Missing padding
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE"));
+        // Characters outside alphabet before padding.
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE ="));
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE*="));
+        // Trailing characters, otherwise valid.
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE= "));
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=*"));
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=X");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=XY");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=XYZ");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=XYZA");
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=\n"));
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=\r\n"));
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE= "));
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=="));
+        // Characters outside alphabet intermixed with (too much) padding.
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE =="));
+        assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE = = "));
+
+        // padding 2
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg=="));
+        // Missing padding
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg"));
+        // Partially missing padding
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=");
+        // Characters outside alphabet before padding.
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg =="));
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg*=="));
+        // Trailing characters, otherwise valid.
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg== "));
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==*"));
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==X");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==XY");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==XYZ");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==XYZA");
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==\n"));
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==\r\n"));
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg== "));
+        assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==="));
+
+        // Characters outside alphabet inside padding are not allowed by the MIME decoder.
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg= =");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=*=");
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=\r\n=");
+
+        // Characters inside alphabet inside padding.
+        assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=X=");
+    }
+
+    public void testDecoder_nonPrintableBytes_basic() throws Exception {
+        checkDecoder_nonPrintableBytes_table1(Base64.getDecoder());
+    }
+
+    public void testDecoder_nonPrintableBytes_mime() throws Exception {
+        checkDecoder_nonPrintableBytes_table1(Base64.getMimeDecoder());
+    }
+
+    /**
+     * Check decoding sample non-ASCII byte[] values from a {@link #TABLE_1}
+     * encoded String.
+     */
+    private static void checkDecoder_nonPrintableBytes_table1(Decoder decoder) throws Exception {
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 0, decoder.decode(""));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 1, decoder.decode("/w=="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 2, decoder.decode("/+4="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 3, decoder.decode("/+7d"));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 4, decoder.decode("/+7dzA=="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 5, decoder.decode("/+7dzLs="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 6, decoder.decode("/+7dzLuq"));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 7, decoder.decode("/+7dzLuqmQ=="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 8, decoder.decode("/+7dzLuqmYg="));
+    }
+
+    /**
+     * Check decoding sample non-ASCII byte[] values from a {@link #TABLE_2}
+     * (url safe) encoded String.
+     */
+    public void testDecoder_nonPrintableBytes_url() throws Exception {
+        Decoder decoder = Base64.getUrlDecoder();
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 0, decoder.decode(""));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 1, decoder.decode("_w=="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 2, decoder.decode("_-4="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 3, decoder.decode("_-7d"));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 4, decoder.decode("_-7dzA=="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 5, decoder.decode("_-7dzLs="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 6, decoder.decode("_-7dzLuq"));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 7, decoder.decode("_-7dzLuqmQ=="));
+        assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 8, decoder.decode("_-7dzLuqmYg="));
+    }
+
+    private static final byte[] SAMPLE_NON_ASCII_BYTES = { (byte) 0xff, (byte) 0xee, (byte) 0xdd,
+            (byte) 0xcc, (byte) 0xbb, (byte) 0xaa,
+            (byte) 0x99, (byte) 0x88, (byte) 0x77 };
+
+    public void testDecoder_closedStream() {
+        try {
+            closedDecodeStream().available();
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+        try {
+            closedDecodeStream().read();
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+        try {
+            closedDecodeStream().read(new byte[23]);
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+
+        try {
+            closedDecodeStream().read(new byte[23], 0, 1);
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+    }
+
+    private static InputStream closedDecodeStream() {
+        InputStream result = Base64.getDecoder().wrap(new ByteArrayInputStream(new byte[0]));
+        try {
+            result.close();
+        } catch (IOException e) {
+            fail(e.getMessage());
+        }
+        return result;
+    }
+
+    /**
+     * Tests {@link Decoder#decode(byte[], byte[])} for correctness as well as
+     * for consistency with other methods tested elsewhere.
+     */
+    public void testDecoder_decodeArrayToArray() {
+        Decoder decoder = Base64.getDecoder();
+
+        // Empty input
+        assertEquals(0, decoder.decode(new byte[0], new byte[0]));
+
+        // Test data for non-empty input
+        String inputString = "YWJjZWZnaGk=";
+        byte[] input = inputString.getBytes(US_ASCII);
+        String expectedString = "abcefghi";
+        byte[] decodedBytes = expectedString.getBytes(US_ASCII);
+        // check test data consistency with other methods that are tested elsewhere
+        assertRoundTrip(Base64.getEncoder(), decoder, inputString, decodedBytes);
+
+        // Non-empty input: output array too short
+        byte[] tooShort = new byte[decodedBytes.length - 1];
+        try {
+            decoder.decode(input, tooShort);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        // Non-empty input: output array longer than required
+        byte[] tooLong = new byte[decodedBytes.length + 1];
+        int tooLongBytesDecoded = decoder.decode(input, tooLong);
+        assertEquals(decodedBytes.length, tooLongBytesDecoded);
+        assertEquals(0, tooLong[tooLong.length - 1]);
+        assertArrayPrefixEquals(tooLong, decodedBytes.length, decodedBytes);
+
+        // Non-empty input: output array has exact minimum required size
+        byte[] justRight = new byte[decodedBytes.length];
+        int justRightBytesDecoded = decoder.decode(input, justRight);
+        assertEquals(decodedBytes.length, justRightBytesDecoded);
+        assertArrayEquals(decodedBytes, justRight);
+
+    }
+
+    public void testDecoder_decodeByteBuffer() {
+        Decoder decoder = Base64.getDecoder();
+
+        byte[] emptyByteArray = new byte[0];
+        ByteBuffer emptyByteBuffer = ByteBuffer.wrap(emptyByteArray);
+        ByteBuffer emptyDecodedBuffer = decoder.decode(emptyByteBuffer);
+        assertEquals(emptyByteBuffer, emptyDecodedBuffer);
+        assertNotSame(emptyByteArray, emptyDecodedBuffer);
+
+        // Test the two types of byte buffer.
+        String inputString = "YWJjZWZnaGk=";
+        byte[] input = inputString.getBytes(US_ASCII);
+        String expectedString = "abcefghi";
+        byte[] expectedBytes = expectedString.getBytes(US_ASCII);
+
+        ByteBuffer inputBuffer = ByteBuffer.allocate(input.length);
+        inputBuffer.put(input);
+        inputBuffer.position(0);
+        checkDecoder_decodeByteBuffer(decoder, inputBuffer, expectedBytes);
+
+        inputBuffer = ByteBuffer.allocateDirect(input.length);
+        inputBuffer.put(input);
+        inputBuffer.position(0);
+        checkDecoder_decodeByteBuffer(decoder, inputBuffer, expectedBytes);
+    }
+
+    private static void checkDecoder_decodeByteBuffer(
+            Decoder decoder, ByteBuffer inputBuffer, byte[] expectedBytes) {
+        assertEquals(0, inputBuffer.position());
+        assertEquals(inputBuffer.remaining(), inputBuffer.limit());
+        int inputLength = inputBuffer.remaining();
+
+        ByteBuffer decodedBuffer = decoder.decode(inputBuffer);
+
+        assertEquals(inputLength, inputBuffer.position());
+        assertEquals(0, inputBuffer.remaining());
+        assertEquals(inputLength, inputBuffer.limit());
+        assertEquals(0, decodedBuffer.position());
+        assertEquals(expectedBytes.length, decodedBuffer.remaining());
+        assertEquals(expectedBytes.length, decodedBuffer.limit());
+    }
+
+    public void testDecoder_decodeByteBuffer_invalidData() {
+        Decoder decoder = Base64.getDecoder();
+
+        // Test the two types of byte buffer.
+        String inputString = "AAAA AAAA";
+        byte[] input = inputString.getBytes(US_ASCII);
+
+        ByteBuffer inputBuffer = ByteBuffer.allocate(input.length);
+        inputBuffer.put(input);
+        inputBuffer.position(0);
+        checkDecoder_decodeByteBuffer_invalidData(decoder, inputBuffer);
+
+        inputBuffer = ByteBuffer.allocateDirect(input.length);
+        inputBuffer.put(input);
+        inputBuffer.position(0);
+        checkDecoder_decodeByteBuffer_invalidData(decoder, inputBuffer);
+    }
+
+    private static void checkDecoder_decodeByteBuffer_invalidData(
+            Decoder decoder, ByteBuffer inputBuffer) {
+        assertEquals(0, inputBuffer.position());
+        assertEquals(inputBuffer.remaining(), inputBuffer.limit());
+        int limit = inputBuffer.limit();
+
+        try {
+            decoder.decode(inputBuffer);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        assertEquals(0, inputBuffer.position());
+        assertEquals(limit, inputBuffer.remaining());
+        assertEquals(limit, inputBuffer.limit());
+    }
+
+    public void testDecoder_nullArgs() {
+        checkDecoder_nullArgs(Base64.getDecoder());
+        checkDecoder_nullArgs(Base64.getMimeDecoder());
+        checkDecoder_nullArgs(Base64.getUrlDecoder());
+    }
+
+    private static void checkDecoder_nullArgs(Decoder decoder) {
+        assertThrowsNpe(() -> decoder.decode((byte[]) null));
+        assertThrowsNpe(() -> decoder.decode((String) null));
+        assertThrowsNpe(() -> decoder.decode(null, null));
+        assertThrowsNpe(() -> decoder.decode((ByteBuffer) null));
+        assertThrowsNpe(() -> decoder.wrap(null));
+    }
+
+    public void testEncoder_nullArgs() {
+        checkEncoder_nullArgs(Base64.getEncoder());
+        checkEncoder_nullArgs(Base64.getMimeEncoder());
+        checkEncoder_nullArgs(Base64.getUrlEncoder());
+        checkEncoder_nullArgs(Base64.getMimeEncoder(20, new byte[] { '*' }));
+        checkEncoder_nullArgs(Base64.getEncoder().withoutPadding());
+        checkEncoder_nullArgs(Base64.getMimeEncoder().withoutPadding());
+        checkEncoder_nullArgs(Base64.getUrlEncoder().withoutPadding());
+        checkEncoder_nullArgs(Base64.getMimeEncoder(20, new byte[] { '*' }).withoutPadding());
+
+    }
+
+    private static void checkEncoder_nullArgs(Encoder encoder) {
+        assertThrowsNpe(() -> encoder.encode((byte[]) null));
+        assertThrowsNpe(() -> encoder.encodeToString(null));
+        assertThrowsNpe(() -> encoder.encode(null, null));
+        assertThrowsNpe(() -> encoder.encode((ByteBuffer) null));
+        assertThrowsNpe(() -> encoder.wrap(null));
+    }
+
+    public void testEncoder_nonPrintableBytes() throws Exception {
+        Encoder encoder = Base64.getUrlEncoder();
+        assertEquals("", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 0)));
+        assertEquals("_w==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 1)));
+        assertEquals("_-4=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 2)));
+        assertEquals("_-7d", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 3)));
+        assertEquals("_-7dzA==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 4)));
+        assertEquals("_-7dzLs=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 5)));
+        assertEquals("_-7dzLuq", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 6)));
+        assertEquals("_-7dzLuqmQ==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 7)));
+        assertEquals("_-7dzLuqmYg=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 8)));
+    }
+
+    public void testEncoder_lineLength() throws Exception {
+        String in_56 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd";
+        String in_57 = in_56 + "e";
+        String in_58 = in_56 + "ef";
+        String in_59 = in_56 + "efg";
+        String in_60 = in_56 + "efgh";
+        String in_61 = in_56 + "efghi";
+
+        String prefix = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFi";
+        String out_56 = prefix + "Y2Q=";
+        String out_57 = prefix + "Y2Rl";
+        String out_58 = prefix + "Y2Rl\r\nZg==";
+        String out_59 = prefix + "Y2Rl\r\nZmc=";
+        String out_60 = prefix + "Y2Rl\r\nZmdo";
+        String out_61 = prefix + "Y2Rl\r\nZmdoaQ==";
+
+        Encoder encoder = Base64.getMimeEncoder();
+        Decoder decoder = Base64.getMimeDecoder();
+        assertEquals("", encodeFromAscii(encoder, decoder, ""));
+        assertEquals(out_56, encodeFromAscii(encoder, decoder, in_56));
+        assertEquals(out_57, encodeFromAscii(encoder, decoder, in_57));
+        assertEquals(out_58, encodeFromAscii(encoder, decoder, in_58));
+        assertEquals(out_59, encodeFromAscii(encoder, decoder, in_59));
+        assertEquals(out_60, encodeFromAscii(encoder, decoder, in_60));
+        assertEquals(out_61, encodeFromAscii(encoder, decoder, in_61));
+
+        encoder = Base64.getUrlEncoder();
+        decoder = Base64.getUrlDecoder();
+        assertEquals(out_56.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_56));
+        assertEquals(out_57.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_57));
+        assertEquals(out_58.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_58));
+        assertEquals(out_59.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_59));
+        assertEquals(out_60.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_60));
+        assertEquals(out_61.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_61));
+    }
+
+    public void testGetMimeEncoder_invalidLineSeparator() {
+        byte[] invalidLineSeparator = { 'A' };
+        try {
+            Base64.getMimeEncoder(20, invalidLineSeparator);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            Base64.getMimeEncoder(0, invalidLineSeparator);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            Base64.getMimeEncoder(20, null);
+            fail();
+        } catch (NullPointerException expected) {
+        }
+
+        try {
+            Base64.getMimeEncoder(0, null);
+            fail();
+        } catch (NullPointerException expected) {
+        }
+    }
+
+    public void testEncoder_closedStream() {
+        try {
+            closedEncodeStream().write(100);
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+        try {
+            closedEncodeStream().write(new byte[100]);
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+
+        try {
+            closedEncodeStream().write(new byte[100], 0, 1);
+            fail("Should have thrown");
+        } catch (IOException expected) {
+        }
+    }
+
+    private static OutputStream closedEncodeStream() {
+        OutputStream result = Base64.getEncoder().wrap(new ByteArrayOutputStream());
+        try {
+            result.close();
+        } catch (IOException e) {
+            fail(e.getMessage());
+        }
+        return result;
+    }
+
+
+    /**
+     * Tests {@link Decoder#decode(byte[], byte[])} for correctness.
+     */
+    public void testEncoder_encodeArrayToArray() {
+        Encoder encoder = Base64.getEncoder();
+
+        // Empty input
+        assertEquals(0, encoder.encode(new byte[0], new byte[0]));
+
+        // Test data for non-empty input
+        byte[] input = "abcefghi".getBytes(US_ASCII);
+        String expectedString = "YWJjZWZnaGk=";
+        byte[] encodedBytes = expectedString.getBytes(US_ASCII);
+
+        // Non-empty input: output array too short
+        byte[] tooShort = new byte[encodedBytes.length - 1];
+        try {
+            encoder.encode(input, tooShort);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        // Non-empty input: output array longer than required
+        byte[] tooLong = new byte[encodedBytes.length + 1];
+        int tooLongBytesEncoded = encoder.encode(input, tooLong);
+        assertEquals(encodedBytes.length, tooLongBytesEncoded);
+        assertEquals(0, tooLong[tooLong.length - 1]);
+        assertArrayPrefixEquals(tooLong, encodedBytes.length, encodedBytes);
+
+        // Non-empty input: output array has exact minimum required size
+        byte[] justRight = new byte[encodedBytes.length];
+        int justRightBytesEncoded = encoder.encode(input, justRight);
+        assertEquals(encodedBytes.length, justRightBytesEncoded);
+        assertArrayEquals(encodedBytes, justRight);
+    }
+
+    public void testEncoder_encodeByteBuffer() {
+        Encoder encoder = Base64.getEncoder();
+
+        byte[] emptyByteArray = new byte[0];
+        ByteBuffer emptyByteBuffer = ByteBuffer.wrap(emptyByteArray);
+        ByteBuffer emptyEncodedBuffer = encoder.encode(emptyByteBuffer);
+        assertEquals(emptyByteBuffer, emptyEncodedBuffer);
+        assertNotSame(emptyByteArray, emptyEncodedBuffer);
+
+        // Test the two types of byte buffer.
+        byte[] input = "abcefghi".getBytes(US_ASCII);
+        String expectedString = "YWJjZWZnaGk=";
+        byte[] expectedBytes = expectedString.getBytes(US_ASCII);
+
+        ByteBuffer inputBuffer = ByteBuffer.allocate(input.length);
+        inputBuffer.put(input);
+        inputBuffer.position(0);
+        testEncoder_encodeByteBuffer(encoder, inputBuffer, expectedBytes);
+
+        inputBuffer = ByteBuffer.allocateDirect(input.length);
+        inputBuffer.put(input);
+        inputBuffer.position(0);
+        testEncoder_encodeByteBuffer(encoder, inputBuffer, expectedBytes);
+    }
+
+    private static void testEncoder_encodeByteBuffer(
+            Encoder encoder, ByteBuffer inputBuffer, byte[] expectedBytes) {
+        assertEquals(0, inputBuffer.position());
+        assertEquals(inputBuffer.remaining(), inputBuffer.limit());
+        int inputLength = inputBuffer.remaining();
+
+        ByteBuffer encodedBuffer = encoder.encode(inputBuffer);
+
+        assertEquals(inputLength, inputBuffer.position());
+        assertEquals(0, inputBuffer.remaining());
+        assertEquals(inputLength, inputBuffer.limit());
+        assertEquals(0, encodedBuffer.position());
+        assertEquals(expectedBytes.length, encodedBuffer.remaining());
+        assertEquals(expectedBytes.length, encodedBuffer.limit());
+    }
+
+    /**
+     * Checks that all encoders/decoders map {@code new byte[0]} to "" and vice versa.
+     */
+    public void testRoundTrip_empty() {
+        checkRoundTrip_empty(Base64.getEncoder(), Base64.getDecoder());
+        checkRoundTrip_empty(Base64.getMimeEncoder(), Base64.getMimeDecoder());
+        byte[] sep = new byte[] { '\r', '\n' };
+        checkRoundTrip_empty(Base64.getMimeEncoder(-1, sep), Base64.getMimeDecoder());
+        checkRoundTrip_empty(Base64.getMimeEncoder(20, new byte[0]), Base64.getMimeDecoder());
+        checkRoundTrip_empty(Base64.getMimeEncoder(23, sep), Base64.getMimeDecoder());
+        checkRoundTrip_empty(Base64.getMimeEncoder(76, sep), Base64.getMimeDecoder());
+        checkRoundTrip_empty(Base64.getUrlEncoder(), Base64.getUrlDecoder());
+    }
+
+    private static void checkRoundTrip_empty(Encoder encoder, Decoder decoder) {
+        assertRoundTrip(encoder, decoder, "", new byte[0]);
+    }
+
+    /**
+     * Encoding of byte values 0..255 using the non-URL alphabet.
+     */
+    private static final String ALL_BYTE_VALUES_ENCODED =
+            "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4" +
+                    "OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx" +
+                    "cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmq" +
+                    "q6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj" +
+                    "5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
+
+    public void testRoundTrip_allBytes_plain() {
+        checkRoundTrip_allBytes_singleLine(Base64.getEncoder(), Base64.getDecoder());
+    }
+
+    /**
+     * Checks that if the lineSeparator is empty or the line length is {@code <= 3}
+     * or larger than the data to be encoded, a single line is returned.
+     */
+    public void testRoundTrip_allBytes_mime_singleLine() {
+        Decoder decoder = Base64.getMimeDecoder();
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(76, new byte[0]), decoder);
+
+        // Line lengths <= 3 mean no wrapping; the separator is ignored in that case.
+        byte[] separator = new byte[] { '*' };
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(Integer.MIN_VALUE, separator),
+                decoder);
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(-1, separator), decoder);
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(0, separator), decoder);
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(1, separator), decoder);
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(2, separator), decoder);
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(3, separator), decoder);
+
+        // output fits into the permitted line length
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(
+                ALL_BYTE_VALUES_ENCODED.length(), separator), decoder);
+        checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(Integer.MAX_VALUE, separator),
+                decoder);
+    }
+
+    /**
+     * Checks round-trip encoding/decoding for a few simple examples that
+     * should work the same across three Encoder/Decoder pairs: This is
+     * because they only use characters that are in both RFC 4648 Table 1
+     * and Table 2, and are short enough to fit into a single line.
+     */
+    public void testRoundTrip_simple_basic() throws Exception {
+        // uses Table 1, never adds linebreaks
+        checkRoundTrip_simple(Base64.getEncoder(), Base64.getDecoder());
+        // uses Table 1, allows 76 chars in a line
+        checkRoundTrip_simple(Base64.getMimeEncoder(), Base64.getMimeDecoder());
+        // uses Table 2, never adds linebreaks
+        checkRoundTrip_simple(Base64.getUrlEncoder(), Base64.getUrlDecoder());
+    }
+
+    private static void checkRoundTrip_simple(Encoder encoder, Decoder decoder) throws Exception {
+        assertRoundTrip(encoder, decoder, "YQ==", "a".getBytes(US_ASCII));
+        assertRoundTrip(encoder, decoder, "YWI=", "ab".getBytes(US_ASCII));
+        assertRoundTrip(encoder, decoder, "YWJj", "abc".getBytes(US_ASCII));
+        assertRoundTrip(encoder, decoder, "YWJjZA==", "abcd".getBytes(US_ASCII));
+    }
+
+    /** check a range of possible line lengths */
+    public void testRoundTrip_allBytes_mime_lineLength() {
+        Decoder decoder = Base64.getMimeDecoder();
+        byte[] separator = new byte[] { '*' };
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(4, separator), decoder,
+                wrapLines("*", ALL_BYTE_VALUES_ENCODED, 4));
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(8, separator), decoder,
+                wrapLines("*", ALL_BYTE_VALUES_ENCODED, 8));
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(20, separator), decoder,
+                wrapLines("*", ALL_BYTE_VALUES_ENCODED, 20));
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(100, separator), decoder,
+                wrapLines("*", ALL_BYTE_VALUES_ENCODED, 100));
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(Integer.MAX_VALUE & ~3, separator), decoder,
+                wrapLines("*", ALL_BYTE_VALUES_ENCODED, Integer.MAX_VALUE & ~3));
+    }
+
+    public void testRoundTrip_allBytes_mime_lineLength_defaultsTo76Chars() {
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(), Base64.getMimeDecoder(),
+                wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 76));
+    }
+
+    /**
+     * checks that the specified line length is rounded down to the nearest multiple of 4.
+     */
+    public void testRoundTrip_allBytes_mime_lineLength_isRoundedDown() {
+        Decoder decoder = Base64.getMimeDecoder();
+        byte[] separator = new byte[] { '\r', '\n' };
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(60, separator), decoder,
+                wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 60));
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(63, separator), decoder,
+                wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 60));
+        checkRoundTrip_allBytes(Base64.getMimeEncoder(10, separator), decoder,
+                wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 8));
+    }
+
+    public void testRoundTrip_allBytes_url() {
+        String encodedUrl = ALL_BYTE_VALUES_ENCODED.replace('+', '-').replace('/', '_');
+        checkRoundTrip_allBytes(Base64.getUrlEncoder(), Base64.getUrlDecoder(), encodedUrl);
+    }
+
+    /**
+     * Checks round-trip encoding/decoding of all byte values 0..255 for
+     * the case where the Encoder doesn't add any linebreaks.
+     */
+    private static void checkRoundTrip_allBytes_singleLine(Encoder encoder, Decoder decoder) {
+        checkRoundTrip_allBytes(encoder, decoder, ALL_BYTE_VALUES_ENCODED);
+    }
+
+    /**
+     * Checks that byte values 0..255, in order, are encoded to exactly
+     * the given String (including any linebreaks, if present) and that
+     * that String can be decoded back to the same byte values.
+     *
+     * @param encoded the expected encoded representation of the (unsigned)
+     *        byte values 0..255, in order.
+     */
+    private static void checkRoundTrip_allBytes(Encoder encoder, Decoder decoder, String encoded) {
+        byte[] bytes = new byte[256];
+        for (int i = 0; i < 256; i++) {
+            bytes[i] = (byte) i;
+        }
+        assertRoundTrip(encoder, decoder, encoded, bytes);
+    }
+
+    public void testRoundTrip_variousSizes_plain() {
+        checkRoundTrip_variousSizes(Base64.getEncoder(), Base64.getDecoder());
+    }
+
+    public void testRoundTrip_variousSizes_mime() {
+        checkRoundTrip_variousSizes(Base64.getMimeEncoder(), Base64.getMimeDecoder());
+    }
+
+    public void testRoundTrip_variousSizes_url() {
+        checkRoundTrip_variousSizes(Base64.getUrlEncoder(), Base64.getUrlDecoder());
+    }
+
+    /**
+     * Checks that various-sized inputs survive a round trip.
+     */
+    private static void checkRoundTrip_variousSizes(Encoder encoder, Decoder decoder) {
+        Random random = new Random(7654321);
+        for (int numBytes : new int [] { 0, 1, 2, 75, 76, 77, 80, 100, 1234 }) {
+            byte[] bytes = new byte[numBytes];
+            random.nextBytes(bytes);
+            byte[] result = decoder.decode(encoder.encode(bytes));
+            assertArrayEquals(bytes, result);
+        }
+    }
+
+    public void testRoundtrip_wrap_basic() throws Exception {
+        Encoder encoder = Base64.getEncoder();
+        Decoder decoder = Base64.getDecoder();
+        checkRoundTrip_wrapInputStream(encoder, decoder);
+    }
+
+    public void testRoundtrip_wrap_mime() throws Exception {
+        Encoder encoder = Base64.getMimeEncoder();
+        Decoder decoder = Base64.getMimeDecoder();
+        checkRoundTrip_wrapInputStream(encoder, decoder);
+    }
+
+    public void testRoundTrip_wrap_url() throws Exception {
+        Encoder encoder = Base64.getUrlEncoder();
+        Decoder decoder = Base64.getUrlDecoder();
+        checkRoundTrip_wrapInputStream(encoder, decoder);
+    }
+
+    /**
+     * Checks that the {@link Decoder#wrap(InputStream) wrapping} an
+     * InputStream of encoded data yields the plain data that was
+     * previously {@link Encoder#encode(byte[]) encoded}.
+     */
+    private static void checkRoundTrip_wrapInputStream(Encoder encoder, Decoder decoder)
+            throws IOException {
+        Random random = new Random(32176L);
+        int[] writeLengths = { -10, -5, -1, 0, 1, 1, 2, 2, 3, 10, 100 };
+
+        // Test input needs to be at least 2048 bytes to fill up the
+        // read buffer of Base64InputStream.
+        byte[] plain = new byte[4567];
+        random.nextBytes(plain);
+        byte[] encoded = encoder.encode(plain);
+        byte[] actual = new byte[plain.length * 2];
+        int b;
+
+        // ----- test decoding ("encoded" -> "plain") -----
+
+        // read as much as it will give us in one chunk
+        ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
+        InputStream b64is = decoder.wrap(bais);
+        int ap = 0;
+        while ((b = b64is.read(actual, ap, actual.length - ap)) != -1) {
+            ap += b;
+        }
+        assertArrayPrefixEquals(actual, ap, plain);
+
+        // read individual bytes
+        bais = new ByteArrayInputStream(encoded);
+        b64is = decoder.wrap(bais);
+        ap = 0;
+        while ((b = b64is.read()) != -1) {
+            actual[ap++] = (byte) b;
+        }
+        assertArrayPrefixEquals(actual, ap, plain);
+
+        // mix reads of variously-sized arrays with one-byte reads
+        bais = new ByteArrayInputStream(encoded);
+        b64is = decoder.wrap(bais);
+        ap = 0;
+        while (true) {
+            int l = writeLengths[random.nextInt(writeLengths.length)];
+            if (l >= 0) {
+                b = b64is.read(actual, ap, l);
+                if (b == -1) {
+                    break;
+                }
+                ap += b;
+            } else {
+                for (int i = 0; i < -l; ++i) {
+                    if ((b = b64is.read()) == -1) {
+                        break;
+                    }
+                    actual[ap++] = (byte) b;
+                }
+            }
+        }
+        assertArrayPrefixEquals(actual, ap, plain);
+    }
+
+    public void testDecoder_wrap_singleByteReads() throws IOException {
+        InputStream in = Base64.getDecoder().wrap(new ByteArrayInputStream("/v8=".getBytes()));
+        assertEquals(254, in.read());
+        assertEquals(255, in.read());
+        assertEquals(-1, in.read());
+    }
+
+    public void testEncoder_withoutPadding() {
+        byte[] bytes = new byte[] { (byte) 0xFE, (byte) 0xFF };
+        assertEquals("/v8=", Base64.getEncoder().encodeToString(bytes));
+        assertEquals("/v8", Base64.getEncoder().withoutPadding().encodeToString(bytes));
+
+        assertEquals("/v8=", Base64.getMimeEncoder().encodeToString(bytes));
+        assertEquals("/v8", Base64.getMimeEncoder().withoutPadding().encodeToString(bytes));
+
+        assertEquals("_v8=", Base64.getUrlEncoder().encodeToString(bytes));
+        assertEquals("_v8", Base64.getUrlEncoder().withoutPadding().encodeToString(bytes));
+    }
+
+    public void testEncoder_wrap_plain() throws Exception {
+        checkWrapOutputStreamConsistentWithEncode(Base64.getEncoder());
+    }
+
+    public void testEncoder_wrap_url() throws Exception {
+        checkWrapOutputStreamConsistentWithEncode(Base64.getUrlEncoder());
+    }
+
+    public void testEncoder_wrap_mime() throws Exception {
+        checkWrapOutputStreamConsistentWithEncode(Base64.getMimeEncoder());
+    }
+
+    /** A way of writing bytes to an OutputStream. */
+    interface WriteStrategy {
+        void write(byte[] bytes, OutputStream out) throws IOException;
+    }
+
+    private static void checkWrapOutputStreamConsistentWithEncode(Encoder encoder)
+            throws Exception {
+        final Random random = new Random(32176L);
+
+        // one large write(byte[]) of the whole input
+        WriteStrategy allAtOnce = (bytes, out) -> out.write(bytes);
+        checkWrapOutputStreamConsistentWithEncode(encoder, allAtOnce);
+
+        // many calls to write(int)
+        WriteStrategy byteWise = (bytes, out) -> {
+            for (byte b : bytes) {
+                out.write(b);
+            }
+        };
+        checkWrapOutputStreamConsistentWithEncode(encoder, byteWise);
+
+        // intermixed sequences of write(int) with
+        // write(byte[],int,int) of various lengths.
+        WriteStrategy mixed = (bytes, out) -> {
+            int[] writeLengths = { -10, -5, -1, 0, 1, 1, 2, 2, 3, 10, 100 };
+            int p = 0;
+            while (p < bytes.length) {
+                int l = writeLengths[random.nextInt(writeLengths.length)];
+                l = Math.min(l, bytes.length - p);
+                if (l >= 0) {
+                    out.write(bytes, p, l);
+                    p += l;
+                } else {
+                    l = Math.min(-l, bytes.length - p);
+                    for (int i = 0; i < l; ++i) {
+                        out.write(bytes[p + i]);
+                    }
+                    p += l;
+                }
+            }
+        };
+        checkWrapOutputStreamConsistentWithEncode(encoder, mixed);
+    }
+
+    /**
+     * Checks that writing to a wrap()ping OutputStream produces the same
+     * output on the wrapped stream as {@link Encoder#encode(byte[])}.
+     */
+    private static void checkWrapOutputStreamConsistentWithEncode(Encoder encoder,
+            WriteStrategy writeStrategy) throws IOException {
+        Random random = new Random(32176L);
+        // Test input needs to be at least 1024 bytes to test filling
+        // up the write(int) buffer of Base64OutputStream.
+        byte[] plain = new byte[1234];
+        random.nextBytes(plain);
+        byte[] encodeResult = encoder.encode(plain);
+        ByteArrayOutputStream wrappedOutputStream = new ByteArrayOutputStream();
+        try (OutputStream plainOutputStream = encoder.wrap(wrappedOutputStream)) {
+            writeStrategy.write(plain, plainOutputStream);
+        }
+        assertArrayEquals(encodeResult, wrappedOutputStream.toByteArray());
+    }
+
+    /** Decodes a string, returning the resulting bytes interpreted as an ASCII String. */
+    private static String decodeToAscii(Decoder decoder, String encoded) throws Exception {
+        byte[] plain = decoder.decode(encoded);
+        return new String(plain, US_ASCII);
+    }
+
+    /**
+     * Checks round-trip encoding/decoding of {@code plain}.
+     *
+     * @param plain an ASCII String
+     * @return the Base64-encoded value of the ASCII codepoints from {@code plain}
+     */
+    private static String encodeFromAscii(Encoder encoder, Decoder decoder, String plain)
+            throws Exception {
+        String encoded = encoder.encodeToString(plain.getBytes(US_ASCII));
+        String decoded = decodeToAscii(decoder, encoded);
+        assertEquals(plain, decoded);
+        return encoded;
+    }
+
+    /**
+     * Rewraps {@code s} by inserting {@lineSeparator} every {@code lineLength} characters,
+     * but not at the end.
+     */
+    private static String wrapLines(String lineSeparator, String s, int lineLength) {
+        return String.join(lineSeparator, breakLines(s, lineLength));
+    }
+
+    /**
+     * Splits {@code s} into a list of substrings, each except possibly the last one
+     * exactly {@code lineLength} characters long.
+     */
+    private static List<String> breakLines(String longString, int lineLength) {
+        List<String> lines = new ArrayList<>();
+        for (int pos = 0; pos < longString.length(); pos += lineLength) {
+            lines.add(longString.substring(pos, Math.min(longString.length(), pos + lineLength)));
+        }
+        return lines;
+    }
+
+    /** Assert that decoding the specific String throws IllegalArgumentException. */
+    private static void assertDecodeThrowsIAe(Decoder decoder, String invalidEncoded)
+            throws Exception {
+        try {
+            decoder.decode(invalidEncoded);
+            fail("should have failed to decode");
+        } catch (IllegalArgumentException e) {
+        }
+    }
+
+    /**
+     * Asserts that the given String decodes to the bytes, and that the bytes encode
+     * to the given String.
+     */
+    private static void assertRoundTrip(Encoder encoder, Decoder decoder, String encoded,
+            byte[] bytes) {
+        assertEquals(encoded, encoder.encodeToString(bytes));
+        assertArrayEquals(bytes, decoder.decode(encoded));
+    }
+
+    /** Asserts that actual equals the first len bytes of expected. */
+    private static void assertArrayPrefixEquals(byte[] expected, int len, byte[] actual) {
+        assertArrayEquals(copyOfRange(expected, 0, len), actual);
+    }
+
+    /** Checks array contents. */
+    private static void assertArrayEquals(byte[] expected, byte[] actual) {
+        if (!Arrays.equals(expected, actual)) {
+            fail("Expected " + hexString(expected) + ", got " + hexString(actual));
+        }
+    }
+
+    private static String hexString(byte[] bytes) {
+        StringBuilder sb = new StringBuilder("0x");
+        for (byte b : bytes) {
+            sb.append(Integer.toHexString(b & 0xff));
+        }
+        return sb.toString();
+    }
+
+    private static void assertThrowsNpe(Runnable runnable) {
+        try {
+            runnable.run();
+            fail("Should have thrown NullPointerException");
+        } catch (NullPointerException expected) {
+        }
+    }
+
+}
diff --git a/luni/src/test/java/libcore/java/util/InvalidPropertiesFormatExceptionTest.java b/luni/src/test/java/libcore/java/util/InvalidPropertiesFormatExceptionTest.java
new file mode 100644
index 0000000..694c069
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/InvalidPropertiesFormatExceptionTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package libcore.java.util;
+
+import junit.framework.TestCase;
+
+import java.io.ByteArrayOutputStream;
+import java.io.NotSerializableException;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.InvalidPropertiesFormatException;
+import libcore.util.SerializationTester;
+
+public class InvalidPropertiesFormatExceptionTest extends TestCase {
+
+    public void testConstructorArgs() {
+        InvalidPropertiesFormatException e = new InvalidPropertiesFormatException("testing");
+        assertEquals("testing", e.getMessage());
+        assertNull(e.getCause());
+
+        InvalidPropertiesFormatException e2 = new InvalidPropertiesFormatException(e);
+        assertSame(e, e2.getCause());
+        assertEquals(e.toString(), e2.getMessage());
+    }
+
+    public void testDeserialize_notSupported() throws Exception {
+        // Result of
+        // SerializationTester.serializeHex(new InvalidPropertiesFormatException("testing"))
+        // using a InvalidPropertiesFormatException class that had its
+        // writeObject() method commented out.
+        String hex = "aced00057372002a6a6176612e7574696c2e496e76616c696450726f"
+                + "70657274696573466f726d6174457863657074696f6e6bbbea5ee5f9cb5"
+                + "b020000787200136a6176612e696f2e494f457863657074696f6e6c8073"
+                + "646525f0ab020000787200136a6176612e6c616e672e457863657074696"
+                + "f6ed0fd1f3e1a3b1cc4020000787200136a6176612e6c616e672e546872"
+                + "6f7761626c65d5c635273977b8cb0300044c000563617573657400154c6"
+                + "a6176612f6c616e672f5468726f7761626c653b4c000d64657461696c4d"
+                + "6573736167657400124c6a6176612f6c616e672f537472696e673b5b000"
+                + "a737461636b547261636574001e5b4c6a6176612f6c616e672f53746163"
+                + "6b5472616365456c656d656e743b4c00147375707072657373656445786"
+                + "3657074696f6e737400104c6a6176612f7574696c2f4c6973743b787071"
+                + "007e000874000774657374696e677572001e5b4c6a6176612e6c616e672"
+                + "e537461636b5472616365456c656d656e743b02462a3c3cfd2239020000"
+                + "78700000000a7372001b6a6176612e6c616e672e537461636b547261636"
+                + "5456c656d656e746109c59a2636dd8502000449000a6c696e654e756d62"
+                + "65724c000e6465636c6172696e67436c61737371007e00054c000866696"
+                + "c654e616d6571007e00054c000a6d6574686f644e616d6571007e000578"
+                + "70000000457400366c6962636f72652e6a6176612e7574696c2e496e766"
+                + "16c696450726f70657274696573466f726d6174457863657074696f6e54"
+                + "657374740029496e76616c696450726f70657274696573466f726d61744"
+                + "57863657074696f6e546573742e6a61766174001a746573745365726961"
+                + "6c697a655f6e6f74537570706f727465647371007e000cfffffffe74001"
+                + "86a6176612e6c616e672e7265666c6563742e4d6574686f6474000b4d65"
+                + "74686f642e6a617661740006696e766f6b657371007e000c000000c2740"
+                + "028766f6761722e7461726765742e6a756e69742e4a756e69743324566f"
+                + "6761724a556e69745465737474000b4a756e6974332e6a6176617400037"
+                + "2756e7371007e000c0000003b740024766f6761722e7461726765742e6a"
+                + "756e69742e566f6761725465737452756e6e65722431740014566f67617"
+                + "25465737452756e6e65722e6a6176617400086576616c75617465737100"
+                + "7e000c0000004874002b766f6761722e7461726765742e6a756e69742e5"
+                + "4696d656f7574416e6441626f727452756e52756c65243274001b54696d"
+                + "656f7574416e6441626f727452756e52756c652e6a61766174000463616"
+                + "c6c7371007e000c0000004474002b766f6761722e7461726765742e6a75"
+                + "6e69742e54696d656f7574416e6441626f727452756e52756c652432740"
+                + "01b54696d656f7574416e6441626f727452756e52756c652e6a61766174"
+                + "000463616c6c7371007e000c000000ed74001f6a6176612e7574696c2e6"
+                + "36f6e63757272656e742e4675747572655461736b74000f467574757265"
+                + "5461736b2e6a61766174000372756e7371007e000c0000046d7400276a6"
+                + "176612e7574696c2e636f6e63757272656e742e546872656164506f6f6c"
+                + "4578656375746f72740017546872656164506f6f6c4578656375746f722"
+                + "e6a61766174000972756e576f726b65727371007e000c0000025f74002e"
+                + "6a6176612e7574696c2e636f6e63757272656e742e546872656164506f6"
+                + "f6c4578656375746f7224576f726b6572740017546872656164506f6f6c"
+                + "4578656375746f722e6a61766174000372756e7371007e000c000002f87"
+                + "400106a6176612e6c616e672e54687265616474000b5468726561642e6a"
+                + "61766174000372756e7372001f6a6176612e7574696c2e436f6c6c65637"
+                + "4696f6e7324456d7074794c6973747ab817b43ca79ede020000787078";
+        try {
+            Object obj = SerializationTester.deserializeHex(hex);
+            fail("Deserialized to " + obj);
+        } catch (NotSerializableException expected) {
+            // Sanity check that this is the right exception that we expected.
+            assertEquals("Not serializable.", expected.getMessage());
+        }
+    }
+
+    public void testSerialize_notSupported() throws Exception {
+        Serializable notActuallySerializable = new InvalidPropertiesFormatException("testing");
+        try {
+            try (ObjectOutputStream out = new ObjectOutputStream(new ByteArrayOutputStream())) {
+                out.writeObject(notActuallySerializable);
+            }
+            fail();
+        } catch (NotSerializableException expected) {
+            // Sanity check that this is the right exception that we expected.
+            assertEquals("Not serializable.", expected.getMessage());
+        }
+    }
+}
diff --git a/luni/src/test/java/libcore/java/util/ResourceLeakageDetectorTest.java b/luni/src/test/java/libcore/java/util/ResourceLeakageDetectorTest.java
index d86c9f2..5509f73 100644
--- a/luni/src/test/java/libcore/java/util/ResourceLeakageDetectorTest.java
+++ b/luni/src/test/java/libcore/java/util/ResourceLeakageDetectorTest.java
@@ -26,17 +26,24 @@
    * This test will not work on RI as it does not support the <code>CloseGuard</code> or similar
    * mechanism.
    */
-  public void testDetectsUnclosedCloseGuard() throws Exception {
+  // TODO(paulduffin): b/31542223 - Work out why this is failing in CTS, fix and reenable.
+  public void notestDetectsUnclosedCloseGuard() throws Exception {
     ResourceLeakageDetector detector = ResourceLeakageDetector.newDetector();
     try {
       CloseGuard closeGuard = createCloseGuard();
       closeGuard.open("open");
     } finally {
+      boolean leaksDetected = true;
       try {
         System.logI("Checking for leaks");
         detector.checkForLeaks();
-        fail();
+        leaksDetected = false;
       } catch (AssertionError expected) {
+        // The leak detector should throw this error.
+      }
+
+      if (!leaksDetected) {
+        fail("Did not detect any leaks");
       }
     }
   }
diff --git a/luni/src/test/java/libcore/java/util/logging/OldFileHandlerTest.java b/luni/src/test/java/libcore/java/util/logging/OldFileHandlerTest.java
index 785b265..ead0b2e 100644
--- a/luni/src/test/java/libcore/java/util/logging/OldFileHandlerTest.java
+++ b/luni/src/test/java/libcore/java/util/logging/OldFileHandlerTest.java
@@ -33,6 +33,7 @@
 import java.util.logging.Level;
 import java.util.logging.LogManager;
 import java.util.logging.LogRecord;
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
 public class OldFileHandlerTest extends TestCase {
@@ -162,11 +163,15 @@
         FileHandler h7 = new FileHandler("%t/log/string%u.log");
         h7.publish(r);
         h7.close();
+        boolean assertionPassed = false;
         try {
-            assertFileContent(TEMPPATH + SEP + "log", "string0.log", h
-                    .getFormatter());
-            fail("should assertion failed");
-        } catch (Error e) {
+            assertFileContent(TEMPPATH + SEP + "log", "string0.log", h.getFormatter());
+            assertionPassed = true;
+        } catch (AssertionFailedError e) {
+            // Assertion failed as expected.
+        }
+        if (assertionPassed) {
+            fail("assertion should have failed");
         }
         File file = new File(TEMPPATH + SEP + "log");
         assertTrue("length list of file is incorrect", file.list().length <= 2);
diff --git a/luni/src/test/java/libcore/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
index 4bb0c07..7299c00 100644
--- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java
+++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
@@ -36,7 +36,7 @@
 import java.security.Security;
 import java.security.cert.Certificate;
 import java.security.spec.AlgorithmParameterSpec;
-import java.security.spec.RSAPrivateKeySpec;
+import java.security.spec.RSAPrivateCrtKeySpec;
 import java.security.spec.RSAPublicKeySpec;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -243,42 +243,64 @@
                 || algorithm.contains("/CFB");
     }
 
+    private static boolean isRandomizedEncryption(String algorithm) {
+        return algorithm.endsWith("/PKCS1PADDING");
+    }
+
     private static Map<String, Key> ENCRYPT_KEYS = new HashMap<String, Key>();
-    private synchronized static Key getEncryptKey(String algorithm) throws Exception {
+
+    /**
+     * Returns the key meant for enciphering for {@code algorithm}.
+     */
+    private synchronized static Key getEncryptKey(String algorithm) {
         Key key = ENCRYPT_KEYS.get(algorithm);
         if (key != null) {
             return key;
         }
-        if (algorithm.startsWith("RSA")) {
-            KeyFactory kf = KeyFactory.getInstance("RSA");
-            RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                                                              RSA_2048_privateExponent);
-            key = kf.generatePrivate(keySpec);
-        } else if (isPBE(algorithm)) {
-            SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
-            key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
-        } else {
-            KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
-            key = kg.generateKey();
+        try {
+            if (algorithm.startsWith("RSA")) {
+                KeyFactory kf = KeyFactory.getInstance("RSA");
+                RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus,
+                        RSA_2048_publicExponent);
+                key = kf.generatePublic(keySpec);
+            } else if (isPBE(algorithm)) {
+                SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
+                key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
+            } else {
+                KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
+                key = kg.generateKey();
+            }
+        } catch (Exception e) {
+            throw new AssertionError("Error generating keys for test setup", e);
         }
         ENCRYPT_KEYS.put(algorithm, key);
         return key;
     }
 
     private static Map<String, Key> DECRYPT_KEYS = new HashMap<String, Key>();
-    private synchronized static Key getDecryptKey(String algorithm) throws Exception {
+
+    /**
+     * Returns the key meant for deciphering for {@code algorithm}.
+     */
+    private synchronized static Key getDecryptKey(String algorithm) {
         Key key = DECRYPT_KEYS.get(algorithm);
         if (key != null) {
             return key;
         }
-        if (algorithm.startsWith("RSA")) {
-            KeyFactory kf = KeyFactory.getInstance("RSA");
-            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus,
-                                                            RSA_2048_publicExponent);
-            key = kf.generatePublic(keySpec);
-        } else {
-            assertFalse(algorithm, isAsymmetric(algorithm));
-            key = getEncryptKey(algorithm);
+        try {
+            if (algorithm.startsWith("RSA")) {
+                KeyFactory kf = KeyFactory.getInstance("RSA");
+                RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus,
+                        RSA_2048_publicExponent, RSA_2048_privateExponent, RSA_2048_primeP,
+                        RSA_2048_primeQ, RSA_2048_primeExponentP, RSA_2048_primeExponentQ,
+                        RSA_2048_crtCoefficient);
+                key = kf.generatePrivate(keySpec);
+            } else {
+                assertFalse(algorithm, isAsymmetric(algorithm));
+                key = getEncryptKey(algorithm);
+            }
+        } catch (Exception e) {
+            throw new AssertionError("Error generating keys for test setup", e);
         }
         DECRYPT_KEYS.put(algorithm, key);
         return key;
@@ -1395,7 +1417,7 @@
 
         test_Cipher_init_Decrypt_NullParameters(c, decryptMode, encryptKey, decryptSpec != null);
 
-        c.init(decryptMode, encryptKey, decryptSpec);
+        c.init(decryptMode, getDecryptKey(algorithm), decryptSpec);
         assertEquals(cipherID + " getBlockSize() decryptMode",
                      getExpectedBlockSize(algorithm, decryptMode, providerName), c.getBlockSize());
         assertEquals(cipherID + " getOutputSize(0) decryptMode",
@@ -1484,13 +1506,13 @@
                 c.updateAAD(new byte[24]);
             }
             byte[] cipherText = c.doFinal(getActualPlainText(algorithm));
-            if (isAEAD(algorithm)) {
-                c.updateAAD(new byte[24]);
+            if (!isRandomizedEncryption(algorithm)) {
+                if (isAEAD(algorithm)) {
+                    c.updateAAD(new byte[24]);
+                }
+                byte[] cipherText2 = c.doFinal(getActualPlainText(algorithm));
+                assertEquals(cipherID, Arrays.toString(cipherText), Arrays.toString(cipherText2));
             }
-            byte[] cipherText2 = c.doFinal(getActualPlainText(algorithm));
-            assertEquals(cipherID,
-                         Arrays.toString(cipherText),
-                         Arrays.toString(cipherText2));
             c.init(Cipher.DECRYPT_MODE, getDecryptKey(algorithm), decryptSpec);
             if (isAEAD(algorithm)) {
                 c.updateAAD(new byte[24]);
@@ -1602,18 +1624,21 @@
     }
 
     private void testInputPKCS1Padding(String provider) throws Exception {
-        testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT, getEncryptKey("RSA"), getDecryptKey("RSA"));
+        // Type 1 is for signatures (PrivateKey to "encrypt")
+        testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT, getDecryptKey("RSA"), getEncryptKey("RSA"));
         try {
-            testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT, getEncryptKey("RSA"), getDecryptKey("RSA"));
+            testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT, getDecryptKey("RSA"), getEncryptKey("RSA"));
             fail();
         } catch (BadPaddingException expected) {
         }
+
+        // Type 2 is for enciphering (PublicKey to "encrypt")
+        testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT, getEncryptKey("RSA"), getDecryptKey("RSA"));
         try {
-            testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT, getDecryptKey("RSA"), getEncryptKey("RSA"));
+            testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT, getEncryptKey("RSA"), getDecryptKey("RSA"));
             fail();
         } catch (BadPaddingException expected) {
         }
-        testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT, getDecryptKey("RSA"), getEncryptKey("RSA"));
     }
 
     private void testInputPKCS1Padding(String provider, byte[] prePaddedPlainText, Key encryptKey, Key decryptKey) throws Exception {
@@ -1645,8 +1670,10 @@
     }
 
     private void testOutputPKCS1Padding(String provider) throws Exception {
-       testOutputPKCS1Padding(provider, (byte) 1, getEncryptKey("RSA"), getDecryptKey("RSA"));
-       testOutputPKCS1Padding(provider, (byte) 2, getDecryptKey("RSA"), getEncryptKey("RSA"));
+        // Type 1 is for signatures (PrivateKey to "encrypt")
+        testOutputPKCS1Padding(provider, (byte) 1, getDecryptKey("RSA"), getEncryptKey("RSA"));
+        // Type 2 is for enciphering (PublicKey to "encrypt")
+        testOutputPKCS1Padding(provider, (byte) 2, getEncryptKey("RSA"), getDecryptKey("RSA"));
     }
 
     private void testOutputPKCS1Padding(String provider, byte expectedBlockType, Key encryptKey, Key decryptKey) throws Exception {
@@ -1918,6 +1945,63 @@
         (byte) 0x39,
     });
 
+    private static final BigInteger RSA_2048_primeExponentP = new BigInteger(1, new byte[] {
+        (byte) 0x51, (byte) 0x82, (byte) 0x8F, (byte) 0x1E, (byte) 0xC6, (byte) 0xFD, (byte) 0x99, (byte) 0x60,
+        (byte) 0x29, (byte) 0x90, (byte) 0x1B, (byte) 0xAF, (byte) 0x1D, (byte) 0x7E, (byte) 0x33, (byte) 0x7B,
+        (byte) 0xA5, (byte) 0xF0, (byte) 0xAF, (byte) 0x27, (byte) 0xE9, (byte) 0x84, (byte) 0xEA, (byte) 0xD8,
+        (byte) 0x95, (byte) 0xAC, (byte) 0xE6, (byte) 0x2B, (byte) 0xD7, (byte) 0xDF, (byte) 0x4E, (byte) 0xE4,
+        (byte) 0x5A, (byte) 0x22, (byte) 0x40, (byte) 0x89, (byte) 0xF2, (byte) 0xCC, (byte) 0x15, (byte) 0x1A,
+        (byte) 0xF3, (byte) 0xCD, (byte) 0x17, (byte) 0x3F, (byte) 0xCE, (byte) 0x04, (byte) 0x74, (byte) 0xBC,
+        (byte) 0xB0, (byte) 0x4F, (byte) 0x38, (byte) 0x6A, (byte) 0x2C, (byte) 0xDC, (byte) 0xC0, (byte) 0xE0,
+        (byte) 0x03, (byte) 0x6B, (byte) 0xA2, (byte) 0x41, (byte) 0x9F, (byte) 0x54, (byte) 0x57, (byte) 0x92,
+        (byte) 0x62, (byte) 0xD4, (byte) 0x71, (byte) 0x00, (byte) 0xBE, (byte) 0x93, (byte) 0x19, (byte) 0x84,
+        (byte) 0xA3, (byte) 0xEF, (byte) 0xA0, (byte) 0x5B, (byte) 0xEC, (byte) 0xF1, (byte) 0x41, (byte) 0x57,
+        (byte) 0x4D, (byte) 0xC0, (byte) 0x79, (byte) 0xB3, (byte) 0xA9, (byte) 0x5C, (byte) 0x4A, (byte) 0x83,
+        (byte) 0xE6, (byte) 0xC4, (byte) 0x3F, (byte) 0x32, (byte) 0x14, (byte) 0xD6, (byte) 0xDF, (byte) 0x32,
+        (byte) 0xD5, (byte) 0x12, (byte) 0xDE, (byte) 0x19, (byte) 0x80, (byte) 0x85, (byte) 0xE5, (byte) 0x31,
+        (byte) 0xE6, (byte) 0x16, (byte) 0xB8, (byte) 0x3F, (byte) 0xD7, (byte) 0xDD, (byte) 0x9D, (byte) 0x1F,
+        (byte) 0x4E, (byte) 0x26, (byte) 0x07, (byte) 0xC3, (byte) 0x33, (byte) 0x3D, (byte) 0x07, (byte) 0xC5,
+        (byte) 0x5D, (byte) 0x10, (byte) 0x7D, (byte) 0x1D, (byte) 0x38, (byte) 0x93, (byte) 0x58, (byte) 0x71,
+    });
+
+    private static final BigInteger RSA_2048_primeExponentQ = new BigInteger(1, new byte[] {
+        (byte) 0xDB, (byte) 0x4F, (byte) 0xB5, (byte) 0x0F, (byte) 0x50, (byte) 0xDE, (byte) 0x8E, (byte) 0xDB,
+        (byte) 0x53, (byte) 0xFF, (byte) 0x34, (byte) 0xC8, (byte) 0x09, (byte) 0x31, (byte) 0x88, (byte) 0xA0,
+        (byte) 0x51, (byte) 0x28, (byte) 0x67, (byte) 0xDA, (byte) 0x2C, (byte) 0xCA, (byte) 0x04, (byte) 0x89,
+        (byte) 0x77, (byte) 0x59, (byte) 0xE5, (byte) 0x87, (byte) 0xC2, (byte) 0x44, (byte) 0x01, (byte) 0x0D,
+        (byte) 0xAF, (byte) 0x86, (byte) 0x64, (byte) 0xD5, (byte) 0x9E, (byte) 0x80, (byte) 0x83, (byte) 0xD1,
+        (byte) 0x6C, (byte) 0x16, (byte) 0x47, (byte) 0x89, (byte) 0x30, (byte) 0x1F, (byte) 0x67, (byte) 0xA9,
+        (byte) 0xF0, (byte) 0x78, (byte) 0x06, (byte) 0x0D, (byte) 0x83, (byte) 0x4A, (byte) 0x2A, (byte) 0xDB,
+        (byte) 0xD3, (byte) 0x67, (byte) 0x57, (byte) 0x5B, (byte) 0x68, (byte) 0xA8, (byte) 0xA8, (byte) 0x42,
+        (byte) 0xC2, (byte) 0xB0, (byte) 0x2A, (byte) 0x89, (byte) 0xB3, (byte) 0xF3, (byte) 0x1F, (byte) 0xCC,
+        (byte) 0xEC, (byte) 0x8A, (byte) 0x22, (byte) 0xFE, (byte) 0x39, (byte) 0x57, (byte) 0x95, (byte) 0xC5,
+        (byte) 0xC6, (byte) 0xC7, (byte) 0x42, (byte) 0x2B, (byte) 0x4E, (byte) 0x5D, (byte) 0x74, (byte) 0xA1,
+        (byte) 0xE9, (byte) 0xA8, (byte) 0xF3, (byte) 0x0E, (byte) 0x77, (byte) 0x59, (byte) 0xB9, (byte) 0xFC,
+        (byte) 0x2D, (byte) 0x63, (byte) 0x9C, (byte) 0x1F, (byte) 0x15, (byte) 0x67, (byte) 0x3E, (byte) 0x84,
+        (byte) 0xE9, (byte) 0x3A, (byte) 0x5E, (byte) 0xF1, (byte) 0x50, (byte) 0x6F, (byte) 0x43, (byte) 0x15,
+        (byte) 0x38, (byte) 0x3C, (byte) 0x38, (byte) 0xD4, (byte) 0x5C, (byte) 0xBD, (byte) 0x1B, (byte) 0x14,
+        (byte) 0x04, (byte) 0x8F, (byte) 0x47, (byte) 0x21, (byte) 0xDC, (byte) 0x82, (byte) 0x32, (byte) 0x61,
+    });
+
+    private static final BigInteger RSA_2048_crtCoefficient = new BigInteger(1, new byte[] {
+        (byte) 0xD8, (byte) 0x11, (byte) 0x45, (byte) 0x93, (byte) 0xAF, (byte) 0x41, (byte) 0x5F, (byte) 0xB6,
+        (byte) 0x12, (byte) 0xDB, (byte) 0xF1, (byte) 0x92, (byte) 0x37, (byte) 0x10, (byte) 0xD5, (byte) 0x4D,
+        (byte) 0x07, (byte) 0x48, (byte) 0x62, (byte) 0x05, (byte) 0xA7, (byte) 0x6A, (byte) 0x3B, (byte) 0x43,
+        (byte) 0x19, (byte) 0x49, (byte) 0x68, (byte) 0xC0, (byte) 0xDF, (byte) 0xF1, (byte) 0xF1, (byte) 0x1E,
+        (byte) 0xF0, (byte) 0xF6, (byte) 0x1A, (byte) 0x4A, (byte) 0x33, (byte) 0x7D, (byte) 0x5F, (byte) 0xD3,
+        (byte) 0x74, (byte) 0x1B, (byte) 0xBC, (byte) 0x96, (byte) 0x40, (byte) 0xE4, (byte) 0x47, (byte) 0xB8,
+        (byte) 0xB6, (byte) 0xB6, (byte) 0xC4, (byte) 0x7C, (byte) 0x3A, (byte) 0xC1, (byte) 0x20, (byte) 0x43,
+        (byte) 0x57, (byte) 0xD3, (byte) 0xB0, (byte) 0xC5, (byte) 0x5B, (byte) 0xA9, (byte) 0x28, (byte) 0x6B,
+        (byte) 0xDA, (byte) 0x73, (byte) 0xF6, (byte) 0x29, (byte) 0x29, (byte) 0x6F, (byte) 0x5F, (byte) 0xA9,
+        (byte) 0x14, (byte) 0x6D, (byte) 0x89, (byte) 0x76, (byte) 0x35, (byte) 0x7D, (byte) 0x3C, (byte) 0x75,
+        (byte) 0x1E, (byte) 0x75, (byte) 0x14, (byte) 0x86, (byte) 0x96, (byte) 0xA4, (byte) 0x0B, (byte) 0x74,
+        (byte) 0x68, (byte) 0x5C, (byte) 0x82, (byte) 0xCE, (byte) 0x30, (byte) 0x90, (byte) 0x2D, (byte) 0x63,
+        (byte) 0x9D, (byte) 0x72, (byte) 0x4F, (byte) 0xF2, (byte) 0x4D, (byte) 0x5E, (byte) 0x2E, (byte) 0x94,
+        (byte) 0x07, (byte) 0xEE, (byte) 0x34, (byte) 0xED, (byte) 0xED, (byte) 0x2E, (byte) 0x3B, (byte) 0x4D,
+        (byte) 0xF6, (byte) 0x5A, (byte) 0xA9, (byte) 0xBC, (byte) 0xFE, (byte) 0xB6, (byte) 0xDF, (byte) 0x28,
+        (byte) 0xD0, (byte) 0x7B, (byte) 0xA6, (byte) 0x90, (byte) 0x3F, (byte) 0x16, (byte) 0x57, (byte) 0x68,
+    });
+
     /**
      * Test data is PKCS#1 padded "Android.\n" which can be generated by:
      * echo "Android." | openssl rsautl -inkey rsa.key -sign | openssl rsautl -inkey rsa.key -raw -verify | recode ../x1
@@ -2127,11 +2211,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2158,11 +2238,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Private_UpdateThenEmptyDoFinal_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2193,11 +2269,7 @@
 
     private void testRSA_ECB_NoPadding_Private_SingleByteUpdateThenEmptyDoFinal_Success(String provider)
             throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2231,10 +2303,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2268,10 +2337,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Public_OnlyDoFinal_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
-
-        final PublicKey privKey = kf.generatePublic(keySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2280,11 +2346,11 @@
          * distinction made here. It's all keyed off of what kind of key you're
          * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
          */
-        c.init(Cipher.ENCRYPT_MODE, privKey);
+        c.init(Cipher.ENCRYPT_MODE, pubKey);
         byte[] encrypted = c.doFinal(RSA_Vector1_Encrypt_Private);
         assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, RSA_2048_Vector1, encrypted);
 
-        c.init(Cipher.DECRYPT_MODE, privKey);
+        c.init(Cipher.DECRYPT_MODE, pubKey);
         encrypted = c.doFinal(RSA_Vector1_Encrypt_Private);
         assertEncryptedEqualsNoPadding(provider, Cipher.DECRYPT_MODE, RSA_2048_Vector1, encrypted);
     }
@@ -2296,10 +2362,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Public_OnlyDoFinalWithOffset_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
-
-        final PublicKey pubKey = kf.generatePublic(keySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2334,10 +2397,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Public_UpdateThenEmptyDoFinal_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
-
-        final PublicKey privKey = kf.generatePublic(keySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2346,12 +2406,12 @@
          * distinction made here. It's all keyed off of what kind of key you're
          * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
          */
-        c.init(Cipher.ENCRYPT_MODE, privKey);
+        c.init(Cipher.ENCRYPT_MODE, pubKey);
         c.update(RSA_Vector1_Encrypt_Private);
         byte[] encrypted = c.doFinal();
         assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, RSA_2048_Vector1, encrypted);
 
-        c.init(Cipher.DECRYPT_MODE, privKey);
+        c.init(Cipher.DECRYPT_MODE, pubKey);
         c.update(RSA_Vector1_Encrypt_Private);
         encrypted = c.doFinal();
         assertEncryptedEqualsNoPadding(provider, Cipher.DECRYPT_MODE, RSA_2048_Vector1, encrypted);
@@ -2366,10 +2426,7 @@
 
     private void testRSA_ECB_NoPadding_Public_SingleByteUpdateThenEmptyDoFinal_Success(String provider)
             throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
-
-        final PublicKey privKey = kf.generatePublic(keySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2378,7 +2435,7 @@
          * distinction made here. It's all keyed off of what kind of key you're
          * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
          */
-        c.init(Cipher.ENCRYPT_MODE, privKey);
+        c.init(Cipher.ENCRYPT_MODE, pubKey);
         int i;
         for (i = 0; i < RSA_Vector1_Encrypt_Private.length / 2; i++) {
             c.update(RSA_Vector1_Encrypt_Private, i, 1);
@@ -2386,7 +2443,7 @@
         byte[] encrypted = c.doFinal(RSA_Vector1_Encrypt_Private, i, RSA_2048_Vector1.length - i);
         assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, RSA_2048_Vector1, encrypted);
 
-        c.init(Cipher.DECRYPT_MODE, privKey);
+        c.init(Cipher.DECRYPT_MODE, pubKey);
         for (i = 0; i < RSA_Vector1_Encrypt_Private.length / 2; i++) {
             c.update(RSA_Vector1_Encrypt_Private, i, 1);
         }
@@ -2401,10 +2458,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Public_TooSmall_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
-
-        final PublicKey privKey = kf.generatePublic(keySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2413,12 +2467,12 @@
          * distinction made here. It's all keyed off of what kind of key you're
          * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
          */
-        c.init(Cipher.ENCRYPT_MODE, privKey);
+        c.init(Cipher.ENCRYPT_MODE, pubKey);
         byte[] encrypted = c.doFinal(TooShort_Vector);
         assertTrue("Encrypted should match expected",
                 Arrays.equals(RSA_Vector1_ZeroPadded_Encrypted, encrypted));
 
-        c.init(Cipher.DECRYPT_MODE, privKey);
+        c.init(Cipher.DECRYPT_MODE, pubKey);
         encrypted = c.doFinal(TooShort_Vector);
         assertTrue("Encrypted should match expected",
                 Arrays.equals(RSA_Vector1_ZeroPadded_Encrypted, encrypted));
@@ -2431,11 +2485,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Private_TooSmall_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2481,11 +2531,7 @@
 
     private void testRSA_ECB_NoPadding_Private_CombinedUpdateAndDoFinal_TooBig_Failure(String provider)
             throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2516,11 +2562,7 @@
 
     private void testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure(String provider)
             throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2552,11 +2594,7 @@
     }
 
     private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
-                RSA_2048_privateExponent);
-
-        final PrivateKey privKey = kf.generatePrivate(keySpec);
+        final PrivateKey privKey = (PrivateKey) getDecryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
 
@@ -2601,10 +2639,7 @@
             }
         }
 
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus,
-                RSA_2048_publicExponent);
-        final PublicKey pubKey = kf.generatePublic(pubKeySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
         c.init(Cipher.ENCRYPT_MODE, pubKey);
         assertEquals(getExpectedBlockSize("RSA", Cipher.ENCRYPT_MODE, provider), c.getBlockSize());
     }
@@ -2631,10 +2666,7 @@
     }
 
     private void testRSA_ECB_NoPadding_GetOutputSize_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus,
-                RSA_2048_publicExponent);
-        final PublicKey pubKey = kf.generatePublic(pubKeySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
         c.init(Cipher.ENCRYPT_MODE, pubKey);
@@ -2652,10 +2684,7 @@
     }
 
     private void testRSA_ECB_NoPadding_GetIV_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus,
-                RSA_2048_publicExponent);
-        final PublicKey pubKey = kf.generatePublic(pubKeySpec);
+        final PublicKey pubKey = (PublicKey) getEncryptKey("RSA");
 
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
         assertNull("ECB mode has no IV and should be null", c.getIV());
@@ -2672,11 +2701,6 @@
     }
 
     private void testRSA_ECB_NoPadding_GetParameters_NoneProvided_Success(String provider) throws Exception {
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus,
-                RSA_2048_publicExponent);
-        final PublicKey pubKey = kf.generatePublic(pubKeySpec);
-
         Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
         assertNull("Parameters should be null", c.getParameters());
     }
@@ -2685,85 +2709,77 @@
      * Test vector generation:
      * openssl rand -hex 16 | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] DES_112_KEY = new byte[] {
+    private static final SecretKeySpec DES_112_KEY = new SecretKeySpec(new byte[] {
             (byte) 0x6b, (byte) 0xb3, (byte) 0x85, (byte) 0x1c, (byte) 0x3d, (byte) 0x50,
             (byte) 0xd4, (byte) 0x95, (byte) 0x39, (byte) 0x48, (byte) 0x77, (byte) 0x30,
             (byte) 0x1a, (byte) 0xd7, (byte) 0x86, (byte) 0x57,
-    };
+    }, "DESede");
 
     /*
      * Test vector generation:
      * openssl rand -hex 24 | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] DES_168_KEY = new byte[] {
+    private static final SecretKeySpec DES_168_KEY = new SecretKeySpec(new byte[] {
             (byte) 0xfe, (byte) 0xd4, (byte) 0xd7, (byte) 0xc9, (byte) 0x8a, (byte) 0x13,
             (byte) 0x6a, (byte) 0xa8, (byte) 0x5a, (byte) 0xb8, (byte) 0x19, (byte) 0xb8,
             (byte) 0xcf, (byte) 0x3c, (byte) 0x5f, (byte) 0xe0, (byte) 0xa2, (byte) 0xf7,
             (byte) 0x7b, (byte) 0x65, (byte) 0x43, (byte) 0xc0, (byte) 0xc4, (byte) 0xe1,
-    };
+    }, "DESede");
 
     /*
      * Test vector generation:
      * openssl rand -hex 5 | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] ARC4_40BIT_KEY = new byte[] {
+    private static final SecretKeySpec ARC4_40BIT_KEY = new SecretKeySpec(new byte[] {
             (byte) 0x9c, (byte) 0xc8, (byte) 0xb9, (byte) 0x94, (byte) 0x98,
-    };
+    }, "ARC4");
 
     /*
      * Test vector generation:
      * openssl rand -hex 24 | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] ARC4_128BIT_KEY = new byte[] {
+    private static final SecretKeySpec ARC4_128BIT_KEY = new SecretKeySpec(new byte[] {
             (byte) 0xbc, (byte) 0x0a, (byte) 0x3c, (byte) 0xca, (byte) 0xb5, (byte) 0x42,
             (byte) 0xfa, (byte) 0x5d, (byte) 0x86, (byte) 0x5b, (byte) 0x44, (byte) 0x87,
             (byte) 0x83, (byte) 0xd8, (byte) 0xcb, (byte) 0xd4,
-    };
+    }, "ARC4");
 
     /*
      * Test vector generation:
      * openssl rand -hex 16
      * echo '3d4f8970b1f27537f40a39298a41555f' | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] AES_128_KEY = new byte[] {
+    private static final SecretKeySpec AES_128_KEY = new SecretKeySpec(new byte[] {
             (byte) 0x3d, (byte) 0x4f, (byte) 0x89, (byte) 0x70, (byte) 0xb1, (byte) 0xf2,
             (byte) 0x75, (byte) 0x37, (byte) 0xf4, (byte) 0x0a, (byte) 0x39, (byte) 0x29,
             (byte) 0x8a, (byte) 0x41, (byte) 0x55, (byte) 0x5f,
-    };
+    }, "AES");
 
     /*
      * Test key generation:
      * openssl rand -hex 24
      * echo '5a7a3d7e40b64ed996f7afa15f97fd595e27db6af428e342' | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] AES_192_KEY = new byte[] {
+    private static final SecretKeySpec AES_192_KEY = new SecretKeySpec(new byte[] {
             (byte) 0x5a, (byte) 0x7a, (byte) 0x3d, (byte) 0x7e, (byte) 0x40, (byte) 0xb6,
             (byte) 0x4e, (byte) 0xd9, (byte) 0x96, (byte) 0xf7, (byte) 0xaf, (byte) 0xa1,
             (byte) 0x5f, (byte) 0x97, (byte) 0xfd, (byte) 0x59, (byte) 0x5e, (byte) 0x27,
             (byte) 0xdb, (byte) 0x6a, (byte) 0xf4, (byte) 0x28, (byte) 0xe3, (byte) 0x42,
-    };
+    }, "AES");
 
     /*
      * Test key generation:
      * openssl rand -hex 32
      * echo 'ec53c6d51d2c4973585fb0b8e51cd2e39915ff07a1837872715d6121bf861935' | sed 's/\(..\)/(byte) 0x\1, /g'
      */
-    private static final byte[] AES_256_KEY = new byte[] {
+    private static final SecretKeySpec AES_256_KEY = new SecretKeySpec(new byte[] {
             (byte) 0xec, (byte) 0x53, (byte) 0xc6, (byte) 0xd5, (byte) 0x1d, (byte) 0x2c,
             (byte) 0x49, (byte) 0x73, (byte) 0x58, (byte) 0x5f, (byte) 0xb0, (byte) 0xb8,
             (byte) 0xe5, (byte) 0x1c, (byte) 0xd2, (byte) 0xe3, (byte) 0x99, (byte) 0x15,
             (byte) 0xff, (byte) 0x07, (byte) 0xa1, (byte) 0x83, (byte) 0x78, (byte) 0x72,
             (byte) 0x71, (byte) 0x5d, (byte) 0x61, (byte) 0x21, (byte) 0xbf, (byte) 0x86,
             (byte) 0x19, (byte) 0x35,
-    };
-
-    private static final String[] AES_MODES = new String[] {
-            "AES/ECB",
-            "AES/CBC",
-            "AES/CFB",
-            "AES/CTR",
-            "AES/OFB",
-    };
+    }, "AES");
 
     /*
      * Test vector generation:
@@ -2887,11 +2903,11 @@
     /*
      * Taken from BoringSSL test vectors.
      */
-    private static final byte[] AES_128_GCM_TestVector_1_Key = new byte[] {
+    private static final SecretKeySpec AES_128_GCM_TestVector_1_Key = new SecretKeySpec(new byte[] {
             (byte) 0xca, (byte) 0xbd, (byte) 0xcf, (byte) 0x54, (byte) 0x1a, (byte) 0xeb,
             (byte) 0xf9, (byte) 0x17, (byte) 0xba, (byte) 0xc0, (byte) 0x19, (byte) 0xf1,
             (byte) 0x39, (byte) 0x25, (byte) 0xd2, (byte) 0x67,
-    };
+    }, "AES");
 
     /*
      * Taken from BoringSSL test vectors.
@@ -3058,11 +3074,11 @@
     private static class CipherTestParam {
         public final String transformation;
 
-        public final byte[] key;
+        public final AlgorithmParameterSpec spec;
 
-        public final String keyAlgorithm;
+        public final Key encryptKey;
 
-        public final byte[] iv;
+        public final Key decryptKey;
 
         public final byte[] aad;
 
@@ -3074,13 +3090,13 @@
 
         public final boolean isStreamCipher;
 
-        public CipherTestParam(String transformation, String keyAlgorithm, byte[] key, byte[] iv,
-                byte[] aad, byte[] plaintext, byte[] plaintextPadded, byte[] ciphertext,
-                boolean isStreamCipher) {
+        public CipherTestParam(String transformation, AlgorithmParameterSpec spec, Key encryptKey,
+                Key decryptKey, byte[] aad, byte[] plaintext, byte[] plaintextPadded,
+                byte[] ciphertext, boolean isStreamCipher) {
             this.transformation = transformation.toUpperCase(Locale.ROOT);
-            this.keyAlgorithm = keyAlgorithm;
-            this.key = key;
-            this.iv = iv;
+            this.spec = spec;
+            this.encryptKey = encryptKey;
+            this.decryptKey = decryptKey;
             this.aad = aad;
             this.plaintext = plaintext;
             this.plaintextPadded = plaintextPadded;
@@ -3088,9 +3104,16 @@
             this.isStreamCipher = isStreamCipher;
         }
 
-        public CipherTestParam(String transformation, String keyAlgorithm, byte[] key, byte[] iv,
+        public CipherTestParam(String transformation, AlgorithmParameterSpec spec, Key key,
+                byte[] aad, byte[] plaintext, byte[] plaintextPadded, byte[] ciphertext,
+                boolean isStreamCipher) {
+            this(transformation, spec, key, key, aad, plaintext, plaintextPadded, ciphertext,
+                    isStreamCipher);
+        }
+
+        public CipherTestParam(String transformation, AlgorithmParameterSpec spec, Key key,
                 byte[] aad, byte[] plaintext, byte[] plaintextPadded, byte[] ciphertext) {
-            this(transformation, keyAlgorithm, key, iv, aad, plaintext, plaintextPadded, ciphertext,
+            this(transformation, spec, key, aad, plaintext, plaintextPadded, ciphertext,
                     false /* isStreamCipher */);
         }
     }
@@ -3099,9 +3122,8 @@
     static {
         DES_CIPHER_TEST_PARAMS.add(new CipherTestParam(
                 "DESede/CBC/PKCS5Padding",
-                "DESede",
+                new IvParameterSpec(DES_IV1),
                 DES_112_KEY,
-                DES_IV1,
                 null,
                 DES_Plaintext1,
                 DES_Plaintext1_PKCS5_Padded,
@@ -3109,9 +3131,8 @@
                 ));
         DES_CIPHER_TEST_PARAMS.add(new CipherTestParam(
                 "DESede/CBC/PKCS5Padding",
-                "DESede",
+                new IvParameterSpec(DES_IV1),
                 DES_168_KEY,
-                DES_IV1,
                 null,
                 DES_Plaintext1,
                 DES_Plaintext1_PKCS5_Padded,
@@ -3123,9 +3144,8 @@
     static {
         ARC4_CIPHER_TEST_PARAMS.add(new CipherTestParam(
                 "ARC4",
-                "ARC4",
+                null,
                 ARC4_40BIT_KEY,
-                null, // IV,
                 null, // aad
                 ARC4_Plaintext1,
                 null, // padded
@@ -3134,9 +3154,8 @@
         ));
         ARC4_CIPHER_TEST_PARAMS.add(new CipherTestParam(
                 "ARC4",
-                "ARC4",
+                null,
                 ARC4_128BIT_KEY,
-                null, // IV,
                 null, // aad
                 ARC4_Plaintext1,
                 null, // padded
@@ -3147,42 +3166,55 @@
 
     private static List<CipherTestParam> CIPHER_TEST_PARAMS = new ArrayList<CipherTestParam>();
     static {
-        CIPHER_TEST_PARAMS.add(new CipherTestParam("AES/ECB/PKCS5Padding", "AES", AES_128_KEY,
+        CIPHER_TEST_PARAMS.add(new CipherTestParam(
+                "AES/ECB/PKCS5Padding",
                 null,
+                AES_128_KEY,
                 null,
                 AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext,
                 AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded,
                 AES_128_ECB_PKCS5Padding_TestVector_1_Encrypted));
         // PKCS#5 is assumed to be equivalent to PKCS#7 -- same test vectors are thus used for both.
-        CIPHER_TEST_PARAMS.add(new CipherTestParam("AES/ECB/PKCS7Padding", "AES", AES_128_KEY,
+        CIPHER_TEST_PARAMS.add(new CipherTestParam(
+                "AES/ECB/PKCS7Padding",
                 null,
+                AES_128_KEY,
                 null,
                 AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext,
                 AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded,
                 AES_128_ECB_PKCS5Padding_TestVector_1_Encrypted));
-        CIPHER_TEST_PARAMS.add(new CipherTestParam("AES/GCM/NOPADDING",
-                "AES",
+        CIPHER_TEST_PARAMS.add(new CipherTestParam(
+                "AES/GCM/NOPADDING",
+                new GCMParameterSpec(
+                        (AES_128_GCM_TestVector_1_Encrypted.length -
+                                AES_128_GCM_TestVector_1_Plaintext.length) * 8,
+                        AES_128_GCM_TestVector_1_IV),
                 AES_128_GCM_TestVector_1_Key,
-                AES_128_GCM_TestVector_1_IV,
                 AES_128_GCM_TestVector_1_AAD,
                 AES_128_GCM_TestVector_1_Plaintext,
                 AES_128_GCM_TestVector_1_Plaintext,
                 AES_128_GCM_TestVector_1_Encrypted));
         if (IS_UNLIMITED) {
-            CIPHER_TEST_PARAMS.add(new CipherTestParam("AES/CTR/NoPadding", "AES", AES_192_KEY,
-                    AES_192_CTR_NoPadding_TestVector_1_IV,
+            CIPHER_TEST_PARAMS.add(new CipherTestParam(
+                    "AES/CTR/NoPadding",
+                    new IvParameterSpec(AES_192_CTR_NoPadding_TestVector_1_IV),
+                    AES_192_KEY,
                     null,
                     AES_192_CTR_NoPadding_TestVector_1_Plaintext,
                     AES_192_CTR_NoPadding_TestVector_1_Plaintext,
                     AES_192_CTR_NoPadding_TestVector_1_Ciphertext));
-            CIPHER_TEST_PARAMS.add(new CipherTestParam("AES/CBC/PKCS5Padding", "AES", AES_256_KEY,
-                    AES_256_CBC_PKCS5Padding_TestVector_1_IV,
+            CIPHER_TEST_PARAMS.add(new CipherTestParam(
+                    "AES/CBC/PKCS5Padding",
+                    new IvParameterSpec(AES_256_CBC_PKCS5Padding_TestVector_1_IV),
+                    AES_256_KEY,
                     null,
                     AES_256_CBC_PKCS5Padding_TestVector_1_Plaintext,
                     AES_256_CBC_PKCS5Padding_TestVector_1_Plaintext_Padded,
                     AES_256_CBC_PKCS5Padding_TestVector_1_Ciphertext));
-            CIPHER_TEST_PARAMS.add(new CipherTestParam("AES/CBC/PKCS7Padding", "AES", AES_256_KEY,
-                    AES_256_CBC_PKCS5Padding_TestVector_1_IV,
+            CIPHER_TEST_PARAMS.add(new CipherTestParam(
+                    "AES/CBC/PKCS7Padding",
+                    new IvParameterSpec(AES_256_CBC_PKCS5Padding_TestVector_1_IV),
+                    AES_256_KEY,
                     null,
                     AES_256_CBC_PKCS5Padding_TestVector_1_Plaintext,
                     AES_256_CBC_PKCS5Padding_TestVector_1_Plaintext_Padded,
@@ -3219,11 +3251,8 @@
             for (Provider provider : providers) {
                 try {
                     checkCipher(testVector, provider.getName());
-                } catch (Throwable e) {
-                    out.append("Error encountered checking " + testVector.transformation
-                            + ", keySize=" + (testVector.key.length * 8) + " with provider "
-                            + provider.getName() + "\n");
-                    e.printStackTrace(out);
+                } catch (Exception e) {
+                    logTestFailure(out, provider.getName(), testVector, e);
                 }
             }
         }
@@ -3240,11 +3269,7 @@
             try {
                 checkCipher(p, provider);
             } catch (Exception e) {
-                out.append("Error encountered checking " + p.transformation + ", keySize="
-                        + (p.key.length * 8)
-                        + " with provider " + provider + "\n");
-
-                e.printStackTrace(out);
+                logTestFailure(out, provider, p, e);
             }
         }
         out.flush();
@@ -3253,34 +3278,34 @@
         }
     }
 
+    private void logTestFailure(PrintStream logStream, String provider, CipherTestParam params,
+            Exception e) {
+        logStream.append("Error encountered checking " + params.transformation + ", keySize="
+                + (params.encryptKey.getEncoded().length * 8) + " with provider " + provider
+                + "\n");
+        e.printStackTrace(logStream);
+    }
+
     private void checkCipher(CipherTestParam p, String provider) throws Exception {
-        SecretKey key = new SecretKeySpec(p.key, p.keyAlgorithm);
         Cipher c = Cipher.getInstance(p.transformation, provider);
 
-        AlgorithmParameterSpec spec = null;
-        if (p.iv != null) {
-            if (isAEAD(p.transformation)) {
-                spec = new GCMParameterSpec((p.ciphertext.length - p.plaintext.length) * 8, p.iv);
-            } else {
-                spec = new IvParameterSpec(p.iv);
-            }
-        }
-
-        c.init(Cipher.ENCRYPT_MODE, key, spec);
+        c.init(Cipher.ENCRYPT_MODE, p.encryptKey, p.spec);
 
         if (p.aad != null) {
             c.updateAAD(p.aad);
         }
         final byte[] actualCiphertext = c.doFinal(p.plaintext);
-        assertEquals(p.transformation + " " + provider, Arrays.toString(p.ciphertext),
-                Arrays.toString(actualCiphertext));
+        if (!isRandomizedEncryption(p.transformation)) {
+            assertEquals(p.transformation + " " + provider, Arrays.toString(p.ciphertext),
+                    Arrays.toString(actualCiphertext));
+        }
 
         c = Cipher.getInstance(p.transformation, provider);
-        c.init(Cipher.ENCRYPT_MODE, key, spec);
+        c.init(Cipher.ENCRYPT_MODE, p.encryptKey, p.spec);
         byte[] emptyCipherText = c.doFinal();
         assertNotNull(emptyCipherText);
 
-        c.init(Cipher.DECRYPT_MODE, key, spec);
+        c.init(Cipher.DECRYPT_MODE, p.decryptKey, p.spec);
 
         if (!isAEAD(p.transformation)) {
             try {
@@ -3343,7 +3368,7 @@
         }
 
         // Cipher might be in unspecified state from failures above.
-        c.init(Cipher.DECRYPT_MODE, key, spec);
+        c.init(Cipher.DECRYPT_MODE, p.decryptKey, p.spec);
 
         // .doFinal(input)
         {
@@ -3395,7 +3420,7 @@
         if (!p.isStreamCipher && !p.transformation.endsWith("NOPADDING")) {
             Cipher cNoPad = Cipher.getInstance(
                     getCipherTransformationWithNoPadding(p.transformation), provider);
-            cNoPad.init(Cipher.DECRYPT_MODE, key, spec);
+            cNoPad.init(Cipher.DECRYPT_MODE, p.decryptKey, p.spec);
 
             if (p.aad != null) {
                 c.updateAAD(p.aad);
@@ -3414,11 +3439,11 @@
 
             // Wrap it
             c = Cipher.getInstance(p.transformation, provider);
-            c.init(Cipher.WRAP_MODE, key, spec);
+            c.init(Cipher.WRAP_MODE, p.encryptKey, p.spec);
             byte[] cipherText = c.wrap(sk);
 
             // Unwrap it
-            c.init(Cipher.UNWRAP_MODE, key, spec);
+            c.init(Cipher.UNWRAP_MODE, p.decryptKey, p.spec);
             Key decryptedKey = c.unwrap(cipherText, sk.getAlgorithm(), Cipher.SECRET_KEY);
 
             assertEquals(
@@ -3556,10 +3581,7 @@
             try {
                 checkCipher_ShortBlock_Failure(p, provider);
             } catch (Exception e) {
-                out.append("Error encountered checking " + p.transformation + ", keySize="
-                        + (p.key.length * 8)
-                        + " with provider " + provider + "\n");
-                e.printStackTrace(out);
+                logTestFailure(out, provider, p, e);
             }
         }
         out.flush();
@@ -3639,9 +3661,8 @@
     }
 
     public void testCipher_Update_WithZeroLengthInput_ReturnsNull() throws Exception {
-        SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
         Cipher c = Cipher.getInstance("AES/ECB/NoPadding");
-        c.init(Cipher.ENCRYPT_MODE, key);
+        c.init(Cipher.ENCRYPT_MODE, AES_128_KEY);
         assertNull(c.update(new byte[0]));
         assertNull(c.update(new byte[c.getBlockSize() * 2], 0, 0));
 
@@ -3713,7 +3734,6 @@
             return;
         }
 
-        SecretKey key = new SecretKeySpec(p.key, "AES");
         Cipher c = Cipher.getInstance(
                 getCipherTransformationWithNoPadding(p.transformation), provider);
         if (c.getBlockSize() == 0) {
@@ -3721,7 +3741,7 @@
         }
 
         if (!p.transformation.endsWith("NOPADDING")) {
-            c.init(Cipher.ENCRYPT_MODE, key);
+            c.init(Cipher.ENCRYPT_MODE, p.encryptKey);
             try {
                 c.doFinal(new byte[] { 0x01, 0x02, 0x03 });
                 fail("Should throw IllegalBlockSizeException on wrong-sized block; transform="
@@ -3731,6 +3751,9 @@
         }
     }
 
+    // Test that when reading GCM parameters encoded using ASN1, a value for the tag size
+    // not present indicates a value of 12.
+    // https://b/29876633
     public void test_DefaultGCMTagSizeAlgorithmParameterSpec() throws Exception {
         final String AES = "AES";
         final String AES_GCM = "AES/GCM/NoPadding";
@@ -3743,14 +3766,12 @@
             (byte) 14,    // DER encoding : total length
             (byte) 4,     // DER encoding : tag_OctetString
             (byte) 12,    // DER encoding : counter length
-            // Note that IV's size 12 bytes is recommended, but authentication tag size should be 16
-            // bytes.
             (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
             (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 });
         cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, AES), param);
         byte[] ciphertext = cipher.update(input);
         byte[] tag = cipher.doFinal();
-        assertEquals(16, tag.length);
+        assertEquals(12, tag.length);
     }
 
     public void testAES_ECB_PKCS5Padding_ShortBuffer_Failure() throws Exception {
@@ -3760,9 +3781,8 @@
     }
 
     private void testAES_ECB_PKCS5Padding_ShortBuffer_Failure(String provider) throws Exception {
-        SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
         Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding", provider);
-        c.init(Cipher.ENCRYPT_MODE, key);
+        c.init(Cipher.ENCRYPT_MODE, AES_128_KEY);
 
         final byte[] fragmentOutput = c.update(AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext);
         if (fragmentOutput != null) {
@@ -3814,10 +3834,9 @@
     }
 
     private void testAES_ECB_NoPadding_IncrementalUpdate_Success(String provider) throws Exception {
-        SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
         Cipher c = Cipher.getInstance("AES/ECB/NoPadding", provider);
         assertEquals(provider, c.getProvider().getName());
-        c.init(Cipher.ENCRYPT_MODE, key);
+        c.init(Cipher.ENCRYPT_MODE, AES_128_KEY);
 
         for (int i = 0; i < AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded.length - 1; i++) {
             final byte[] outputFragment = c.update(AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded, i, 1);
@@ -3848,12 +3867,11 @@
     }
 
     private void testAES_ECB_NoPadding_IvParameters_Failure(String provider) throws Exception {
-        SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
         Cipher c = Cipher.getInstance("AES/ECB/NoPadding", provider);
 
         AlgorithmParameterSpec spec = new IvParameterSpec(AES_IV_ZEROES);
         try {
-            c.init(Cipher.ENCRYPT_MODE, key, spec);
+            c.init(Cipher.ENCRYPT_MODE, AES_128_KEY, spec);
             fail("Should not accept an IV in ECB mode; provider=" + provider);
         } catch (InvalidAlgorithmParameterException expected) {
         }
@@ -4204,9 +4222,8 @@
 
     private static Cipher createAesCipher(int opmode) {
         try {
-            SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
             final Cipher c = Cipher.getInstance("AES/ECB/NoPadding");
-            c.init(opmode, key);
+            c.init(opmode, AES_128_KEY);
             return c;
         } catch (Exception e) {
             fail("Unexpected Exception: " + e.getMessage());
diff --git a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java
index d17e32b..5e94b21 100644
--- a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java
+++ b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java
@@ -80,6 +80,8 @@
 import libcore.tlswire.handshake.CipherSuite;
 import libcore.tlswire.handshake.ClientHello;
 import libcore.tlswire.handshake.CompressionMethod;
+import libcore.tlswire.handshake.EllipticCurve;
+import libcore.tlswire.handshake.EllipticCurvesHelloExtension;
 import libcore.tlswire.handshake.HandshakeMessage;
 import libcore.tlswire.handshake.HelloExtension;
 import libcore.tlswire.handshake.ServerNameHelloExtension;
@@ -1693,6 +1695,31 @@
         }, getSSLSocketFactoriesToTest());
     }
 
+    public void test_SSLSocket_ClientHello_supportedCurves() throws Exception {
+        ForEachRunner.runNamed(new ForEachRunner.Callback<SSLSocketFactory>() {
+            @Override
+            public void run(SSLSocketFactory sslSocketFactory) throws Exception {
+                ClientHello clientHello = captureTlsHandshakeClientHello(sslSocketFactory);
+
+                EllipticCurvesHelloExtension ecExtension = (EllipticCurvesHelloExtension)
+                        clientHello.findExtensionByType(HelloExtension.TYPE_ELLIPTIC_CURVES);
+                final String[] supportedCurves;
+                if (ecExtension == null) {
+                    supportedCurves = new String[0];
+                } else {
+                    assertTrue(ecExtension.wellFormed);
+                    supportedCurves = new String[ecExtension.supported.size()];
+                    for (int i = 0; i < ecExtension.supported.size(); i++) {
+                        EllipticCurve curve = ecExtension.supported.get(i);
+                        supportedCurves[i] = curve.toString();
+                    }
+                }
+
+                StandardNames.assertDefaultEllipticCurves(supportedCurves);
+            }
+        }, getSSLSocketFactoriesToTest());
+    }
+
     public void test_SSLSocket_ClientHello_clientProtocolVersion() throws Exception {
         ForEachRunner.runNamed(new ForEachRunner.Callback<SSLSocketFactory>() {
             @Override
diff --git a/luni/src/test/java/libcore/sun/invoke/util/VerifyAccessTest.java b/luni/src/test/java/libcore/sun/invoke/util/VerifyAccessTest.java
new file mode 100644
index 0000000..0c02754
--- /dev/null
+++ b/luni/src/test/java/libcore/sun/invoke/util/VerifyAccessTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package libcore.sun.invoke.util;
+
+
+import junit.framework.TestCase;
+import sun.invoke.util.VerifyAccess;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.util.List;
+import java.util.Locale;
+import java.util.Vector;
+
+
+public class VerifyAccessTest extends TestCase {
+    public void testIsClassAccessible() {
+        // Always returns false when allowedModes == 0. Note that the "modes" allowed here
+        // are different from the ones used in MethodHandles.
+        assertFalse(VerifyAccess.isClassAccessible(Inner1.class, Inner2.class, 0));
+
+        // Classes in the same package are accessible when Lookup.PACKAGE is specified.
+        assertTrue(VerifyAccess.isClassAccessible(Inner1.class, Inner2.class,
+                MethodHandles.Lookup.PACKAGE));
+        assertTrue(VerifyAccess.isClassAccessible(Inner1.class, Sibling.class,
+                MethodHandles.Lookup.PACKAGE));
+
+        // Public classes are always accessible.
+        assertTrue(VerifyAccess.isClassAccessible(String.class, Inner1.class,
+                MethodHandles.Lookup.PACKAGE));
+    }
+
+    public static class Inner1 {
+    }
+
+    public static class Inner2 {
+    }
+
+    public void testIsSamePackageMember() {
+        assertTrue(VerifyAccess.isSamePackageMember(Inner1.class, Inner2.class));
+        assertTrue(VerifyAccess.isSamePackageMember(Inner1.class, VerifyAccessTest.class));
+
+        assertFalse(VerifyAccess.isSamePackageMember(Sibling.class, Inner1.class));
+    }
+
+    public void testIsSamePackage() {
+        // Both classes are in package java.util.
+        assertTrue(VerifyAccess.isSamePackage(Vector.class, List.class));
+        // Make sure this works for inner classes.
+        assertTrue(VerifyAccess.isSamePackage(Vector.class, Locale.Builder.class));
+        // Differing packages: java.lang vs java.util.
+        assertFalse(VerifyAccess.isSamePackage(Vector.class, String.class));
+
+        try {
+            VerifyAccess.isSamePackage(String[].class, List.class);
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+}
+
+class Sibling {
+}
diff --git a/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherOutputStream1Test.java b/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherOutputStream1Test.java
index 359ac66..5c88e71 100644
--- a/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherOutputStream1Test.java
+++ b/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherOutputStream1Test.java
@@ -23,20 +23,27 @@
 package org.apache.harmony.crypto.tests.javax.crypto;
 
 import java.io.BufferedOutputStream;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
+import java.security.AlgorithmParameters;
+import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
+import java.security.Security;
+import java.security.spec.AlgorithmParameterSpec;
 import java.util.Arrays;
 
+import javax.crypto.BadPaddingException;
+import javax.crypto.CipherSpi;
+import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.KeyGenerator;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.NullCipher;
 import javax.crypto.CipherOutputStream;
 import javax.crypto.Cipher;
+import javax.crypto.ShortBufferException;
 
 import junit.framework.TestCase;
 
@@ -205,5 +212,121 @@
 
         assertNotNull(cos);
     }
+
+    private static class CipherSpiThatThrowsOnSecondDoFinal extends CipherSpi {
+
+        private boolean wasDoFinalCalled = false;
+
+        @Override
+        protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
+
+        }
+
+        @Override
+        protected void engineSetPadding(String padding) throws NoSuchPaddingException {
+
+        }
+
+        @Override
+        protected int engineGetBlockSize() {
+            return 0;
+        }
+
+        @Override
+        protected int engineGetOutputSize(int inputLen) {
+            return 0;
+        }
+
+        @Override
+        protected byte[] engineGetIV() {
+            return new byte[0];
+        }
+
+        @Override
+        protected AlgorithmParameters engineGetParameters() {
+            return null;
+        }
+
+        @Override
+        protected void engineInit(int opmode, Key key, SecureRandom random)
+                throws InvalidKeyException {
+
+        }
+
+        @Override
+        protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
+                SecureRandom random)
+                throws InvalidKeyException, InvalidAlgorithmParameterException {
+
+        }
+
+        @Override
+        protected void engineInit(int opmode, Key key, AlgorithmParameters params,
+                SecureRandom random)
+                throws InvalidKeyException, InvalidAlgorithmParameterException {
+
+        }
+
+        @Override
+        protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
+            return new byte[0];
+        }
+
+        @Override
+        protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output,
+                int outputOffset) throws ShortBufferException {
+            return 0;
+        }
+
+        @Override
+        protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
+                throws IllegalBlockSizeException, BadPaddingException {
+            // Just call the other overriding for engineDoFinal.
+            try {
+                engineDoFinal(input, inputOffset, inputLen, new byte[10], 0);
+            } catch (ShortBufferException e) {
+                throw new RuntimeException(e);
+            }
+            return new byte[0];
+        }
+
+        @Override
+        protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output,
+                int outputOffset)
+                throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
+            if (wasDoFinalCalled) {
+                throw new UnsupportedOperationException(
+                        "doFinal not supposed to be called two times");
+            }
+            wasDoFinalCalled = true;
+            return 0;
+        }
+    };
+
+
+    public void test_close_doubleCloseDoesntCallDoFinal() throws Exception {
+        CipherSpi cipherSpiThatThrowsOnSecondDoFinal = new CipherSpiThatThrowsOnSecondDoFinal();
+        Cipher cipherThatThrowsOnSecondDoFinal = new Cipher(
+                cipherSpiThatThrowsOnSecondDoFinal,
+                Security.getProviders()[0],
+                "SomeTransformation") {
+        };
+
+        TestOutputStream testOutputStream = new TestOutputStream();
+        CipherOutputStream cipherOutputStream = new CipherOutputStream(
+                testOutputStream, cipherThatThrowsOnSecondDoFinal);
+
+        cipherThatThrowsOnSecondDoFinal.init(Cipher.ENCRYPT_MODE, (Key) null);
+
+        cipherOutputStream.close();
+        // Should just check that it's already closed and return, without calling doFinal, thus
+        // throwing any exception
+        cipherOutputStream.close();
+
+        // Check that the spi didn't change, as it might be changed dynamically by the Cipher
+        // methods.
+        assertEquals(cipherSpiThatThrowsOnSecondDoFinal,
+                cipherThatThrowsOnSecondDoFinal.getCurrentSpi());
+    }
 }
 
diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/DigestOutputStreamTest.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/DigestOutputStreamTest.java
index d90c8ec..2fc09c6 100644
--- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/DigestOutputStreamTest.java
+++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/DigestOutputStreamTest.java
@@ -594,6 +594,67 @@
                        Arrays.equals(digestResult, expected));
     }
 
+    private class MessageDigestWithUnsupportedUpdate extends MessageDigest {
+        private MessageDigestWithUnsupportedUpdate() {
+            super("SomeAlgorithm");
+        }
+
+        @Override
+        protected void engineUpdate(byte input) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        protected void engineUpdate(byte[] input, int offset, int len) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        protected byte[] engineDigest() {
+            return new byte[0];
+        }
+
+        @Override
+        protected void engineReset() {
+
+        }
+    }
+
+    public void test_write_writeToUnderlyingStreamBeforeUpdatingDigest() {
+        MessageDigest messageDigestWithUnsupportedUpdate = new MessageDigestWithUnsupportedUpdate();
+        OutputStream outputStreamThatThrowsIOException = new OutputStream() {
+            @Override
+            public void write(int b) throws IOException {
+                throw new IOException();
+            }
+        };
+
+        DigestOutputStream digestOutputStream = new DigestOutputStream(
+                outputStreamThatThrowsIOException, messageDigestWithUnsupportedUpdate);
+
+        // Writing throws an IOException (and not an UnsupportedOperationException) meaning than
+        // it tried to write to the underlying stream before updating the digest.
+        digestOutputStream.on(true);
+        try {
+            digestOutputStream.write(3);
+            fail();
+        } catch (IOException expected) {
+        }
+
+        digestOutputStream.on(true);
+        try {
+            digestOutputStream.write(new byte[10], 0, 10);
+            fail();
+        } catch (IOException expected) {
+        }
+
+        digestOutputStream.on(true);
+        try {
+            digestOutputStream.write(new byte[10]);
+            fail();
+        } catch (IOException expected) {
+        }
+    }
 
     private class MyOutputStream extends OutputStream {
         @Override
diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/SignatureTest.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/SignatureTest.java
index d00e18a..1f9aa4b 100644
--- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/SignatureTest.java
+++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/SignatureTest.java
@@ -558,7 +558,9 @@
     }
 
     @SuppressWarnings("unused")
-    protected static class MySignature extends Signature implements Cloneable {
+    // Needs to be public as this is checked by the provider class when providing an instance of
+    // a class
+    public static class MySignature extends Signature implements Cloneable {
 
         public MySignature() {
             super("TestSignature");
diff --git a/non_openjdk_java_files.mk b/non_openjdk_java_files.mk
index 839067e..a559619 100644
--- a/non_openjdk_java_files.mk
+++ b/non_openjdk_java_files.mk
@@ -36,6 +36,7 @@
   dalvik/src/main/java/dalvik/annotation/TestTarget.java \
   dalvik/src/main/java/dalvik/annotation/TestTargetClass.java \
   dalvik/src/main/java/dalvik/annotation/Throws.java \
+  dalvik/src/main/java/dalvik/annotation/optimization/CriticalNative.java \
   dalvik/src/main/java/dalvik/annotation/optimization/FastNative.java \
   dalvik/src/main/java/dalvik/bytecode/OpcodeInfo.java \
   dalvik/src/main/java/dalvik/bytecode/Opcodes.java \
@@ -65,7 +66,6 @@
   luni/src/main/java/java/lang/FindBugsSuppressWarnings.java \
   libart/src/main/java/java/lang/VMClassLoader.java \
   luni/src/main/java/java/lang/ref/FinalizerReference.java \
-  libart/src/main/java/java/lang/reflect/AbstractMethod.java \
   luni/src/main/java/java/math/BigDecimal.java \
   luni/src/main/java/java/math/BigInt.java \
   luni/src/main/java/java/math/BigInteger.java \
diff --git a/ojluni/src/main/java/java/io/DataInput.java b/ojluni/src/main/java/java/io/DataInput.java
index e4b7e83..3e0f0dd 100644
--- a/ojluni/src/main/java/java/io/DataInput.java
+++ b/ojluni/src/main/java/java/io/DataInput.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,12 +26,12 @@
 package java.io;
 
 /**
- * The <code>DataInput</code> interface provides
+ * The {@code DataInput} interface provides
  * for reading bytes from a binary stream and
  * reconstructing from them data in any of
  * the Java primitive types. There is also
  * a
- * facility for reconstructing a <code>String</code>
+ * facility for reconstructing a {@code String}
  * from data in
  * <a href="#modified-utf-8">modified UTF-8</a>
  * format.
@@ -39,146 +39,101 @@
  * It is generally true of all the reading
  * routines in this interface that if end of
  * file is reached before the desired number
- * of bytes has been read, an <code>EOFException</code>
- * (which is a kind of <code>IOException</code>)
+ * of bytes has been read, an {@code EOFException}
+ * (which is a kind of {@code IOException})
  * is thrown. If any byte cannot be read for
- * any reason other than end of file, an <code>IOException</code>
- * other than <code>EOFException</code> is
- * thrown. In particular, an <code>IOException</code>
+ * any reason other than end of file, an {@code IOException}
+ * other than {@code EOFException} is
+ * thrown. In particular, an {@code IOException}
  * may be thrown if the input stream has been
  * closed.
  *
- * <h4><a name="modified-utf-8">Modified UTF-8</a></h4>
+ * <h3><a name="modified-utf-8">Modified UTF-8</a></h3>
  * <p>
  * Implementations of the DataInput and DataOutput interfaces represent
  * Unicode strings in a format that is a slight modification of UTF-8.
  * (For information regarding the standard UTF-8 format, see section
  * <i>3.9 Unicode Encoding Forms</i> of <i>The Unicode Standard, Version
  * 4.0</i>).
- * Note that in the following tables, the most significant bit appears in the
+ * Note that in the following table, the most significant bit appears in the
  * far left-hand column.
- * <p>
- * All characters in the range <code>'&#92;u0001'</code> to
- * <code>'&#92;u007F'</code> are represented by a single byte:
  *
  * <blockquote>
- *   <table border="1" cellspacing="0" cellpadding="8" width="50%"
+ *   <table border="1" cellspacing="0" cellpadding="8"
  *          summary="Bit values and bytes">
  *     <tr>
- *       <td></td>
- *       <th id="bit">Bit Values</th>
+ *       <th colspan="9"><span style="font-weight:normal">
+ *         All characters in the range {@code '\u005Cu0001'} to
+ *         {@code '\u005Cu007F'} are represented by a single byte:</span></th>
  *     </tr>
  *     <tr>
- *       <th id="byte1">Byte 1</th>
- *       <td>
- *         <table border="1" cellspacing="0" width="100%">
- *           <tr>
- *             <td width="12%"><center>0</center>
- *             <td colspan="7"><center>bits 6-0</center>
- *           </tr>
- *         </table>
- *       </td>
- *     </tr>
- *   </table>
- * </blockquote>
- *
- * <p>
- * The null character <code>'&#92;u0000'</code> and characters in the
- * range <code>'&#92;u0080'</code> to <code>'&#92;u07FF'</code> are
- * represented by a pair of bytes:
- *
- * <blockquote>
- *   <table border="1" cellspacing="0" cellpadding="8" width="50%"
- *          summary="Bit values and bytes">
- *     <tr>
  *       <td></td>
- *       <th id="bit">Bit Values</th>
+ *       <th colspan="8" id="bit_a">Bit Values</th>
  *     </tr>
  *     <tr>
- *       <th id="byte1">Byte 1</th>
- *       <td>
- *         <table border="1" cellspacing="0" width="100%">
- *           <tr>
- *             <td width="12%"><center>1</center>
- *             <td width="13%"><center>1</center>
- *             <td width="12%"><center>0</center>
- *             <td colspan="5"><center>bits 10-6</center>
- *           </tr>
- *         </table>
- *       </td>
+ *       <th id="byte1_a">Byte 1</th>
+ *       <td><center>0</center>
+ *       <td colspan="7"><center>bits 6-0</center>
  *     </tr>
  *     <tr>
- *       <th id="byte2">Byte 2</th>
- *       <td>
- *         <table border="1" cellspacing="0" width="100%">
- *           <tr>
- *             <td width="12%"><center>1</center>
- *             <td width="13%"><center>0</center>
- *             <td colspan="6"><center>bits 5-0</center>
- *           </tr>
- *         </table>
- *       </td>
+ *       <th colspan="9"><span style="font-weight:normal">
+ *         The null character {@code '\u005Cu0000'} and characters
+ *         in the range {@code '\u005Cu0080'} to {@code '\u005Cu07FF'} are
+ *         represented by a pair of bytes:</span></th>
  *     </tr>
- *   </table>
- *  </blockquote>
- *
- * <br>
- * <code>char</code> values in the range <code>'&#92;u0800'</code> to
- * <code>'&#92;uFFFF'</code> are represented by three bytes:
- *
- * <blockquote>
- *   <table border="1" cellspacing="0" cellpadding="8" width="50%"
- *          summary="Bit values and bytes">
  *     <tr>
  *       <td></td>
- *       <th id="bit">Bit Values</th>
+ *       <th colspan="8" id="bit_b">Bit Values</th>
  *     </tr>
  *     <tr>
- *       <th id="byte1">Byte 1</th>
- *       <td>
- *         <table border="1" cellspacing="0" width="100%">
- *           <tr>
- *             <td width="12%"><center>1</center>
- *             <td width="13%"><center>1</center>
- *             <td width="12%"><center>1</center>
- *             <td width="13%"><center>0</center>
- *             <td colspan="4"><center>bits 15-12</center>
- *           </tr>
- *         </table>
- *       </td>
+ *       <th id="byte1_b">Byte 1</th>
+ *       <td><center>1</center>
+ *       <td><center>1</center>
+ *       <td><center>0</center>
+ *       <td colspan="5"><center>bits 10-6</center>
  *     </tr>
  *     <tr>
- *       <th id="byte2">Byte 2</th>
- *       <td>
- *         <table border="1" cellspacing="0" width="100%">
- *           <tr>
- *             <td width="12%"><center>1</center>
- *             <td width="13%"><center>0</center>
- *             <td colspan="6"><center>bits 11-6</center>
- *           </tr>
- *         </table>
- *       </td>
+ *       <th id="byte2_a">Byte 2</th>
+ *       <td><center>1</center>
+ *       <td><center>0</center>
+ *       <td colspan="6"><center>bits 5-0</center>
+ *     </tr>
+ *     <tr>
+ *       <th colspan="9"><span style="font-weight:normal">
+ *         {@code char} values in the range {@code '\u005Cu0800'}
+ *         to {@code '\u005CuFFFF'} are represented by three bytes:</span></th>
+ *     </tr>
+ *     <tr>
+ *       <td></td>
+ *       <th colspan="8"id="bit_c">Bit Values</th>
+ *     </tr>
+ *     <tr>
+ *       <th id="byte1_c">Byte 1</th>
+ *       <td><center>1</center>
+ *       <td><center>1</center>
+ *       <td><center>1</center>
+ *       <td><center>0</center>
+ *       <td colspan="4"><center>bits 15-12</center>
+ *     </tr>
+ *     <tr>
+ *       <th id="byte2_b">Byte 2</th>
+ *       <td><center>1</center>
+ *       <td><center>0</center>
+ *       <td colspan="6"><center>bits 11-6</center>
  *     </tr>
  *     <tr>
  *       <th id="byte3">Byte 3</th>
- *       <td>
- *         <table border="1" cellspacing="0" width="100%">
- *           <tr>
- *             <td width="12%"><center>1</center>
- *             <td width="13%"><center>0</center>
- *             <td colspan="6"><center>bits 5-0</center>
- *           </tr>
- *         </table>
- *       </td>
+ *       <td><center>1</center>
+ *       <td><center>0</center>
+ *       <td colspan="6"><center>bits 5-0</center>
  *     </tr>
  *   </table>
- *  </blockquote>
- *
+ * </blockquote>
  * <p>
  * The differences between this format and the
  * standard UTF-8 format are the following:
  * <ul>
- * <li>The null byte <code>'&#92;u0000'</code> is encoded in 2-byte format
+ * <li>The null byte {@code '\u005Cu0000'} is encoded in 2-byte format
  *     rather than 1-byte, so that the encoded strings never have
  *     embedded nulls.
  * <li>Only the 1-byte, 2-byte, and 3-byte formats are used.
@@ -195,36 +150,36 @@
     /**
      * Reads some bytes from an input
      * stream and stores them into the buffer
-     * array <code>b</code>. The number of bytes
+     * array {@code b}. The number of bytes
      * read is equal
-     * to the length of <code>b</code>.
+     * to the length of {@code b}.
      * <p>
      * This method blocks until one of the
-     * following conditions occurs:<p>
+     * following conditions occurs:
      * <ul>
-     * <li><code>b.length</code>
+     * <li>{@code b.length}
      * bytes of input data are available, in which
      * case a normal return is made.
      *
      * <li>End of
-     * file is detected, in which case an <code>EOFException</code>
+     * file is detected, in which case an {@code EOFException}
      * is thrown.
      *
      * <li>An I/O error occurs, in
-     * which case an <code>IOException</code> other
-     * than <code>EOFException</code> is thrown.
+     * which case an {@code IOException} other
+     * than {@code EOFException} is thrown.
      * </ul>
      * <p>
-     * If <code>b</code> is <code>null</code>,
-     * a <code>NullPointerException</code> is thrown.
-     * If <code>b.length</code> is zero, then
+     * If {@code b} is {@code null},
+     * a {@code NullPointerException} is thrown.
+     * If {@code b.length} is zero, then
      * no bytes are read. Otherwise, the first
-     * byte read is stored into element <code>b[0]</code>,
-     * the next one into <code>b[1]</code>, and
+     * byte read is stored into element {@code b[0]},
+     * the next one into {@code b[1]}, and
      * so on.
      * If an exception is thrown from
      * this method, then it may be that some but
-     * not all bytes of <code>b</code> have been
+     * not all bytes of {@code b} have been
      * updated with data from the input stream.
      *
      * @param     b   the buffer into which the data is read.
@@ -236,40 +191,40 @@
 
     /**
      *
-     * Reads <code>len</code>
+     * Reads {@code len}
      * bytes from
      * an input stream.
      * <p>
      * This method
      * blocks until one of the following conditions
-     * occurs:<p>
+     * occurs:
      * <ul>
-     * <li><code>len</code> bytes
+     * <li>{@code len} bytes
      * of input data are available, in which case
      * a normal return is made.
      *
      * <li>End of file
-     * is detected, in which case an <code>EOFException</code>
+     * is detected, in which case an {@code EOFException}
      * is thrown.
      *
      * <li>An I/O error occurs, in
-     * which case an <code>IOException</code> other
-     * than <code>EOFException</code> is thrown.
+     * which case an {@code IOException} other
+     * than {@code EOFException} is thrown.
      * </ul>
      * <p>
-     * If <code>b</code> is <code>null</code>,
-     * a <code>NullPointerException</code> is thrown.
-     * If <code>off</code> is negative, or <code>len</code>
-     * is negative, or <code>off+len</code> is
-     * greater than the length of the array <code>b</code>,
-     * then an <code>IndexOutOfBoundsException</code>
+     * If {@code b} is {@code null},
+     * a {@code NullPointerException} is thrown.
+     * If {@code off} is negative, or {@code len}
+     * is negative, or {@code off+len} is
+     * greater than the length of the array {@code b},
+     * then an {@code IndexOutOfBoundsException}
      * is thrown.
-     * If <code>len</code> is zero,
+     * If {@code len} is zero,
      * then no bytes are read. Otherwise, the first
-     * byte read is stored into element <code>b[off]</code>,
-     * the next one into <code>b[off+1]</code>,
+     * byte read is stored into element {@code b[off]},
+     * the next one into {@code b[off+1]},
      * and so on. The number of bytes read is,
-     * at most, equal to <code>len</code>.
+     * at most, equal to {@code len}.
      *
      * @param     b   the buffer into which the data is read.
      * @param off  an int specifying the offset into the data.
@@ -282,7 +237,7 @@
 
     /**
      * Makes an attempt to skip over
-     * <code>n</code> bytes
+     * {@code n} bytes
      * of data from the input
      * stream, discarding the skipped bytes. However,
      * it may skip
@@ -290,10 +245,10 @@
      * bytes, possibly zero. This may result from
      * any of a
      * number of conditions; reaching
-     * end of file before <code>n</code> bytes
+     * end of file before {@code n} bytes
      * have been skipped is
      * only one possibility.
-     * This method never throws an <code>EOFException</code>.
+     * This method never throws an {@code EOFException}.
      * The actual
      * number of bytes skipped is returned.
      *
@@ -305,13 +260,13 @@
 
     /**
      * Reads one input byte and returns
-     * <code>true</code> if that byte is nonzero,
-     * <code>false</code> if that byte is zero.
+     * {@code true} if that byte is nonzero,
+     * {@code false} if that byte is zero.
      * This method is suitable for reading
-     * the byte written by the <code>writeBoolean</code>
-     * method of interface <code>DataOutput</code>.
+     * the byte written by the {@code writeBoolean}
+     * method of interface {@code DataOutput}.
      *
-     * @return     the <code>boolean</code> value read.
+     * @return     the {@code boolean} value read.
      * @exception  EOFException  if this stream reaches the end before reading
      *               all the bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -321,11 +276,11 @@
     /**
      * Reads and returns one input byte.
      * The byte is treated as a signed value in
-     * the range <code>-128</code> through <code>127</code>,
+     * the range {@code -128} through {@code 127},
      * inclusive.
      * This method is suitable for
-     * reading the byte written by the <code>writeByte</code>
-     * method of interface <code>DataOutput</code>.
+     * reading the byte written by the {@code writeByte}
+     * method of interface {@code DataOutput}.
      *
      * @return     the 8-bit value read.
      * @exception  EOFException  if this stream reaches the end before reading
@@ -336,16 +291,16 @@
 
     /**
      * Reads one input byte, zero-extends
-     * it to type <code>int</code>, and returns
+     * it to type {@code int}, and returns
      * the result, which is therefore in the range
-     * <code>0</code>
-     * through <code>255</code>.
+     * {@code 0}
+     * through {@code 255}.
      * This method is suitable for reading
-     * the byte written by the <code>writeByte</code>
-     * method of interface <code>DataOutput</code>
-     * if the argument to <code>writeByte</code>
+     * the byte written by the {@code writeByte}
+     * method of interface {@code DataOutput}
+     * if the argument to {@code writeByte}
      * was intended to be a value in the range
-     * <code>0</code> through <code>255</code>.
+     * {@code 0} through {@code 255}.
      *
      * @return     the unsigned 8-bit value read.
      * @exception  EOFException  if this stream reaches the end before reading
@@ -356,17 +311,17 @@
 
     /**
      * Reads two input bytes and returns
-     * a <code>short</code> value. Let <code>a</code>
-     * be the first byte read and <code>b</code>
+     * a {@code short} value. Let {@code a}
+     * be the first byte read and {@code b}
      * be the second byte. The value
      * returned
      * is:
-     * <p><pre><code>(short)((a &lt;&lt; 8) | (b &amp; 0xff))
-     * </code></pre>
+     * <pre>{@code (short)((a << 8) | (b & 0xff))
+     * }</pre>
      * This method
      * is suitable for reading the bytes written
-     * by the <code>writeShort</code> method of
-     * interface <code>DataOutput</code>.
+     * by the {@code writeShort} method of
+     * interface {@code DataOutput}.
      *
      * @return     the 16-bit value read.
      * @exception  EOFException  if this stream reaches the end before reading
@@ -377,19 +332,19 @@
 
     /**
      * Reads two input bytes and returns
-     * an <code>int</code> value in the range <code>0</code>
-     * through <code>65535</code>. Let <code>a</code>
+     * an {@code int} value in the range {@code 0}
+     * through {@code 65535}. Let {@code a}
      * be the first byte read and
-     * <code>b</code>
+     * {@code b}
      * be the second byte. The value returned is:
-     * <p><pre><code>(((a &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
-     * </code></pre>
+     * <pre>{@code (((a & 0xff) << 8) | (b & 0xff))
+     * }</pre>
      * This method is suitable for reading the bytes
-     * written by the <code>writeShort</code> method
-     * of interface <code>DataOutput</code>  if
-     * the argument to <code>writeShort</code>
+     * written by the {@code writeShort} method
+     * of interface {@code DataOutput}  if
+     * the argument to {@code writeShort}
      * was intended to be a value in the range
-     * <code>0</code> through <code>65535</code>.
+     * {@code 0} through {@code 65535}.
      *
      * @return     the unsigned 16-bit value read.
      * @exception  EOFException  if this stream reaches the end before reading
@@ -399,19 +354,19 @@
     int readUnsignedShort() throws IOException;
 
     /**
-     * Reads two input bytes and returns a <code>char</code> value.
-     * Let <code>a</code>
-     * be the first byte read and <code>b</code>
+     * Reads two input bytes and returns a {@code char} value.
+     * Let {@code a}
+     * be the first byte read and {@code b}
      * be the second byte. The value
      * returned is:
-     * <p><pre><code>(char)((a &lt;&lt; 8) | (b &amp; 0xff))
-     * </code></pre>
+     * <pre>{@code (char)((a << 8) | (b & 0xff))
+     * }</pre>
      * This method
      * is suitable for reading bytes written by
-     * the <code>writeChar</code> method of interface
-     * <code>DataOutput</code>.
+     * the {@code writeChar} method of interface
+     * {@code DataOutput}.
      *
-     * @return     the <code>char</code> value read.
+     * @return     the {@code char} value read.
      * @exception  EOFException  if this stream reaches the end before reading
      *               all the bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -420,18 +375,17 @@
 
     /**
      * Reads four input bytes and returns an
-     * <code>int</code> value. Let <code>a-d</code>
+     * {@code int} value. Let {@code a-d}
      * be the first through fourth bytes read. The value returned is:
-     * <p><pre>
-     * <code>
-     * (((a &amp; 0xff) &lt;&lt; 24) | ((b &amp; 0xff) &lt;&lt; 16) |
-     * &#32;((c &amp; 0xff) &lt;&lt; 8) | (d &amp; 0xff))
-     * </code></pre>
+     * <pre>{@code
+     * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
+     *  ((c & 0xff) <<  8) | (d & 0xff))
+     * }</pre>
      * This method is suitable
-     * for reading bytes written by the <code>writeInt</code>
-     * method of interface <code>DataOutput</code>.
+     * for reading bytes written by the {@code writeInt}
+     * method of interface {@code DataOutput}.
      *
-     * @return     the <code>int</code> value read.
+     * @return     the {@code int} value read.
      * @exception  EOFException  if this stream reaches the end before reading
      *               all the bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -440,25 +394,25 @@
 
     /**
      * Reads eight input bytes and returns
-     * a <code>long</code> value. Let <code>a-h</code>
+     * a {@code long} value. Let {@code a-h}
      * be the first through eighth bytes read.
      * The value returned is:
-     * <p><pre> <code>
-     * (((long)(a &amp; 0xff) &lt;&lt; 56) |
-     *  ((long)(b &amp; 0xff) &lt;&lt; 48) |
-     *  ((long)(c &amp; 0xff) &lt;&lt; 40) |
-     *  ((long)(d &amp; 0xff) &lt;&lt; 32) |
-     *  ((long)(e &amp; 0xff) &lt;&lt; 24) |
-     *  ((long)(f &amp; 0xff) &lt;&lt; 16) |
-     *  ((long)(g &amp; 0xff) &lt;&lt;  8) |
-     *  ((long)(h &amp; 0xff)))
-     * </code></pre>
+     * <pre>{@code
+     * (((long)(a & 0xff) << 56) |
+     *  ((long)(b & 0xff) << 48) |
+     *  ((long)(c & 0xff) << 40) |
+     *  ((long)(d & 0xff) << 32) |
+     *  ((long)(e & 0xff) << 24) |
+     *  ((long)(f & 0xff) << 16) |
+     *  ((long)(g & 0xff) <<  8) |
+     *  ((long)(h & 0xff)))
+     * }</pre>
      * <p>
      * This method is suitable
-     * for reading bytes written by the <code>writeLong</code>
-     * method of interface <code>DataOutput</code>.
+     * for reading bytes written by the {@code writeLong}
+     * method of interface {@code DataOutput}.
      *
-     * @return     the <code>long</code> value read.
+     * @return     the {@code long} value read.
      * @exception  EOFException  if this stream reaches the end before reading
      *               all the bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -467,18 +421,18 @@
 
     /**
      * Reads four input bytes and returns
-     * a <code>float</code> value. It does this
-     * by first constructing an <code>int</code>
+     * a {@code float} value. It does this
+     * by first constructing an {@code int}
      * value in exactly the manner
-     * of the <code>readInt</code>
-     * method, then converting this <code>int</code>
-     * value to a <code>float</code> in
-     * exactly the manner of the method <code>Float.intBitsToFloat</code>.
+     * of the {@code readInt}
+     * method, then converting this {@code int}
+     * value to a {@code float} in
+     * exactly the manner of the method {@code Float.intBitsToFloat}.
      * This method is suitable for reading
-     * bytes written by the <code>writeFloat</code>
-     * method of interface <code>DataOutput</code>.
+     * bytes written by the {@code writeFloat}
+     * method of interface {@code DataOutput}.
      *
-     * @return     the <code>float</code> value read.
+     * @return     the {@code float} value read.
      * @exception  EOFException  if this stream reaches the end before reading
      *               all the bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -487,18 +441,18 @@
 
     /**
      * Reads eight input bytes and returns
-     * a <code>double</code> value. It does this
-     * by first constructing a <code>long</code>
+     * a {@code double} value. It does this
+     * by first constructing a {@code long}
      * value in exactly the manner
-     * of the <code>readlong</code>
-     * method, then converting this <code>long</code>
-     * value to a <code>double</code> in exactly
-     * the manner of the method <code>Double.longBitsToDouble</code>.
+     * of the {@code readLong}
+     * method, then converting this {@code long}
+     * value to a {@code double} in exactly
+     * the manner of the method {@code Double.longBitsToDouble}.
      * This method is suitable for reading
-     * bytes written by the <code>writeDouble</code>
-     * method of interface <code>DataOutput</code>.
+     * bytes written by the {@code writeDouble}
+     * method of interface {@code DataOutput}.
      *
-     * @return     the <code>double</code> value read.
+     * @return     the {@code double} value read.
      * @exception  EOFException  if this stream reaches the end before reading
      *               all the bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -512,35 +466,35 @@
      * until it encounters a line terminator or
      * end of
      * file; the characters read are then
-     * returned as a <code>String</code>. Note
+     * returned as a {@code String}. Note
      * that because this
      * method processes bytes,
      * it does not support input of the full Unicode
      * character set.
      * <p>
      * If end of file is encountered
-     * before even one byte can be read, then <code>null</code>
+     * before even one byte can be read, then {@code null}
      * is returned. Otherwise, each byte that is
-     * read is converted to type <code>char</code>
-     * by zero-extension. If the character <code>'\n'</code>
+     * read is converted to type {@code char}
+     * by zero-extension. If the character {@code '\n'}
      * is encountered, it is discarded and reading
-     * ceases. If the character <code>'\r'</code>
+     * ceases. If the character {@code '\r'}
      * is encountered, it is discarded and, if
      * the following byte converts &#32;to the
-     * character <code>'\n'</code>, then that is
+     * character {@code '\n'}, then that is
      * discarded also; reading then ceases. If
      * end of file is encountered before either
-     * of the characters <code>'\n'</code> and
-     * <code>'\r'</code> is encountered, reading
-     * ceases. Once reading has ceased, a <code>String</code>
+     * of the characters {@code '\n'} and
+     * {@code '\r'} is encountered, reading
+     * ceases. Once reading has ceased, a {@code String}
      * is returned that contains all the characters
      * read and not discarded, taken in order.
      * Note that every character in this string
-     * will have a value less than <code>&#92;u0100</code>,
-     * that is, <code>(char)256</code>.
+     * will have a value less than {@code \u005Cu0100},
+     * that is, {@code (char)256}.
      *
      * @return the next line of text from the input stream,
-     *         or <CODE>null</CODE> if the end of file is
+     *         or {@code null} if the end of file is
      *         encountered before a byte can be read.
      * @exception  IOException  if an I/O error occurs.
      */
@@ -550,15 +504,15 @@
      * Reads in a string that has been encoded using a
      * <a href="#modified-utf-8">modified UTF-8</a>
      * format.
-     * The general contract of <code>readUTF</code>
+     * The general contract of {@code readUTF}
      * is that it reads a representation of a Unicode
      * character string encoded in modified
      * UTF-8 format; this string of characters
-     * is then returned as a <code>String</code>.
+     * is then returned as a {@code String}.
      * <p>
      * First, two bytes are read and used to
      * construct an unsigned 16-bit integer in
-     * exactly the manner of the <code>readUnsignedShort</code>
+     * exactly the manner of the {@code readUnsignedShort}
      * method . This integer value is called the
      * <i>UTF length</i> and specifies the number
      * of additional bytes to be read. These bytes
@@ -570,58 +524,58 @@
      * next group.
      * <p>
      * If the first byte of a group
-     * matches the bit pattern <code>0xxxxxxx</code>
-     * (where <code>x</code> means "may be <code>0</code>
-     * or <code>1</code>"), then the group consists
+     * matches the bit pattern {@code 0xxxxxxx}
+     * (where {@code x} means "may be {@code 0}
+     * or {@code 1}"), then the group consists
      * of just that byte. The byte is zero-extended
      * to form a character.
      * <p>
      * If the first byte
-     * of a group matches the bit pattern <code>110xxxxx</code>,
-     * then the group consists of that byte <code>a</code>
-     * and a second byte <code>b</code>. If there
-     * is no byte <code>b</code> (because byte
-     * <code>a</code> was the last of the bytes
-     * to be read), or if byte <code>b</code> does
-     * not match the bit pattern <code>10xxxxxx</code>,
-     * then a <code>UTFDataFormatException</code>
+     * of a group matches the bit pattern {@code 110xxxxx},
+     * then the group consists of that byte {@code a}
+     * and a second byte {@code b}. If there
+     * is no byte {@code b} (because byte
+     * {@code a} was the last of the bytes
+     * to be read), or if byte {@code b} does
+     * not match the bit pattern {@code 10xxxxxx},
+     * then a {@code UTFDataFormatException}
      * is thrown. Otherwise, the group is converted
-     * to the character:<p>
-     * <pre><code>(char)(((a&amp; 0x1F) &lt;&lt; 6) | (b &amp; 0x3F))
-     * </code></pre>
+     * to the character:
+     * <pre>{@code (char)(((a & 0x1F) << 6) | (b & 0x3F))
+     * }</pre>
      * If the first byte of a group
-     * matches the bit pattern <code>1110xxxx</code>,
-     * then the group consists of that byte <code>a</code>
-     * and two more bytes <code>b</code> and <code>c</code>.
-     * If there is no byte <code>c</code> (because
-     * byte <code>a</code> was one of the last
+     * matches the bit pattern {@code 1110xxxx},
+     * then the group consists of that byte {@code a}
+     * and two more bytes {@code b} and {@code c}.
+     * If there is no byte {@code c} (because
+     * byte {@code a} was one of the last
      * two of the bytes to be read), or either
-     * byte <code>b</code> or byte <code>c</code>
-     * does not match the bit pattern <code>10xxxxxx</code>,
-     * then a <code>UTFDataFormatException</code>
+     * byte {@code b} or byte {@code c}
+     * does not match the bit pattern {@code 10xxxxxx},
+     * then a {@code UTFDataFormatException}
      * is thrown. Otherwise, the group is converted
-     * to the character:<p>
-     * <pre><code>
-     * (char)(((a &amp; 0x0F) &lt;&lt; 12) | ((b &amp; 0x3F) &lt;&lt; 6) | (c &amp; 0x3F))
-     * </code></pre>
+     * to the character:
+     * <pre>{@code
+     * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
+     * }</pre>
      * If the first byte of a group matches the
-     * pattern <code>1111xxxx</code> or the pattern
-     * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
+     * pattern {@code 1111xxxx} or the pattern
+     * {@code 10xxxxxx}, then a {@code UTFDataFormatException}
      * is thrown.
      * <p>
      * If end of file is encountered
      * at any time during this entire process,
-     * then an <code>EOFException</code> is thrown.
+     * then an {@code EOFException} is thrown.
      * <p>
      * After every group has been converted to
      * a character by this process, the characters
      * are gathered, in the same order in which
      * their corresponding groups were read from
-     * the input stream, to form a <code>String</code>,
+     * the input stream, to form a {@code String},
      * which is returned.
      * <p>
-     * The <code>writeUTF</code>
-     * method of interface <code>DataOutput</code>
+     * The {@code writeUTF}
+     * method of interface {@code DataOutput}
      * may be used to write data that is suitable
      * for reading by this method.
      * @return     a Unicode string.
diff --git a/ojluni/src/main/java/java/io/RandomAccessFile.java b/ojluni/src/main/java/java/io/RandomAccessFile.java
index 6008d0f..fc645cf 100644
--- a/ojluni/src/main/java/java/io/RandomAccessFile.java
+++ b/ojluni/src/main/java/java/io/RandomAccessFile.java
@@ -28,7 +28,6 @@
 
 import java.nio.channels.FileChannel;
 import sun.nio.ch.FileChannelImpl;
-import sun.misc.IoTrace;
 import android.system.ErrnoException;
 import dalvik.system.CloseGuard;
 import libcore.io.IoBridge;
@@ -48,16 +47,16 @@
  * the file pointer past the bytes written. Output operations that write
  * past the current end of the implied array cause the array to be
  * extended. The file pointer can be read by the
- * <code>getFilePointer</code> method and set by the <code>seek</code>
+ * {@code getFilePointer} method and set by the {@code seek}
  * method.
  * <p>
  * It is generally true of all the reading routines in this class that
  * if end-of-file is reached before the desired number of bytes has been
- * read, an <code>EOFException</code> (which is a kind of
- * <code>IOException</code>) is thrown. If any byte cannot be read for
- * any reason other than end-of-file, an <code>IOException</code> other
- * than <code>EOFException</code> is thrown. In particular, an
- * <code>IOException</code> may be thrown if the stream has been closed.
+ * read, an {@code EOFException} (which is a kind of
+ * {@code IOException}) is thrown. If any byte cannot be read for
+ * any reason other than end-of-file, an {@code IOException} other
+ * than {@code EOFException} is thrown. In particular, an
+ * {@code IOException} may be thrown if the stream has been closed.
  *
  * @author  unascribed
  * @since   JDK1.0
@@ -74,7 +73,10 @@
     private FileChannel channel = null;
     private boolean rw;
 
-    /* The path of the referenced file */
+    /**
+     * The path of the referenced file
+     * (null if the stream is created with a file descriptor)
+     */
     private final String path;
 
     private Object closeLock = new Object();
@@ -92,12 +94,12 @@
      * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.
      *
      * <p>
-     * If there is a security manager, its <code>checkRead</code> method
-     * is called with the <code>name</code> argument
+     * If there is a security manager, its {@code checkRead} method
+     * is called with the {@code name} argument
      * as its argument to see if read access to the file is allowed.
      * If the mode allows writing, the security manager's
-     * <code>checkWrite</code> method
-     * is also called with the <code>name</code> argument
+     * {@code checkWrite} method
+     * is also called with the {@code name} argument
      * as its argument to see if write access to the file is allowed.
      *
      * @param      name   the system-dependent filename
@@ -113,9 +115,9 @@
      *            that name cannot be created, or if some other error occurs
      *            while opening or creating the file
      * @exception  SecurityException         if a security manager exists and its
-     *               <code>checkRead</code> method denies read access to the file
+     *               {@code checkRead} method denies read access to the file
      *               or the mode is "rw" and the security manager's
-     *               <code>checkWrite</code> method denies write access to the file
+     *               {@code checkWrite} method denies write access to the file
      * @see        java.lang.SecurityException
      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
@@ -133,12 +135,12 @@
      * write to, the file specified by the {@link File} argument.  A new {@link
      * FileDescriptor} object is created to represent this file connection.
      *
-     * <a name="mode"><p> The <tt>mode</tt> argument specifies the access mode
+     * <p>The <a name="mode"><tt>mode</tt></a> argument specifies the access mode
      * in which the file is to be opened.  The permitted values and their
      * meanings are:
      *
-     * <blockquote><table summary="Access mode permitted values and meanings">
-     * <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr>
+     * <table summary="Access mode permitted values and meanings">
+     * <tr><th align="left">Value</th><th align="left">Meaning</th></tr>
      * <tr><td valign="top"><tt>"r"</tt></td>
      *     <td> Open for reading only.  Invoking any of the <tt>write</tt>
      *     methods of the resulting object will cause an {@link
@@ -154,7 +156,7 @@
      *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
      *     require that every update to the file's content be written
      *     synchronously to the underlying storage device. </td></tr>
-     * </table></blockquote>
+     * </table>
      *
      * The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link
      * java.nio.channels.FileChannel#force(boolean) force(boolean)} method of
@@ -168,16 +170,16 @@
      * event of a system crash.  If the file does not reside on a local device
      * then no such guarantee is made.
      *
-     * <p> The <tt>"rwd"</tt> mode can be used to reduce the number of I/O
+     * <p>The <tt>"rwd"</tt> mode can be used to reduce the number of I/O
      * operations performed.  Using <tt>"rwd"</tt> only requires updates to the
      * file's content to be written to storage; using <tt>"rws"</tt> requires
      * updates to both the file's content and its metadata to be written, which
      * generally requires at least one more low-level I/O operation.
      *
-     * <p> If there is a security manager, its <code>checkRead</code> method is
-     * called with the pathname of the <code>file</code> argument as its
+     * <p>If there is a security manager, its {@code checkRead} method is
+     * called with the pathname of the {@code file} argument as its
      * argument to see if read access to the file is allowed.  If the mode
-     * allows writing, the security manager's <code>checkWrite</code> method is
+     * allows writing, the security manager's {@code checkWrite} method is
      * also called with the path argument to see if write access to the file is
      * allowed.
      *
@@ -195,9 +197,9 @@
      *            that name cannot be created, or if some other error occurs
      *            while opening or creating the file
      * @exception  SecurityException         if a security manager exists and its
-     *               <code>checkRead</code> method denies read access to the file
+     *               {@code checkRead} method denies read access to the file
      *               or the mode is "rw" and the security manager's
-     *               <code>checkWrite</code> method denies write access to the file
+     *               {@code checkWrite} method denies write access to the file
      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
      * @see        java.nio.channels.FileChannel#force(boolean)
@@ -257,14 +259,16 @@
 
     /**
      * Returns the opaque file descriptor object associated with this
-     * stream. </p>
+     * stream.
      *
      * @return     the file descriptor object associated with this stream.
      * @exception  IOException  if an I/O error occurs.
      * @see        java.io.FileDescriptor
      */
     public final FileDescriptor getFD() throws IOException {
-        if (fd != null) return fd;
+        if (fd != null) {
+            return fd;
+        }
         throw new IOException();
     }
 
@@ -273,7 +277,7 @@
      * object associated with this file.
      *
      * <p> The {@link java.nio.channels.FileChannel#position()
-     * </code>position<code>} of the returned channel will always be equal to
+     * position} of the returned channel will always be equal to
      * this object's file-pointer offset as returned by the {@link
      * #getFilePointer getFilePointer} method.  Changing this object's
      * file-pointer offset, whether explicitly or by reading or writing bytes,
@@ -297,15 +301,15 @@
 
     /**
      * Reads a byte of data from this file. The byte is returned as an
-     * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This
+     * integer in the range 0 to 255 ({@code 0x00-0x0ff}). This
      * method blocks if no input is yet available.
      * <p>
-     * Although <code>RandomAccessFile</code> is not a subclass of
-     * <code>InputStream</code>, this method behaves in exactly the same
+     * Although {@code RandomAccessFile} is not a subclass of
+     * {@code InputStream}, this method behaves in exactly the same
      * way as the {@link InputStream#read()} method of
-     * <code>InputStream</code>.
+     * {@code InputStream}.
      *
-     * @return     the next byte of data, or <code>-1</code> if the end of the
+     * @return     the next byte of data, or {@code -1} if the end of the
      *             file has been reached.
      * @exception  IOException  if an I/O error occurs. Not thrown if
      *                          end-of-file has been reached.
@@ -326,59 +330,59 @@
     }
 
     /**
-     * Reads up to <code>len</code> bytes of data from this file into an
+     * Reads up to {@code len} bytes of data from this file into an
      * array of bytes. This method blocks until at least one byte of input
      * is available.
      * <p>
-     * Although <code>RandomAccessFile</code> is not a subclass of
-     * <code>InputStream</code>, this method behaves in exactly the
+     * Although {@code RandomAccessFile} is not a subclass of
+     * {@code InputStream}, this method behaves in exactly the
      * same way as the {@link InputStream#read(byte[], int, int)} method of
-     * <code>InputStream</code>.
+     * {@code InputStream}.
      *
      * @param      b     the buffer into which the data is read.
-     * @param      off   the start offset in array <code>b</code>
+     * @param      off   the start offset in array {@code b}
      *                   at which the data is written.
      * @param      len   the maximum number of bytes read.
      * @return     the total number of bytes read into the buffer, or
-     *             <code>-1</code> if there is no more data because the end of
+     *             {@code -1} if there is no more data because the end of
      *             the file has been reached.
      * @exception  IOException If the first byte cannot be read for any reason
      * other than end of file, or if the random access file has been closed, or if
      * some other I/O error occurs.
-     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
-     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
-     * <code>len</code> is negative, or <code>len</code> is greater than
-     * <code>b.length - off</code>
+     * @exception  NullPointerException If {@code b} is {@code null}.
+     * @exception  IndexOutOfBoundsException If {@code off} is negative,
+     * {@code len} is negative, or {@code len} is greater than
+     * {@code b.length - off}
      */
     public int read(byte b[], int off, int len) throws IOException {
         return readBytes(b, off, len);
     }
 
     /**
-     * Reads up to <code>b.length</code> bytes of data from this file
+     * Reads up to {@code b.length} bytes of data from this file
      * into an array of bytes. This method blocks until at least one byte
      * of input is available.
      * <p>
-     * Although <code>RandomAccessFile</code> is not a subclass of
-     * <code>InputStream</code>, this method behaves in exactly the
+     * Although {@code RandomAccessFile} is not a subclass of
+     * {@code InputStream}, this method behaves in exactly the
      * same way as the {@link InputStream#read(byte[])} method of
-     * <code>InputStream</code>.
+     * {@code InputStream}.
      *
      * @param      b   the buffer into which the data is read.
      * @return     the total number of bytes read into the buffer, or
-     *             <code>-1</code> if there is no more data because the end of
+     *             {@code -1} if there is no more data because the end of
      *             this file has been reached.
      * @exception  IOException If the first byte cannot be read for any reason
      * other than end of file, or if the random access file has been closed, or if
      * some other I/O error occurs.
-     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
+     * @exception  NullPointerException If {@code b} is {@code null}.
      */
     public int read(byte b[]) throws IOException {
         return readBytes(b, 0, b.length);
     }
 
     /**
-     * Reads <code>b.length</code> bytes from this file into the byte
+     * Reads {@code b.length} bytes from this file into the byte
      * array, starting at the current file pointer. This method reads
      * repeatedly from the file until the requested number of bytes are
      * read. This method blocks until the requested number of bytes are
@@ -394,7 +398,7 @@
     }
 
     /**
-     * Reads exactly <code>len</code> bytes from this file into the byte
+     * Reads exactly {@code len} bytes from this file into the byte
      * array, starting at the current file pointer. This method reads
      * repeatedly from the file until the requested number of bytes are
      * read. This method blocks until the requested number of bytes are
@@ -418,15 +422,15 @@
     }
 
     /**
-     * Attempts to skip over <code>n</code> bytes of input discarding the
+     * Attempts to skip over {@code n} bytes of input discarding the
      * skipped bytes.
      * <p>
      *
      * This method may skip over some smaller number of bytes, possibly zero.
      * This may result from any of a number of conditions; reaching end of
-     * file before <code>n</code> bytes have been skipped is only one
-     * possibility. This method never throws an <code>EOFException</code>.
-     * The actual number of bytes skipped is returned.  If <code>n</code>
+     * file before {@code n} bytes have been skipped is only one
+     * possibility. This method never throws an {@code EOFException}.
+     * The actual number of bytes skipped is returned.  If {@code n}
      * is negative, no bytes are skipped.
      *
      * @param      n   the number of bytes to be skipped.
@@ -459,7 +463,7 @@
      * Writes the specified byte to this file. The write starts at
      * the current file pointer.
      *
-     * @param      b   the <code>byte</code> to be written.
+     * @param      b   the {@code byte} to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public void write(int b) throws IOException {
@@ -483,7 +487,7 @@
     }
 
     /**
-     * Writes <code>b.length</code> bytes from the specified byte array
+     * Writes {@code b.length} bytes from the specified byte array
      * to this file, starting at the current file pointer.
      *
      * @param      b   the data.
@@ -494,8 +498,8 @@
     }
 
     /**
-     * Writes <code>len</code> bytes from the specified byte array
-     * starting at offset <code>off</code> to this file.
+     * Writes {@code len} bytes from the specified byte array
+     * starting at offset {@code off} to this file.
      *
      * @param      b     the data.
      * @param      off   the start offset in the data.
@@ -534,8 +538,8 @@
      * @param      offset   the offset position, measured in bytes from the
      *                   beginning of the file, at which to set the file
      *                   pointer.
-     * @exception  IOException  if <code>pos</code> is less than
-     *                          <code>0</code> or if an I/O error occurs.
+     * @exception  IOException  if {@code pos} is less than
+     *                          {@code 0} or if an I/O error occurs.
      */
     public void seek(long offset) throws IOException {
         if (offset < 0) {
@@ -566,14 +570,14 @@
      * Sets the length of this file.
      *
      * <p> If the present length of the file as returned by the
-     * <code>length</code> method is greater than the <code>newLength</code>
+     * {@code length} method is greater than the {@code newLength}
      * argument then the file will be truncated.  In this case, if the file
-     * offset as returned by the <code>getFilePointer</code> method is greater
-     * than <code>newLength</code> then after this method returns the offset
-     * will be equal to <code>newLength</code>.
+     * offset as returned by the {@code getFilePointer} method is greater
+     * than {@code newLength} then after this method returns the offset
+     * will be equal to {@code newLength}.
      *
      * <p> If the present length of the file as returned by the
-     * <code>length</code> method is smaller than the <code>newLength</code>
+     * {@code length} method is smaller than the {@code newLength}
      * argument then the file will be extended.  In this case, the contents of
      * the extended portion of the file are not defined.
      *
@@ -637,14 +641,14 @@
     //
 
     /**
-     * Reads a <code>boolean</code> from this file. This method reads a
+     * Reads a {@code boolean} from this file. This method reads a
      * single byte from the file, starting at the current file pointer.
-     * A value of <code>0</code> represents
-     * <code>false</code>. Any other value represents <code>true</code>.
+     * A value of {@code 0} represents
+     * {@code false}. Any other value represents {@code true}.
      * This method blocks until the byte is read, the end of the stream
      * is detected, or an exception is thrown.
      *
-     * @return     the <code>boolean</code> value read.
+     * @return     the {@code boolean} value read.
      * @exception  EOFException  if this file has reached the end.
      * @exception  IOException   if an I/O error occurs.
      */
@@ -658,7 +662,7 @@
     /**
      * Reads a signed eight-bit value from this file. This method reads a
      * byte from the file, starting from the current file pointer.
-     * If the byte read is <code>b</code>, where
+     * If the byte read is {@code b}, where
      * <code>0&nbsp;&lt;=&nbsp;b&nbsp;&lt;=&nbsp;255</code>,
      * then the result is:
      * <blockquote><pre>
@@ -669,7 +673,7 @@
      * is detected, or an exception is thrown.
      *
      * @return     the next byte of this file as a signed eight-bit
-     *             <code>byte</code>.
+     *             {@code byte}.
      * @exception  EOFException  if this file has reached the end.
      * @exception  IOException   if an I/O error occurs.
      */
@@ -704,8 +708,8 @@
      * Reads a signed 16-bit number from this file. The method reads two
      * bytes from this file, starting at the current file pointer.
      * If the two bytes read, in order, are
-     * <code>b1</code> and <code>b2</code>, where each of the two values is
-     * between <code>0</code> and <code>255</code>, inclusive, then the
+     * {@code b1} and {@code b2}, where each of the two values is
+     * between {@code 0} and {@code 255}, inclusive, then the
      * result is equal to:
      * <blockquote><pre>
      *     (short)((b1 &lt;&lt; 8) | b2)
@@ -732,7 +736,7 @@
      * Reads an unsigned 16-bit number from this file. This method reads
      * two bytes from the file, starting at the current file pointer.
      * If the bytes read, in order, are
-     * <code>b1</code> and <code>b2</code>, where
+     * {@code b1} and {@code b2}, where
      * <code>0&nbsp;&lt;=&nbsp;b1, b2&nbsp;&lt;=&nbsp;255</code>,
      * then the result is equal to:
      * <blockquote><pre>
@@ -760,7 +764,7 @@
      * Reads a character from this file. This method reads two
      * bytes from the file, starting at the current file pointer.
      * If the bytes read, in order, are
-     * <code>b1</code> and <code>b2</code>, where
+     * {@code b1} and {@code b2}, where
      * <code>0&nbsp;&lt;=&nbsp;b1,&nbsp;b2&nbsp;&lt;=&nbsp;255</code>,
      * then the result is equal to:
      * <blockquote><pre>
@@ -771,7 +775,7 @@
      * stream is detected, or an exception is thrown.
      *
      * @return     the next two bytes of this file, interpreted as a
-     *                  <code>char</code>.
+     *                  {@code char}.
      * @exception  EOFException  if this file reaches the end before reading
      *               two bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -787,8 +791,8 @@
     /**
      * Reads a signed 32-bit integer from this file. This method reads 4
      * bytes from the file, starting at the current file pointer.
-     * If the bytes read, in order, are <code>b1</code>,
-     * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
+     * If the bytes read, in order, are {@code b1},
+     * {@code b2}, {@code b3}, and {@code b4}, where
      * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
      * then the result is equal to:
      * <blockquote><pre>
@@ -799,7 +803,7 @@
      * stream is detected, or an exception is thrown.
      *
      * @return     the next four bytes of this file, interpreted as an
-     *             <code>int</code>.
+     *             {@code int}.
      * @exception  EOFException  if this file reaches the end before reading
      *               four bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -818,15 +822,15 @@
      * Reads a signed 64-bit integer from this file. This method reads eight
      * bytes from the file, starting at the current file pointer.
      * If the bytes read, in order, are
-     * <code>b1</code>, <code>b2</code>, <code>b3</code>,
-     * <code>b4</code>, <code>b5</code>, <code>b6</code>,
-     * <code>b7</code>, and <code>b8,</code> where:
+     * {@code b1}, {@code b2}, {@code b3},
+     * {@code b4}, {@code b5}, {@code b6},
+     * {@code b7}, and {@code b8,} where:
      * <blockquote><pre>
      *     0 &lt;= b1, b2, b3, b4, b5, b6, b7, b8 &lt;=255,
      * </pre></blockquote>
      * <p>
      * then the result is equal to:
-     * <p><blockquote><pre>
+     * <blockquote><pre>
      *     ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
      *     + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
      *     + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
@@ -837,7 +841,7 @@
      * stream is detected, or an exception is thrown.
      *
      * @return     the next eight bytes of this file, interpreted as a
-     *             <code>long</code>.
+     *             {@code long}.
      * @exception  EOFException  if this file reaches the end before reading
      *               eight bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -847,18 +851,18 @@
     }
 
     /**
-     * Reads a <code>float</code> from this file. This method reads an
-     * <code>int</code> value, starting at the current file pointer,
-     * as if by the <code>readInt</code> method
-     * and then converts that <code>int</code> to a <code>float</code>
-     * using the <code>intBitsToFloat</code> method in class
-     * <code>Float</code>.
+     * Reads a {@code float} from this file. This method reads an
+     * {@code int} value, starting at the current file pointer,
+     * as if by the {@code readInt} method
+     * and then converts that {@code int} to a {@code float}
+     * using the {@code intBitsToFloat} method in class
+     * {@code Float}.
      * <p>
      * This method blocks until the four bytes are read, the end of the
      * stream is detected, or an exception is thrown.
      *
      * @return     the next four bytes of this file, interpreted as a
-     *             <code>float</code>.
+     *             {@code float}.
      * @exception  EOFException  if this file reaches the end before reading
      *             four bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -870,18 +874,18 @@
     }
 
     /**
-     * Reads a <code>double</code> from this file. This method reads a
-     * <code>long</code> value, starting at the current file pointer,
-     * as if by the <code>readLong</code> method
-     * and then converts that <code>long</code> to a <code>double</code>
-     * using the <code>longBitsToDouble</code> method in
-     * class <code>Double</code>.
+     * Reads a {@code double} from this file. This method reads a
+     * {@code long} value, starting at the current file pointer,
+     * as if by the {@code readLong} method
+     * and then converts that {@code long} to a {@code double}
+     * using the {@code longBitsToDouble} method in
+     * class {@code Double}.
      * <p>
      * This method blocks until the eight bytes are read, the end of the
      * stream is detected, or an exception is thrown.
      *
      * @return     the next eight bytes of this file, interpreted as a
-     *             <code>double</code>.
+     *             {@code double}.
      * @exception  EOFException  if this file reaches the end before reading
      *             eight bytes.
      * @exception  IOException   if an I/O error occurs.
@@ -902,7 +906,7 @@
      * therefore, support the full Unicode character set.
      *
      * <p> A line of text is terminated by a carriage-return character
-     * (<code>'&#92;r'</code>), a newline character (<code>'&#92;n'</code>), a
+     * ({@code '\u005Cr'}), a newline character ({@code '\u005Cn'}), a
      * carriage-return character immediately followed by a newline character,
      * or the end of the file.  Line-terminating characters are discarded and
      * are not included as part of the string returned.
@@ -954,7 +958,7 @@
      * <p>
      * The first two bytes are read, starting from the current file
      * pointer, as if by
-     * <code>readUnsignedShort</code>. This value gives the number of
+     * {@code readUnsignedShort}. This value gives the number of
      * following bytes that are in the encoded string, not
      * the length of the resulting string. The following bytes are then
      * interpreted as bytes encoding characters in the modified UTF-8 format
@@ -976,13 +980,13 @@
     }
 
     /**
-     * Writes a <code>boolean</code> to the file as a one-byte value. The
-     * value <code>true</code> is written out as the value
-     * <code>(byte)1</code>; the value <code>false</code> is written out
-     * as the value <code>(byte)0</code>. The write starts at
+     * Writes a {@code boolean} to the file as a one-byte value. The
+     * value {@code true} is written out as the value
+     * {@code (byte)1}; the value {@code false} is written out
+     * as the value {@code (byte)0}. The write starts at
      * the current position of the file pointer.
      *
-     * @param      v   a <code>boolean</code> value to be written.
+     * @param      v   a {@code boolean} value to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public final void writeBoolean(boolean v) throws IOException {
@@ -991,10 +995,10 @@
     }
 
     /**
-     * Writes a <code>byte</code> to the file as a one-byte value. The
+     * Writes a {@code byte} to the file as a one-byte value. The
      * write starts at the current position of the file pointer.
      *
-     * @param      v   a <code>byte</code> value to be written.
+     * @param      v   a {@code byte} value to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public final void writeByte(int v) throws IOException {
@@ -1003,10 +1007,10 @@
     }
 
     /**
-     * Writes a <code>short</code> to the file as two bytes, high byte first.
+     * Writes a {@code short} to the file as two bytes, high byte first.
      * The write starts at the current position of the file pointer.
      *
-     * @param      v   a <code>short</code> to be written.
+     * @param      v   a {@code short} to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public final void writeShort(int v) throws IOException {
@@ -1016,11 +1020,11 @@
     }
 
     /**
-     * Writes a <code>char</code> to the file as a two-byte value, high
+     * Writes a {@code char} to the file as a two-byte value, high
      * byte first. The write starts at the current position of the
      * file pointer.
      *
-     * @param      v   a <code>char</code> value to be written.
+     * @param      v   a {@code char} value to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public final void writeChar(int v) throws IOException {
@@ -1030,10 +1034,10 @@
     }
 
     /**
-     * Writes an <code>int</code> to the file as four bytes, high byte first.
+     * Writes an {@code int} to the file as four bytes, high byte first.
      * The write starts at the current position of the file pointer.
      *
-     * @param      v   an <code>int</code> to be written.
+     * @param      v   an {@code int} to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public final void writeInt(int v) throws IOException {
@@ -1045,10 +1049,10 @@
     }
 
     /**
-     * Writes a <code>long</code> to the file as eight bytes, high byte first.
+     * Writes a {@code long} to the file as eight bytes, high byte first.
      * The write starts at the current position of the file pointer.
      *
-     * @param      v   a <code>long</code> to be written.
+     * @param      v   a {@code long} to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public final void writeLong(long v) throws IOException {
@@ -1064,13 +1068,13 @@
     }
 
     /**
-     * Converts the float argument to an <code>int</code> using the
-     * <code>floatToIntBits</code> method in class <code>Float</code>,
-     * and then writes that <code>int</code> value to the file as a
+     * Converts the float argument to an {@code int} using the
+     * {@code floatToIntBits} method in class {@code Float},
+     * and then writes that {@code int} value to the file as a
      * four-byte quantity, high byte first. The write starts at the
      * current position of the file pointer.
      *
-     * @param      v   a <code>float</code> value to be written.
+     * @param      v   a {@code float} value to be written.
      * @exception  IOException  if an I/O error occurs.
      * @see        java.lang.Float#floatToIntBits(float)
      */
@@ -1079,13 +1083,13 @@
     }
 
     /**
-     * Converts the double argument to a <code>long</code> using the
-     * <code>doubleToLongBits</code> method in class <code>Double</code>,
-     * and then writes that <code>long</code> value to the file as an
+     * Converts the double argument to a {@code long} using the
+     * {@code doubleToLongBits} method in class {@code Double},
+     * and then writes that {@code long} value to the file as an
      * eight-byte quantity, high byte first. The write starts at the current
      * position of the file pointer.
      *
-     * @param      v   a <code>double</code> value to be written.
+     * @param      v   a {@code double} value to be written.
      * @exception  IOException  if an I/O error occurs.
      * @see        java.lang.Double#doubleToLongBits(double)
      */
@@ -1102,6 +1106,7 @@
      * @param      s   a string of bytes to be written.
      * @exception  IOException  if an I/O error occurs.
      */
+    @SuppressWarnings("deprecation")
     public final void writeBytes(String s) throws IOException {
         int len = s.length();
         byte[] b = new byte[len];
@@ -1112,10 +1117,10 @@
     /**
      * Writes a string to the file as a sequence of characters. Each
      * character is written to the data output stream as if by the
-     * <code>writeChar</code> method. The write starts at the current
+     * {@code writeChar} method. The write starts at the current
      * position of the file pointer.
      *
-     * @param      s   a <code>String</code> value to be written.
+     * @param      s   a {@code String} value to be written.
      * @exception  IOException  if an I/O error occurs.
      * @see        java.io.RandomAccessFile#writeChar(int)
      */
@@ -1139,7 +1144,7 @@
      * <p>
      * First, two bytes are written to the file, starting at the
      * current file pointer, as if by the
-     * <code>writeShort</code> method giving the number of bytes to
+     * {@code writeShort} method giving the number of bytes to
      * follow. This value is the number of bytes actually written out,
      * not the length of the string. Following the length, each character
      * of the string is output, in sequence, using the modified UTF-8 encoding
diff --git a/ojluni/src/main/java/java/lang/invoke/MethodHandle.java b/ojluni/src/main/java/java/lang/invoke/MethodHandle.java
index ffbc401..dfc7021 100644
--- a/ojluni/src/main/java/java/lang/invoke/MethodHandle.java
+++ b/ojluni/src/main/java/java/lang/invoke/MethodHandle.java
@@ -446,6 +446,20 @@
     private final MethodType type;
     /*private*/ MethodHandle asTypeCache;
     // asTypeCache is not private so that invokers can easily fetch it
+    // Used by the runtime.
+
+    // The kind of this method handle (used by the runtime). This is a well defined
+    // mapping from the MethodHandleInfo.REF_* constants, but is not intended for
+    // public use.
+    private final int handleKind;
+    // The ArtMethod* or ArtField* associated with this method handle (used by the runtime).
+    private final long artFieldOrMethod;
+
+    protected MethodHandle(long artFieldOrMethod, int handleKind, MethodType type) {
+        this.artFieldOrMethod = artFieldOrMethod;
+        this.handleKind = handleKind;
+        this.type = type;
+    }
 
     /**
      * Reports the type of this method handle.
@@ -457,19 +471,6 @@
     }
 
     /**
-     * Package-private constructor for the method handle implementation hierarchy.
-     * Method handle inheritance will be contained completely within
-     * the {@code java.lang.invoke} package.
-     */
-    // @param type type (permanently assigned) of the new method handle
-    /*non-public*/ MethodHandle(MethodType type) {
-        type.getClass();  // explicit NPE
-        this.type = type;
-
-        // Android-changed: No LambdaForms associated with this MH.
-    }
-
-    /**
      * Invokes the method handle, allowing any caller type descriptor, but requiring an exact type match.
      * The symbolic type descriptor at the call site of {@code invokeExact} must
      * exactly match this method handle's {@link #type type}.
diff --git a/ojluni/src/main/java/java/lang/invoke/MethodHandleImpl.java b/ojluni/src/main/java/java/lang/invoke/MethodHandleImpl.java
new file mode 100644
index 0000000..06ff553
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/invoke/MethodHandleImpl.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Google designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Google in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package java.lang.invoke;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/**
+ * A method handle that's directly associated with an ArtField or an ArtMethod and
+ * specifies no additional transformations.
+ *
+ * @hide
+ */
+public class MethodHandleImpl extends MethodHandle {
+
+    MethodHandleImpl(long artFieldOrMethod, int handleKind, MethodType type) {
+        super(artFieldOrMethod, handleKind, type);
+    }
+}
diff --git a/ojluni/src/main/java/java/lang/invoke/MethodHandleStatics.java b/ojluni/src/main/java/java/lang/invoke/MethodHandleStatics.java
index 6928c22..17d7882 100644
--- a/ojluni/src/main/java/java/lang/invoke/MethodHandleStatics.java
+++ b/ojluni/src/main/java/java/lang/invoke/MethodHandleStatics.java
@@ -25,8 +25,6 @@
 
 package java.lang.invoke;
 
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import sun.misc.Unsafe;
 
 /**
@@ -41,56 +39,8 @@
 
     static final Unsafe UNSAFE = Unsafe.getUnsafe();
 
-    static final boolean DEBUG_METHOD_HANDLE_NAMES;
-    static final boolean DUMP_CLASS_FILES;
-    static final boolean TRACE_INTERPRETER;
-    static final boolean TRACE_METHOD_LINKAGE;
-    static final int COMPILE_THRESHOLD;
-    static final int DONT_INLINE_THRESHOLD;
-    static final int PROFILE_LEVEL;
-    static final boolean PROFILE_GWT;
-    static final int CUSTOMIZE_THRESHOLD;
-
-    static {
-        final Object[] values = new Object[9];
-        AccessController.doPrivileged(new PrivilegedAction<Void>() {
-                public Void run() {
-                    values[0] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
-                    values[1] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES");
-                    values[2] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_INTERPRETER");
-                    values[3] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE");
-                    values[4] = Integer.getInteger("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD", 0);
-                    values[5] = Integer.getInteger("java.lang.invoke.MethodHandle.DONT_INLINE_THRESHOLD", 30);
-                    values[6] = Integer.getInteger("java.lang.invoke.MethodHandle.PROFILE_LEVEL", 0);
-                    values[7] = Boolean.parseBoolean(System.getProperty("java.lang.invoke.MethodHandle.PROFILE_GWT", "true"));
-                    values[8] = Integer.getInteger("java.lang.invoke.MethodHandle.CUSTOMIZE_THRESHOLD", 127);
-                    return null;
-                }
-            });
-        DEBUG_METHOD_HANDLE_NAMES = (Boolean) values[0];
-        DUMP_CLASS_FILES          = (Boolean) values[1];
-        TRACE_INTERPRETER         = (Boolean) values[2];
-        TRACE_METHOD_LINKAGE      = (Boolean) values[3];
-        COMPILE_THRESHOLD         = (Integer) values[4];
-        DONT_INLINE_THRESHOLD     = (Integer) values[5];
-        PROFILE_LEVEL             = (Integer) values[6];
-        PROFILE_GWT               = (Boolean) values[7];
-        CUSTOMIZE_THRESHOLD       = (Integer) values[8];
-
-        if (CUSTOMIZE_THRESHOLD < -1 || CUSTOMIZE_THRESHOLD > 127) {
-            throw newInternalError("CUSTOMIZE_THRESHOLD should be in [-1...127] range");
-        }
-    }
-
-    /** Tell if any of the debugging switches are turned on.
-     *  If this is the case, it is reasonable to perform extra checks or save extra information.
-     */
-    /*non-public*/ static boolean debugEnabled() {
-        return (DEBUG_METHOD_HANDLE_NAMES |
-                DUMP_CLASS_FILES |
-                TRACE_INTERPRETER |
-                TRACE_METHOD_LINKAGE);
-    }
+    // Android-changed: Remove debugging related static fields. They are unused and have
+    // no equivalent on Android.
 
     // Android-changed: Temporarily hide methods that operate on MethodHandles until the
     // MethodHandle class is imported.
diff --git a/ojluni/src/main/java/java/lang/invoke/MethodHandles.java b/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
new file mode 100644
index 0000000..17adc59
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
@@ -0,0 +1,2539 @@
+/*
+ * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.lang.invoke;
+
+import java.lang.reflect.*;
+import java.util.List;
+import java.util.Arrays;
+
+import dalvik.system.VMStack;
+import sun.invoke.util.VerifyAccess;
+import sun.invoke.util.Wrapper;
+import static java.lang.invoke.MethodHandleStatics.*;
+
+/**
+ * This class consists exclusively of static methods that operate on or return
+ * method handles. They fall into several categories:
+ * <ul>
+ * <li>Lookup methods which help create method handles for methods and fields.
+ * <li>Combinator methods, which combine or transform pre-existing method handles into new ones.
+ * <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns.
+ * </ul>
+ * <p>
+ * @author John Rose, JSR 292 EG
+ * @since 1.7
+ */
+public class MethodHandles {
+
+    private MethodHandles() { }  // do not instantiate
+
+    // Android-changed: We do not use MemberName / MethodHandleImpl.
+    //
+    // private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
+    // static { MethodHandleImpl.initStatics(); }
+    // See IMPL_LOOKUP below.
+
+    //// Method handle creation from ordinary methods.
+
+    /**
+     * Returns a {@link Lookup lookup object} with
+     * full capabilities to emulate all supported bytecode behaviors of the caller.
+     * These capabilities include <a href="MethodHandles.Lookup.html#privacc">private access</a> to the caller.
+     * Factory methods on the lookup object can create
+     * <a href="MethodHandleInfo.html#directmh">direct method handles</a>
+     * for any member that the caller has access to via bytecodes,
+     * including protected and private fields and methods.
+     * This lookup object is a <em>capability</em> which may be delegated to trusted agents.
+     * Do not store it in place where untrusted code can access it.
+     * <p>
+     * This method is caller sensitive, which means that it may return different
+     * values to different callers.
+     * <p>
+     * For any given caller class {@code C}, the lookup object returned by this call
+     * has equivalent capabilities to any lookup object
+     * supplied by the JVM to the bootstrap method of an
+     * <a href="package-summary.html#indyinsn">invokedynamic instruction</a>
+     * executing in the same caller class {@code C}.
+     * @return a lookup object for the caller of this method, with private access
+     */
+    // Android-changed: Remove caller sensitive.
+    // @CallerSensitive
+    public static Lookup lookup() {
+        // Android-changed: Do not use Reflection.getCallerClass().
+        return new Lookup(VMStack.getStackClass1());
+    }
+
+    /**
+     * Returns a {@link Lookup lookup object} which is trusted minimally.
+     * It can only be used to create method handles to
+     * publicly accessible fields and methods.
+     * <p>
+     * As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
+     * of this lookup object will be {@link java.lang.Object}.
+     *
+     * <p style="font-size:smaller;">
+     * <em>Discussion:</em>
+     * The lookup class can be changed to any other class {@code C} using an expression of the form
+     * {@link Lookup#in publicLookup().in(C.class)}.
+     * Since all classes have equal access to public names,
+     * such a change would confer no new access rights.
+     * A public lookup object is always subject to
+     * <a href="MethodHandles.Lookup.html#secmgr">security manager checks</a>.
+     * Also, it cannot access
+     * <a href="MethodHandles.Lookup.html#callsens">caller sensitive methods</a>.
+     * @return a lookup object which is trusted minimally
+     */
+    public static Lookup publicLookup() {
+        return Lookup.PUBLIC_LOOKUP;
+    }
+
+    /**
+     * Performs an unchecked "crack" of a
+     * <a href="MethodHandleInfo.html#directmh">direct method handle</a>.
+     * The result is as if the user had obtained a lookup object capable enough
+     * to crack the target method handle, called
+     * {@link java.lang.invoke.MethodHandles.Lookup#revealDirect Lookup.revealDirect}
+     * on the target to obtain its symbolic reference, and then called
+     * {@link java.lang.invoke.MethodHandleInfo#reflectAs MethodHandleInfo.reflectAs}
+     * to resolve the symbolic reference to a member.
+     * <p>
+     * If there is a security manager, its {@code checkPermission} method
+     * is called with a {@code ReflectPermission("suppressAccessChecks")} permission.
+     * @param <T> the desired type of the result, either {@link Member} or a subtype
+     * @param target a direct method handle to crack into symbolic reference components
+     * @param expected a class object representing the desired result type {@code T}
+     * @return a reference to the method, constructor, or field object
+     * @exception SecurityException if the caller is not privileged to call {@code setAccessible}
+     * @exception NullPointerException if either argument is {@code null}
+     * @exception IllegalArgumentException if the target is not a direct method handle
+     * @exception ClassCastException if the member is not of the expected type
+     * @since 1.8
+     */
+    public static <T extends Member> T
+    reflectAs(Class<T> expected, MethodHandle target) {
+        SecurityManager smgr = System.getSecurityManager();
+        if (smgr != null)  smgr.checkPermission(ACCESS_PERMISSION);
+        Lookup lookup = Lookup.IMPL_LOOKUP;  // use maximally privileged lookup
+        return lookup.revealDirect(target).reflectAs(expected, lookup);
+    }
+    // Copied from AccessibleObject, as used by Method.setAccessible, etc.:
+    static final private java.security.Permission ACCESS_PERMISSION =
+        new ReflectPermission("suppressAccessChecks");
+
+    /**
+     * A <em>lookup object</em> is a factory for creating method handles,
+     * when the creation requires access checking.
+     * Method handles do not perform
+     * access checks when they are called, but rather when they are created.
+     * Therefore, method handle access
+     * restrictions must be enforced when a method handle is created.
+     * The caller class against which those restrictions are enforced
+     * is known as the {@linkplain #lookupClass lookup class}.
+     * <p>
+     * A lookup class which needs to create method handles will call
+     * {@link MethodHandles#lookup MethodHandles.lookup} to create a factory for itself.
+     * When the {@code Lookup} factory object is created, the identity of the lookup class is
+     * determined, and securely stored in the {@code Lookup} object.
+     * The lookup class (or its delegates) may then use factory methods
+     * on the {@code Lookup} object to create method handles for access-checked members.
+     * This includes all methods, constructors, and fields which are allowed to the lookup class,
+     * even private ones.
+     *
+     * <h1><a name="lookups"></a>Lookup Factory Methods</h1>
+     * The factory methods on a {@code Lookup} object correspond to all major
+     * use cases for methods, constructors, and fields.
+     * Each method handle created by a factory method is the functional
+     * equivalent of a particular <em>bytecode behavior</em>.
+     * (Bytecode behaviors are described in section 5.4.3.5 of the Java Virtual Machine Specification.)
+     * Here is a summary of the correspondence between these factory methods and
+     * the behavior the resulting method handles:
+     * <table border=1 cellpadding=5 summary="lookup method behaviors">
+     * <tr>
+     *     <th><a name="equiv"></a>lookup expression</th>
+     *     <th>member</th>
+     *     <th>bytecode behavior</th>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td>
+     *     <td>{@code FT f;}</td><td>{@code (T) this.f;}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</td>
+     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</td>
+     *     <td>{@code FT f;}</td><td>{@code this.f = x;}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</td>
+     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code C.f = arg;}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</td>
+     *     <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</td>
+     *     <td>{@code static}<br>{@code T m(A*);}</td><td>{@code (T) C.m(arg*);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</td>
+     *     <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</td>
+     *     <td>{@code C(A*);}</td><td>{@code new C(arg*);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</td>
+     *     <td>({@code static})?<br>{@code FT f;}</td><td>{@code (FT) aField.get(thisOrNull);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</td>
+     *     <td>({@code static})?<br>{@code FT f;}</td><td>{@code aField.set(thisOrNull, arg);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
+     *     <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</td>
+     *     <td>{@code C(A*);}</td><td>{@code (C) aConstructor.newInstance(arg*);}</td>
+     * </tr>
+     * <tr>
+     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
+     *     <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td>
+     * </tr>
+     * </table>
+     *
+     * Here, the type {@code C} is the class or interface being searched for a member,
+     * documented as a parameter named {@code refc} in the lookup methods.
+     * The method type {@code MT} is composed from the return type {@code T}
+     * and the sequence of argument types {@code A*}.
+     * The constructor also has a sequence of argument types {@code A*} and
+     * is deemed to return the newly-created object of type {@code C}.
+     * Both {@code MT} and the field type {@code FT} are documented as a parameter named {@code type}.
+     * The formal parameter {@code this} stands for the self-reference of type {@code C};
+     * if it is present, it is always the leading argument to the method handle invocation.
+     * (In the case of some {@code protected} members, {@code this} may be
+     * restricted in type to the lookup class; see below.)
+     * The name {@code arg} stands for all the other method handle arguments.
+     * In the code examples for the Core Reflection API, the name {@code thisOrNull}
+     * stands for a null reference if the accessed method or field is static,
+     * and {@code this} otherwise.
+     * The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
+     * for reflective objects corresponding to the given members.
+     * <p>
+     * In cases where the given member is of variable arity (i.e., a method or constructor)
+     * the returned method handle will also be of {@linkplain MethodHandle#asVarargsCollector variable arity}.
+     * In all other cases, the returned method handle will be of fixed arity.
+     * <p style="font-size:smaller;">
+     * <em>Discussion:</em>
+     * The equivalence between looked-up method handles and underlying
+     * class members and bytecode behaviors
+     * can break down in a few ways:
+     * <ul style="font-size:smaller;">
+     * <li>If {@code C} is not symbolically accessible from the lookup class's loader,
+     * the lookup can still succeed, even when there is no equivalent
+     * Java expression or bytecoded constant.
+     * <li>Likewise, if {@code T} or {@code MT}
+     * is not symbolically accessible from the lookup class's loader,
+     * the lookup can still succeed.
+     * For example, lookups for {@code MethodHandle.invokeExact} and
+     * {@code MethodHandle.invoke} will always succeed, regardless of requested type.
+     * <li>If there is a security manager installed, it can forbid the lookup
+     * on various grounds (<a href="MethodHandles.Lookup.html#secmgr">see below</a>).
+     * By contrast, the {@code ldc} instruction on a {@code CONSTANT_MethodHandle}
+     * constant is not subject to security manager checks.
+     * <li>If the looked-up method has a
+     * <a href="MethodHandle.html#maxarity">very large arity</a>,
+     * the method handle creation may fail, due to the method handle
+     * type having too many parameters.
+     * </ul>
+     *
+     * <h1><a name="access"></a>Access checking</h1>
+     * Access checks are applied in the factory methods of {@code Lookup},
+     * when a method handle is created.
+     * This is a key difference from the Core Reflection API, since
+     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
+     * performs access checking against every caller, on every call.
+     * <p>
+     * All access checks start from a {@code Lookup} object, which
+     * compares its recorded lookup class against all requests to
+     * create method handles.
+     * A single {@code Lookup} object can be used to create any number
+     * of access-checked method handles, all checked against a single
+     * lookup class.
+     * <p>
+     * A {@code Lookup} object can be shared with other trusted code,
+     * such as a metaobject protocol.
+     * A shared {@code Lookup} object delegates the capability
+     * to create method handles on private members of the lookup class.
+     * Even if privileged code uses the {@code Lookup} object,
+     * the access checking is confined to the privileges of the
+     * original lookup class.
+     * <p>
+     * A lookup can fail, because
+     * the containing class is not accessible to the lookup class, or
+     * because the desired class member is missing, or because the
+     * desired class member is not accessible to the lookup class, or
+     * because the lookup object is not trusted enough to access the member.
+     * In any of these cases, a {@code ReflectiveOperationException} will be
+     * thrown from the attempted lookup.  The exact class will be one of
+     * the following:
+     * <ul>
+     * <li>NoSuchMethodException &mdash; if a method is requested but does not exist
+     * <li>NoSuchFieldException &mdash; if a field is requested but does not exist
+     * <li>IllegalAccessException &mdash; if the member exists but an access check fails
+     * </ul>
+     * <p>
+     * In general, the conditions under which a method handle may be
+     * looked up for a method {@code M} are no more restrictive than the conditions
+     * under which the lookup class could have compiled, verified, and resolved a call to {@code M}.
+     * Where the JVM would raise exceptions like {@code NoSuchMethodError},
+     * a method handle lookup will generally raise a corresponding
+     * checked exception, such as {@code NoSuchMethodException}.
+     * And the effect of invoking the method handle resulting from the lookup
+     * is <a href="MethodHandles.Lookup.html#equiv">exactly equivalent</a>
+     * to executing the compiled, verified, and resolved call to {@code M}.
+     * The same point is true of fields and constructors.
+     * <p style="font-size:smaller;">
+     * <em>Discussion:</em>
+     * Access checks only apply to named and reflected methods,
+     * constructors, and fields.
+     * Other method handle creation methods, such as
+     * {@link MethodHandle#asType MethodHandle.asType},
+     * do not require any access checks, and are used
+     * independently of any {@code Lookup} object.
+     * <p>
+     * If the desired member is {@code protected}, the usual JVM rules apply,
+     * including the requirement that the lookup class must be either be in the
+     * same package as the desired member, or must inherit that member.
+     * (See the Java Virtual Machine Specification, sections 4.9.2, 5.4.3.5, and 6.4.)
+     * In addition, if the desired member is a non-static field or method
+     * in a different package, the resulting method handle may only be applied
+     * to objects of the lookup class or one of its subclasses.
+     * This requirement is enforced by narrowing the type of the leading
+     * {@code this} parameter from {@code C}
+     * (which will necessarily be a superclass of the lookup class)
+     * to the lookup class itself.
+     * <p>
+     * The JVM imposes a similar requirement on {@code invokespecial} instruction,
+     * that the receiver argument must match both the resolved method <em>and</em>
+     * the current class.  Again, this requirement is enforced by narrowing the
+     * type of the leading parameter to the resulting method handle.
+     * (See the Java Virtual Machine Specification, section 4.10.1.9.)
+     * <p>
+     * The JVM represents constructors and static initializer blocks as internal methods
+     * with special names ({@code "<init>"} and {@code "<clinit>"}).
+     * The internal syntax of invocation instructions allows them to refer to such internal
+     * methods as if they were normal methods, but the JVM bytecode verifier rejects them.
+     * A lookup of such an internal method will produce a {@code NoSuchMethodException}.
+     * <p>
+     * In some cases, access between nested classes is obtained by the Java compiler by creating
+     * an wrapper method to access a private method of another class
+     * in the same top-level declaration.
+     * For example, a nested class {@code C.D}
+     * can access private members within other related classes such as
+     * {@code C}, {@code C.D.E}, or {@code C.B},
+     * but the Java compiler may need to generate wrapper methods in
+     * those related classes.  In such cases, a {@code Lookup} object on
+     * {@code C.E} would be unable to those private members.
+     * A workaround for this limitation is the {@link Lookup#in Lookup.in} method,
+     * which can transform a lookup on {@code C.E} into one on any of those other
+     * classes, without special elevation of privilege.
+     * <p>
+     * The accesses permitted to a given lookup object may be limited,
+     * according to its set of {@link #lookupModes lookupModes},
+     * to a subset of members normally accessible to the lookup class.
+     * For example, the {@link MethodHandles#publicLookup publicLookup}
+     * method produces a lookup object which is only allowed to access
+     * public members in public classes.
+     * The caller sensitive method {@link MethodHandles#lookup lookup}
+     * produces a lookup object with full capabilities relative to
+     * its caller class, to emulate all supported bytecode behaviors.
+     * Also, the {@link Lookup#in Lookup.in} method may produce a lookup object
+     * with fewer access modes than the original lookup object.
+     *
+     * <p style="font-size:smaller;">
+     * <a name="privacc"></a>
+     * <em>Discussion of private access:</em>
+     * We say that a lookup has <em>private access</em>
+     * if its {@linkplain #lookupModes lookup modes}
+     * include the possibility of accessing {@code private} members.
+     * As documented in the relevant methods elsewhere,
+     * only lookups with private access possess the following capabilities:
+     * <ul style="font-size:smaller;">
+     * <li>access private fields, methods, and constructors of the lookup class
+     * <li>create method handles which invoke <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a> methods,
+     *     such as {@code Class.forName}
+     * <li>create method handles which {@link Lookup#findSpecial emulate invokespecial} instructions
+     * <li>avoid <a href="MethodHandles.Lookup.html#secmgr">package access checks</a>
+     *     for classes accessible to the lookup class
+     * <li>create {@link Lookup#in delegated lookup objects} which have private access to other classes
+     *     within the same package member
+     * </ul>
+     * <p style="font-size:smaller;">
+     * Each of these permissions is a consequence of the fact that a lookup object
+     * with private access can be securely traced back to an originating class,
+     * whose <a href="MethodHandles.Lookup.html#equiv">bytecode behaviors</a> and Java language access permissions
+     * can be reliably determined and emulated by method handles.
+     *
+     * <h1><a name="secmgr"></a>Security manager interactions</h1>
+     * Although bytecode instructions can only refer to classes in
+     * a related class loader, this API can search for methods in any
+     * class, as long as a reference to its {@code Class} object is
+     * available.  Such cross-loader references are also possible with the
+     * Core Reflection API, and are impossible to bytecode instructions
+     * such as {@code invokestatic} or {@code getfield}.
+     * There is a {@linkplain java.lang.SecurityManager security manager API}
+     * to allow applications to check such cross-loader references.
+     * These checks apply to both the {@code MethodHandles.Lookup} API
+     * and the Core Reflection API
+     * (as found on {@link java.lang.Class Class}).
+     * <p>
+     * If a security manager is present, member lookups are subject to
+     * additional checks.
+     * From one to three calls are made to the security manager.
+     * Any of these calls can refuse access by throwing a
+     * {@link java.lang.SecurityException SecurityException}.
+     * Define {@code smgr} as the security manager,
+     * {@code lookc} as the lookup class of the current lookup object,
+     * {@code refc} as the containing class in which the member
+     * is being sought, and {@code defc} as the class in which the
+     * member is actually defined.
+     * The value {@code lookc} is defined as <em>not present</em>
+     * if the current lookup object does not have
+     * <a href="MethodHandles.Lookup.html#privacc">private access</a>.
+     * The calls are made according to the following rules:
+     * <ul>
+     * <li><b>Step 1:</b>
+     *     If {@code lookc} is not present, or if its class loader is not
+     *     the same as or an ancestor of the class loader of {@code refc},
+     *     then {@link SecurityManager#checkPackageAccess
+     *     smgr.checkPackageAccess(refcPkg)} is called,
+     *     where {@code refcPkg} is the package of {@code refc}.
+     * <li><b>Step 2:</b>
+     *     If the retrieved member is not public and
+     *     {@code lookc} is not present, then
+     *     {@link SecurityManager#checkPermission smgr.checkPermission}
+     *     with {@code RuntimePermission("accessDeclaredMembers")} is called.
+     * <li><b>Step 3:</b>
+     *     If the retrieved member is not public,
+     *     and if {@code lookc} is not present,
+     *     and if {@code defc} and {@code refc} are different,
+     *     then {@link SecurityManager#checkPackageAccess
+     *     smgr.checkPackageAccess(defcPkg)} is called,
+     *     where {@code defcPkg} is the package of {@code defc}.
+     * </ul>
+     * Security checks are performed after other access checks have passed.
+     * Therefore, the above rules presuppose a member that is public,
+     * or else that is being accessed from a lookup class that has
+     * rights to access the member.
+     *
+     * <h1><a name="callsens"></a>Caller sensitive methods</h1>
+     * A small number of Java methods have a special property called caller sensitivity.
+     * A <em>caller-sensitive</em> method can behave differently depending on the
+     * identity of its immediate caller.
+     * <p>
+     * If a method handle for a caller-sensitive method is requested,
+     * the general rules for <a href="MethodHandles.Lookup.html#equiv">bytecode behaviors</a> apply,
+     * but they take account of the lookup class in a special way.
+     * The resulting method handle behaves as if it were called
+     * from an instruction contained in the lookup class,
+     * so that the caller-sensitive method detects the lookup class.
+     * (By contrast, the invoker of the method handle is disregarded.)
+     * Thus, in the case of caller-sensitive methods,
+     * different lookup classes may give rise to
+     * differently behaving method handles.
+     * <p>
+     * In cases where the lookup object is
+     * {@link MethodHandles#publicLookup() publicLookup()},
+     * or some other lookup object without
+     * <a href="MethodHandles.Lookup.html#privacc">private access</a>,
+     * the lookup class is disregarded.
+     * In such cases, no caller-sensitive method handle can be created,
+     * access is forbidden, and the lookup fails with an
+     * {@code IllegalAccessException}.
+     * <p style="font-size:smaller;">
+     * <em>Discussion:</em>
+     * For example, the caller-sensitive method
+     * {@link java.lang.Class#forName(String) Class.forName(x)}
+     * can return varying classes or throw varying exceptions,
+     * depending on the class loader of the class that calls it.
+     * A public lookup of {@code Class.forName} will fail, because
+     * there is no reasonable way to determine its bytecode behavior.
+     * <p style="font-size:smaller;">
+     * If an application caches method handles for broad sharing,
+     * it should use {@code publicLookup()} to create them.
+     * If there is a lookup of {@code Class.forName}, it will fail,
+     * and the application must take appropriate action in that case.
+     * It may be that a later lookup, perhaps during the invocation of a
+     * bootstrap method, can incorporate the specific identity
+     * of the caller, making the method accessible.
+     * <p style="font-size:smaller;">
+     * The function {@code MethodHandles.lookup} is caller sensitive
+     * so that there can be a secure foundation for lookups.
+     * Nearly all other methods in the JSR 292 API rely on lookup
+     * objects to check access requests.
+     */
+    public static final
+    class Lookup {
+        /** The class on behalf of whom the lookup is being performed. */
+        private final Class<?> lookupClass;
+
+        /** The allowed sorts of members which may be looked up (PUBLIC, etc.). */
+        private final int allowedModes;
+
+        /** A single-bit mask representing {@code public} access,
+         *  which may contribute to the result of {@link #lookupModes lookupModes}.
+         *  The value, {@code 0x01}, happens to be the same as the value of the
+         *  {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}.
+         */
+        public static final int PUBLIC = Modifier.PUBLIC;
+
+        /** A single-bit mask representing {@code private} access,
+         *  which may contribute to the result of {@link #lookupModes lookupModes}.
+         *  The value, {@code 0x02}, happens to be the same as the value of the
+         *  {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}.
+         */
+        public static final int PRIVATE = Modifier.PRIVATE;
+
+        /** A single-bit mask representing {@code protected} access,
+         *  which may contribute to the result of {@link #lookupModes lookupModes}.
+         *  The value, {@code 0x04}, happens to be the same as the value of the
+         *  {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}.
+         */
+        public static final int PROTECTED = Modifier.PROTECTED;
+
+        /** A single-bit mask representing {@code package} access (default access),
+         *  which may contribute to the result of {@link #lookupModes lookupModes}.
+         *  The value is {@code 0x08}, which does not correspond meaningfully to
+         *  any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
+         */
+        public static final int PACKAGE = Modifier.STATIC;
+
+        private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE);
+        private static final int TRUSTED   = -1;
+
+        private static int fixmods(int mods) {
+            mods &= (ALL_MODES - PACKAGE);
+            return (mods != 0) ? mods : PACKAGE;
+        }
+
+        /** Tells which class is performing the lookup.  It is this class against
+         *  which checks are performed for visibility and access permissions.
+         *  <p>
+         *  The class implies a maximum level of access permission,
+         *  but the permissions may be additionally limited by the bitmask
+         *  {@link #lookupModes lookupModes}, which controls whether non-public members
+         *  can be accessed.
+         *  @return the lookup class, on behalf of which this lookup object finds members
+         */
+        public Class<?> lookupClass() {
+            return lookupClass;
+        }
+
+        // This is just for calling out to MethodHandleImpl.
+        private Class<?> lookupClassOrNull() {
+            return (allowedModes == TRUSTED) ? null : lookupClass;
+        }
+
+        /** Tells which access-protection classes of members this lookup object can produce.
+         *  The result is a bit-mask of the bits
+         *  {@linkplain #PUBLIC PUBLIC (0x01)},
+         *  {@linkplain #PRIVATE PRIVATE (0x02)},
+         *  {@linkplain #PROTECTED PROTECTED (0x04)},
+         *  and {@linkplain #PACKAGE PACKAGE (0x08)}.
+         *  <p>
+         *  A freshly-created lookup object
+         *  on the {@linkplain java.lang.invoke.MethodHandles#lookup() caller's class}
+         *  has all possible bits set, since the caller class can access all its own members.
+         *  A lookup object on a new lookup class
+         *  {@linkplain java.lang.invoke.MethodHandles.Lookup#in created from a previous lookup object}
+         *  may have some mode bits set to zero.
+         *  The purpose of this is to restrict access via the new lookup object,
+         *  so that it can access only names which can be reached by the original
+         *  lookup object, and also by the new lookup class.
+         *  @return the lookup modes, which limit the kinds of access performed by this lookup object
+         */
+        public int lookupModes() {
+            return allowedModes & ALL_MODES;
+        }
+
+        /** Embody the current class (the lookupClass) as a lookup class
+         * for method handle creation.
+         * Must be called by from a method in this package,
+         * which in turn is called by a method not in this package.
+         */
+        Lookup(Class<?> lookupClass) {
+            this(lookupClass, ALL_MODES);
+            // make sure we haven't accidentally picked up a privileged class:
+            checkUnprivilegedlookupClass(lookupClass, ALL_MODES);
+        }
+
+        private Lookup(Class<?> lookupClass, int allowedModes) {
+            this.lookupClass = lookupClass;
+            this.allowedModes = allowedModes;
+        }
+
+        /**
+         * Creates a lookup on the specified new lookup class.
+         * The resulting object will report the specified
+         * class as its own {@link #lookupClass lookupClass}.
+         * <p>
+         * However, the resulting {@code Lookup} object is guaranteed
+         * to have no more access capabilities than the original.
+         * In particular, access capabilities can be lost as follows:<ul>
+         * <li>If the new lookup class differs from the old one,
+         * protected members will not be accessible by virtue of inheritance.
+         * (Protected members may continue to be accessible because of package sharing.)
+         * <li>If the new lookup class is in a different package
+         * than the old one, protected and default (package) members will not be accessible.
+         * <li>If the new lookup class is not within the same package member
+         * as the old one, private members will not be accessible.
+         * <li>If the new lookup class is not accessible to the old lookup class,
+         * then no members, not even public members, will be accessible.
+         * (In all other cases, public members will continue to be accessible.)
+         * </ul>
+         *
+         * @param requestedLookupClass the desired lookup class for the new lookup object
+         * @return a lookup object which reports the desired lookup class
+         * @throws NullPointerException if the argument is null
+         */
+        public Lookup in(Class<?> requestedLookupClass) {
+            requestedLookupClass.getClass();  // null check
+            if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
+                return new Lookup(requestedLookupClass, ALL_MODES);
+            if (requestedLookupClass == this.lookupClass)
+                return this;  // keep same capabilities
+            int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
+            if ((newModes & PACKAGE) != 0
+                && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
+                newModes &= ~(PACKAGE|PRIVATE);
+            }
+            // Allow nestmate lookups to be created without special privilege:
+            if ((newModes & PRIVATE) != 0
+                && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
+                newModes &= ~PRIVATE;
+            }
+            if ((newModes & PUBLIC) != 0
+                && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
+                // The requested class it not accessible from the lookup class.
+                // No permissions.
+                newModes = 0;
+            }
+            checkUnprivilegedlookupClass(requestedLookupClass, newModes);
+            return new Lookup(requestedLookupClass, newModes);
+        }
+
+        // Make sure outer class is initialized first.
+        //
+        // Android-changed: Removed unnecessary reference to IMPL_NAMES.
+        // static { IMPL_NAMES.getClass(); }
+
+        /** Version of lookup which is trusted minimally.
+         *  It can only be used to create method handles to
+         *  publicly accessible members.
+         */
+        static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, PUBLIC);
+
+        /** Package-private version of lookup which is trusted. */
+        static final Lookup IMPL_LOOKUP = new Lookup(Object.class, TRUSTED);
+
+        private static void checkUnprivilegedlookupClass(Class<?> lookupClass, int allowedModes) {
+            String name = lookupClass.getName();
+            if (name.startsWith("java.lang.invoke."))
+                throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
+
+            // For caller-sensitive MethodHandles.lookup()
+            // disallow lookup more restricted packages
+            //
+            // Android-changed: The bootstrap classloader isn't null.
+            if (allowedModes == ALL_MODES &&
+                    lookupClass.getClassLoader() == Object.class.getClassLoader()) {
+                if (name.startsWith("java.") ||
+                        (name.startsWith("sun.") && !name.startsWith("sun.invoke."))) {
+                    throw newIllegalArgumentException("illegal lookupClass: " + lookupClass);
+                }
+            }
+        }
+
+        /**
+         * Displays the name of the class from which lookups are to be made.
+         * (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.)
+         * If there are restrictions on the access permitted to this lookup,
+         * this is indicated by adding a suffix to the class name, consisting
+         * of a slash and a keyword.  The keyword represents the strongest
+         * allowed access, and is chosen as follows:
+         * <ul>
+         * <li>If no access is allowed, the suffix is "/noaccess".
+         * <li>If only public access is allowed, the suffix is "/public".
+         * <li>If only public and package access are allowed, the suffix is "/package".
+         * <li>If only public, package, and private access are allowed, the suffix is "/private".
+         * </ul>
+         * If none of the above cases apply, it is the case that full
+         * access (public, package, private, and protected) is allowed.
+         * In this case, no suffix is added.
+         * This is true only of an object obtained originally from
+         * {@link java.lang.invoke.MethodHandles#lookup MethodHandles.lookup}.
+         * Objects created by {@link java.lang.invoke.MethodHandles.Lookup#in Lookup.in}
+         * always have restricted access, and will display a suffix.
+         * <p>
+         * (It may seem strange that protected access should be
+         * stronger than private access.  Viewed independently from
+         * package access, protected access is the first to be lost,
+         * because it requires a direct subclass relationship between
+         * caller and callee.)
+         * @see #in
+         */
+        @Override
+        public String toString() {
+            String cname = lookupClass.getName();
+            switch (allowedModes) {
+            case 0:  // no privileges
+                return cname + "/noaccess";
+            case PUBLIC:
+                return cname + "/public";
+            case PUBLIC|PACKAGE:
+                return cname + "/package";
+            case ALL_MODES & ~PROTECTED:
+                return cname + "/private";
+            case ALL_MODES:
+                return cname;
+            case TRUSTED:
+                return "/trusted";  // internal only; not exported
+            default:  // Should not happen, but it's a bitfield...
+                cname = cname + "/" + Integer.toHexString(allowedModes);
+                assert(false) : cname;
+                return cname;
+            }
+        }
+
+        /**
+         * Produces a method handle for a static method.
+         * The type of the method handle will be that of the method.
+         * (Since static methods do not take receivers, there is no
+         * additional receiver argument inserted into the method handle type,
+         * as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
+         * The method and all its argument types must be accessible to the lookup object.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the method's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If the returned method handle is invoked, the method's class will
+         * be initialized, if it has not already been initialized.
+         * <p><b>Example:</b>
+         * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle MH_asList = publicLookup().findStatic(Arrays.class,
+  "asList", methodType(List.class, Object[].class));
+assertEquals("[x, y]", MH_asList.invoke("x", "y").toString());
+         * }</pre></blockquote>
+         * @param refc the class from which the method is accessed
+         * @param name the name of the method
+         * @param type the type of the method
+         * @return the desired method handle
+         * @throws NoSuchMethodException if the method does not exist
+         * @throws IllegalAccessException if access checking fails,
+         *                                or if the method is not {@code static},
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public
+        MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findStatic is not implemented");
+        }
+
+        /**
+         * Produces a method handle for a virtual method.
+         * The type of the method handle will be that of the method,
+         * with the receiver type (usually {@code refc}) prepended.
+         * The method and all its argument types must be accessible to the lookup object.
+         * <p>
+         * When called, the handle will treat the first argument as a receiver
+         * and dispatch on the receiver's type to determine which method
+         * implementation to enter.
+         * (The dispatching action is identical with that performed by an
+         * {@code invokevirtual} or {@code invokeinterface} instruction.)
+         * <p>
+         * The first argument will be of type {@code refc} if the lookup
+         * class has full privileges to access the member.  Otherwise
+         * the member must be {@code protected} and the first argument
+         * will be restricted in type to the lookup class.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the method's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * Because of the general <a href="MethodHandles.Lookup.html#equiv">equivalence</a> between {@code invokevirtual}
+         * instructions and method handles produced by {@code findVirtual},
+         * if the class is {@code MethodHandle} and the name string is
+         * {@code invokeExact} or {@code invoke}, the resulting
+         * method handle is equivalent to one produced by
+         * {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or
+         * {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}
+         * with the same {@code type} argument.
+         *
+         * <b>Example:</b>
+         * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle MH_concat = publicLookup().findVirtual(String.class,
+  "concat", methodType(String.class, String.class));
+MethodHandle MH_hashCode = publicLookup().findVirtual(Object.class,
+  "hashCode", methodType(int.class));
+MethodHandle MH_hashCode_String = publicLookup().findVirtual(String.class,
+  "hashCode", methodType(int.class));
+assertEquals("xy", (String) MH_concat.invokeExact("x", "y"));
+assertEquals("xy".hashCode(), (int) MH_hashCode.invokeExact((Object)"xy"));
+assertEquals("xy".hashCode(), (int) MH_hashCode_String.invokeExact("xy"));
+// interface method:
+MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
+  "subSequence", methodType(CharSequence.class, int.class, int.class));
+assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
+// constructor "internal method" must be accessed differently:
+MethodType MT_newString = methodType(void.class); //()V for new String()
+try { assertEquals("impossible", lookup()
+        .findVirtual(String.class, "<init>", MT_newString));
+ } catch (NoSuchMethodException ex) { } // OK
+MethodHandle MH_newString = publicLookup()
+  .findConstructor(String.class, MT_newString);
+assertEquals("", (String) MH_newString.invokeExact());
+         * }</pre></blockquote>
+         *
+         * @param refc the class or interface from which the method is accessed
+         * @param name the name of the method
+         * @param type the type of the method, with the receiver argument omitted
+         * @return the desired method handle
+         * @throws NoSuchMethodException if the method does not exist
+         * @throws IllegalAccessException if access checking fails,
+         *                                or if the method is {@code static}
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findVirtual is not implemented");
+        }
+
+        /**
+         * Produces a method handle which creates an object and initializes it, using
+         * the constructor of the specified type.
+         * The parameter types of the method handle will be those of the constructor,
+         * while the return type will be a reference to the constructor's class.
+         * The constructor and all its argument types must be accessible to the lookup object.
+         * <p>
+         * The requested type must have a return type of {@code void}.
+         * (This is consistent with the JVM's treatment of constructor type descriptors.)
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If the returned method handle is invoked, the constructor's class will
+         * be initialized, if it has not already been initialized.
+         * <p><b>Example:</b>
+         * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle MH_newArrayList = publicLookup().findConstructor(
+  ArrayList.class, methodType(void.class, Collection.class));
+Collection orig = Arrays.asList("x", "y");
+Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig);
+assert(orig != copy);
+assertEquals(orig, copy);
+// a variable-arity constructor:
+MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
+  ProcessBuilder.class, methodType(void.class, String[].class));
+ProcessBuilder pb = (ProcessBuilder)
+  MH_newProcessBuilder.invoke("x", "y", "z");
+assertEquals("[x, y, z]", pb.command().toString());
+         * }</pre></blockquote>
+         * @param refc the class or interface from which the method is accessed
+         * @param type the type of the method, with the receiver argument omitted, and a void return type
+         * @return the desired method handle
+         * @throws NoSuchMethodException if the constructor does not exist
+         * @throws IllegalAccessException if access checking fails
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findConstructor is not implemented");
+        }
+
+        /**
+         * Produces an early-bound method handle for a virtual method.
+         * It will bypass checks for overriding methods on the receiver,
+         * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
+         * instruction from within the explicitly specified {@code specialCaller}.
+         * The type of the method handle will be that of the method,
+         * with a suitably restricted receiver type prepended.
+         * (The receiver type will be {@code specialCaller} or a subtype.)
+         * The method and all its argument types must be accessible
+         * to the lookup object.
+         * <p>
+         * Before method resolution,
+         * if the explicitly specified caller class is not identical with the
+         * lookup class, or if this lookup object does not have
+         * <a href="MethodHandles.Lookup.html#privacc">private access</a>
+         * privileges, the access fails.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the method's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p style="font-size:smaller;">
+         * <em>(Note:  JVM internal methods named {@code "<init>"} are not visible to this API,
+         * even though the {@code invokespecial} instruction can refer to them
+         * in special circumstances.  Use {@link #findConstructor findConstructor}
+         * to access instance initialization methods in a safe manner.)</em>
+         * <p><b>Example:</b>
+         * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+static class Listie extends ArrayList {
+  public String toString() { return "[wee Listie]"; }
+  static Lookup lookup() { return MethodHandles.lookup(); }
+}
+...
+// no access to constructor via invokeSpecial:
+MethodHandle MH_newListie = Listie.lookup()
+  .findConstructor(Listie.class, methodType(void.class));
+Listie l = (Listie) MH_newListie.invokeExact();
+try { assertEquals("impossible", Listie.lookup().findSpecial(
+        Listie.class, "<init>", methodType(void.class), Listie.class));
+ } catch (NoSuchMethodException ex) { } // OK
+// access to super and self methods via invokeSpecial:
+MethodHandle MH_super = Listie.lookup().findSpecial(
+  ArrayList.class, "toString" , methodType(String.class), Listie.class);
+MethodHandle MH_this = Listie.lookup().findSpecial(
+  Listie.class, "toString" , methodType(String.class), Listie.class);
+MethodHandle MH_duper = Listie.lookup().findSpecial(
+  Object.class, "toString" , methodType(String.class), Listie.class);
+assertEquals("[]", (String) MH_super.invokeExact(l));
+assertEquals(""+l, (String) MH_this.invokeExact(l));
+assertEquals("[]", (String) MH_duper.invokeExact(l)); // ArrayList method
+try { assertEquals("inaccessible", Listie.lookup().findSpecial(
+        String.class, "toString", methodType(String.class), Listie.class));
+ } catch (IllegalAccessException ex) { } // OK
+Listie subl = new Listie() { public String toString() { return "[subclass]"; } };
+assertEquals(""+l, (String) MH_this.invokeExact(subl)); // Listie method
+         * }</pre></blockquote>
+         *
+         * @param refc the class or interface from which the method is accessed
+         * @param name the name of the method (which must not be "&lt;init&gt;")
+         * @param type the type of the method, with the receiver argument omitted
+         * @param specialCaller the proposed calling class to perform the {@code invokespecial}
+         * @return the desired method handle
+         * @throws NoSuchMethodException if the method does not exist
+         * @throws IllegalAccessException if access checking fails
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findSpecial(Class<?> refc, String name, MethodType type,
+                                        Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findSpecial is not implemented");
+        }
+
+        /**
+         * Produces a method handle giving read access to a non-static field.
+         * The type of the method handle will have a return type of the field's
+         * value type.
+         * The method handle's single argument will be the instance containing
+         * the field.
+         * Access checking is performed immediately on behalf of the lookup class.
+         * @param refc the class or interface from which the method is accessed
+         * @param name the field's name
+         * @param type the field's type
+         * @return a method handle which can load values from the field
+         * @throws NoSuchFieldException if the field does not exist
+         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findGetter is not implemented");
+        }
+
+        /**
+         * Produces a method handle giving write access to a non-static field.
+         * The type of the method handle will have a void return type.
+         * The method handle will take two arguments, the instance containing
+         * the field, and the value to be stored.
+         * The second argument will be of the field's value type.
+         * Access checking is performed immediately on behalf of the lookup class.
+         * @param refc the class or interface from which the method is accessed
+         * @param name the field's name
+         * @param type the field's type
+         * @return a method handle which can store values into the field
+         * @throws NoSuchFieldException if the field does not exist
+         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findSetter is not implemented");
+        }
+
+        /**
+         * Produces a method handle giving read access to a static field.
+         * The type of the method handle will have a return type of the field's
+         * value type.
+         * The method handle will take no arguments.
+         * Access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
+         * @param refc the class or interface from which the method is accessed
+         * @param name the field's name
+         * @param type the field's type
+         * @return a method handle which can load values from the field
+         * @throws NoSuchFieldException if the field does not exist
+         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findStaticGetter is not implemented");
+        }
+
+        /**
+         * Produces a method handle giving write access to a static field.
+         * The type of the method handle will have a void return type.
+         * The method handle will take a single
+         * argument, of the field's value type, the value to be stored.
+         * Access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
+         * @param refc the class or interface from which the method is accessed
+         * @param name the field's name
+         * @param type the field's type
+         * @return a method handle which can store values into the field
+         * @throws NoSuchFieldException if the field does not exist
+         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.findStaticSetter is not implemented");
+        }
+
+        /**
+         * Produces an early-bound method handle for a non-static method.
+         * The receiver must have a supertype {@code defc} in which a method
+         * of the given name and type is accessible to the lookup class.
+         * The method and all its argument types must be accessible to the lookup object.
+         * The type of the method handle will be that of the method,
+         * without any insertion of an additional receiver parameter.
+         * The given receiver will be bound into the method handle,
+         * so that every call to the method handle will invoke the
+         * requested method on the given receiver.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the method's variable arity modifier bit ({@code 0x0080}) is set
+         * <em>and</em> the trailing array argument is not the only argument.
+         * (If the trailing array argument is the only argument,
+         * the given receiver value will be bound to it.)
+         * <p>
+         * This is equivalent to the following code:
+         * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle mh0 = lookup().findVirtual(defc, name, type);
+MethodHandle mh1 = mh0.bindTo(receiver);
+MethodType mt1 = mh1.type();
+if (mh0.isVarargsCollector())
+  mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
+return mh1;
+         * }</pre></blockquote>
+         * where {@code defc} is either {@code receiver.getClass()} or a super
+         * type of that class, in which the requested method is accessible
+         * to the lookup class.
+         * (Note that {@code bindTo} does not preserve variable arity.)
+         * @param receiver the object from which the method is accessed
+         * @param name the name of the method
+         * @param type the type of the method, with the receiver argument omitted
+         * @return the desired method handle
+         * @throws NoSuchMethodException if the method does not exist
+         * @throws IllegalAccessException if access checking fails
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws NullPointerException if any argument is null
+         * @see MethodHandle#bindTo
+         * @see #findVirtual
+         */
+        public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.bind is not implemented");
+        }
+
+        /**
+         * Makes a <a href="MethodHandleInfo.html#directmh">direct method handle</a>
+         * to <i>m</i>, if the lookup class has permission.
+         * If <i>m</i> is non-static, the receiver argument is treated as an initial argument.
+         * If <i>m</i> is virtual, overriding is respected on every call.
+         * Unlike the Core Reflection API, exceptions are <em>not</em> wrapped.
+         * The type of the method handle will be that of the method,
+         * with the receiver type prepended (but only if it is non-static).
+         * If the method's {@code accessible} flag is not set,
+         * access checking is performed immediately on behalf of the lookup class.
+         * If <i>m</i> is not public, do not share the resulting handle with untrusted parties.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the method's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If <i>m</i> is static, and
+         * if the returned method handle is invoked, the method's class will
+         * be initialized, if it has not already been initialized.
+         * @param m the reflected method
+         * @return a method handle which can invoke the reflected method
+         * @throws IllegalAccessException if access checking fails
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @throws NullPointerException if the argument is null
+         */
+        public MethodHandle unreflect(Method m) throws IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.unreflect is not implemented");
+        }
+
+        /**
+         * Produces a method handle for a reflected method.
+         * It will bypass checks for overriding methods on the receiver,
+         * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
+         * instruction from within the explicitly specified {@code specialCaller}.
+         * The type of the method handle will be that of the method,
+         * with a suitably restricted receiver type prepended.
+         * (The receiver type will be {@code specialCaller} or a subtype.)
+         * If the method's {@code accessible} flag is not set,
+         * access checking is performed immediately on behalf of the lookup class,
+         * as if {@code invokespecial} instruction were being linked.
+         * <p>
+         * Before method resolution,
+         * if the explicitly specified caller class is not identical with the
+         * lookup class, or if this lookup object does not have
+         * <a href="MethodHandles.Lookup.html#privacc">private access</a>
+         * privileges, the access fails.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the method's variable arity modifier bit ({@code 0x0080}) is set.
+         * @param m the reflected method
+         * @param specialCaller the class nominally calling the method
+         * @return a method handle which can invoke the reflected method
+         * @throws IllegalAccessException if access checking fails
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @throws NullPointerException if any argument is null
+         */
+        public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.unreflectSpecial is not implemented");
+        }
+
+        /**
+         * Produces a method handle for a reflected constructor.
+         * The type of the method handle will be that of the constructor,
+         * with the return type changed to the declaring class.
+         * The method handle will perform a {@code newInstance} operation,
+         * creating a new instance of the constructor's class on the
+         * arguments passed to the method handle.
+         * <p>
+         * If the constructor's {@code accessible} flag is not set,
+         * access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * The returned method handle will have
+         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
+         * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If the returned method handle is invoked, the constructor's class will
+         * be initialized, if it has not already been initialized.
+         * @param c the reflected constructor
+         * @return a method handle which can invoke the reflected constructor
+         * @throws IllegalAccessException if access checking fails
+         *                                or if the method's variable arity modifier bit
+         *                                is set and {@code asVarargsCollector} fails
+         * @throws NullPointerException if the argument is null
+         */
+        public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.unreflectConstructor is not implemented");
+        }
+
+        /**
+         * Produces a method handle giving read access to a reflected field.
+         * The type of the method handle will have a return type of the field's
+         * value type.
+         * If the field is static, the method handle will take no arguments.
+         * Otherwise, its single argument will be the instance containing
+         * the field.
+         * If the field's {@code accessible} flag is not set,
+         * access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the field is static, and
+         * if the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
+         * @param f the reflected field
+         * @return a method handle which can load values from the reflected field
+         * @throws IllegalAccessException if access checking fails
+         * @throws NullPointerException if the argument is null
+         */
+        public MethodHandle unreflectGetter(Field f) throws IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.unreflectGetter is not implemented");
+        }
+
+        /**
+         * Produces a method handle giving write access to a reflected field.
+         * The type of the method handle will have a void return type.
+         * If the field is static, the method handle will take a single
+         * argument, of the field's value type, the value to be stored.
+         * Otherwise, the two arguments will be the instance containing
+         * the field, and the value to be stored.
+         * If the field's {@code accessible} flag is not set,
+         * access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the field is static, and
+         * if the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
+         * @param f the reflected field
+         * @return a method handle which can store values into the reflected field
+         * @throws IllegalAccessException if access checking fails
+         * @throws NullPointerException if the argument is null
+         */
+        public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
+            // TODO(narayan): Implement this method.
+            throw new UnsupportedOperationException("MethodHandles.Lookup.unreflectSetter is not implemented");
+        }
+
+        /**
+         * Cracks a <a href="MethodHandleInfo.html#directmh">direct method handle</a>
+         * created by this lookup object or a similar one.
+         * Security and access checks are performed to ensure that this lookup object
+         * is capable of reproducing the target method handle.
+         * This means that the cracking may fail if target is a direct method handle
+         * but was created by an unrelated lookup object.
+         * This can happen if the method handle is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>
+         * and was created by a lookup object for a different class.
+         * @param target a direct method handle to crack into symbolic reference components
+         * @return a symbolic reference which can be used to reconstruct this method handle from this lookup object
+         * @exception SecurityException if a security manager is present and it
+         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
+         * @throws IllegalArgumentException if the target is not a direct method handle or if access checking fails
+         * @exception NullPointerException if the target is {@code null}
+         * @see MethodHandleInfo
+         * @since 1.8
+         */
+        public MethodHandleInfo revealDirect(MethodHandle target) {
+            // TODO(narayan): Implement this method.
+            //
+            // Notes: Only works for direct method handles. Must check access.
+            //
+            throw new UnsupportedOperationException("MethodHandles.Lookup.revealDirect is not implemented");
+        }
+    }
+
+    /**
+     * Produces a method handle giving read access to elements of an array.
+     * The type of the method handle will have a return type of the array's
+     * element type.  Its first argument will be the array type,
+     * and the second will be {@code int}.
+     * @param arrayClass an array type
+     * @return a method handle which can load values from the given array type
+     * @throws NullPointerException if the argument is null
+     * @throws  IllegalArgumentException if arrayClass is not an array type
+     */
+    public static
+    MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException {
+        // return MethodHandleImpl.makeArrayElementAccessor(arrayClass, false);
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.arrayElementGetter is not implemented");
+    }
+
+    /**
+     * Produces a method handle giving write access to elements of an array.
+     * The type of the method handle will have a void return type.
+     * Its last argument will be the array's element type.
+     * The first and second arguments will be the array type and int.
+     * @param arrayClass the class of an array
+     * @return a method handle which can store values into the array type
+     * @throws NullPointerException if the argument is null
+     * @throws IllegalArgumentException if arrayClass is not an array type
+     */
+    public static
+    MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException {
+        // return MethodHandleImpl.makeArrayElementAccessor(arrayClass, true);
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.arrayElementSetter is not implemented");
+    }
+
+    /// method handle invocation (reflective style)
+
+    /**
+     * Produces a method handle which will invoke any method handle of the
+     * given {@code type}, with a given number of trailing arguments replaced by
+     * a single trailing {@code Object[]} array.
+     * The resulting invoker will be a method handle with the following
+     * arguments:
+     * <ul>
+     * <li>a single {@code MethodHandle} target
+     * <li>zero or more leading values (counted by {@code leadingArgCount})
+     * <li>an {@code Object[]} array containing trailing arguments
+     * </ul>
+     * <p>
+     * The invoker will invoke its target like a call to {@link MethodHandle#invoke invoke} with
+     * the indicated {@code type}.
+     * That is, if the target is exactly of the given {@code type}, it will behave
+     * like {@code invokeExact}; otherwise it behave as if {@link MethodHandle#asType asType}
+     * is used to convert the target to the required {@code type}.
+     * <p>
+     * The type of the returned invoker will not be the given {@code type}, but rather
+     * will have all parameters except the first {@code leadingArgCount}
+     * replaced by a single array of type {@code Object[]}, which will be
+     * the final parameter.
+     * <p>
+     * Before invoking its target, the invoker will spread the final array, apply
+     * reference casts as necessary, and unbox and widen primitive arguments.
+     * If, when the invoker is called, the supplied array argument does
+     * not have the correct number of elements, the invoker will throw
+     * an {@link IllegalArgumentException} instead of invoking the target.
+     * <p>
+     * This method is equivalent to the following code (though it may be more efficient):
+     * <blockquote><pre>{@code
+MethodHandle invoker = MethodHandles.invoker(type);
+int spreadArgCount = type.parameterCount() - leadingArgCount;
+invoker = invoker.asSpreader(Object[].class, spreadArgCount);
+return invoker;
+     * }</pre></blockquote>
+     * This method throws no reflective or security exceptions.
+     * @param type the desired target type
+     * @param leadingArgCount number of fixed arguments, to be passed unchanged to the target
+     * @return a method handle suitable for invoking any method handle of the given type
+     * @throws NullPointerException if {@code type} is null
+     * @throws IllegalArgumentException if {@code leadingArgCount} is not in
+     *                  the range from 0 to {@code type.parameterCount()} inclusive,
+     *                  or if the resulting method handle's type would have
+     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
+     */
+    static public
+    MethodHandle spreadInvoker(MethodType type, int leadingArgCount) {
+        if (leadingArgCount < 0 || leadingArgCount > type.parameterCount())
+            throw newIllegalArgumentException("bad argument count", leadingArgCount);
+        type = type.asSpreaderType(Object[].class, type.parameterCount() - leadingArgCount);
+        // return type.invokers().spreadInvoker(leadingArgCount);
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.spreadInvoker is not implemented");
+    }
+
+    /**
+     * Produces a special <em>invoker method handle</em> which can be used to
+     * invoke any method handle of the given type, as if by {@link MethodHandle#invokeExact invokeExact}.
+     * The resulting invoker will have a type which is
+     * exactly equal to the desired type, except that it will accept
+     * an additional leading argument of type {@code MethodHandle}.
+     * <p>
+     * This method is equivalent to the following code (though it may be more efficient):
+     * {@code publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)}
+     *
+     * <p style="font-size:smaller;">
+     * <em>Discussion:</em>
+     * Invoker method handles can be useful when working with variable method handles
+     * of unknown types.
+     * For example, to emulate an {@code invokeExact} call to a variable method
+     * handle {@code M}, extract its type {@code T},
+     * look up the invoker method {@code X} for {@code T},
+     * and call the invoker method, as {@code X.invoke(T, A...)}.
+     * (It would not work to call {@code X.invokeExact}, since the type {@code T}
+     * is unknown.)
+     * If spreading, collecting, or other argument transformations are required,
+     * they can be applied once to the invoker {@code X} and reused on many {@code M}
+     * method handle values, as long as they are compatible with the type of {@code X}.
+     * <p style="font-size:smaller;">
+     * <em>(Note:  The invoker method is not available via the Core Reflection API.
+     * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
+     * on the declared {@code invokeExact} or {@code invoke} method will raise an
+     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
+     * <p>
+     * This method throws no reflective or security exceptions.
+     * @param type the desired target type
+     * @return a method handle suitable for invoking any method handle of the given type
+     * @throws IllegalArgumentException if the resulting method handle's type would have
+     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
+     */
+    static public
+    MethodHandle exactInvoker(MethodType type) {
+        // return type.invokers().exactInvoker();
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.exactInvoker is not implemented");
+    }
+
+    /**
+     * Produces a special <em>invoker method handle</em> which can be used to
+     * invoke any method handle compatible with the given type, as if by {@link MethodHandle#invoke invoke}.
+     * The resulting invoker will have a type which is
+     * exactly equal to the desired type, except that it will accept
+     * an additional leading argument of type {@code MethodHandle}.
+     * <p>
+     * Before invoking its target, if the target differs from the expected type,
+     * the invoker will apply reference casts as
+     * necessary and box, unbox, or widen primitive values, as if by {@link MethodHandle#asType asType}.
+     * Similarly, the return value will be converted as necessary.
+     * If the target is a {@linkplain MethodHandle#asVarargsCollector variable arity method handle},
+     * the required arity conversion will be made, again as if by {@link MethodHandle#asType asType}.
+     * <p>
+     * This method is equivalent to the following code (though it may be more efficient):
+     * {@code publicLookup().findVirtual(MethodHandle.class, "invoke", type)}
+     * <p style="font-size:smaller;">
+     * <em>Discussion:</em>
+     * A {@linkplain MethodType#genericMethodType general method type} is one which
+     * mentions only {@code Object} arguments and return values.
+     * An invoker for such a type is capable of calling any method handle
+     * of the same arity as the general type.
+     * <p style="font-size:smaller;">
+     * <em>(Note:  The invoker method is not available via the Core Reflection API.
+     * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
+     * on the declared {@code invokeExact} or {@code invoke} method will raise an
+     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
+     * <p>
+     * This method throws no reflective or security exceptions.
+     * @param type the desired target type
+     * @return a method handle suitable for invoking any method handle convertible to the given type
+     * @throws IllegalArgumentException if the resulting method handle's type would have
+     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
+     */
+    static public
+    MethodHandle invoker(MethodType type) {
+        // return type.invokers().genericInvoker();
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.invoker is not implemented");
+    }
+
+    // Android-changed: Basic invokers are not supported.
+    //
+    // static /*non-public*/
+    // MethodHandle basicInvoker(MethodType type) {
+    //     return type.invokers().basicInvoker();
+    // }
+
+     /// method handle modification (creation from other method handles)
+
+    /**
+     * Produces a method handle which adapts the type of the
+     * given method handle to a new type by pairwise argument and return type conversion.
+     * The original type and new type must have the same number of arguments.
+     * The resulting method handle is guaranteed to report a type
+     * which is equal to the desired new type.
+     * <p>
+     * If the original type and new type are equal, returns target.
+     * <p>
+     * The same conversions are allowed as for {@link MethodHandle#asType MethodHandle.asType},
+     * and some additional conversions are also applied if those conversions fail.
+     * Given types <em>T0</em>, <em>T1</em>, one of the following conversions is applied
+     * if possible, before or instead of any conversions done by {@code asType}:
+     * <ul>
+     * <li>If <em>T0</em> and <em>T1</em> are references, and <em>T1</em> is an interface type,
+     *     then the value of type <em>T0</em> is passed as a <em>T1</em> without a cast.
+     *     (This treatment of interfaces follows the usage of the bytecode verifier.)
+     * <li>If <em>T0</em> is boolean and <em>T1</em> is another primitive,
+     *     the boolean is converted to a byte value, 1 for true, 0 for false.
+     *     (This treatment follows the usage of the bytecode verifier.)
+     * <li>If <em>T1</em> is boolean and <em>T0</em> is another primitive,
+     *     <em>T0</em> is converted to byte via Java casting conversion (JLS 5.5),
+     *     and the low order bit of the result is tested, as if by {@code (x & 1) != 0}.
+     * <li>If <em>T0</em> and <em>T1</em> are primitives other than boolean,
+     *     then a Java casting conversion (JLS 5.5) is applied.
+     *     (Specifically, <em>T0</em> will convert to <em>T1</em> by
+     *     widening and/or narrowing.)
+     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing
+     *     conversion will be applied at runtime, possibly followed
+     *     by a Java casting conversion (JLS 5.5) on the primitive value,
+     *     possibly followed by a conversion from byte to boolean by testing
+     *     the low-order bit.
+     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive,
+     *     and if the reference is null at runtime, a zero value is introduced.
+     * </ul>
+     * @param target the method handle to invoke after arguments are retyped
+     * @param newType the expected type of the new method handle
+     * @return a method handle which delegates to the target after performing
+     *           any necessary argument conversions, and arranges for any
+     *           necessary return value conversions
+     * @throws NullPointerException if either argument is null
+     * @throws WrongMethodTypeException if the conversion cannot be made
+     * @see MethodHandle#asType
+     */
+    public static
+    MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
+        explicitCastArgumentsChecks(target, newType);
+        // use the asTypeCache when possible:
+        MethodType oldType = target.type();
+        if (oldType == newType)  return target;
+        if (oldType.explicitCastEquivalentToAsType(newType)) {
+            return target.asFixedArity().asType(newType);
+        }
+
+        // return MethodHandleImpl.makePairwiseConvert(target, newType, false);
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.explicitCastArguments is not implemented");
+    }
+
+    private static void explicitCastArgumentsChecks(MethodHandle target, MethodType newType) {
+        if (target.type().parameterCount() != newType.parameterCount()) {
+            throw new WrongMethodTypeException("cannot explicitly cast " + target + " to " + newType);
+        }
+    }
+
+    /**
+     * Produces a method handle which adapts the calling sequence of the
+     * given method handle to a new type, by reordering the arguments.
+     * The resulting method handle is guaranteed to report a type
+     * which is equal to the desired new type.
+     * <p>
+     * The given array controls the reordering.
+     * Call {@code #I} the number of incoming parameters (the value
+     * {@code newType.parameterCount()}, and call {@code #O} the number
+     * of outgoing parameters (the value {@code target.type().parameterCount()}).
+     * Then the length of the reordering array must be {@code #O},
+     * and each element must be a non-negative number less than {@code #I}.
+     * For every {@code N} less than {@code #O}, the {@code N}-th
+     * outgoing argument will be taken from the {@code I}-th incoming
+     * argument, where {@code I} is {@code reorder[N]}.
+     * <p>
+     * No argument or return value conversions are applied.
+     * The type of each incoming argument, as determined by {@code newType},
+     * must be identical to the type of the corresponding outgoing parameter
+     * or parameters in the target method handle.
+     * The return type of {@code newType} must be identical to the return
+     * type of the original target.
+     * <p>
+     * The reordering array need not specify an actual permutation.
+     * An incoming argument will be duplicated if its index appears
+     * more than once in the array, and an incoming argument will be dropped
+     * if its index does not appear in the array.
+     * As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments},
+     * incoming arguments which are not mentioned in the reordering array
+     * are may be any type, as determined only by {@code newType}.
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodType intfn1 = methodType(int.class, int.class);
+MethodType intfn2 = methodType(int.class, int.class, int.class);
+MethodHandle sub = ... (int x, int y) -> (x-y) ...;
+assert(sub.type().equals(intfn2));
+MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1);
+MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0);
+assert((int)rsub.invokeExact(1, 100) == 99);
+MethodHandle add = ... (int x, int y) -> (x+y) ...;
+assert(add.type().equals(intfn2));
+MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
+assert(twice.type().equals(intfn1));
+assert((int)twice.invokeExact(21) == 42);
+     * }</pre></blockquote>
+     * @param target the method handle to invoke after arguments are reordered
+     * @param newType the expected type of the new method handle
+     * @param reorder an index array which controls the reordering
+     * @return a method handle which delegates to the target after it
+     *           drops unused arguments and moves and/or duplicates the other arguments
+     * @throws NullPointerException if any argument is null
+     * @throws IllegalArgumentException if the index array length is not equal to
+     *                  the arity of the target, or if any index array element
+     *                  not a valid index for a parameter of {@code newType},
+     *                  or if two corresponding parameter types in
+     *                  {@code target.type()} and {@code newType} are not identical,
+     */
+    public static
+    MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) {
+        reorder = reorder.clone();  // get a private copy
+        MethodType oldType = target.type();
+        permuteArgumentChecks(reorder, newType, oldType);
+
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.permuteArguments is not implemented");
+    }
+
+    // Android-changed: findFirstDupOrDrop is unused and removed.
+    // private static int findFirstDupOrDrop(int[] reorder, int newArity);
+
+    private static boolean permuteArgumentChecks(int[] reorder, MethodType newType, MethodType oldType) {
+        if (newType.returnType() != oldType.returnType())
+            throw newIllegalArgumentException("return types do not match",
+                    oldType, newType);
+        if (reorder.length == oldType.parameterCount()) {
+            int limit = newType.parameterCount();
+            boolean bad = false;
+            for (int j = 0; j < reorder.length; j++) {
+                int i = reorder[j];
+                if (i < 0 || i >= limit) {
+                    bad = true; break;
+                }
+                Class<?> src = newType.parameterType(i);
+                Class<?> dst = oldType.parameterType(j);
+                if (src != dst)
+                    throw newIllegalArgumentException("parameter types do not match after reorder",
+                            oldType, newType);
+            }
+            if (!bad)  return true;
+        }
+        throw newIllegalArgumentException("bad reorder array: "+Arrays.toString(reorder));
+    }
+
+    /**
+     * Produces a method handle of the requested return type which returns the given
+     * constant value every time it is invoked.
+     * <p>
+     * Before the method handle is returned, the passed-in value is converted to the requested type.
+     * If the requested type is primitive, widening primitive conversions are attempted,
+     * else reference conversions are attempted.
+     * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
+     * @param type the return type of the desired method handle
+     * @param value the value to return
+     * @return a method handle of the given return type and no arguments, which always returns the given value
+     * @throws NullPointerException if the {@code type} argument is null
+     * @throws ClassCastException if the value cannot be converted to the required return type
+     * @throws IllegalArgumentException if the given type is {@code void.class}
+     */
+    public static
+    MethodHandle constant(Class<?> type, Object value) {
+        if (type.isPrimitive()) {
+            if (type == void.class)
+                throw newIllegalArgumentException("void type");
+            Wrapper w = Wrapper.forPrimitiveType(type);
+            value = w.convert(value, type);
+            if (w.zero().equals(value))
+                return zero(w, type);
+            return insertArguments(identity(type), 0, value);
+        } else {
+            if (value == null)
+                return zero(Wrapper.OBJECT, type);
+            return identity(type).bindTo(value);
+        }
+    }
+
+    /**
+     * Produces a method handle which returns its sole argument when invoked.
+     * @param type the type of the sole parameter and return value of the desired method handle
+     * @return a unary method handle which accepts and returns the given type
+     * @throws NullPointerException if the argument is null
+     * @throws IllegalArgumentException if the given type is {@code void.class}
+     */
+    public static
+    MethodHandle identity(Class<?> type) {
+        Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
+        int pos = btw.ordinal();
+        MethodHandle ident = IDENTITY_MHS[pos];
+        if (ident == null) {
+            ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType()));
+        }
+        if (ident.type().returnType() == type)
+            return ident;
+        // something like identity(Foo.class); do not bother to intern these
+        assert(btw == Wrapper.OBJECT);
+        return makeIdentity(type);
+    }
+    private static final MethodHandle[] IDENTITY_MHS = new MethodHandle[Wrapper.values().length];
+    private static MethodHandle makeIdentity(Class<?> ptype) {
+        // MethodType mtype = MethodType.methodType(ptype, ptype);
+        // LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
+        // return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.makeIdentity is not implemented");
+    }
+
+    private static MethodHandle zero(Wrapper btw, Class<?> rtype) {
+        int pos = btw.ordinal();
+        MethodHandle zero = ZERO_MHS[pos];
+        if (zero == null) {
+            zero = setCachedMethodHandle(ZERO_MHS, pos, makeZero(btw.primitiveType()));
+        }
+        if (zero.type().returnType() == rtype)
+            return zero;
+        assert(btw == Wrapper.OBJECT);
+        return makeZero(rtype);
+    }
+    private static final MethodHandle[] ZERO_MHS = new MethodHandle[Wrapper.values().length];
+    private static MethodHandle makeZero(Class<?> rtype) {
+        // MethodType mtype = MethodType.methodType(rtype);
+        // LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
+        // return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.makeZero is not implemented");
+    }
+
+    synchronized private static MethodHandle setCachedMethodHandle(MethodHandle[] cache, int pos, MethodHandle value) {
+        // Simulate a CAS, to avoid racy duplication of results.
+        MethodHandle prev = cache[pos];
+        if (prev != null) return prev;
+        return cache[pos] = value;
+    }
+
+    /**
+     * Provides a target method handle with one or more <em>bound arguments</em>
+     * in advance of the method handle's invocation.
+     * The formal parameters to the target corresponding to the bound
+     * arguments are called <em>bound parameters</em>.
+     * Returns a new method handle which saves away the bound arguments.
+     * When it is invoked, it receives arguments for any non-bound parameters,
+     * binds the saved arguments to their corresponding parameters,
+     * and calls the original target.
+     * <p>
+     * The type of the new method handle will drop the types for the bound
+     * parameters from the original target type, since the new method handle
+     * will no longer require those arguments to be supplied by its callers.
+     * <p>
+     * Each given argument object must match the corresponding bound parameter type.
+     * If a bound parameter type is a primitive, the argument object
+     * must be a wrapper, and will be unboxed to produce the primitive value.
+     * <p>
+     * The {@code pos} argument selects which parameters are to be bound.
+     * It may range between zero and <i>N-L</i> (inclusively),
+     * where <i>N</i> is the arity of the target method handle
+     * and <i>L</i> is the length of the values array.
+     * @param target the method handle to invoke after the argument is inserted
+     * @param pos where to insert the argument (zero for the first)
+     * @param values the series of arguments to insert
+     * @return a method handle which inserts an additional argument,
+     *         before calling the original method handle
+     * @throws NullPointerException if the target or the {@code values} array is null
+     * @see MethodHandle#bindTo
+     */
+    public static
+    MethodHandle insertArguments(MethodHandle target, int pos, Object... values) {
+        int insCount = values.length;
+        Class<?>[] ptypes = insertArgumentsChecks(target, insCount, pos);
+        if (insCount == 0)  return target;
+
+        // BoundMethodHandle result = target.rebind();
+        // for (int i = 0; i < insCount; i++) {
+        //     Object value = values[i];
+        //     Class<?> ptype = ptypes[pos+i];
+        //     if (ptype.isPrimitive()) {
+        //         result = insertArgumentPrimitive(result, pos, ptype, value);
+        //     } else {
+        //         value = ptype.cast(value);  // throw CCE if needed
+        //         result = result.bindArgumentL(pos, value);
+        //     }
+        // }
+        // return result;
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.insertArguments is not implemented");
+    }
+
+    // Android-changed: insertArgumentPrimitive is unused.
+    //
+    // private static BoundMethodHandle insertArgumentPrimitive(BoundMethodHandle result, int pos,
+    //                                                          Class<?> ptype, Object value) {
+    //     Wrapper w = Wrapper.forPrimitiveType(ptype);
+    //     // perform unboxing and/or primitive conversion
+    //     value = w.convert(value, ptype);
+    //     switch (w) {
+    //     case INT:     return result.bindArgumentI(pos, (int)value);
+    //     case LONG:    return result.bindArgumentJ(pos, (long)value);
+    //     case FLOAT:   return result.bindArgumentF(pos, (float)value);
+    //     case DOUBLE:  return result.bindArgumentD(pos, (double)value);
+    //     default:      return result.bindArgumentI(pos, ValueConversions.widenSubword(value));
+    //     }
+    // }
+
+    private static Class<?>[] insertArgumentsChecks(MethodHandle target, int insCount, int pos) throws RuntimeException {
+        MethodType oldType = target.type();
+        int outargs = oldType.parameterCount();
+        int inargs  = outargs - insCount;
+        if (inargs < 0)
+            throw newIllegalArgumentException("too many values to insert");
+        if (pos < 0 || pos > inargs)
+            throw newIllegalArgumentException("no argument type to append");
+        return oldType.ptypes();
+    }
+
+    /**
+     * Produces a method handle which will discard some dummy arguments
+     * before calling some other specified <i>target</i> method handle.
+     * The type of the new method handle will be the same as the target's type,
+     * except it will also include the dummy argument types,
+     * at some given position.
+     * <p>
+     * The {@code pos} argument may range between zero and <i>N</i>,
+     * where <i>N</i> is the arity of the target.
+     * If {@code pos} is zero, the dummy arguments will precede
+     * the target's real arguments; if {@code pos} is <i>N</i>
+     * they will come after.
+     * <p>
+     * <b>Example:</b>
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle cat = lookup().findVirtual(String.class,
+  "concat", methodType(String.class, String.class));
+assertEquals("xy", (String) cat.invokeExact("x", "y"));
+MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
+MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
+assertEquals(bigType, d0.type());
+assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
+     * }</pre></blockquote>
+     * <p>
+     * This method is also equivalent to the following code:
+     * <blockquote><pre>
+     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
+     * </pre></blockquote>
+     * @param target the method handle to invoke after the arguments are dropped
+     * @param valueTypes the type(s) of the argument(s) to drop
+     * @param pos position of first argument to drop (zero for the leftmost)
+     * @return a method handle which drops arguments of the given types,
+     *         before calling the original method handle
+     * @throws NullPointerException if the target is null,
+     *                              or if the {@code valueTypes} list or any of its elements is null
+     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
+     *                  or if {@code pos} is negative or greater than the arity of the target,
+     *                  or if the new method handle's type would have too many parameters
+     */
+    public static
+    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
+        MethodType oldType = target.type();  // get NPE
+        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
+
+        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
+        // if (dropped == 0)  return target;
+        // BoundMethodHandle result = target.rebind();
+        // LambdaForm lform = result.form;
+        // int insertFormArg = 1 + pos;
+        // for (Class<?> ptype : valueTypes) {
+        //     lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
+        // }
+        // result = result.copyWith(newType, lform);
+        // return result;
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.dropArguments is not implemented");
+    }
+
+    private static int dropArgumentChecks(MethodType oldType, int pos, List<Class<?>> valueTypes) {
+        int dropped = valueTypes.size();
+        MethodType.checkSlotCount(dropped);
+        int outargs = oldType.parameterCount();
+        int inargs  = outargs + dropped;
+        if (pos < 0 || pos > outargs)
+            throw newIllegalArgumentException("no argument type to remove"
+                    + Arrays.asList(oldType, pos, valueTypes, inargs, outargs)
+                    );
+        return dropped;
+    }
+
+    /**
+     * Produces a method handle which will discard some dummy arguments
+     * before calling some other specified <i>target</i> method handle.
+     * The type of the new method handle will be the same as the target's type,
+     * except it will also include the dummy argument types,
+     * at some given position.
+     * <p>
+     * The {@code pos} argument may range between zero and <i>N</i>,
+     * where <i>N</i> is the arity of the target.
+     * If {@code pos} is zero, the dummy arguments will precede
+     * the target's real arguments; if {@code pos} is <i>N</i>
+     * they will come after.
+     * <p>
+     * <b>Example:</b>
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle cat = lookup().findVirtual(String.class,
+  "concat", methodType(String.class, String.class));
+assertEquals("xy", (String) cat.invokeExact("x", "y"));
+MethodHandle d0 = dropArguments(cat, 0, String.class);
+assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
+MethodHandle d1 = dropArguments(cat, 1, String.class);
+assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
+MethodHandle d2 = dropArguments(cat, 2, String.class);
+assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
+MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
+assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
+     * }</pre></blockquote>
+     * <p>
+     * This method is also equivalent to the following code:
+     * <blockquote><pre>
+     * {@link #dropArguments(MethodHandle,int,List) dropArguments}{@code (target, pos, Arrays.asList(valueTypes))}
+     * </pre></blockquote>
+     * @param target the method handle to invoke after the arguments are dropped
+     * @param valueTypes the type(s) of the argument(s) to drop
+     * @param pos position of first argument to drop (zero for the leftmost)
+     * @return a method handle which drops arguments of the given types,
+     *         before calling the original method handle
+     * @throws NullPointerException if the target is null,
+     *                              or if the {@code valueTypes} array or any of its elements is null
+     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
+     *                  or if {@code pos} is negative or greater than the arity of the target,
+     *                  or if the new method handle's type would have
+     *                  <a href="MethodHandle.html#maxarity">too many parameters</a>
+     */
+    public static
+    MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) {
+        return dropArguments(target, pos, Arrays.asList(valueTypes));
+    }
+
+    /**
+     * Adapts a target method handle by pre-processing
+     * one or more of its arguments, each with its own unary filter function,
+     * and then calling the target with each pre-processed argument
+     * replaced by the result of its corresponding filter function.
+     * <p>
+     * The pre-processing is performed by one or more method handles,
+     * specified in the elements of the {@code filters} array.
+     * The first element of the filter array corresponds to the {@code pos}
+     * argument of the target, and so on in sequence.
+     * <p>
+     * Null arguments in the array are treated as identity functions,
+     * and the corresponding arguments left unchanged.
+     * (If there are no non-null elements in the array, the original target is returned.)
+     * Each filter is applied to the corresponding argument of the adapter.
+     * <p>
+     * If a filter {@code F} applies to the {@code N}th argument of
+     * the target, then {@code F} must be a method handle which
+     * takes exactly one argument.  The type of {@code F}'s sole argument
+     * replaces the corresponding argument type of the target
+     * in the resulting adapted method handle.
+     * The return type of {@code F} must be identical to the corresponding
+     * parameter type of the target.
+     * <p>
+     * It is an error if there are elements of {@code filters}
+     * (null or not)
+     * which do not correspond to argument positions in the target.
+     * <p><b>Example:</b>
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle cat = lookup().findVirtual(String.class,
+  "concat", methodType(String.class, String.class));
+MethodHandle upcase = lookup().findVirtual(String.class,
+  "toUpperCase", methodType(String.class));
+assertEquals("xy", (String) cat.invokeExact("x", "y"));
+MethodHandle f0 = filterArguments(cat, 0, upcase);
+assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
+MethodHandle f1 = filterArguments(cat, 1, upcase);
+assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
+MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
+assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
+     * }</pre></blockquote>
+     * <p> Here is pseudocode for the resulting adapter:
+     * <blockquote><pre>{@code
+     * V target(P... p, A[i]... a[i], B... b);
+     * A[i] filter[i](V[i]);
+     * T adapter(P... p, V[i]... v[i], B... b) {
+     *   return target(p..., f[i](v[i])..., b...);
+     * }
+     * }</pre></blockquote>
+     *
+     * @param target the method handle to invoke after arguments are filtered
+     * @param pos the position of the first argument to filter
+     * @param filters method handles to call initially on filtered arguments
+     * @return method handle which incorporates the specified argument filtering logic
+     * @throws NullPointerException if the target is null
+     *                              or if the {@code filters} array is null
+     * @throws IllegalArgumentException if a non-null element of {@code filters}
+     *          does not match a corresponding argument type of target as described above,
+     *          or if the {@code pos+filters.length} is greater than {@code target.type().parameterCount()},
+     *          or if the resulting method handle's type would have
+     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
+     */
+    public static
+    MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) {
+        filterArgumentsCheckArity(target, pos, filters);
+        MethodHandle adapter = target;
+        int curPos = pos-1;  // pre-incremented
+        for (MethodHandle filter : filters) {
+            curPos += 1;
+            if (filter == null)  continue;  // ignore null elements of filters
+            adapter = filterArgument(adapter, curPos, filter);
+        }
+        return adapter;
+    }
+
+    /*non-public*/ static
+    MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
+        filterArgumentChecks(target, pos, filter);
+        // MethodType targetType = target.type();
+        // MethodType filterType = filter.type();
+        // BoundMethodHandle result = target.rebind();
+        // Class<?> newParamType = filterType.parameterType(0);
+        // LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
+        // MethodType newType = targetType.changeParameterType(pos, newParamType);
+        // result = result.copyWithExtendL(newType, lform, filter);
+        // return result;
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.filterArgument is not implemented");
+    }
+
+    private static void filterArgumentsCheckArity(MethodHandle target, int pos, MethodHandle[] filters) {
+        MethodType targetType = target.type();
+        int maxPos = targetType.parameterCount();
+        if (pos + filters.length > maxPos)
+            throw newIllegalArgumentException("too many filters");
+    }
+
+    private static void filterArgumentChecks(MethodHandle target, int pos, MethodHandle filter) throws RuntimeException {
+        MethodType targetType = target.type();
+        MethodType filterType = filter.type();
+        if (filterType.parameterCount() != 1
+            || filterType.returnType() != targetType.parameterType(pos))
+            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
+    }
+
+    /**
+     * Adapts a target method handle by pre-processing
+     * a sub-sequence of its arguments with a filter (another method handle).
+     * The pre-processed arguments are replaced by the result (if any) of the
+     * filter function.
+     * The target is then called on the modified (usually shortened) argument list.
+     * <p>
+     * If the filter returns a value, the target must accept that value as
+     * its argument in position {@code pos}, preceded and/or followed by
+     * any arguments not passed to the filter.
+     * If the filter returns void, the target must accept all arguments
+     * not passed to the filter.
+     * No arguments are reordered, and a result returned from the filter
+     * replaces (in order) the whole subsequence of arguments originally
+     * passed to the adapter.
+     * <p>
+     * The argument types (if any) of the filter
+     * replace zero or one argument types of the target, at position {@code pos},
+     * in the resulting adapted method handle.
+     * The return type of the filter (if any) must be identical to the
+     * argument type of the target at position {@code pos}, and that target argument
+     * is supplied by the return value of the filter.
+     * <p>
+     * In all cases, {@code pos} must be greater than or equal to zero, and
+     * {@code pos} must also be less than or equal to the target's arity.
+     * <p><b>Example:</b>
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle deepToString = publicLookup()
+  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
+
+MethodHandle ts1 = deepToString.asCollector(String[].class, 1);
+assertEquals("[strange]", (String) ts1.invokeExact("strange"));
+
+MethodHandle ts2 = deepToString.asCollector(String[].class, 2);
+assertEquals("[up, down]", (String) ts2.invokeExact("up", "down"));
+
+MethodHandle ts3 = deepToString.asCollector(String[].class, 3);
+MethodHandle ts3_ts2 = collectArguments(ts3, 1, ts2);
+assertEquals("[top, [up, down], strange]",
+             (String) ts3_ts2.invokeExact("top", "up", "down", "strange"));
+
+MethodHandle ts3_ts2_ts1 = collectArguments(ts3_ts2, 3, ts1);
+assertEquals("[top, [up, down], [strange]]",
+             (String) ts3_ts2_ts1.invokeExact("top", "up", "down", "strange"));
+
+MethodHandle ts3_ts2_ts3 = collectArguments(ts3_ts2, 1, ts3);
+assertEquals("[top, [[up, down, strange], charm], bottom]",
+             (String) ts3_ts2_ts3.invokeExact("top", "up", "down", "strange", "charm", "bottom"));
+     * }</pre></blockquote>
+     * <p> Here is pseudocode for the resulting adapter:
+     * <blockquote><pre>{@code
+     * T target(A...,V,C...);
+     * V filter(B...);
+     * T adapter(A... a,B... b,C... c) {
+     *   V v = filter(b...);
+     *   return target(a...,v,c...);
+     * }
+     * // and if the filter has no arguments:
+     * T target2(A...,V,C...);
+     * V filter2();
+     * T adapter2(A... a,C... c) {
+     *   V v = filter2();
+     *   return target2(a...,v,c...);
+     * }
+     * // and if the filter has a void return:
+     * T target3(A...,C...);
+     * void filter3(B...);
+     * void adapter3(A... a,B... b,C... c) {
+     *   filter3(b...);
+     *   return target3(a...,c...);
+     * }
+     * }</pre></blockquote>
+     * <p>
+     * A collection adapter {@code collectArguments(mh, 0, coll)} is equivalent to
+     * one which first "folds" the affected arguments, and then drops them, in separate
+     * steps as follows:
+     * <blockquote><pre>{@code
+     * mh = MethodHandles.dropArguments(mh, 1, coll.type().parameterList()); //step 2
+     * mh = MethodHandles.foldArguments(mh, coll); //step 1
+     * }</pre></blockquote>
+     * If the target method handle consumes no arguments besides than the result
+     * (if any) of the filter {@code coll}, then {@code collectArguments(mh, 0, coll)}
+     * is equivalent to {@code filterReturnValue(coll, mh)}.
+     * If the filter method handle {@code coll} consumes one argument and produces
+     * a non-void result, then {@code collectArguments(mh, N, coll)}
+     * is equivalent to {@code filterArguments(mh, N, coll)}.
+     * Other equivalences are possible but would require argument permutation.
+     *
+     * @param target the method handle to invoke after filtering the subsequence of arguments
+     * @param pos the position of the first adapter argument to pass to the filter,
+     *            and/or the target argument which receives the result of the filter
+     * @param filter method handle to call on the subsequence of arguments
+     * @return method handle which incorporates the specified argument subsequence filtering logic
+     * @throws NullPointerException if either argument is null
+     * @throws IllegalArgumentException if the return type of {@code filter}
+     *          is non-void and is not the same as the {@code pos} argument of the target,
+     *          or if {@code pos} is not between 0 and the target's arity, inclusive,
+     *          or if the resulting method handle's type would have
+     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
+     * @see MethodHandles#foldArguments
+     * @see MethodHandles#filterArguments
+     * @see MethodHandles#filterReturnValue
+     */
+    public static
+    MethodHandle collectArguments(MethodHandle target, int pos, MethodHandle filter) {
+        MethodType newType = collectArgumentsChecks(target, pos, filter);
+        MethodType collectorType = filter.type();
+
+        // BoundMethodHandle result = target.rebind();
+        // LambdaForm lform;
+        // if (collectorType.returnType().isArray() && filter.intrinsicName() == Intrinsic.NEW_ARRAY) {
+        //     lform = result.editor().collectArgumentArrayForm(1 + pos, filter);
+        //     if (lform != null) {
+        //         return result.copyWith(newType, lform);
+        //     }
+        // }
+        // lform = result.editor().collectArgumentsForm(1 + pos, collectorType.basicType());
+        // return result.copyWithExtendL(newType, lform, filter);
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.collectArguments is not implemented");
+    }
+
+    private static MethodType collectArgumentsChecks(MethodHandle target, int pos, MethodHandle filter) throws RuntimeException {
+        MethodType targetType = target.type();
+        MethodType filterType = filter.type();
+        Class<?> rtype = filterType.returnType();
+        List<Class<?>> filterArgs = filterType.parameterList();
+        if (rtype == void.class) {
+            return targetType.insertParameterTypes(pos, filterArgs);
+        }
+        if (rtype != targetType.parameterType(pos)) {
+            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
+        }
+        return targetType.dropParameterTypes(pos, pos+1).insertParameterTypes(pos, filterArgs);
+    }
+
+    /**
+     * Adapts a target method handle by post-processing
+     * its return value (if any) with a filter (another method handle).
+     * The result of the filter is returned from the adapter.
+     * <p>
+     * If the target returns a value, the filter must accept that value as
+     * its only argument.
+     * If the target returns void, the filter must accept no arguments.
+     * <p>
+     * The return type of the filter
+     * replaces the return type of the target
+     * in the resulting adapted method handle.
+     * The argument type of the filter (if any) must be identical to the
+     * return type of the target.
+     * <p><b>Example:</b>
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle cat = lookup().findVirtual(String.class,
+  "concat", methodType(String.class, String.class));
+MethodHandle length = lookup().findVirtual(String.class,
+  "length", methodType(int.class));
+System.out.println((String) cat.invokeExact("x", "y")); // xy
+MethodHandle f0 = filterReturnValue(cat, length);
+System.out.println((int) f0.invokeExact("x", "y")); // 2
+     * }</pre></blockquote>
+     * <p> Here is pseudocode for the resulting adapter:
+     * <blockquote><pre>{@code
+     * V target(A...);
+     * T filter(V);
+     * T adapter(A... a) {
+     *   V v = target(a...);
+     *   return filter(v);
+     * }
+     * // and if the target has a void return:
+     * void target2(A...);
+     * T filter2();
+     * T adapter2(A... a) {
+     *   target2(a...);
+     *   return filter2();
+     * }
+     * // and if the filter has a void return:
+     * V target3(A...);
+     * void filter3(V);
+     * void adapter3(A... a) {
+     *   V v = target3(a...);
+     *   filter3(v);
+     * }
+     * }</pre></blockquote>
+     * @param target the method handle to invoke before filtering the return value
+     * @param filter method handle to call on the return value
+     * @return method handle which incorporates the specified return value filtering logic
+     * @throws NullPointerException if either argument is null
+     * @throws IllegalArgumentException if the argument list of {@code filter}
+     *          does not match the return type of target as described above
+     */
+    public static
+    MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
+        MethodType targetType = target.type();
+        MethodType filterType = filter.type();
+        filterReturnValueChecks(targetType, filterType);
+
+        // BoundMethodHandle result = target.rebind();
+        // BasicType rtype = BasicType.basicType(filterType.returnType());
+        // LambdaForm lform = result.editor().filterReturnForm(rtype, false);
+        // MethodType newType = targetType.changeReturnType(filterType.returnType());
+        // result = result.copyWithExtendL(newType, lform, filter);
+        // return result;
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.filterReturnValue is not implemented");
+    }
+
+    private static void filterReturnValueChecks(MethodType targetType, MethodType filterType) throws RuntimeException {
+        Class<?> rtype = targetType.returnType();
+        int filterValues = filterType.parameterCount();
+        if (filterValues == 0
+                ? (rtype != void.class)
+                : (rtype != filterType.parameterType(0)))
+            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
+    }
+
+    /**
+     * Adapts a target method handle by pre-processing
+     * some of its arguments, and then calling the target with
+     * the result of the pre-processing, inserted into the original
+     * sequence of arguments.
+     * <p>
+     * The pre-processing is performed by {@code combiner}, a second method handle.
+     * Of the arguments passed to the adapter, the first {@code N} arguments
+     * are copied to the combiner, which is then called.
+     * (Here, {@code N} is defined as the parameter count of the combiner.)
+     * After this, control passes to the target, with any result
+     * from the combiner inserted before the original {@code N} incoming
+     * arguments.
+     * <p>
+     * If the combiner returns a value, the first parameter type of the target
+     * must be identical with the return type of the combiner, and the next
+     * {@code N} parameter types of the target must exactly match the parameters
+     * of the combiner.
+     * <p>
+     * If the combiner has a void return, no result will be inserted,
+     * and the first {@code N} parameter types of the target
+     * must exactly match the parameters of the combiner.
+     * <p>
+     * The resulting adapter is the same type as the target, except that the
+     * first parameter type is dropped,
+     * if it corresponds to the result of the combiner.
+     * <p>
+     * (Note that {@link #dropArguments(MethodHandle,int,List) dropArguments} can be used to remove any arguments
+     * that either the combiner or the target does not wish to receive.
+     * If some of the incoming arguments are destined only for the combiner,
+     * consider using {@link MethodHandle#asCollector asCollector} instead, since those
+     * arguments will not need to be live on the stack on entry to the
+     * target.)
+     * <p><b>Example:</b>
+     * <blockquote><pre>{@code
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+...
+MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
+  "println", methodType(void.class, String.class))
+    .bindTo(System.out);
+MethodHandle cat = lookup().findVirtual(String.class,
+  "concat", methodType(String.class, String.class));
+assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
+MethodHandle catTrace = foldArguments(cat, trace);
+// also prints "boo":
+assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
+     * }</pre></blockquote>
+     * <p> Here is pseudocode for the resulting adapter:
+     * <blockquote><pre>{@code
+     * // there are N arguments in A...
+     * T target(V, A[N]..., B...);
+     * V combiner(A...);
+     * T adapter(A... a, B... b) {
+     *   V v = combiner(a...);
+     *   return target(v, a..., b...);
+     * }
+     * // and if the combiner has a void return:
+     * T target2(A[N]..., B...);
+     * void combiner2(A...);
+     * T adapter2(A... a, B... b) {
+     *   combiner2(a...);
+     *   return target2(a..., b...);
+     * }
+     * }</pre></blockquote>
+     * @param target the method handle to invoke after arguments are combined
+     * @param combiner method handle to call initially on the incoming arguments
+     * @return method handle which incorporates the specified argument folding logic
+     * @throws NullPointerException if either argument is null
+     * @throws IllegalArgumentException if {@code combiner}'s return type
+     *          is non-void and not the same as the first argument type of
+     *          the target, or if the initial {@code N} argument types
+     *          of the target
+     *          (skipping one matching the {@code combiner}'s return type)
+     *          are not identical with the argument types of {@code combiner}
+     */
+    public static
+    MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) {
+        int foldPos = 0;
+        MethodType targetType = target.type();
+        MethodType combinerType = combiner.type();
+        Class<?> rtype = foldArgumentChecks(foldPos, targetType, combinerType);
+
+        // BoundMethodHandle result = target.rebind();
+        // boolean dropResult = (rtype == void.class);
+        // // Note:  This may cache too many distinct LFs. Consider backing off to varargs code.
+        // LambdaForm lform = result.editor().foldArgumentsForm(1 + foldPos, dropResult, combinerType.basicType());
+        // MethodType newType = targetType;
+        // if (!dropResult)
+        //     newType = newType.dropParameterTypes(foldPos, foldPos + 1);
+        // result = result.copyWithExtendL(newType, lform, combiner);
+        // return result;
+
+        // TODO(narayan): Implement this method.
+        throw new UnsupportedOperationException("MethodHandles.foldArguments is not implemented");
+    }
+
+    private static Class<?> foldArgumentChecks(int foldPos, MethodType targetType, MethodType combinerType) {
+        int foldArgs   = combinerType.parameterCount();
+        Class<?> rtype = combinerType.returnType();
+        int foldVals = rtype == void.class ? 0 : 1;
+        int afterInsertPos = foldPos + foldVals;
+        boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs);
+        if (ok && !(combinerType.parameterList()
+                    .equals(targetType.parameterList().subList(afterInsertPos,
+                                                               afterInsertPos + foldArgs))))
+            ok = false;
+        if (ok && foldVals != 0 && combinerType.returnType() != targetType.parameterType(0))
+            ok = false;
+        if (!ok)
+            throw misMatchedTypes("target and combiner types", targetType, combinerType);
+        return rtype;
+    }
+
+    /**
+     * Makes a method handle which adapts a target method handle,
+     * by guarding it with a test, a boolean-valued method handle.
+     * If the guard fails, a fallback handle is called instead.
+     * All three method handles must have the same corresponding
+     * argument and return types, except that the return type
+     * of the test must be boolean, and the test is allowed
+     * to have fewer arguments than the other two method handles.
+     * <p> Here is pseudocode for the resulting adapter:
+     * <blockquote><pre>{@code
+     * boolean test(A...);
+     * T target(A...,B...);
+     * T fallback(A...,B...);
+     * T adapter(A... a,B... b) {
+     *   if (test(a...))
+     *     return target(a..., b...);
+     *   else
+     *     return fallback(a..., b...);
+     * }
+     * }</pre></blockquote>
+     * Note that the test arguments ({@code a...} in the pseudocode) cannot
+     * be modified by execution of the test, and so are passed unchanged
+     * from the caller to the target or fallback as appropriate.
+     * @param test method handle used for test, must return boolean
+     * @param target method handle to call if test passes
+     * @param fallback method handle to call if test fails
+     * @return method handle which incorporates the specified if/then/else logic
+     * @throws NullPointerException if any argument is null
+     * @throws IllegalArgumentException if {@code test} does not return boolean,
+     *          or if all three method types do not match (with the return
+     *          type of {@code test} changed to match that of the target).
+     */
+    public static
+    MethodHandle guardWithTest(MethodHandle test,
+                               MethodHandle target,
+                               MethodHandle fallback) {
+        MethodType gtype = test.type();
+        MethodType ttype = target.type();
+        MethodType ftype = fallback.type();
+        if (!ttype.equals(ftype))
+            throw misMatchedTypes("target and fallback types", ttype, ftype);
+        if (gtype.returnType() != boolean.class)
+            throw newIllegalArgumentException("guard type is not a predicate "+gtype);
+        List<Class<?>> targs = ttype.parameterList();
+        List<Class<?>> gargs = gtype.parameterList();
+        if (!targs.equals(gargs)) {
+            int gpc = gargs.size(), tpc = targs.size();
+            if (gpc >= tpc || !targs.subList(0, gpc).equals(gargs))
+                throw misMatchedTypes("target and test types", ttype, gtype);
+            test = dropArguments(test, gpc, targs.subList(gpc, tpc));
+            gtype = test.type();
+        }
+
+        // TODO(narayan): Implement this method.
+        // return MethodHandleImpl.makeGuardWithTest(test, target, fallback);
+        throw new UnsupportedOperationException("MethodHandles.guardWithTest is not implemented");
+    }
+
+    static RuntimeException misMatchedTypes(String what, MethodType t1, MethodType t2) {
+        return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2);
+    }
+
+    /**
+     * Makes a method handle which adapts a target method handle,
+     * by running it inside an exception handler.
+     * If the target returns normally, the adapter returns that value.
+     * If an exception matching the specified type is thrown, the fallback
+     * handle is called instead on the exception, plus the original arguments.
+     * <p>
+     * The target and handler must have the same corresponding
+     * argument and return types, except that handler may omit trailing arguments
+     * (similarly to the predicate in {@link #guardWithTest guardWithTest}).
+     * Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
+     * <p> Here is pseudocode for the resulting adapter:
+     * <blockquote><pre>{@code
+     * T target(A..., B...);
+     * T handler(ExType, A...);
+     * T adapter(A... a, B... b) {
+     *   try {
+     *     return target(a..., b...);
+     *   } catch (ExType ex) {
+     *     return handler(ex, a...);
+     *   }
+     * }
+     * }</pre></blockquote>
+     * Note that the saved arguments ({@code a...} in the pseudocode) cannot
+     * be modified by execution of the target, and so are passed unchanged
+     * from the caller to the handler, if the handler is invoked.
+     * <p>
+     * The target and handler must return the same type, even if the handler
+     * always throws.  (This might happen, for instance, because the handler
+     * is simulating a {@code finally} clause).
+     * To create such a throwing handler, compose the handler creation logic
+     * with {@link #throwException throwException},
+     * in order to create a method handle of the correct return type.
+     * @param target method handle to call
+     * @param exType the type of exception which the handler will catch
+     * @param handler method handle to call if a matching exception is thrown
+     * @return method handle which incorporates the specified try/catch logic
+     * @throws NullPointerException if any argument is null
+     * @throws IllegalArgumentException if {@code handler} does not accept
+     *          the given exception type, or if the method handle types do
+     *          not match in their return types and their
+     *          corresponding parameters
+     */
+    public static
+    MethodHandle catchException(MethodHandle target,
+                                Class<? extends Throwable> exType,
+                                MethodHandle handler) {
+        MethodType ttype = target.type();
+        MethodType htype = handler.type();
+        if (htype.parameterCount() < 1 ||
+            !htype.parameterType(0).isAssignableFrom(exType))
+            throw newIllegalArgumentException("handler does not accept exception type "+exType);
+        if (htype.returnType() != ttype.returnType())
+            throw misMatchedTypes("target and handler return types", ttype, htype);
+        List<Class<?>> targs = ttype.parameterList();
+        List<Class<?>> hargs = htype.parameterList();
+        hargs = hargs.subList(1, hargs.size());  // omit leading parameter from handler
+        if (!targs.equals(hargs)) {
+            int hpc = hargs.size(), tpc = targs.size();
+            if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs))
+                throw misMatchedTypes("target and handler types", ttype, htype);
+            handler = dropArguments(handler, 1+hpc, targs.subList(hpc, tpc));
+            htype = handler.type();
+        }
+
+        // TODO(narayan): Implement this method.
+        // return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
+        throw new UnsupportedOperationException("MethodHandles.catchException is not implemented");
+    }
+
+    /**
+     * Produces a method handle which will throw exceptions of the given {@code exType}.
+     * The method handle will accept a single argument of {@code exType},
+     * and immediately throw it as an exception.
+     * The method type will nominally specify a return of {@code returnType}.
+     * The return type may be anything convenient:  It doesn't matter to the
+     * method handle's behavior, since it will never return normally.
+     * @param returnType the return type of the desired method handle
+     * @param exType the parameter type of the desired method handle
+     * @return method handle which can throw the given exceptions
+     * @throws NullPointerException if either argument is null
+     */
+    public static
+    MethodHandle throwException(Class<?> returnType, Class<? extends Throwable> exType) {
+        if (!Throwable.class.isAssignableFrom(exType))
+            throw new ClassCastException(exType.getName());
+
+        // TODO(narayan): Implement this method.
+        // return MethodHandleImpl.throwException(MethodType.methodType(returnType, exType));
+        throw new UnsupportedOperationException("MethodHandles.throwException is not implemented");
+    }
+}
diff --git a/ojluni/src/main/java/java/lang/reflect/Constructor.java b/ojluni/src/main/java/java/lang/reflect/Constructor.java
index 3e26cbb..7e3a71d 100644
--- a/ojluni/src/main/java/java/lang/reflect/Constructor.java
+++ b/ojluni/src/main/java/java/lang/reflect/Constructor.java
@@ -26,10 +26,8 @@
 
 package java.lang.reflect;
 
-import java.util.Comparator;
-import libcore.reflect.Types;
-
 import java.lang.annotation.Annotation;
+import java.util.Comparator;
 
 /**
  * {@code Constructor} provides information about, and access to, a single
@@ -51,7 +49,7 @@
  * @author      Kenneth Russell
  * @author      Nakul Saraiya
  */
-public final class Constructor<T> extends AbstractMethod {
+public final class Constructor<T> extends Executable {
     private static final Comparator<Method> ORDER_BY_SIGNATURE = null; // Unused; must match Method.
 
     private final Class<?> serializationClass;
@@ -74,14 +72,20 @@
         return new Constructor<T>(ctor, cl);
     }
 
+    @Override
+    boolean hasGenericInformation() {
+        // Android-changed: Signature retrieval is handled in Executable.
+        return super.hasGenericInformationInternal();
+    }
+
     /**
      * {@inheritDoc}
      */
     @Override
     @SuppressWarnings({"rawtypes", "unchecked"})
     public Class<T> getDeclaringClass() {
-        // Android-changed: This is handled by AbstractMethod.
-        return (Class<T>) super.getDeclaringClass();
+        // Android-changed: This is handled by Executable.
+        return (Class<T>) super.getDeclaringClassInternal();
     }
 
     /**
@@ -98,8 +102,8 @@
      */
     @Override
     public int getModifiers() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getModifiers();
+        // Android-changed: This is handled by Executable.
+        return super.getModifiersInternal();
     }
 
     /**
@@ -110,7 +114,7 @@
     @Override
     @SuppressWarnings({"rawtypes", "unchecked"})
     public TypeVariable<Constructor<T>>[] getTypeParameters() {
-        // Android-changed: This is mostly handled by AbstractMethod.
+        // Android-changed: This is mostly handled by Executable.
         GenericInfo info = getMethodOrConstructorGenericInfoInternal();
         return (TypeVariable<Constructor<T>>[]) info.formalTypeParameters.clone();
     }
@@ -121,16 +125,16 @@
      */
     @Override
     public Class<?>[] getParameterTypes() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getParameterTypes();
+        // Android-changed: This is handled by Executable.
+        return super.getParameterTypesInternal();
     }
 
     /**
      * {@inheritDoc}
      */
     public int getParameterCount() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getParameterCount();
+        // Android-changed: This is handled by Executable.
+        return super.getParameterCountInternal();
     }
 
     /**
@@ -371,7 +375,7 @@
      */
     @Override
     public Annotation[][] getParameterAnnotations() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getParameterAnnotations();
+        // Android-changed: This is handled by Executable.
+        return super.getParameterAnnotationsInternal();
     }
 }
diff --git a/ojluni/src/main/java/java/lang/reflect/Executable.java b/ojluni/src/main/java/java/lang/reflect/Executable.java
index 1840566..38e6f99 100644
--- a/ojluni/src/main/java/java/lang/reflect/Executable.java
+++ b/ojluni/src/main/java/java/lang/reflect/Executable.java
@@ -25,8 +25,15 @@
 
 package java.lang.reflect;
 
+import com.android.dex.Dex;
+
 import java.lang.annotation.Annotation;
+import java.util.Objects;
 import libcore.reflect.AnnotatedElements;
+import libcore.reflect.GenericSignatureParser;
+import libcore.reflect.ListOfTypes;
+import libcore.reflect.Types;
+import libcore.util.EmptyArray;
 
 /**
  * A shared superclass for the common functionality of {@link Method}
@@ -41,6 +48,11 @@
      */
     Executable() {}
 
+    /**
+     * Does the Executable have generic information.
+     */
+    abstract boolean hasGenericInformation();
+
     boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
         /* Avoid unnecessary cloning */
         if (params1.length == params2.length) {
@@ -108,7 +120,6 @@
      */
     abstract void specificToStringHeader(StringBuilder sb);
 
-
     String sharedToGenericString(int modifierMask, boolean isDefault) {
         try {
             StringBuilder sb = new StringBuilder();
@@ -250,12 +261,165 @@
      *     type that cannot be instantiated for any reason
      */
     public Type[] getGenericParameterTypes() {
-        // Android-changed: Implemented in AbstractMethod instead.
-        throw new UnsupportedOperationException(
-                "Executable.getGenericParameterTypes() not implemented");
+        // Android-changed: Changed to use Types / getMethodOrConstructorGenericInfoInternal()
+        return Types.getTypeArray(
+                getMethodOrConstructorGenericInfoInternal().genericParameterTypes, false);
     }
 
     /**
+     * Behaves like {@code getGenericParameterTypes}, but returns type
+     * information for all parameters, including synthetic parameters.
+     */
+    Type[] getAllGenericParameterTypes() {
+        final boolean genericInfo = hasGenericInformation();
+
+        // Easy case: we don't have generic parameter information.  In
+        // this case, we just return the result of
+        // getParameterTypes().
+        if (!genericInfo) {
+            return getParameterTypes();
+        } else {
+            final boolean realParamData = hasRealParameterData();
+            final Type[] genericParamTypes = getGenericParameterTypes();
+            final Type[] nonGenericParamTypes = getParameterTypes();
+            final Type[] out = new Type[nonGenericParamTypes.length];
+            final Parameter[] params = getParameters();
+            int fromidx = 0;
+            // If we have real parameter data, then we use the
+            // synthetic and mandate flags to our advantage.
+            if (realParamData) {
+                for (int i = 0; i < out.length; i++) {
+                    final Parameter param = params[i];
+                    if (param.isSynthetic() || param.isImplicit()) {
+                        // If we hit a synthetic or mandated parameter,
+                        // use the non generic parameter info.
+                        out[i] = nonGenericParamTypes[i];
+                    } else {
+                        // Otherwise, use the generic parameter info.
+                        out[i] = genericParamTypes[fromidx];
+                        fromidx++;
+                    }
+                }
+            } else {
+                // Otherwise, use the non-generic parameter data.
+                // Without method parameter reflection data, we have
+                // no way to figure out which parameters are
+                // synthetic/mandated, thus, no way to match up the
+                // indexes.
+                return genericParamTypes.length == nonGenericParamTypes.length ?
+                    genericParamTypes : nonGenericParamTypes;
+            }
+            return out;
+        }
+    }
+
+    /**
+     * Returns an array of {@code Parameter} objects that represent
+     * all the parameters to the underlying executable represented by
+     * this object.  Returns an array of length 0 if the executable
+     * has no parameters.
+     *
+     * <p>The parameters of the underlying executable do not necessarily
+     * have unique names, or names that are legal identifiers in the
+     * Java programming language (JLS 3.8).
+     *
+     * @since 1.8
+     * @throws MalformedParametersException if the class file contains
+     * a MethodParameters attribute that is improperly formatted.
+     * @return an array of {@code Parameter} objects representing all
+     * the parameters to the executable this object represents.
+     */
+    public Parameter[] getParameters() {
+        // TODO: This may eventually need to be guarded by security
+        // mechanisms similar to those in Field, Method, etc.
+        //
+        // Need to copy the cached array to prevent users from messing
+        // with it.  Since parameters are immutable, we can
+        // shallow-copy.
+        return privateGetParameters().clone();
+    }
+
+    private Parameter[] synthesizeAllParams() {
+        final int realparams = getParameterCount();
+        final Parameter[] out = new Parameter[realparams];
+        for (int i = 0; i < realparams; i++)
+            // TODO: is there a way to synthetically derive the
+            // modifiers?  Probably not in the general case, since
+            // we'd have no way of knowing about them, but there
+            // may be specific cases.
+            out[i] = new Parameter("arg" + i, 0, this, i);
+        return out;
+    }
+
+    private void verifyParameters(final Parameter[] parameters) {
+        final int mask = Modifier.FINAL | Modifier.SYNTHETIC | Modifier.MANDATED;
+
+        if (getParameterTypes().length != parameters.length)
+            throw new MalformedParametersException("Wrong number of parameters in MethodParameters attribute");
+
+        for (Parameter parameter : parameters) {
+            final String name = parameter.getRealName();
+            final int mods = parameter.getModifiers();
+
+            if (name != null) {
+                if (name.isEmpty() || name.indexOf('.') != -1 ||
+                    name.indexOf(';') != -1 || name.indexOf('[') != -1 ||
+                    name.indexOf('/') != -1) {
+                    throw new MalformedParametersException("Invalid parameter name \"" + name + "\"");
+                }
+            }
+
+            if (mods != (mods & mask)) {
+                throw new MalformedParametersException("Invalid parameter modifiers");
+            }
+        }
+    }
+
+    private Parameter[] privateGetParameters() {
+        // Use tmp to avoid multiple writes to a volatile.
+        Parameter[] tmp = parameters;
+
+        if (tmp == null) {
+            // Android-changed: Commented for now.
+            /*
+            // Otherwise, go to the JVM to get them
+            try {
+                tmp = getParameters0();
+            } catch(IllegalArgumentException e) {
+                // Rethrow ClassFormatErrors
+                throw new MalformedParametersException("Invalid constant pool index");
+            }
+            */
+            // Android-changed: End of changes.
+
+            // If we get back nothing, then synthesize parameters
+            if (tmp == null) {
+                hasRealParameterData = false;
+                tmp = synthesizeAllParams();
+            } else {
+                hasRealParameterData = true;
+                verifyParameters(tmp);
+            }
+
+            parameters = tmp;
+        }
+
+        return tmp;
+    }
+
+    boolean hasRealParameterData() {
+        // If this somehow gets called before parameters gets
+        // initialized, force it into existence.
+        if (parameters == null) {
+            privateGetParameters();
+        }
+        return hasRealParameterData;
+    }
+
+    private transient volatile boolean hasRealParameterData;
+    private transient volatile Parameter[] parameters;
+
+    /**
      * Returns an array of {@code Class} objects that represent the
      * types of exceptions declared to be thrown by the underlying
      * executable represented by this object.  Returns an array of
@@ -289,9 +453,9 @@
      *     parameterized type that cannot be instantiated for any reason
      */
     public Type[] getGenericExceptionTypes() {
-        // Android-changed: Implemented in AbstractMethod instead.
-        throw new UnsupportedOperationException(
-                "Executable.getGenericExceptionTypes() not implemented");
+        // Android-changed: Changed to use Types / getMethodOrConstructorGenericInfoInternal()
+        return Types.getTypeArray(
+                getMethodOrConstructorGenericInfoInternal().genericExceptionTypes, false);
     }
 
     /**
@@ -310,7 +474,8 @@
      * to take a variable number of arguments.
      */
     public boolean isVarArgs()  {
-        return (getModifiers() & Modifier.VARARGS) != 0;
+        // Android-changed: Slightly more efficient.
+        return (accessFlags & Modifier.VARARGS) != 0;
     }
 
     /**
@@ -323,7 +488,8 @@
      * @jls 13.1 The Form of a Binary
      */
     public boolean isSynthetic() {
-        return Modifier.isSynthetic(getModifiers());
+        // Android-changed: Slightly more efficient.
+        return (accessFlags & Modifier.SYNTHETIC) != 0;
     }
 
     /**
@@ -345,8 +511,11 @@
      * A compiler may add extra parameters that are implicitly
      * declared in source ("mandated"), as well as parameters that
      * are neither implicitly nor explicitly declared in source
-     * ("synthetic") to the parameter list for a method.
+     * ("synthetic") to the parameter list for a method.  See {@link
+     * java.lang.reflect.Parameter} for more information.
      *
+     * @see java.lang.reflect.Parameter
+     * @see java.lang.reflect.Parameter#getAnnotations
      * @return an array of arrays that represent the annotations on
      *    the formal and implicit parameters, in declaration order, of
      *    the executable represented by this object
@@ -358,9 +527,11 @@
      * @throws NullPointerException  {@inheritDoc}
      */
     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
-        // Android-changed: Implemented in AbstractMethod instead.
-        throw new UnsupportedOperationException("Executable.getAnnotation(Class) not implemented");
+        Objects.requireNonNull(annotationClass);
+        // Android-changed: Implemented natively.
+        return getAnnotationNative(annotationClass);
     }
+    private native <T extends Annotation> T getAnnotationNative(Class<T> annotationClass);
 
     /**
      * {@inheritDoc}
@@ -377,8 +548,226 @@
      * {@inheritDoc}
      */
     public Annotation[] getDeclaredAnnotations()  {
-        // Android-changed: Implemented in AbstractMethod instead.
-        throw new UnsupportedOperationException(
-                "Executable.getDeclaredAnnotations() not implemented");
+        // Android-changed: Implemented natively.
+        return getDeclaredAnnotationsNative();
+    }
+    private native Annotation[] getDeclaredAnnotationsNative();
+
+    // Android-changed: Additional ART-related fields and logic below that is shared between
+    // Method and Constructor.
+
+    /** Bits encoding access (e.g. public, private) as well as other runtime specific flags */
+    @SuppressWarnings("unused") // set by runtime
+    private int accessFlags;
+
+    /**
+     * The ArtMethod associated with this Executable, required for dispatching due to entrypoints
+     * Classloader is held live by the declaring class.
+     */
+    @SuppressWarnings("unused") // set by runtime
+    private long artMethod;
+
+    /** Executable's declaring class */
+    @SuppressWarnings("unused") // set by runtime
+    private Class<?> declaringClass;
+
+    /**
+     * Overriden method's declaring class (same as declaringClass unless declaringClass is a proxy
+     * class).
+     */
+    @SuppressWarnings("unused") // set by runtime
+    private Class<?> declaringClassOfOverriddenMethod;
+
+    /** The method index of this method within its defining dex file */
+    @SuppressWarnings("unused") // set by runtime
+    private int dexMethodIndex;
+
+    /**
+     * We insert native method stubs for abstract methods so we don't have to
+     * check the access flags at the time of the method call.  This results in
+     * "native abstract" methods, which can't exist.  If we see the "abstract"
+     * flag set, clear the "native" flag.
+     *
+     * We also move the DECLARED_SYNCHRONIZED flag into the SYNCHRONIZED
+     * position, because the callers of this function are trying to convey
+     * the "traditional" meaning of the flags to their callers.
+     */
+    private static int fixMethodFlags(int flags) {
+        if ((flags & Modifier.ABSTRACT) != 0) {
+            flags &= ~Modifier.NATIVE;
+        }
+        flags &= ~Modifier.SYNCHRONIZED;
+        int ACC_DECLARED_SYNCHRONIZED = 0x00020000;
+        if ((flags & ACC_DECLARED_SYNCHRONIZED) != 0) {
+            flags |= Modifier.SYNCHRONIZED;
+        }
+        return flags & 0xffff;  // mask out bits not used by Java
+    }
+
+    final int getModifiersInternal() {
+        return fixMethodFlags(accessFlags);
+    }
+
+    final Class<?> getDeclaringClassInternal() {
+        return declaringClass;
+    }
+
+    /**
+     * Returns an array of {@code Class} objects associated with the parameter types of this
+     * Executable. If the Executable was declared with no parameters, an empty array will be
+     * returned.
+     *
+     * @return the parameter types
+     */
+    final Class<?>[] getParameterTypesInternal() {
+        Dex dex = declaringClassOfOverriddenMethod.getDex();
+        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
+        if (types.length == 0) {
+            return EmptyArray.CLASS;
+        }
+        Class<?>[] parametersArray = new Class[types.length];
+        for (int i = 0; i < types.length; i++) {
+            // Note, in the case of a Proxy the dex cache types are equal.
+            parametersArray[i] = declaringClassOfOverriddenMethod.getDexCacheType(dex, types[i]);
+        }
+        return parametersArray;
+    }
+
+    final int getParameterCountInternal() {
+        Dex dex = declaringClassOfOverriddenMethod.getDex();
+        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
+        return types.length;
+    }
+
+    // Android provides a more efficient implementation of this method for Executable than the one
+    // implemented in AnnotatedElement.
+    @Override
+    public final boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
+        Objects.requireNonNull(annotationType);
+        return isAnnotationPresentNative(annotationType);
+    }
+    private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType);
+
+    final Annotation[][] getParameterAnnotationsInternal() {
+        Annotation[][] parameterAnnotations = getParameterAnnotationsNative();
+        if (parameterAnnotations == null) {
+            parameterAnnotations = new Annotation[getParameterTypes().length][0];
+        }
+        return parameterAnnotations;
+    }
+    private native Annotation[][] getParameterAnnotationsNative();
+
+    /**
+     * @hide - exposed for use by {@link Class}.
+     */
+    public final int getAccessFlags() {
+        return accessFlags;
+    }
+
+    static final class GenericInfo {
+        final ListOfTypes genericExceptionTypes;
+        final ListOfTypes genericParameterTypes;
+        final Type genericReturnType;
+        final TypeVariable<?>[] formalTypeParameters;
+
+        GenericInfo(ListOfTypes exceptions, ListOfTypes parameters, Type ret,
+                TypeVariable<?>[] formal) {
+            genericExceptionTypes = exceptions;
+            genericParameterTypes = parameters;
+            genericReturnType = ret;
+            formalTypeParameters = formal;
+        }
+    }
+
+    final boolean hasGenericInformationInternal() {
+        return getSignatureAnnotation() != null;
+    }
+
+    /**
+     * Returns generic information associated with this method/constructor member.
+     */
+    final GenericInfo getMethodOrConstructorGenericInfoInternal() {
+        String signatureAttribute = getSignatureAttribute();
+        Class<?>[] exceptionTypes = this.getExceptionTypes();
+        GenericSignatureParser parser =
+                new GenericSignatureParser(this.getDeclaringClass().getClassLoader());
+        if (this instanceof Method) {
+            parser.parseForMethod(this, signatureAttribute, exceptionTypes);
+        } else {
+            parser.parseForConstructor(this, signatureAttribute, exceptionTypes);
+        }
+        return new GenericInfo(parser.exceptionTypes, parser.parameterTypes,
+                parser.returnType, parser.formalTypeParameters);
+    }
+
+    private String getSignatureAttribute() {
+        String[] annotation = getSignatureAnnotation();
+        if (annotation == null) {
+            return null;
+        }
+        StringBuilder result = new StringBuilder();
+        for (String s : annotation) {
+            result.append(s);
+        }
+        return result.toString();
+    }
+    private native String[] getSignatureAnnotation();
+
+    final boolean equalNameAndParametersInternal(Method m) {
+        return getName().equals(m.getName()) && equalMethodParameters(m.getParameterTypes());
+    }
+
+    private boolean equalMethodParameters(Class<?>[] params) {
+        Dex dex = declaringClassOfOverriddenMethod.getDex();
+        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
+        if (types.length != params.length) {
+            return false;
+        }
+        for (int i = 0; i < types.length; i++) {
+            if (declaringClassOfOverriddenMethod.getDexCacheType(dex, types[i]) != params[i]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    final int compareMethodParametersInternal(Class<?>[] params) {
+        Dex dex = declaringClassOfOverriddenMethod.getDex();
+        short[] types = dex.parameterTypeIndicesFromMethodIndex(dexMethodIndex);
+        int length = Math.min(types.length, params.length);
+        for (int i = 0; i < length; i++) {
+            Class<?> aType = declaringClassOfOverriddenMethod.getDexCacheType(dex, types[i]);
+            Class<?> bType = params[i];
+            if (aType != bType) {
+                int comparison = aType.getName().compareTo(bType.getName());
+                if (comparison != 0) {
+                    return comparison;
+                }
+            }
+        }
+        return types.length - params.length;
+    }
+
+    final String getMethodNameInternal() {
+        Dex dex = declaringClassOfOverriddenMethod.getDex();
+        int nameIndex = dex.nameIndexFromMethodIndex(dexMethodIndex);
+        return declaringClassOfOverriddenMethod.getDexCacheString(dex, nameIndex);
+    }
+
+    final Class<?> getMethodReturnTypeInternal() {
+        Dex dex = declaringClassOfOverriddenMethod.getDex();
+        int returnTypeIndex = dex.returnTypeIndexFromMethodIndex(dexMethodIndex);
+        // Note, in the case of a Proxy the dex cache types are equal.
+        return declaringClassOfOverriddenMethod.getDexCacheType(dex, returnTypeIndex);
+    }
+
+    /** A cheap implementation for {@link Method#isDefault()}. */
+    final boolean isDefaultMethodInternal() {
+        return (accessFlags & Modifier.DEFAULT) != 0;
+    }
+
+    /** A cheap implementation for {@link Method#isBridge()}. */
+    final boolean isBridgeMethodInternal() {
+        return (accessFlags & Modifier.BRIDGE) != 0;
     }
 }
diff --git a/ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java b/ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java
new file mode 100644
index 0000000..6b14b15
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.lang.reflect;
+
+/**
+ * Thrown when {@link java.lang.reflect.Executable#getParameters the
+ * java.lang.reflect package} attempts to read method parameters from
+ * a class file and determines that one or more parameters are
+ * malformed.
+ *
+ * <p>The following is a list of conditions under which this exception
+ * can be thrown:
+ * <ul>
+ * <li> The number of parameters (parameter_count) is wrong for the method
+ * <li> A constant pool index is out of bounds.
+ * <li> A constant pool index does not refer to a UTF-8 entry
+ * <li> A parameter's name is "", or contains an illegal character
+ * <li> The flags field contains an illegal flag (something other than
+ *     FINAL, SYNTHETIC, or MANDATED)
+ * </ul>
+ *
+ * See {@link java.lang.reflect.Executable#getParameters} for more
+ * information.
+ *
+ * @see java.lang.reflect.Executable#getParameters
+ * @since 1.8
+ * @hide Hidden pending tests
+ */
+public class MalformedParametersException extends RuntimeException {
+
+    /**
+     * Version for serialization.
+     */
+    private static final long serialVersionUID = 20130919L;
+
+    /**
+     * Create a {@code MalformedParametersException} with an empty
+     * reason.
+     */
+    public MalformedParametersException() {}
+
+    /**
+     * Create a {@code MalformedParametersException}.
+     *
+     * @param reason The reason for the exception.
+     */
+    public MalformedParametersException(String reason) {
+        super(reason);
+    }
+}
diff --git a/ojluni/src/main/java/java/lang/reflect/Method.java b/ojluni/src/main/java/java/lang/reflect/Method.java
index a9f2eb7..e3fa1c9 100644
--- a/ojluni/src/main/java/java/lang/reflect/Method.java
+++ b/ojluni/src/main/java/java/lang/reflect/Method.java
@@ -50,7 +50,7 @@
  * @author Kenneth Russell
  * @author Nakul Saraiya
  */
-public final class Method extends AbstractMethod  {
+public final class Method extends Executable  {
     /**
      * Orders methods by their name, parameters and return type.
      *
@@ -82,13 +82,19 @@
     private Method() {
     }
 
+    @Override
+    boolean hasGenericInformation() {
+        // Android-changed: Signature retrieval is handled in Executable.
+        return super.hasGenericInformationInternal();
+    }
+
     /**
      * {@inheritDoc}
-      */
+     */
     @Override
     public Class<?> getDeclaringClass() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getDeclaringClass();
+        // Android-changed: This is handled by Executable.
+        return super.getDeclaringClassInternal();
     }
 
     /**
@@ -97,7 +103,7 @@
      */
     @Override
     public String getName() {
-        // Android-changed: This is handled by AbstractMethod.
+        // Android-changed: This is handled by Executable.
         return getMethodNameInternal();
     }
 
@@ -106,8 +112,8 @@
      */
     @Override
     public int getModifiers() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getModifiers();
+        // Android-changed: This is handled by Executable.
+        return super.getModifiersInternal();
     }
 
     /**
@@ -118,7 +124,7 @@
     @Override
     @SuppressWarnings({"rawtypes", "unchecked"})
     public TypeVariable<Method>[] getTypeParameters() {
-        // Android-changed: This is mostly handled by AbstractMethod.
+        // Android-changed: This is mostly handled by Executable.
         GenericInfo info = getMethodOrConstructorGenericInfoInternal();
         return (TypeVariable<Method>[]) info.formalTypeParameters.clone();
     }
@@ -130,7 +136,7 @@
      * @return the return type for the method this object represents
      */
     public Class<?> getReturnType() {
-        // Android-changed: This is handled by AbstractMethod.
+        // Android-changed: This is handled by Executable.
         return getMethodReturnTypeInternal();
     }
 
@@ -159,6 +165,7 @@
      * @since 1.5
      */
     public Type getGenericReturnType() {
+        // Android-changed: Modified implementation to use Executable.
       return Types.getType(getMethodOrConstructorGenericInfoInternal().genericReturnType);
     }
 
@@ -167,16 +174,16 @@
      */
     @Override
     public Class<?>[] getParameterTypes() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getParameterTypes();
+        // Android-changed: This is handled by Executable.
+        return super.getParameterTypesInternal();
     }
 
     /**
      * {@inheritDoc}
      */
     public int getParameterCount() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getParameterCount();
+        // Android-changed: This is handled by Executable.
+        return super.getParameterCountInternal();
     }
 
     /**
@@ -238,7 +245,6 @@
         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
     }
 
-
     /**
      * Returns a string describing this {@code Method}.  The string is
      * formatted as the method access modifiers, if any, followed by
@@ -332,7 +338,6 @@
         sb.append(getName());
     }
 
-
     /**
      * Invokes the underlying method represented by this {@code Method}
      * object, on the specified object with the specified parameters.
@@ -402,7 +407,7 @@
      * @since 1.5
      */
     public boolean isBridge() {
-        // Android-changed: This is handled by AbstractMethod.
+        // Android-changed: This is handled by Executable.
         return super.isBridgeMethodInternal();
     }
 
@@ -425,7 +430,6 @@
         return super.isSynthetic();
     }
 
-
     /**
      * Returns {@code true} if this method is a default
      * method; returns {@code false} otherwise.
@@ -439,7 +443,7 @@
      * @since 1.8
      */
     public boolean isDefault() {
-        // Android-changed: This is handled by AbstractMethod.
+        // Android-changed: This is handled by Executable.
         return super.isDefaultMethodInternal();
     }
 
@@ -482,8 +486,8 @@
      */
     @Override
     public Annotation[][] getParameterAnnotations() {
-        // Android-changed: This is handled by AbstractMethod.
-        return super.getParameterAnnotations();
+        // Android-changed: This is handled by Executable.
+        return super.getParameterAnnotationsInternal();
     }
 
     /**
diff --git a/ojluni/src/main/java/java/lang/reflect/Parameter.java b/ojluni/src/main/java/java/lang/reflect/Parameter.java
new file mode 100644
index 0000000..2d96307
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/reflect/Parameter.java
@@ -0,0 +1,323 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.lang.reflect;
+
+import java.lang.annotation.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import libcore.reflect.AnnotatedElements;
+
+/**
+ * Information about method parameters.
+ *
+ * A {@code Parameter} provides information about method parameters,
+ * including its name and modifiers.  It also provides an alternate
+ * means of obtaining attributes for the parameter.
+ *
+ * @since 1.8
+ */
+public final class Parameter implements AnnotatedElement {
+
+    private final String name;
+    private final int modifiers;
+    private final Executable executable;
+    private final int index;
+
+    /**
+     * Package-private constructor for {@code Parameter}.
+     *
+     * If method parameter data is present in the classfile, then the
+     * JVM creates {@code Parameter} objects directly.  If it is
+     * absent, however, then {@code Executable} uses this constructor
+     * to synthesize them.
+     *
+     * @param name The name of the parameter.
+     * @param modifiers The modifier flags for the parameter.
+     * @param executable The executable which defines this parameter.
+     * @param index The index of the parameter.
+     */
+    Parameter(String name,
+              int modifiers,
+              Executable executable,
+              int index) {
+        this.name = name;
+        this.modifiers = modifiers;
+        this.executable = executable;
+        this.index = index;
+    }
+
+    /**
+     * Compares based on the executable and the index.
+     *
+     * @param obj The object to compare.
+     * @return Whether or not this is equal to the argument.
+     */
+    public boolean equals(Object obj) {
+        if(obj instanceof Parameter) {
+            Parameter other = (Parameter)obj;
+            return (other.executable.equals(executable) &&
+                    other.index == index);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code based on the executable's hash code and the
+     * index.
+     *
+     * @return A hash code based on the executable's hash code.
+     */
+    public int hashCode() {
+        return executable.hashCode() ^ index;
+    }
+
+    // Android-changed: Removed references to the class file format.
+    /**
+     * Returns true if the parameter has a name; returns false otherwise.
+     * Whether a parameter has a name is determined by compiler options
+     * and whether the parameter is synthesized.
+     *
+     * @return true if and only if the parameter has a name
+     */
+    public boolean isNamePresent() {
+        return executable.hasRealParameterData() && name != null;
+    }
+
+    /**
+     * Returns a string describing this parameter.  The format is the
+     * modifiers for the parameter, if any, in canonical order as
+     * recommended by <cite>The Java&trade; Language
+     * Specification</cite>, followed by the fully- qualified type of
+     * the parameter (excluding the last [] if the parameter is
+     * variable arity), followed by "..." if the parameter is variable
+     * arity, followed by a space, followed by the name of the
+     * parameter.
+     *
+     * @return A string representation of the parameter and associated
+     * information.
+     */
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        final Type type = getParameterizedType();
+        final String typename = type.getTypeName();
+
+        sb.append(Modifier.toString(getModifiers()));
+
+        if(0 != modifiers)
+            sb.append(' ');
+
+        if(isVarArgs())
+            sb.append(typename.replaceFirst("\\[\\]$", "..."));
+        else
+            sb.append(typename);
+
+        sb.append(' ');
+        sb.append(getName());
+
+        return sb.toString();
+    }
+
+    /**
+     * Return the {@code Executable} which declares this parameter.
+     *
+     * @return The {@code Executable} declaring this parameter.
+     */
+    public Executable getDeclaringExecutable() {
+        return executable;
+    }
+
+    /**
+     * Get the modifier flags for this the parameter represented by
+     * this {@code Parameter} object.
+     *
+     * @return The modifier flags for this parameter.
+     */
+    public int getModifiers() {
+        return modifiers;
+    }
+
+    /**
+     * Returns the name of the parameter.  If the parameter's name is
+     * {@linkplain #isNamePresent() present}, then this method returns
+     * the name provided by the class file. Otherwise, this method
+     * synthesizes a name of the form argN, where N is the index of
+     * the parameter in the descriptor of the method which declares
+     * the parameter.
+     *
+     * @return The name of the parameter, either provided by the class
+     *         file or synthesized if the class file does not provide
+     *         a name.
+     */
+    public String getName() {
+        // Note: empty strings as paramete names are now outlawed.
+        // The .equals("") is for compatibility with current JVM
+        // behavior.  It may be removed at some point.
+        if(name == null || name.equals(""))
+            return "arg" + index;
+        else
+            return name;
+    }
+
+    // Package-private accessor to the real name field.
+    String getRealName() {
+        return name;
+    }
+
+    /**
+     * Returns a {@code Type} object that identifies the parameterized
+     * type for the parameter represented by this {@code Parameter}
+     * object.
+     *
+     * @return a {@code Type} object identifying the parameterized
+     * type of the parameter represented by this object
+     */
+    public Type getParameterizedType() {
+        Type tmp = parameterTypeCache;
+        if (null == tmp) {
+            tmp = executable.getAllGenericParameterTypes()[index];
+            parameterTypeCache = tmp;
+        }
+
+        return tmp;
+    }
+
+    private transient volatile Type parameterTypeCache = null;
+
+    /**
+     * Returns a {@code Class} object that identifies the
+     * declared type for the parameter represented by this
+     * {@code Parameter} object.
+     *
+     * @return a {@code Class} object identifying the declared
+     * type of the parameter represented by this object
+     */
+    public Class<?> getType() {
+        Class<?> tmp = parameterClassCache;
+        if (null == tmp) {
+            tmp = executable.getParameterTypes()[index];
+            parameterClassCache = tmp;
+        }
+        return tmp;
+    }
+
+    private transient volatile Class<?> parameterClassCache = null;
+
+    /**
+     * Returns {@code true} if this parameter is implicitly declared
+     * in source code; returns {@code false} otherwise.
+     *
+     * @return true if and only if this parameter is implicitly
+     * declared as defined by <cite>The Java&trade; Language
+     * Specification</cite>.
+     */
+    public boolean isImplicit() {
+        return Modifier.isMandated(getModifiers());
+    }
+
+    /**
+     * Returns {@code true} if this parameter is neither implicitly
+     * nor explicitly declared in source code; returns {@code false}
+     * otherwise.
+     *
+     * @jls 13.1 The Form of a Binary
+     * @return true if and only if this parameter is a synthetic
+     * construct as defined by
+     * <cite>The Java&trade; Language Specification</cite>.
+     */
+    public boolean isSynthetic() {
+        return Modifier.isSynthetic(getModifiers());
+    }
+
+    /**
+     * Returns {@code true} if this parameter represents a variable
+     * argument list; returns {@code false} otherwise.
+     *
+     * @return {@code true} if an only if this parameter represents a
+     * variable argument list.
+     */
+    public boolean isVarArgs() {
+        return executable.isVarArgs() &&
+            index == executable.getParameterCount() - 1;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     * @throws NullPointerException {@inheritDoc}
+     */
+    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
+        Objects.requireNonNull(annotationClass);
+        // Android-changed: Uses native code to obtain annotation information.
+        return getAnnotationNative(executable, index, annotationClass);
+    }
+    private static native <A extends Annotation> A getAnnotationNative(
+            Executable executable, int parameterIndex, Class<A> annotationType);
+
+    /**
+     * {@inheritDoc}
+     * @throws NullPointerException {@inheritDoc}
+     */
+    @Override
+    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
+        // Android-changed: Uses AnnotatedElements instead.
+        return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Annotation[] getDeclaredAnnotations() {
+        return executable.getParameterAnnotations()[index];
+    }
+
+    /**
+     * @throws NullPointerException {@inheritDoc}
+     */
+    public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
+        // Only annotations on classes are inherited, for all other
+        // objects getDeclaredAnnotation is the same as
+        // getAnnotation.
+        return getAnnotation(annotationClass);
+    }
+
+    /**
+     * @throws NullPointerException {@inheritDoc}
+     */
+    @Override
+    public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
+        // Only annotations on classes are inherited, for all other
+        // objects getDeclaredAnnotations is the same as
+        // getAnnotations.
+        return getAnnotationsByType(annotationClass);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Annotation[] getAnnotations() {
+        return getDeclaredAnnotations();
+    }
+}
diff --git a/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java b/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java
index daa7b92..6f9f7dc 100644
--- a/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java
+++ b/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java
@@ -303,11 +303,16 @@
             ret = socketGetOption(opt, null);
             return new Integer(ret);
         case IP_TOS:
-            ret = socketGetOption(opt, null);
-            if (ret == -1) { // ipv6 tos
-                return new Integer(trafficClass);
-            } else {
-                return new Integer(ret);
+            try {
+                ret = socketGetOption(opt, null);
+                if (ret == -1) { // ipv6 tos
+                    return trafficClass;
+                } else {
+                    return ret;
+                }
+            } catch (SocketException se) {
+                // TODO - should make better effort to read TOS or TCLASS
+                return trafficClass; // ipv6 tos
             }
         case SO_KEEPALIVE:
             ret = socketGetOption(opt, null);
diff --git a/ojluni/src/main/java/java/net/DatagramSocket.java b/ojluni/src/main/java/java/net/DatagramSocket.java
index a6f09fa..f9dbaff 100755
--- a/ojluni/src/main/java/java/net/DatagramSocket.java
+++ b/ojluni/src/main/java/java/net/DatagramSocket.java
@@ -152,46 +152,44 @@
         if (!isBound())
           bind(new InetSocketAddress(0));
 
-        // ----- BEGIN android -----
-        connectedAddress = address;
-        connectedPort = port;
-        // ----- END android -----
-
-        // old impls do not support connect/disconnect
-        if (oldImpl || (impl instanceof AbstractPlainDatagramSocketImpl &&
-             ((AbstractPlainDatagramSocketImpl)impl).nativeConnectDisabled())) {
-            connectState = ST_CONNECTED_NO_IMPL;
-        } else {
-          /* ----- BEGIN android -----
-          try {
-              getImpl().connect(address, port);
-
-              // socket is now connected by the impl
-              connectState = ST_CONNECTED;
-          } catch (SocketException se) {
-                // connection will be emulated by DatagramSocket
+        // Android-changed: This section now throws any SocketException generated by connect()
+        // to enable it to be recorded as the pendingConnectException. It has been enclosed in a
+        // try-finally to ensure connectedAddress and connectedPort are set when the exception
+        // is thrown.
+        try {
+            // old impls do not support connect/disconnect
+            // Android-changed: Added special handling for AbstractPlainDatagramSocketImpl in
+            // the condition below.
+            if (oldImpl || (impl instanceof AbstractPlainDatagramSocketImpl &&
+                    ((AbstractPlainDatagramSocketImpl)impl).nativeConnectDisabled())) {
                 connectState = ST_CONNECTED_NO_IMPL;
-          }*/
-          getImpl().connect(address, port);
+            } else {
+                try {
+                    getImpl().connect(address, port);
 
-          // socket is now connected by the impl
-          connectState = ST_CONNECTED;
-          // Do we need to filter some packets?
-          int avail = getImpl().dataAvailable();
-          if (avail == -1) {
-              throw new SocketException();
-          }
-          explicitFilter = avail > 0;
-          if (explicitFilter) {
-              bytesLeftToFilter = getReceiveBufferSize();
-          }
-          // ----- END android -----
+                    // socket is now connected by the impl
+                    connectState = ST_CONNECTED;
+
+                    // Do we need to filter some packets?
+                    int avail = getImpl().dataAvailable();
+                    if (avail == -1) {
+                        throw new SocketException();
+                    }
+                    explicitFilter = avail > 0;
+                    if (explicitFilter) {
+                        bytesLeftToFilter = getReceiveBufferSize();
+                    }
+                } catch (SocketException se) {
+                    // connection will be emulated by DatagramSocket
+                    connectState = ST_CONNECTED_NO_IMPL;
+                    // Android-changed: Propagate the SocketException so connect() can store it.
+                    throw se;
+                }
+           }
+        } finally {
+            connectedAddress = address;
+            connectedPort = port;
         }
-
-        /* ----- BEGIN android -----
-        connectedAddress = address;
-        connectedPort = port;
-        ----- END android ----- */
     }
 
 
@@ -1219,7 +1217,14 @@
 
         if (isClosed())
             throw new SocketException("Socket is closed");
-        getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
+        try {
+            getImpl().setOption(SocketOptions.IP_TOS, tc);
+        } catch (SocketException se) {
+            // not supported if socket already connected
+            // Solaris returns error in such cases
+            if(!isConnected())
+                throw se;
+        }
     }
 
     /**
diff --git a/ojluni/src/main/java/java/net/PlainDatagramSocketImpl.java b/ojluni/src/main/java/java/net/PlainDatagramSocketImpl.java
index ae1e3a5..ea45500 100644
--- a/ojluni/src/main/java/java/net/PlainDatagramSocketImpl.java
+++ b/ojluni/src/main/java/java/net/PlainDatagramSocketImpl.java
@@ -69,6 +69,15 @@
         return (T)flow;
     }
 
+    protected void socketSetOption(int opt, Object val) throws SocketException {
+        try {
+            socketSetOption0(opt, val);
+        } catch (SocketException se) {
+            if (!connected)
+                throw se;
+        }
+    }
+
     protected synchronized native void bind0(int lport, InetAddress laddr)
         throws SocketException;
 
@@ -99,7 +108,7 @@
 
     protected native void datagramSocketClose();
 
-    protected native void socketSetOption(int opt, Object val)
+    protected native void socketSetOption0(int opt, Object val)
         throws SocketException;
 
     protected native Object socketGetOption(int opt) throws SocketException;
diff --git a/ojluni/src/main/java/java/net/PlainSocketImpl.java b/ojluni/src/main/java/java/net/PlainSocketImpl.java
index 1409c7b..e2cf44e 100644
--- a/ojluni/src/main/java/java/net/PlainSocketImpl.java
+++ b/ojluni/src/main/java/java/net/PlainSocketImpl.java
@@ -82,6 +82,15 @@
         return (T)flow;
     }
 
+    protected void socketSetOption(int opt, boolean b, Object val) throws SocketException {
+        try {
+            socketSetOption0(opt, b, val);
+        } catch (SocketException se) {
+            if (socket == null || !socket.isConnected())
+                throw se;
+        }
+    }
+
     native void socketCreate(boolean isServer) throws IOException;
 
     native void socketConnect(InetAddress address, int port, int timeout)
@@ -100,7 +109,7 @@
 
     native void socketShutdown(int howto) throws IOException;
 
-    native void socketSetOption(int cmd, boolean on, Object value)
+    native void socketSetOption0(int cmd, boolean on, Object value)
         throws SocketException;
 
     native int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
diff --git a/ojluni/src/main/java/java/net/Socket.java b/ojluni/src/main/java/java/net/Socket.java
index 54f599f..28cd8c4 100644
--- a/ojluni/src/main/java/java/net/Socket.java
+++ b/ojluni/src/main/java/java/net/Socket.java
@@ -1405,7 +1405,14 @@
 
         if (isClosed())
             throw new SocketException("Socket is closed");
-        getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
+        try {
+            getImpl().setOption(SocketOptions.IP_TOS, tc);
+        } catch (SocketException se) {
+            // not supported if socket already connected
+            // Solaris returns error in such cases
+            if(!isConnected())
+                throw se;
+        }
     }
 
     /**
diff --git a/ojluni/src/main/java/java/security/DigestOutputStream.java b/ojluni/src/main/java/java/security/DigestOutputStream.java
index 1307bdf..d7f777b 100644
--- a/ojluni/src/main/java/java/security/DigestOutputStream.java
+++ b/ojluni/src/main/java/java/security/DigestOutputStream.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,13 +37,13 @@
  * the bits going through the stream.
  *
  * <p>To complete the message digest computation, call one of the
- * <code>digest</code> methods on the associated message
- * digest after your calls to one of this digest ouput stream's
+ * {@code digest} methods on the associated message
+ * digest after your calls to one of this digest output stream's
  * {@link #write(int) write} methods.
  *
  * <p>It is possible to turn this stream on or off (see
  * {@link #on(boolean) on}). When it is on, a call to one of the
- * <code>write</code> methods results in
+ * {@code write} methods results in
  * an update on the message digest.  But when it is off, the message
  * digest is not updated. The default is for the stream to be on.
  *
@@ -99,8 +99,8 @@
      * the specified byte, and in any case writes the byte
      * to the output stream. That is, if the digest function is on
      * (see {@link #on(boolean) on}), this method calls
-     * <code>update</code> on the message digest associated with this
-     * stream, passing it the byte <code>b</code>. This method then
+     * {@code update} on the message digest associated with this
+     * stream, passing it the byte {@code b}. This method then
      * writes the byte to the output stream, blocking until the byte
      * is actually written.
      *
@@ -112,17 +112,17 @@
      * @see MessageDigest#update(byte)
      */
     public void write(int b) throws IOException {
+        out.write(b);
         if (on) {
             digest.update((byte)b);
         }
-        out.write(b);
     }
 
     /**
      * Updates the message digest (if the digest function is on) using
      * the specified subarray, and in any case writes the subarray to
      * the output stream. That is, if the digest function is on (see
-     * {@link #on(boolean) on}), this method calls <code>update</code>
+     * {@link #on(boolean) on}), this method calls {@code update}
      * on the message digest associated with this stream, passing it
      * the subarray specifications. This method then writes the subarray
      * bytes to the output stream, blocking until the bytes are actually
@@ -131,26 +131,35 @@
      * @param b the array containing the subarray to be used for updating
      * and writing to the output stream.
      *
-     * @param off the offset into <code>b</code> of the first byte to
+     * @param off the offset into {@code b} of the first byte to
      * be updated and written.
      *
      * @param len the number of bytes of data to be updated and written
-     * from <code>b</code>, starting at offset <code>off</code>.
+     * from {@code b}, starting at offset {@code off}.
      *
      * @exception IOException if an I/O error occurs.
      *
      * @see MessageDigest#update(byte[], int, int)
      */
     public void write(byte[] b, int off, int len) throws IOException {
+        // BEGIN ANDROID-ADDED: perform checks for parameters first.
+        // See org.apache.harmony.security.tests.j.s.DigestOutputStreamTest#test_write$BII_6
+        if (b == null || off + len > b.length) {
+            throw new IllegalArgumentException("wrong parameters for write");
+        }
+        if (off < 0 || len < 0) {
+            throw new IndexOutOfBoundsException("wrong index for write");
+        }
+        // END ANDROID-ADDED
+        out.write(b, off, len);
         if (on) {
             digest.update(b, off, len);
         }
-        out.write(b, off, len);
     }
 
     /**
      * Turns the digest function on or off. The default is on.  When
-     * it is on, a call to one of the <code>write</code> methods results in an
+     * it is on, a call to one of the {@code write} methods results in an
      * update on the message digest.  But when it is off, the message
      * digest is not updated.
      *
diff --git a/ojluni/src/main/java/java/security/PrivilegedActionException.java b/ojluni/src/main/java/java/security/PrivilegedActionException.java
index af89fb5..83d855a 100644
--- a/ojluni/src/main/java/java/security/PrivilegedActionException.java
+++ b/ojluni/src/main/java/java/security/PrivilegedActionException.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,11 +27,81 @@
 
 /**
  * Legacy security code; do not use.
+ *
+ * This exception is thrown by
+ * {@code doPrivileged(PrivilegedExceptionAction)} and
+ * {@code doPrivileged(PrivilegedExceptionAction,
+ * AccessControlContext context)} to indicate
+ * that the action being performed threw a checked exception.  The exception
+ * thrown by the action can be obtained by calling the
+ * {@code getException} method.  In effect, an
+ * {@code PrivilegedActionException} is a "wrapper"
+ * for an exception thrown by a privileged action.
+ *
+ * <p>As of release 1.4, this exception has been retrofitted to conform to
+ * the general purpose exception-chaining mechanism.  The "exception thrown
+ * by the privileged computation" that is provided at construction time and
+ * accessed via the {@link #getException()} method is now known as the
+ * <i>cause</i>, and may be accessed via the {@link Throwable#getCause()}
+ * method, as well as the aforementioned "legacy method."
+ *
+ * @see PrivilegedExceptionAction
+ * @see AccessController#doPrivileged(PrivilegedExceptionAction)
+ * @see AccessController#doPrivileged(PrivilegedExceptionAction,AccessControlContext)
  */
-
 public class PrivilegedActionException extends Exception {
+    // use serialVersionUID from JDK 1.2.2 for interoperability
+    private static final long serialVersionUID = 4724086851538908602L;
 
-    public PrivilegedActionException(Exception exception) { super(exception); }
+    /**
+     * @serial
+     */
+    private Exception exception;
 
-    public Exception getException() { return null; }
+    /**
+     * Constructs a new PrivilegedActionException &quot;wrapping&quot;
+     * the specific Exception.
+     *
+     * @param exception The exception thrown
+     */
+    public PrivilegedActionException(Exception exception) {
+        super((Throwable)null);  // Disallow initCause
+        this.exception = exception;
+    }
+
+    /**
+     * Returns the exception thrown by the privileged computation that
+     * resulted in this {@code PrivilegedActionException}.
+     *
+     * <p>This method predates the general-purpose exception chaining facility.
+     * The {@link Throwable#getCause()} method is now the preferred means of
+     * obtaining this information.
+     *
+     * @return the exception thrown by the privileged computation that
+     *         resulted in this {@code PrivilegedActionException}.
+     * @see PrivilegedExceptionAction
+     * @see AccessController#doPrivileged(PrivilegedExceptionAction)
+     * @see AccessController#doPrivileged(PrivilegedExceptionAction,
+     *                                            AccessControlContext)
+     */
+    public Exception getException() {
+        return exception;
+    }
+
+    /**
+     * Returns the cause of this exception (the exception thrown by
+     * the privileged computation that resulted in this
+     * {@code PrivilegedActionException}).
+     *
+     * @return  the cause of this exception.
+     * @since   1.4
+     */
+    public Throwable getCause() {
+        return exception;
+    }
+
+    public String toString() {
+        String s = getClass().getName();
+        return (exception != null) ? (s + ": " + exception.toString()) : s;
+    }
 }
diff --git a/ojluni/src/main/java/java/security/Provider.java b/ojluni/src/main/java/java/security/Provider.java
index 81ae9f3..a932893 100644
--- a/ojluni/src/main/java/java/security/Provider.java
+++ b/ojluni/src/main/java/java/security/Provider.java
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,8 +35,9 @@
 import java.lang.ref.*;
 import java.lang.reflect.*;
 import java.security.Security;
-import java.security.cert.CertStoreParameters;
 import java.util.function.BiConsumer;
+import java.util.function.BiFunction;
+import java.util.function.Function;
 
 /**
  * This class represents a "provider" for the
@@ -69,20 +70,21 @@
  * security token. A {@link ProviderException} should be used to indicate
  * such errors.
  *
- * <p>The service type <code>Provider</code> is reserved for use by the
+ * <p>The service type {@code Provider} is reserved for use by the
  * security framework. Services of this type cannot be added, removed,
  * or modified by applications.
  * The following attributes are automatically placed in each Provider object:
  * <table cellspacing=4>
+ * <caption><b>Attributes Automatically Placed in a Provider Object</b></caption>
  * <tr><th>Name</th><th>Value</th>
- * <tr><td><code>Provider.id name</code></td>
-  *    <td><code>String.valueOf(provider.getName())</code></td>
- * <tr><td><code>Provider.id version</code></td>
- *     <td><code>String.valueOf(provider.getVersion())</code></td>
- * <tr><td><code>Provider.id info</code></td>
-       <td><code>String.valueOf(provider.getInfo())</code></td>
- * <tr><td><code>Provider.id className</code></td>
- *     <td><code>provider.getClass().getName()</code></td>
+ * <tr><td>{@code Provider.id name}</td>
+  *    <td>{@code String.valueOf(provider.getName())}</td>
+ * <tr><td>{@code Provider.id version}</td>
+ *     <td>{@code String.valueOf(provider.getVersion())}</td>
+ * <tr><td>{@code Provider.id info}</td>
+       <td>{@code String.valueOf(provider.getInfo())}</td>
+ * <tr><td>{@code Provider.id className}</td>
+ *     <td>{@code provider.getClass().getName()}</td>
  * </table>
  *
  * @author Benjamin Renaud
@@ -193,23 +195,19 @@
      * Clears this provider so that it no longer contains the properties
      * used to look up facilities implemented by the provider.
      *
-     * <p>First, if there is a security manager, its
-     * <code>checkSecurityAccess</code> method is called with the string
-     * <code>"clearProviderProperties."+name</code> (where <code>name</code>
-     * is the provider name) to see if it's ok to clear this provider.
-     * If the default implementation of <code>checkSecurityAccess</code>
-     * is used (that is, that method is not overriden), then this results in
-     * a call to the security manager's <code>checkPermission</code> method
-     * with a <code>SecurityPermission("clearProviderProperties."+name)</code>
-     * permission.
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "clearProviderProperties."+name}
+     * (where {@code name} is the provider name) to see if it's ok to clear
+     * this provider.
      *
      * @throws  SecurityException
-     *          if a security manager exists and its <code>{@link
-     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
      *          denies access to clear this provider
      *
      * @since 1.2
      */
+    @Override
     public synchronized void clear() {
         check("clearProviderProperties."+name);
         if (debug != null) {
@@ -226,6 +224,7 @@
      *               input stream.
      * @see java.util.Properties#load
      */
+    @Override
     public synchronized void load(InputStream inStream) throws IOException {
         check("putProviderProperty."+name);
         if (debug != null) {
@@ -243,6 +242,7 @@
      *
      * @since 1.2
      */
+    @Override
     public synchronized void putAll(Map<?,?> t) {
         check("putProviderProperty."+name);
         if (debug != null) {
@@ -258,6 +258,7 @@
      * @see   java.util.Map.Entry
      * @since 1.2
      */
+    @Override
     public synchronized Set<Map.Entry<Object,Object>> entrySet() {
         checkInitialized();
         if (entrySet == null) {
@@ -284,6 +285,7 @@
      *
      * @since 1.2
      */
+    @Override
     public Set<Object> keySet() {
         checkInitialized();
         return Collections.unmodifiableSet(super.keySet());
@@ -295,39 +297,29 @@
      *
      * @since 1.2
      */
+    @Override
     public Collection<Object> values() {
         checkInitialized();
         return Collections.unmodifiableCollection(super.values());
     }
 
     /**
-     * Sets the <code>key</code> property to have the specified
-     * <code>value</code>.
+     * Sets the {@code key} property to have the specified
+     * {@code value}.
      *
-     * <p>First, if there is a security manager, its
-     * <code>checkSecurityAccess</code> method is called with the string
-     * <code>"putProviderProperty."+name</code>, where <code>name</code> is the
-     * provider name, to see if it's ok to set this provider's property values.
-     * If the default implementation of <code>checkSecurityAccess</code>
-     * is used (that is, that method is not overriden), then this results in
-     * a call to the security manager's <code>checkPermission</code> method
-     * with a <code>SecurityPermission("putProviderProperty."+name)</code>
-     * permission.
-     *
-     * @param key the property key.
-     *
-     * @param value the property value.
-     *
-     * @return the previous value of the specified property
-     * (<code>key</code>), or null if it did not have one.
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "putProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to set this
+     * provider's property values.
      *
      * @throws  SecurityException
-     *          if a security manager exists and its <code>{@link
-     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
      *          denies access to set property values.
      *
      * @since 1.2
      */
+    @Override
     public synchronized Object put(Object key, Object value) {
         check("putProviderProperty."+name);
         if (debug != null) {
@@ -338,32 +330,49 @@
     }
 
     /**
-     * Removes the <code>key</code> property (and its corresponding
-     * <code>value</code>).
+     * If the specified key is not already associated with a value (or is mapped
+     * to {@code null}) associates it with the given value and returns
+     * {@code null}, else returns the current value.
      *
-     * <p>First, if there is a security manager, its
-     * <code>checkSecurityAccess</code> method is called with the string
-     * <code>"removeProviderProperty."+name</code>, where <code>name</code> is
-     * the provider name, to see if it's ok to remove this provider's
-     * properties. If the default implementation of
-     * <code>checkSecurityAccess</code> is used (that is, that method is not
-     * overriden), then this results in a call to the security manager's
-     * <code>checkPermission</code> method with a
-     * <code>SecurityPermission("removeProviderProperty."+name)</code>
-     * permission.
-     *
-     * @param key the key for the property to be removed.
-     *
-     * @return the value to which the key had been mapped,
-     * or null if the key did not have a mapping.
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "putProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to set this
+     * provider's property values.
      *
      * @throws  SecurityException
-     *          if a security manager exists and its <code>{@link
-     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object putIfAbsent(Object key, Object value) {
+        check("putProviderProperty."+name);
+        if (debug != null) {
+            debug.println("Set " + name + " provider property [" +
+                          key + "/" + value +"]");
+        }
+        return implPutIfAbsent(key, value);
+    }
+
+    /**
+     * Removes the {@code key} property (and its corresponding
+     * {@code value}).
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "removeProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to remove this
+     * provider's properties.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
      *          denies access to remove this provider's properties.
      *
      * @since 1.2
      */
+    @Override
     public synchronized Object remove(Object key) {
         check("removeProviderProperty."+name);
         if (debug != null) {
@@ -372,11 +381,247 @@
         return implRemove(key);
     }
 
+    /**
+     * Removes the entry for the specified key only if it is currently
+     * mapped to the specified value.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "removeProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to remove this
+     * provider's properties.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to remove this provider's properties.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized boolean remove(Object key, Object value) {
+        check("removeProviderProperty."+name);
+        if (debug != null) {
+            debug.println("Remove " + name + " provider property " + key);
+        }
+        return implRemove(key, value);
+    }
+
+    /**
+     * Replaces the entry for the specified key only if currently
+     * mapped to the specified value.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "putProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to set this
+     * provider's property values.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized boolean replace(Object key, Object oldValue,
+            Object newValue) {
+        check("putProviderProperty." + name);
+
+        if (debug != null) {
+            debug.println("Replace " + name + " provider property " + key);
+        }
+        return implReplace(key, oldValue, newValue);
+    }
+
+    /**
+     * Replaces the entry for the specified key only if it is
+     * currently mapped to some value.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "putProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to set this
+     * provider's property values.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object replace(Object key, Object value) {
+        check("putProviderProperty." + name);
+
+        if (debug != null) {
+            debug.println("Replace " + name + " provider property " + key);
+        }
+        return implReplace(key, value);
+    }
+
+    /**
+     * Replaces each entry's value with the result of invoking the given
+     * function on that entry, in the order entries are returned by an entry
+     * set iterator, until all entries have been processed or the function
+     * throws an exception.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the string {@code "putProviderProperty."+name},
+     * where {@code name} is the provider name, to see if it's ok to set this
+     * provider's property values.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized void replaceAll(BiFunction<? super Object, ? super Object, ? extends Object> function) {
+        check("putProviderProperty." + name);
+
+        if (debug != null) {
+            debug.println("ReplaceAll " + name + " provider property ");
+        }
+        implReplaceAll(function);
+    }
+
+    /**
+     * Attempts to compute a mapping for the specified key and its
+     * current mapped value (or {@code null} if there is no current
+     * mapping).
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the strings {@code "putProviderProperty."+name}
+     * and {@code "removeProviderProperty."+name}, where {@code name} is the
+     * provider name, to see if it's ok to set this provider's property values
+     * and remove this provider's properties.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values or remove properties.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object compute(Object key,
+        BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
+        check("putProviderProperty." + name);
+        check("removeProviderProperty" + name);
+
+        if (debug != null) {
+            debug.println("Compute " + name + " provider property " + key);
+        }
+        return implCompute(key, remappingFunction);
+    }
+
+    /**
+     * If the specified key is not already associated with a value (or
+     * is mapped to {@code null}), attempts to compute its value using
+     * the given mapping function and enters it into this map unless
+     * {@code null}.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the strings {@code "putProviderProperty."+name}
+     * and {@code "removeProviderProperty."+name}, where {@code name} is the
+     * provider name, to see if it's ok to set this provider's property values
+     * and remove this provider's properties.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values and remove properties.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object computeIfAbsent(Object key, Function<? super Object, ? extends Object> mappingFunction) {
+        check("putProviderProperty." + name);
+        check("removeProviderProperty" + name);
+
+        if (debug != null) {
+            debug.println("ComputeIfAbsent " + name + " provider property " +
+                    key);
+        }
+        return implComputeIfAbsent(key, mappingFunction);
+    }
+
+    /**
+     * If the value for the specified key is present and non-null, attempts to
+     * compute a new mapping given the key and its current mapped value.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the strings {@code "putProviderProperty."+name}
+     * and {@code "removeProviderProperty."+name}, where {@code name} is the
+     * provider name, to see if it's ok to set this provider's property values
+     * and remove this provider's properties.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values or remove properties.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object computeIfPresent(Object key, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
+        check("putProviderProperty." + name);
+        check("removeProviderProperty" + name);
+
+        if (debug != null) {
+            debug.println("ComputeIfPresent " + name + " provider property " +
+                    key);
+        }
+        return implComputeIfPresent(key, remappingFunction);
+    }
+
+    /**
+     * If the specified key is not already associated with a value or is
+     * associated with null, associates it with the given value. Otherwise,
+     * replaces the value with the results of the given remapping function,
+     * or removes if the result is null. This method may be of use when
+     * combining multiple mapped values for a key.
+     *
+     * <p>If a security manager is enabled, its {@code checkSecurityAccess}
+     * method is called with the strings {@code "putProviderProperty."+name}
+     * and {@code "removeProviderProperty."+name}, where {@code name} is the
+     * provider name, to see if it's ok to set this provider's property values
+     * and remove this provider's properties.
+     *
+     * @throws  SecurityException
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method
+     *          denies access to set property values or remove properties.
+     *
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object merge(Object key, Object value,  BiFunction<? super Object, ? super Object, ? extends Object>  remappingFunction) {
+        check("putProviderProperty." + name);
+        check("removeProviderProperty" + name);
+
+        if (debug != null) {
+            debug.println("Merge " + name + " provider property " + key);
+        }
+        return implMerge(key, value, remappingFunction);
+    }
+
     // let javadoc show doc from superclass
+    @Override
     public Object get(Object key) {
         checkInitialized();
         return super.get(key);
     }
+    /**
+     * @since 1.8
+     */
+    @Override
+    public synchronized Object getOrDefault(Object key, Object defaultValue) {
+        checkInitialized();
+        return super.getOrDefault(key, defaultValue);
+    }
 
     /**
      * @since 1.8
@@ -388,12 +633,14 @@
     }
 
     // let javadoc show doc from superclass
+    @Override
     public Enumeration<Object> keys() {
         checkInitialized();
         return super.keys();
     }
 
     // let javadoc show doc from superclass
+    @Override
     public Enumeration<Object> elements() {
         checkInitialized();
         return super.elements();
@@ -485,8 +732,8 @@
      * Internal method to be called AFTER the security check has been
      * performed.
      */
-    private void implPutAll(Map t) {
-        for (Map.Entry e : ((Map<?,?>)t).entrySet()) {
+    private void implPutAll(Map<?,?> t) {
+        for (Map.Entry<?,?> e : t.entrySet()) {
             implPut(e.getKey(), e.getValue());
         }
         if (registered) {
@@ -504,6 +751,99 @@
         return super.remove(key);
     }
 
+    private boolean implRemove(Object key, Object value) {
+        if (key instanceof String && value instanceof String) {
+            if (!checkLegacy(key)) {
+                return false;
+            }
+            legacyStrings.remove((String)key, value);
+        }
+        return super.remove(key, value);
+    }
+
+    private boolean implReplace(Object key, Object oldValue, Object newValue) {
+        if ((key instanceof String) && (oldValue instanceof String) &&
+                (newValue instanceof String)) {
+            if (!checkLegacy(key)) {
+                return false;
+            }
+            legacyStrings.replace((String)key, (String)oldValue,
+                    (String)newValue);
+        }
+        return super.replace(key, oldValue, newValue);
+    }
+
+    private Object implReplace(Object key, Object value) {
+        if ((key instanceof String) && (value instanceof String)) {
+            if (!checkLegacy(key)) {
+                return null;
+            }
+            legacyStrings.replace((String)key, (String)value);
+        }
+        return super.replace(key, value);
+    }
+
+    private void implReplaceAll(BiFunction<? super Object, ? super Object, ? extends Object> function) {
+        legacyChanged = true;
+        if (legacyStrings == null) {
+            legacyStrings = new LinkedHashMap<String,String>();
+        } else {
+            legacyStrings.replaceAll((BiFunction<? super String, ? super String, ? extends String>) function);
+        }
+        super.replaceAll(function);
+    }
+
+
+    private Object implMerge(Object key, Object value, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
+        if ((key instanceof String) && (value instanceof String)) {
+            if (!checkLegacy(key)) {
+                return null;
+            }
+            legacyStrings.merge((String)key, (String)value,
+                    (BiFunction<? super String, ? super String, ? extends String>) remappingFunction);
+        }
+        return super.merge(key, value, remappingFunction);
+    }
+
+    private Object implCompute(Object key, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
+        if (key instanceof String) {
+            if (!checkLegacy(key)) {
+                return null;
+            }
+            // BEGIN ANDROID-CHANGED: was
+            // legacyStrings.computeIfAbsent((String) key,
+            //         (Function<? super String, ? extends String>) remappingFunction);
+            // which cannot ever succeed as the cast from BiFunction to Function always fails
+            legacyStrings.compute((String) key,
+                    (BiFunction<? super String, ? super String, ? extends String>)
+                            remappingFunction);
+            // END ANDROID-CHANGED
+        }
+        return super.compute(key, remappingFunction);
+    }
+
+    private Object implComputeIfAbsent(Object key, Function<? super Object, ? extends Object> mappingFunction) {
+        if (key instanceof String) {
+            if (!checkLegacy(key)) {
+                return null;
+            }
+            legacyStrings.computeIfAbsent((String) key,
+                    (Function<? super String, ? extends String>) mappingFunction);
+        }
+        return super.computeIfAbsent(key, mappingFunction);
+    }
+
+    private Object implComputeIfPresent(Object key, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
+        if (key instanceof String) {
+            if (!checkLegacy(key)) {
+                return null;
+            }
+            legacyStrings.computeIfPresent((String) key,
+                    (BiFunction<? super String, ? super String, ? extends String>) remappingFunction);
+        }
+        return super.computeIfPresent(key, remappingFunction);
+    }
+
     private Object implPut(Object key, Object value) {
         if ((key instanceof String) && (value instanceof String)) {
             if (!checkLegacy(key)) {
@@ -514,6 +854,16 @@
         return super.put(key, value);
     }
 
+    private Object implPutIfAbsent(Object key, Object value) {
+        if ((key instanceof String) && (value instanceof String)) {
+            if (!checkLegacy(key)) {
+                return null;
+            }
+            legacyStrings.putIfAbsent((String)key, (String)value);
+        }
+        return super.putIfAbsent(key, value);
+    }
+
     private void implClear() {
         if (legacyStrings != null) {
             legacyStrings.clear();
@@ -590,9 +940,9 @@
      * occur if the legacy properties are inconsistent or incomplete.
      */
     private void removeInvalidServices(Map<ServiceKey,Service> map) {
-        for (Iterator t = map.entrySet().iterator(); t.hasNext(); ) {
-            Map.Entry entry = (Map.Entry)t.next();
-            Service s = (Service)entry.getValue();
+        for (Iterator<Map.Entry<ServiceKey, Service>> t =
+                map.entrySet().iterator(); t.hasNext(); ) {
+            Service s = t.next().getValue();
             if (s.isValid() == false) {
                 t.remove();
             }
@@ -693,9 +1043,9 @@
      * the service added via {@link #putService putService()} is returned.
      *
      * @param type the type of {@link Service service} requested
-     * (for example, <code>MessageDigest</code>)
+     * (for example, {@code MessageDigest})
      * @param algorithm the case insensitive algorithm name (or alternate
-     * alias) of the service requested (for example, <code>SHA-1</code>)
+     * alias) of the service requested (for example, {@code SHA-1})
      *
      * @return the service describing this Provider's matching service
      * or null if no such service exists
@@ -770,20 +1120,20 @@
      * Java Cryptography Architecture API Specification &amp; Reference </a>.
      *
      * <p>Also, if there is a security manager, its
-     * <code>checkSecurityAccess</code> method is called with the string
-     * <code>"putProviderProperty."+name</code>, where <code>name</code> is
+     * {@code checkSecurityAccess} method is called with the string
+     * {@code "putProviderProperty."+name}, where {@code name} is
      * the provider name, to see if it's ok to set this provider's property
-     * values. If the default implementation of <code>checkSecurityAccess</code>
+     * values. If the default implementation of {@code checkSecurityAccess}
      * is used (that is, that method is not overriden), then this results in
-     * a call to the security manager's <code>checkPermission</code> method with
-     * a <code>SecurityPermission("putProviderProperty."+name)</code>
+     * a call to the security manager's {@code checkPermission} method with
+     * a {@code SecurityPermission("putProviderProperty."+name)}
      * permission.
      *
      * @param s the Service to add
      *
      * @throws SecurityException
-     *      if a security manager exists and its <code>{@link
-     *      java.lang.SecurityManager#checkSecurityAccess}</code> method denies
+     *      if a security manager exists and its {@link
+     *      java.lang.SecurityManager#checkSecurityAccess} method denies
      *      access to set property values.
      * @throws NullPointerException if s is null
      *
@@ -867,21 +1217,21 @@
      * from this provider's Hashtable.
      *
      * <p>Also, if there is a security manager, its
-     * <code>checkSecurityAccess</code> method is called with the string
-     * <code>"removeProviderProperty."+name</code>, where <code>name</code> is
+     * {@code checkSecurityAccess} method is called with the string
+     * {@code "removeProviderProperty."+name}, where {@code name} is
      * the provider name, to see if it's ok to remove this provider's
      * properties. If the default implementation of
-     * <code>checkSecurityAccess</code> is used (that is, that method is not
+     * {@code checkSecurityAccess} is used (that is, that method is not
      * overriden), then this results in a call to the security manager's
-     * <code>checkPermission</code> method with a
-     * <code>SecurityPermission("removeProviderProperty."+name)</code>
+     * {@code checkPermission} method with a
+     * {@code SecurityPermission("removeProviderProperty."+name)}
      * permission.
      *
      * @param s the Service to be removed
      *
      * @throws  SecurityException
-     *          if a security manager exists and its <code>{@link
-     *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies
+     *          if a security manager exists and its {@link
+     *          java.lang.SecurityManager#checkSecurityAccess} method denies
      *          access to remove this provider's properties.
      * @throws NullPointerException if s is null
      *
@@ -952,15 +1302,15 @@
         final String name;
         final boolean supportsParameter;
         final String constructorParameterClassName;
-        private volatile Class constructorParameterClass;
+        private volatile Class<?> constructorParameterClass;
 
         EngineDescription(String name, boolean sp, String paramName) {
             this.name = name;
             this.supportsParameter = sp;
             this.constructorParameterClassName = paramName;
         }
-        Class getConstructorParameterClass() throws ClassNotFoundException {
-            Class clazz = constructorParameterClass;
+        Class<?> getConstructorParameterClass() throws ClassNotFoundException {
+            Class<?> clazz = constructorParameterClass;
             if (clazz == null) {
                 clazz = Class.forName(constructorParameterClassName);
                 constructorParameterClass = clazz;
@@ -1042,7 +1392,7 @@
      * <p>This class defines the methods {@link #supportsParameter
      * supportsParameter()} and {@link #newInstance newInstance()}
      * which are used by the Java security framework when it searches for
-     * suitable services and instantes them. The valid arguments to those
+     * suitable services and instantiates them. The valid arguments to those
      * methods depend on the type of service. For the service types defined
      * within Java SE, see the
      * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/security/crypto/CryptoSpec.html">
@@ -1063,7 +1413,7 @@
         private Map<UString,String> attributes;
 
         // Reference to the cached implementation Class object
-        private volatile Reference<Class> classRef;
+        private volatile Reference<Class<?>> classRef;
 
         // flag indicating whether this service has its attributes for
         // supportedKeyFormats or supportedKeyClasses set
@@ -1080,7 +1430,7 @@
         // whether this service has been registered with the Provider
         private boolean registered;
 
-        private static final Class[] CLASS0 = new Class[0];
+        private static final Class<?>[] CLASS0 = new Class<?>[0];
 
         // this constructor and these methods are used for parsing
         // the legacy string properties.
@@ -1151,7 +1501,7 @@
         }
 
         /**
-         * Get the type of this service. For example, <code>MessageDigest</code>.
+         * Get the type of this service. For example, {@code MessageDigest}.
          *
          * @return the type of this service
          */
@@ -1161,7 +1511,7 @@
 
         /**
          * Return the name of the algorithm of this service. For example,
-         * <code>SHA-1</code>.
+         * {@code SHA-1}.
          *
          * @return the algorithm of this service
          */
@@ -1233,7 +1583,7 @@
          *
          * @throws InvalidParameterException if the value of
          * constructorParameter is invalid for this type of service.
-         * @throws NoSuchAlgorithmException if instantation failed for
+         * @throws NoSuchAlgorithmException if instantiation failed for
          * any other reason.
          */
         public Object newInstance(Object constructorParameter)
@@ -1260,12 +1610,14 @@
                             ("constructorParameter not used with " + type
                             + " engines");
                     }
-                    Class clazz = getImplClass();
-                    return clazz.newInstance();
+                    Class<?> clazz = getImplClass();
+                    Class<?>[] empty = {};
+                    Constructor<?> con = clazz.getConstructor(empty);
+                    return con.newInstance();
                 } else {
-                    Class paramClass = cap.getConstructorParameterClass();
+                    Class<?> paramClass = cap.getConstructorParameterClass();
                     if (constructorParameter != null) {
-                        Class argClass = constructorParameter.getClass();
+                        Class<?> argClass = constructorParameter.getClass();
                         if (paramClass.isAssignableFrom(argClass) == false) {
                             throw new InvalidParameterException
                             ("constructorParameter must be instanceof "
@@ -1273,8 +1625,8 @@
                             + " for engine type " + type);
                         }
                     }
-                    Class clazz = getImplClass();
-                    Constructor cons = clazz.getConstructor(paramClass);
+                    Class<?> clazz = getImplClass();
+                    Constructor<?> cons = clazz.getConstructor(paramClass);
                     return cons.newInstance(constructorParameter);
                 }
             } catch (NoSuchAlgorithmException e) {
@@ -1293,10 +1645,10 @@
         }
 
         // return the implementation Class object for this service
-        private Class getImplClass() throws NoSuchAlgorithmException {
+        private Class<?> getImplClass() throws NoSuchAlgorithmException {
             try {
-                Reference<Class> ref = classRef;
-                Class clazz = (ref == null) ? null : ref.get();
+                Reference<Class<?>> ref = classRef;
+                Class<?> clazz = (ref == null) ? null : ref.get();
                 if (clazz == null) {
                     ClassLoader cl = provider.getClass().getClassLoader();
                     if (cl == null) {
@@ -1304,13 +1656,18 @@
                     } else {
                         clazz = cl.loadClass(className);
                     }
-                    classRef = new WeakReference<Class>(clazz);
+                    if (!Modifier.isPublic(clazz.getModifiers())) {
+                        throw new NoSuchAlgorithmException
+                            ("class configured for " + type + " (provider: " +
+                            provider.getName() + ") is not public.");
+                    }
+                    classRef = new WeakReference<Class<?>>(clazz);
                 }
                 return clazz;
             } catch (ClassNotFoundException e) {
                 throw new NoSuchAlgorithmException
-                    ("class configured for " + type + "(provider: " +
-                    provider.getName() + ")" + "cannot be found.", e);
+                    ("class configured for " + type + " (provider: " +
+                    provider.getName() + ") cannot be found.", e);
             }
         }
 
@@ -1321,28 +1678,33 @@
          */
         private Object newInstanceGeneric(Object constructorParameter)
                 throws Exception {
-            Class clazz = getImplClass();
+            Class<?> clazz = getImplClass();
             if (constructorParameter == null) {
-                Object o = clazz.newInstance();
-                return o;
+                // create instance with public no-arg constructor if it exists
+                try {
+                    Class<?>[] empty = {};
+                    Constructor<?> con = clazz.getConstructor(empty);
+                    return con.newInstance();
+                } catch (NoSuchMethodException e) {
+                    throw new NoSuchAlgorithmException("No public no-arg "
+                        + "constructor found in class " + className);
+                }
             }
-            Class argClass = constructorParameter.getClass();
+            Class<?> argClass = constructorParameter.getClass();
             Constructor[] cons = clazz.getConstructors();
             // find first public constructor that can take the
             // argument as parameter
-            for (int i = 0; i < cons.length; i++) {
-                Constructor con = cons[i];
-                Class[] paramTypes = con.getParameterTypes();
+            for (Constructor<?> con : cons) {
+                Class<?>[] paramTypes = con.getParameterTypes();
                 if (paramTypes.length != 1) {
                     continue;
                 }
                 if (paramTypes[0].isAssignableFrom(argClass) == false) {
                     continue;
                 }
-                Object o = con.newInstance(new Object[] {constructorParameter});
-                return o;
+                return con.newInstance(constructorParameter);
             }
-            throw new NoSuchAlgorithmException("No constructor matching "
+            throw new NoSuchAlgorithmException("No public constructor matching "
                 + argClass.getName() + " found in class " + className);
         }
 
@@ -1420,10 +1782,10 @@
                     s = getAttribute("SupportedKeyClasses");
                     if (s != null) {
                         String[] classNames = s.split("\\|");
-                        List<Class> classList =
+                        List<Class<?>> classList =
                             new ArrayList<>(classNames.length);
                         for (String className : classNames) {
-                            Class clazz = getKeyClass(className);
+                            Class<?> clazz = getKeyClass(className);
                             if (clazz != null) {
                                 classList.add(clazz);
                             }
@@ -1440,7 +1802,7 @@
         }
 
         // get the key class object of the specified name
-        private Class getKeyClass(String name) {
+        private Class<?> getKeyClass(String name) {
             try {
                 return Class.forName(name);
             } catch (ClassNotFoundException e) {
@@ -1477,8 +1839,8 @@
             if (supportedClasses == null) {
                 return false;
             }
-            Class keyClass = key.getClass();
-            for (Class clazz : supportedClasses) {
+            Class<?> keyClass = key.getClass();
+            for (Class<?> clazz : supportedClasses) {
                 if (clazz.isAssignableFrom(keyClass)) {
                     return true;
                 }
diff --git a/ojluni/src/main/java/java/util/Base64.java b/ojluni/src/main/java/java/util/Base64.java
new file mode 100644
index 0000000..172acbe
--- /dev/null
+++ b/ojluni/src/main/java/java/util/Base64.java
@@ -0,0 +1,998 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.util;
+
+import java.io.FilterOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * This class consists exclusively of static methods for obtaining
+ * encoders and decoders for the Base64 encoding scheme. The
+ * implementation of this class supports the following types of Base64
+ * as specified in
+ * <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a> and
+ * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>.
+ *
+ * <ul>
+ * <li><a name="basic"><b>Basic</b></a>
+ * <p> Uses "The Base64 Alphabet" as specified in Table 1 of
+ *     RFC 4648 and RFC 2045 for encoding and decoding operation.
+ *     The encoder does not add any line feed (line separator)
+ *     character. The decoder rejects data that contains characters
+ *     outside the base64 alphabet.</p></li>
+ *
+ * <li><a name="url"><b>URL and Filename safe</b></a>
+ * <p> Uses the "URL and Filename safe Base64 Alphabet" as specified
+ *     in Table 2 of RFC 4648 for encoding and decoding. The
+ *     encoder does not add any line feed (line separator) character.
+ *     The decoder rejects data that contains characters outside the
+ *     base64 alphabet.</p></li>
+ *
+ * <li><a name="mime"><b>MIME</b></a>
+ * <p> Uses the "The Base64 Alphabet" as specified in Table 1 of
+ *     RFC 2045 for encoding and decoding operation. The encoded output
+ *     must be represented in lines of no more than 76 characters each
+ *     and uses a carriage return {@code '\r'} followed immediately by
+ *     a linefeed {@code '\n'} as the line separator. No line separator
+ *     is added to the end of the encoded output. All line separators
+ *     or other characters not found in the base64 alphabet table are
+ *     ignored in decoding operation.</p></li>
+ * </ul>
+ *
+ * <p> Unless otherwise noted, passing a {@code null} argument to a
+ * method of this class will cause a {@link java.lang.NullPointerException
+ * NullPointerException} to be thrown.
+ *
+ * @author  Xueming Shen
+ * @since   1.8
+ */
+
+public class Base64 {
+
+    private Base64() {}
+
+    /**
+     * Returns a {@link Encoder} that encodes using the
+     * <a href="#basic">Basic</a> type base64 encoding scheme.
+     *
+     * @return  A Base64 encoder.
+     */
+    public static Encoder getEncoder() {
+         return Encoder.RFC4648;
+    }
+
+    /**
+     * Returns a {@link Encoder} that encodes using the
+     * <a href="#url">URL and Filename safe</a> type base64
+     * encoding scheme.
+     *
+     * @return  A Base64 encoder.
+     */
+    public static Encoder getUrlEncoder() {
+         return Encoder.RFC4648_URLSAFE;
+    }
+
+    /**
+     * Returns a {@link Encoder} that encodes using the
+     * <a href="#mime">MIME</a> type base64 encoding scheme.
+     *
+     * @return  A Base64 encoder.
+     */
+    public static Encoder getMimeEncoder() {
+        return Encoder.RFC2045;
+    }
+
+    /**
+     * Returns a {@link Encoder} that encodes using the
+     * <a href="#mime">MIME</a> type base64 encoding scheme
+     * with specified line length and line separators.
+     *
+     * @param   lineLength
+     *          the length of each output line (rounded down to nearest multiple
+     *          of 4). If {@code lineLength <= 0} the output will not be separated
+     *          in lines
+     * @param   lineSeparator
+     *          the line separator for each output line
+     *
+     * @return  A Base64 encoder.
+     *
+     * @throws  IllegalArgumentException if {@code lineSeparator} includes any
+     *          character of "The Base64 Alphabet" as specified in Table 1 of
+     *          RFC 2045.
+     */
+    public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) {
+         Objects.requireNonNull(lineSeparator);
+         int[] base64 = Decoder.fromBase64;
+         for (byte b : lineSeparator) {
+             if (base64[b & 0xff] != -1)
+                 throw new IllegalArgumentException(
+                     "Illegal base64 line separator character 0x" + Integer.toString(b, 16));
+         }
+         if (lineLength <= 0) {
+             return Encoder.RFC4648;
+         }
+         return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true);
+    }
+
+    /**
+     * Returns a {@link Decoder} that decodes using the
+     * <a href="#basic">Basic</a> type base64 encoding scheme.
+     *
+     * @return  A Base64 decoder.
+     */
+    public static Decoder getDecoder() {
+         return Decoder.RFC4648;
+    }
+
+    /**
+     * Returns a {@link Decoder} that decodes using the
+     * <a href="#url">URL and Filename safe</a> type base64
+     * encoding scheme.
+     *
+     * @return  A Base64 decoder.
+     */
+    public static Decoder getUrlDecoder() {
+         return Decoder.RFC4648_URLSAFE;
+    }
+
+    /**
+     * Returns a {@link Decoder} that decodes using the
+     * <a href="#mime">MIME</a> type base64 decoding scheme.
+     *
+     * @return  A Base64 decoder.
+     */
+    public static Decoder getMimeDecoder() {
+         return Decoder.RFC2045;
+    }
+
+    /**
+     * This class implements an encoder for encoding byte data using
+     * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
+     *
+     * <p> Instances of {@link Encoder} class are safe for use by
+     * multiple concurrent threads.
+     *
+     * <p> Unless otherwise noted, passing a {@code null} argument to
+     * a method of this class will cause a
+     * {@link java.lang.NullPointerException NullPointerException} to
+     * be thrown.
+     *
+     * @see     Decoder
+     * @since   1.8
+     */
+    public static class Encoder {
+
+        private final byte[] newline;
+        private final int linemax;
+        private final boolean isURL;
+        private final boolean doPadding;
+
+        private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) {
+            this.isURL = isURL;
+            this.newline = newline;
+            this.linemax = linemax;
+            this.doPadding = doPadding;
+        }
+
+        /**
+         * This array is a lookup table that translates 6-bit positive integer
+         * index values into their "Base64 Alphabet" equivalents as specified
+         * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).
+         */
+        private static final char[] toBase64 = {
+            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
+        };
+
+        /**
+         * It's the lookup table for "URL and Filename safe Base64" as specified
+         * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and
+         * '_'. This table is used when BASE64_URL is specified.
+         */
+        private static final char[] toBase64URL = {
+            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
+        };
+
+        private static final int MIMELINEMAX = 76;
+        private static final byte[] CRLF = new byte[] {'\r', '\n'};
+
+        static final Encoder RFC4648 = new Encoder(false, null, -1, true);
+        static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true);
+        static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true);
+
+        private final int outLength(int srclen) {
+            int len = 0;
+            if (doPadding) {
+                len = 4 * ((srclen + 2) / 3);
+            } else {
+                int n = srclen % 3;
+                len = 4 * (srclen / 3) + (n == 0 ? 0 : n + 1);
+            }
+            if (linemax > 0)                                  // line separators
+                len += (len - 1) / linemax * newline.length;
+            return len;
+        }
+
+        /**
+         * Encodes all bytes from the specified byte array into a newly-allocated
+         * byte array using the {@link Base64} encoding scheme. The returned byte
+         * array is of the length of the resulting bytes.
+         *
+         * @param   src
+         *          the byte array to encode
+         * @return  A newly-allocated byte array containing the resulting
+         *          encoded bytes.
+         */
+        public byte[] encode(byte[] src) {
+            int len = outLength(src.length);          // dst array size
+            byte[] dst = new byte[len];
+            int ret = encode0(src, 0, src.length, dst);
+            if (ret != dst.length)
+                 return Arrays.copyOf(dst, ret);
+            return dst;
+        }
+
+        /**
+         * Encodes all bytes from the specified byte array using the
+         * {@link Base64} encoding scheme, writing the resulting bytes to the
+         * given output byte array, starting at offset 0.
+         *
+         * <p> It is the responsibility of the invoker of this method to make
+         * sure the output byte array {@code dst} has enough space for encoding
+         * all bytes from the input byte array. No bytes will be written to the
+         * output byte array if the output byte array is not big enough.
+         *
+         * @param   src
+         *          the byte array to encode
+         * @param   dst
+         *          the output byte array
+         * @return  The number of bytes written to the output byte array
+         *
+         * @throws  IllegalArgumentException if {@code dst} does not have enough
+         *          space for encoding all input bytes.
+         */
+        public int encode(byte[] src, byte[] dst) {
+            int len = outLength(src.length);         // dst array size
+            if (dst.length < len)
+                throw new IllegalArgumentException(
+                    "Output byte array is too small for encoding all input bytes");
+            return encode0(src, 0, src.length, dst);
+        }
+
+        /**
+         * Encodes the specified byte array into a String using the {@link Base64}
+         * encoding scheme.
+         *
+         * <p> This method first encodes all input bytes into a base64 encoded
+         * byte array and then constructs a new String by using the encoded byte
+         * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1
+         * ISO-8859-1} charset.
+         *
+         * <p> In other words, an invocation of this method has exactly the same
+         * effect as invoking
+         * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}.
+         *
+         * @param   src
+         *          the byte array to encode
+         * @return  A String containing the resulting Base64 encoded characters
+         */
+        @SuppressWarnings("deprecation")
+        public String encodeToString(byte[] src) {
+            byte[] encoded = encode(src);
+            return new String(encoded, 0, 0, encoded.length);
+        }
+
+        /**
+         * Encodes all remaining bytes from the specified byte buffer into
+         * a newly-allocated ByteBuffer using the {@link Base64} encoding
+         * scheme.
+         *
+         * Upon return, the source buffer's position will be updated to
+         * its limit; its limit will not have been changed. The returned
+         * output buffer's position will be zero and its limit will be the
+         * number of resulting encoded bytes.
+         *
+         * @param   buffer
+         *          the source ByteBuffer to encode
+         * @return  A newly-allocated byte buffer containing the encoded bytes.
+         */
+        public ByteBuffer encode(ByteBuffer buffer) {
+            int len = outLength(buffer.remaining());
+            byte[] dst = new byte[len];
+            int ret = 0;
+            if (buffer.hasArray()) {
+                ret = encode0(buffer.array(),
+                              buffer.arrayOffset() + buffer.position(),
+                              buffer.arrayOffset() + buffer.limit(),
+                              dst);
+                buffer.position(buffer.limit());
+            } else {
+                byte[] src = new byte[buffer.remaining()];
+                buffer.get(src);
+                ret = encode0(src, 0, src.length, dst);
+            }
+            if (ret != dst.length)
+                 dst = Arrays.copyOf(dst, ret);
+            return ByteBuffer.wrap(dst);
+        }
+
+        /**
+         * Wraps an output stream for encoding byte data using the {@link Base64}
+         * encoding scheme.
+         *
+         * <p> It is recommended to promptly close the returned output stream after
+         * use, during which it will flush all possible leftover bytes to the underlying
+         * output stream. Closing the returned output stream will close the underlying
+         * output stream.
+         *
+         * @param   os
+         *          the output stream.
+         * @return  the output stream for encoding the byte data into the
+         *          specified Base64 encoded format
+         */
+        public OutputStream wrap(OutputStream os) {
+            Objects.requireNonNull(os);
+            return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
+                                       newline, linemax, doPadding);
+        }
+
+        /**
+         * Returns an encoder instance that encodes equivalently to this one,
+         * but without adding any padding character at the end of the encoded
+         * byte data.
+         *
+         * <p> The encoding scheme of this encoder instance is unaffected by
+         * this invocation. The returned encoder instance should be used for
+         * non-padding encoding operation.
+         *
+         * @return an equivalent encoder that encodes without adding any
+         *         padding character at the end
+         */
+        public Encoder withoutPadding() {
+            if (!doPadding)
+                return this;
+            return new Encoder(isURL, newline, linemax, false);
+        }
+
+        private int encode0(byte[] src, int off, int end, byte[] dst) {
+            char[] base64 = isURL ? toBase64URL : toBase64;
+            int sp = off;
+            int slen = (end - off) / 3 * 3;
+            int sl = off + slen;
+            if (linemax > 0 && slen  > linemax / 4 * 3)
+                slen = linemax / 4 * 3;
+            int dp = 0;
+            while (sp < sl) {
+                int sl0 = Math.min(sp + slen, sl);
+                for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) {
+                    int bits = (src[sp0++] & 0xff) << 16 |
+                               (src[sp0++] & 0xff) <<  8 |
+                               (src[sp0++] & 0xff);
+                    dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f];
+                    dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f];
+                    dst[dp0++] = (byte)base64[(bits >>> 6)  & 0x3f];
+                    dst[dp0++] = (byte)base64[bits & 0x3f];
+                }
+                int dlen = (sl0 - sp) / 3 * 4;
+                dp += dlen;
+                sp = sl0;
+                if (dlen == linemax && sp < end) {
+                    for (byte b : newline){
+                        dst[dp++] = b;
+                    }
+                }
+            }
+            if (sp < end) {               // 1 or 2 leftover bytes
+                int b0 = src[sp++] & 0xff;
+                dst[dp++] = (byte)base64[b0 >> 2];
+                if (sp == end) {
+                    dst[dp++] = (byte)base64[(b0 << 4) & 0x3f];
+                    if (doPadding) {
+                        dst[dp++] = '=';
+                        dst[dp++] = '=';
+                    }
+                } else {
+                    int b1 = src[sp++] & 0xff;
+                    dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)];
+                    dst[dp++] = (byte)base64[(b1 << 2) & 0x3f];
+                    if (doPadding) {
+                        dst[dp++] = '=';
+                    }
+                }
+            }
+            return dp;
+        }
+    }
+
+    /**
+     * This class implements a decoder for decoding byte data using the
+     * Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
+     *
+     * <p> The Base64 padding character {@code '='} is accepted and
+     * interpreted as the end of the encoded byte data, but is not
+     * required. So if the final unit of the encoded byte data only has
+     * two or three Base64 characters (without the corresponding padding
+     * character(s) padded), they are decoded as if followed by padding
+     * character(s). If there is a padding character present in the
+     * final unit, the correct number of padding character(s) must be
+     * present, otherwise {@code IllegalArgumentException} (
+     * {@code IOException} when reading from a Base64 stream) is thrown
+     * during decoding.
+     *
+     * <p> Instances of {@link Decoder} class are safe for use by
+     * multiple concurrent threads.
+     *
+     * <p> Unless otherwise noted, passing a {@code null} argument to
+     * a method of this class will cause a
+     * {@link java.lang.NullPointerException NullPointerException} to
+     * be thrown.
+     *
+     * @see     Encoder
+     * @since   1.8
+     */
+    public static class Decoder {
+
+        private final boolean isURL;
+        private final boolean isMIME;
+
+        private Decoder(boolean isURL, boolean isMIME) {
+            this.isURL = isURL;
+            this.isMIME = isMIME;
+        }
+
+        /**
+         * Lookup table for decoding unicode characters drawn from the
+         * "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into
+         * their 6-bit positive integer equivalents.  Characters that
+         * are not in the Base64 alphabet but fall within the bounds of
+         * the array are encoded to -1.
+         *
+         */
+        private static final int[] fromBase64 = new int[256];
+        static {
+            Arrays.fill(fromBase64, -1);
+            for (int i = 0; i < Encoder.toBase64.length; i++)
+                fromBase64[Encoder.toBase64[i]] = i;
+            fromBase64['='] = -2;
+        }
+
+        /**
+         * Lookup table for decoding "URL and Filename safe Base64 Alphabet"
+         * as specified in Table2 of the RFC 4648.
+         */
+        private static final int[] fromBase64URL = new int[256];
+
+        static {
+            Arrays.fill(fromBase64URL, -1);
+            for (int i = 0; i < Encoder.toBase64URL.length; i++)
+                fromBase64URL[Encoder.toBase64URL[i]] = i;
+            fromBase64URL['='] = -2;
+        }
+
+        static final Decoder RFC4648         = new Decoder(false, false);
+        static final Decoder RFC4648_URLSAFE = new Decoder(true, false);
+        static final Decoder RFC2045         = new Decoder(false, true);
+
+        /**
+         * Decodes all bytes from the input byte array using the {@link Base64}
+         * encoding scheme, writing the results into a newly-allocated output
+         * byte array. The returned byte array is of the length of the resulting
+         * bytes.
+         *
+         * @param   src
+         *          the byte array to decode
+         *
+         * @return  A newly-allocated byte array containing the decoded bytes.
+         *
+         * @throws  IllegalArgumentException
+         *          if {@code src} is not in valid Base64 scheme
+         */
+        public byte[] decode(byte[] src) {
+            byte[] dst = new byte[outLength(src, 0, src.length)];
+            int ret = decode0(src, 0, src.length, dst);
+            if (ret != dst.length) {
+                dst = Arrays.copyOf(dst, ret);
+            }
+            return dst;
+        }
+
+        /**
+         * Decodes a Base64 encoded String into a newly-allocated byte array
+         * using the {@link Base64} encoding scheme.
+         *
+         * <p> An invocation of this method has exactly the same effect as invoking
+         * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))}
+         *
+         * @param   src
+         *          the string to decode
+         *
+         * @return  A newly-allocated byte array containing the decoded bytes.
+         *
+         * @throws  IllegalArgumentException
+         *          if {@code src} is not in valid Base64 scheme
+         */
+        public byte[] decode(String src) {
+            return decode(src.getBytes(StandardCharsets.ISO_8859_1));
+        }
+
+        /**
+         * Decodes all bytes from the input byte array using the {@link Base64}
+         * encoding scheme, writing the results into the given output byte array,
+         * starting at offset 0.
+         *
+         * <p> It is the responsibility of the invoker of this method to make
+         * sure the output byte array {@code dst} has enough space for decoding
+         * all bytes from the input byte array. No bytes will be be written to
+         * the output byte array if the output byte array is not big enough.
+         *
+         * <p> If the input byte array is not in valid Base64 encoding scheme
+         * then some bytes may have been written to the output byte array before
+         * IllegalargumentException is thrown.
+         *
+         * @param   src
+         *          the byte array to decode
+         * @param   dst
+         *          the output byte array
+         *
+         * @return  The number of bytes written to the output byte array
+         *
+         * @throws  IllegalArgumentException
+         *          if {@code src} is not in valid Base64 scheme, or {@code dst}
+         *          does not have enough space for decoding all input bytes.
+         */
+        public int decode(byte[] src, byte[] dst) {
+            int len = outLength(src, 0, src.length);
+            if (dst.length < len)
+                throw new IllegalArgumentException(
+                    "Output byte array is too small for decoding all input bytes");
+            return decode0(src, 0, src.length, dst);
+        }
+
+        /**
+         * Decodes all bytes from the input byte buffer using the {@link Base64}
+         * encoding scheme, writing the results into a newly-allocated ByteBuffer.
+         *
+         * <p> Upon return, the source buffer's position will be updated to
+         * its limit; its limit will not have been changed. The returned
+         * output buffer's position will be zero and its limit will be the
+         * number of resulting decoded bytes
+         *
+         * <p> {@code IllegalArgumentException} is thrown if the input buffer
+         * is not in valid Base64 encoding scheme. The position of the input
+         * buffer will not be advanced in this case.
+         *
+         * @param   buffer
+         *          the ByteBuffer to decode
+         *
+         * @return  A newly-allocated byte buffer containing the decoded bytes
+         *
+         * @throws  IllegalArgumentException
+         *          if {@code src} is not in valid Base64 scheme.
+         */
+        public ByteBuffer decode(ByteBuffer buffer) {
+            int pos0 = buffer.position();
+            try {
+                byte[] src;
+                int sp, sl;
+                if (buffer.hasArray()) {
+                    src = buffer.array();
+                    sp = buffer.arrayOffset() + buffer.position();
+                    sl = buffer.arrayOffset() + buffer.limit();
+                    buffer.position(buffer.limit());
+                } else {
+                    src = new byte[buffer.remaining()];
+                    buffer.get(src);
+                    sp = 0;
+                    sl = src.length;
+                }
+                byte[] dst = new byte[outLength(src, sp, sl)];
+                return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
+            } catch (IllegalArgumentException iae) {
+                buffer.position(pos0);
+                throw iae;
+            }
+        }
+
+        /**
+         * Returns an input stream for decoding {@link Base64} encoded byte stream.
+         *
+         * <p> The {@code read}  methods of the returned {@code InputStream} will
+         * throw {@code IOException} when reading bytes that cannot be decoded.
+         *
+         * <p> Closing the returned input stream will close the underlying
+         * input stream.
+         *
+         * @param   is
+         *          the input stream
+         *
+         * @return  the input stream for decoding the specified Base64 encoded
+         *          byte stream
+         */
+        public InputStream wrap(InputStream is) {
+            Objects.requireNonNull(is);
+            return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME);
+        }
+
+        private int outLength(byte[] src, int sp, int sl) {
+            int[] base64 = isURL ? fromBase64URL : fromBase64;
+            int paddings = 0;
+            int len = sl - sp;
+            if (len == 0)
+                return 0;
+            if (len < 2) {
+                if (isMIME && base64[0] == -1)
+                    return 0;
+                throw new IllegalArgumentException(
+                    "Input byte[] should at least have 2 bytes for base64 bytes");
+            }
+            if (isMIME) {
+                // scan all bytes to fill out all non-alphabet. a performance
+                // trade-off of pre-scan or Arrays.copyOf
+                int n = 0;
+                while (sp < sl) {
+                    int b = src[sp++] & 0xff;
+                    if (b == '=') {
+                        len -= (sl - sp + 1);
+                        break;
+                    }
+                    if ((b = base64[b]) == -1)
+                        n++;
+                }
+                len -= n;
+            } else {
+                if (src[sl - 1] == '=') {
+                    paddings++;
+                    if (src[sl - 2] == '=')
+                        paddings++;
+                }
+            }
+            if (paddings == 0 && (len & 0x3) !=  0)
+                paddings = 4 - (len & 0x3);
+            return 3 * ((len + 3) / 4) - paddings;
+        }
+
+        private int decode0(byte[] src, int sp, int sl, byte[] dst) {
+            int[] base64 = isURL ? fromBase64URL : fromBase64;
+            int dp = 0;
+            int bits = 0;
+            int shiftto = 18;       // pos of first byte of 4-byte atom
+            while (sp < sl) {
+                int b = src[sp++] & 0xff;
+                if ((b = base64[b]) < 0) {
+                    if (b == -2) {         // padding byte '='
+                        // =     shiftto==18 unnecessary padding
+                        // x=    shiftto==12 a dangling single x
+                        // x     to be handled together with non-padding case
+                        // xx=   shiftto==6&&sp==sl missing last =
+                        // xx=y  shiftto==6 last is not =
+                        if (shiftto == 6 && (sp == sl || src[sp++] != '=') ||
+                            shiftto == 18) {
+                            throw new IllegalArgumentException(
+                                "Input byte array has wrong 4-byte ending unit");
+                        }
+                        break;
+                    }
+                    if (isMIME)    // skip if for rfc2045
+                        continue;
+                    else
+                        throw new IllegalArgumentException(
+                            "Illegal base64 character " +
+                            Integer.toString(src[sp - 1], 16));
+                }
+                bits |= (b << shiftto);
+                shiftto -= 6;
+                if (shiftto < 0) {
+                    dst[dp++] = (byte)(bits >> 16);
+                    dst[dp++] = (byte)(bits >>  8);
+                    dst[dp++] = (byte)(bits);
+                    shiftto = 18;
+                    bits = 0;
+                }
+            }
+            // reached end of byte array or hit padding '=' characters.
+            if (shiftto == 6) {
+                dst[dp++] = (byte)(bits >> 16);
+            } else if (shiftto == 0) {
+                dst[dp++] = (byte)(bits >> 16);
+                dst[dp++] = (byte)(bits >>  8);
+            } else if (shiftto == 12) {
+                // dangling single "x", incorrectly encoded.
+                throw new IllegalArgumentException(
+                    "Last unit does not have enough valid bits");
+            }
+            // anything left is invalid, if is not MIME.
+            // if MIME, ignore all non-base64 character
+            while (sp < sl) {
+                if (isMIME && base64[src[sp++]] < 0)
+                    continue;
+                throw new IllegalArgumentException(
+                    "Input byte array has incorrect ending byte at " + sp);
+            }
+            return dp;
+        }
+    }
+
+    /*
+     * An output stream for encoding bytes into the Base64.
+     */
+    private static class EncOutputStream extends FilterOutputStream {
+
+        private int leftover = 0;
+        private int b0, b1, b2;
+        private boolean closed = false;
+
+        private final char[] base64;    // byte->base64 mapping
+        private final byte[] newline;   // line separator, if needed
+        private final int linemax;
+        private final boolean doPadding;// whether or not to pad
+        private int linepos = 0;
+
+        EncOutputStream(OutputStream os, char[] base64,
+                        byte[] newline, int linemax, boolean doPadding) {
+            super(os);
+            this.base64 = base64;
+            this.newline = newline;
+            this.linemax = linemax;
+            this.doPadding = doPadding;
+        }
+
+        @Override
+        public void write(int b) throws IOException {
+            byte[] buf = new byte[1];
+            buf[0] = (byte)(b & 0xff);
+            write(buf, 0, 1);
+        }
+
+        private void checkNewline() throws IOException {
+            if (linepos == linemax) {
+                out.write(newline);
+                linepos = 0;
+            }
+        }
+
+        @Override
+        public void write(byte[] b, int off, int len) throws IOException {
+            if (closed)
+                throw new IOException("Stream is closed");
+            if (off < 0 || len < 0 || off + len > b.length)
+                throw new ArrayIndexOutOfBoundsException();
+            if (len == 0)
+                return;
+            if (leftover != 0) {
+                if (leftover == 1) {
+                    b1 = b[off++] & 0xff;
+                    len--;
+                    if (len == 0) {
+                        leftover++;
+                        return;
+                    }
+                }
+                b2 = b[off++] & 0xff;
+                len--;
+                checkNewline();
+                out.write(base64[b0 >> 2]);
+                out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
+                out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]);
+                out.write(base64[b2 & 0x3f]);
+                linepos += 4;
+            }
+            int nBits24 = len / 3;
+            leftover = len - (nBits24 * 3);
+            while (nBits24-- > 0) {
+                checkNewline();
+                int bits = (b[off++] & 0xff) << 16 |
+                           (b[off++] & 0xff) <<  8 |
+                           (b[off++] & 0xff);
+                out.write(base64[(bits >>> 18) & 0x3f]);
+                out.write(base64[(bits >>> 12) & 0x3f]);
+                out.write(base64[(bits >>> 6)  & 0x3f]);
+                out.write(base64[bits & 0x3f]);
+                linepos += 4;
+           }
+            if (leftover == 1) {
+                b0 = b[off++] & 0xff;
+            } else if (leftover == 2) {
+                b0 = b[off++] & 0xff;
+                b1 = b[off++] & 0xff;
+            }
+        }
+
+        @Override
+        public void close() throws IOException {
+            if (!closed) {
+                closed = true;
+                if (leftover == 1) {
+                    checkNewline();
+                    out.write(base64[b0 >> 2]);
+                    out.write(base64[(b0 << 4) & 0x3f]);
+                    if (doPadding) {
+                        out.write('=');
+                        out.write('=');
+                    }
+                } else if (leftover == 2) {
+                    checkNewline();
+                    out.write(base64[b0 >> 2]);
+                    out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
+                    out.write(base64[(b1 << 2) & 0x3f]);
+                    if (doPadding) {
+                       out.write('=');
+                    }
+                }
+                leftover = 0;
+                out.close();
+            }
+        }
+    }
+
+    /*
+     * An input stream for decoding Base64 bytes
+     */
+    private static class DecInputStream extends InputStream {
+
+        private final InputStream is;
+        private final boolean isMIME;
+        private final int[] base64;      // base64 -> byte mapping
+        private int bits = 0;            // 24-bit buffer for decoding
+        private int nextin = 18;         // next available "off" in "bits" for input;
+                                         // -> 18, 12, 6, 0
+        private int nextout = -8;        // next available "off" in "bits" for output;
+                                         // -> 8, 0, -8 (no byte for output)
+        private boolean eof = false;
+        private boolean closed = false;
+
+        DecInputStream(InputStream is, int[] base64, boolean isMIME) {
+            this.is = is;
+            this.base64 = base64;
+            this.isMIME = isMIME;
+        }
+
+        private byte[] sbBuf = new byte[1];
+
+        @Override
+        public int read() throws IOException {
+            return read(sbBuf, 0, 1) == -1 ? -1 : sbBuf[0] & 0xff;
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) throws IOException {
+            if (closed)
+                throw new IOException("Stream is closed");
+            if (eof && nextout < 0)    // eof and no leftover
+                return -1;
+            if (off < 0 || len < 0 || len > b.length - off)
+                throw new IndexOutOfBoundsException();
+            int oldOff = off;
+            if (nextout >= 0) {       // leftover output byte(s) in bits buf
+                do {
+                    if (len == 0)
+                        return off - oldOff;
+                    b[off++] = (byte)(bits >> nextout);
+                    len--;
+                    nextout -= 8;
+                } while (nextout >= 0);
+                bits = 0;
+            }
+            while (len > 0) {
+                int v = is.read();
+                if (v == -1) {
+                    eof = true;
+                    if (nextin != 18) {
+                        if (nextin == 12)
+                            throw new IOException("Base64 stream has one un-decoded dangling byte.");
+                        // treat ending xx/xxx without padding character legal.
+                        // same logic as v == '=' below
+                        b[off++] = (byte)(bits >> (16));
+                        len--;
+                        if (nextin == 0) {           // only one padding byte
+                            if (len == 0) {          // no enough output space
+                                bits >>= 8;          // shift to lowest byte
+                                nextout = 0;
+                            } else {
+                                b[off++] = (byte) (bits >>  8);
+                            }
+                        }
+                    }
+                    if (off == oldOff)
+                        return -1;
+                    else
+                        return off - oldOff;
+                }
+                if (v == '=') {                  // padding byte(s)
+                    // =     shiftto==18 unnecessary padding
+                    // x=    shiftto==12 dangling x, invalid unit
+                    // xx=   shiftto==6 && missing last '='
+                    // xx=y  or last is not '='
+                    if (nextin == 18 || nextin == 12 ||
+                        nextin == 6 && is.read() != '=') {
+                        throw new IOException("Illegal base64 ending sequence:" + nextin);
+                    }
+                    b[off++] = (byte)(bits >> (16));
+                    len--;
+                    if (nextin == 0) {           // only one padding byte
+                        if (len == 0) {          // no enough output space
+                            bits >>= 8;          // shift to lowest byte
+                            nextout = 0;
+                        } else {
+                            b[off++] = (byte) (bits >>  8);
+                        }
+                    }
+                    eof = true;
+                    break;
+                }
+                if ((v = base64[v]) == -1) {
+                    if (isMIME)                 // skip if for rfc2045
+                        continue;
+                    else
+                        throw new IOException("Illegal base64 character " +
+                            Integer.toString(v, 16));
+                }
+                bits |= (v << nextin);
+                if (nextin == 0) {
+                    nextin = 18;    // clear for next
+                    nextout = 16;
+                    while (nextout >= 0) {
+                        b[off++] = (byte)(bits >> nextout);
+                        len--;
+                        nextout -= 8;
+                        if (len == 0 && nextout >= 0) {  // don't clean "bits"
+                            return off - oldOff;
+                        }
+                    }
+                    bits = 0;
+                } else {
+                    nextin -= 6;
+                }
+            }
+            return off - oldOff;
+        }
+
+        @Override
+        public int available() throws IOException {
+            if (closed)
+                throw new IOException("Stream is closed");
+            return is.available();   // TBD:
+        }
+
+        @Override
+        public void close() throws IOException {
+            if (!closed) {
+                closed = true;
+                is.close();
+            }
+        }
+    }
+}
diff --git a/ojluni/src/main/java/java/util/Formattable.java b/ojluni/src/main/java/java/util/Formattable.java
index 28b788c..f7e1e50 100644
--- a/ojluni/src/main/java/java/util/Formattable.java
+++ b/ojluni/src/main/java/java/util/Formattable.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -36,7 +36,7 @@
  * For example, the following class prints out different representations of a
  * stock's name depending on the flags and length constraints:
  *
- * <blockquote><pre>
+ * {@code
  *   import java.nio.CharBuffer;
  *   import java.util.Formatter;
  *   import java.util.Formattable;
@@ -89,12 +89,12 @@
  *           return String.format("%s - %s", symbol, companyName);
  *       }
  *   }
- * </pre></blockquote>
+ * }
  *
  * <p> When used in conjunction with the {@link java.util.Formatter}, the above
  * class produces the following output for various format strings.
  *
- * <blockquote><pre>
+ * {@code
  *   Formatter fmt = new Formatter();
  *   StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
  *                                "Fruit Titanesque, Inc.");
@@ -104,7 +104,7 @@
  *   fmt.format("%-10.8s", sn);              //   -> "HUGE      "
  *   fmt.format("%.12s", sn);                //   -> "Huge Fruit,*"
  *   fmt.format(Locale.FRANCE, "%25s", sn);  //   -> "   Fruit Titanesque, Inc."
- * </pre></blockquote>
+ * }
  *
  * <p> Formattables are not necessarily safe for multithreaded access.  Thread
  * safety is optional and may be enforced by classes that extend and implement
diff --git a/ojluni/src/main/java/java/util/IllegalFormatConversionException.java b/ojluni/src/main/java/java/util/IllegalFormatConversionException.java
index 9bdbfc4..a9b006f 100644
--- a/ojluni/src/main/java/java/util/IllegalFormatConversionException.java
+++ b/ojluni/src/main/java/java/util/IllegalFormatConversionException.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -40,7 +40,7 @@
     private static final long serialVersionUID = 17000126L;
 
     private char c;
-    private Class arg;
+    private Class<?> arg;
 
     /**
      * Constructs an instance of this class with the mismatched conversion and
diff --git a/ojluni/src/main/java/java/util/InvalidPropertiesFormatException.java b/ojluni/src/main/java/java/util/InvalidPropertiesFormatException.java
index 64dde4d..bacab3d 100644
--- a/ojluni/src/main/java/java/util/InvalidPropertiesFormatException.java
+++ b/ojluni/src/main/java/java/util/InvalidPropertiesFormatException.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,6 +44,9 @@
  */
 
 public class InvalidPropertiesFormatException extends IOException {
+
+    private static final long serialVersionUID = 7763056076009360219L;
+
     /**
      * Constructs an InvalidPropertiesFormatException with the specified
      * cause.
diff --git a/ojluni/src/main/java/java/util/MissingFormatWidthException.java b/ojluni/src/main/java/java/util/MissingFormatWidthException.java
index af76ac5..9650fe2 100644
--- a/ojluni/src/main/java/java/util/MissingFormatWidthException.java
+++ b/ojluni/src/main/java/java/util/MissingFormatWidthException.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,7 @@
 /**
  * Unchecked exception thrown when the format width is required.
  *
- * <p> Unless otherwise specified, passing a <tt>null</tt> argument to anyg
+ * <p> Unless otherwise specified, passing a <tt>null</tt> argument to any
  * method or constructor in this class will cause a {@link
  * NullPointerException} to be thrown.
  *
diff --git a/ojluni/src/main/java/java/util/NoSuchElementException.java b/ojluni/src/main/java/java/util/NoSuchElementException.java
index 88ff2c0..15e1aad 100644
--- a/ojluni/src/main/java/java/util/NoSuchElementException.java
+++ b/ojluni/src/main/java/java/util/NoSuchElementException.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,13 +26,12 @@
 package java.util;
 
 /**
- * Thrown by the <code>nextElement</code> method of an
- * <code>Enumeration</code> to indicate that there are no more
- * elements in the enumeration.
+ * Thrown by various accessor methods to indicate that the element being requested
+ * does not exist.
  *
  * @author  unascribed
- * @see     java.util.Enumeration
  * @see     java.util.Enumeration#nextElement()
+ * @see     java.util.Iterator#next()
  * @since   JDK1.0
  */
 public
diff --git a/ojluni/src/main/java/java/util/Observable.java b/ojluni/src/main/java/java/util/Observable.java
index 1d8be3b..1f21aaa 100644
--- a/ojluni/src/main/java/java/util/Observable.java
+++ b/ojluni/src/main/java/java/util/Observable.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,7 +44,7 @@
  * notifications on separate threads, or may guarantee that their
  * subclass follows this order, as they choose.
  * <p>
- * Note that this notification mechanism is has nothing to do with threads
+ * Note that this notification mechanism has nothing to do with threads
  * and is completely separate from the <tt>wait</tt> and <tt>notify</tt>
  * mechanism of class <tt>Object</tt>.
  * <p>
@@ -61,12 +61,12 @@
  */
 public class Observable {
     private boolean changed = false;
-    private final ArrayList<Observer> observers;
+    private Vector<Observer> obs;
 
     /** Construct an Observable with zero Observers. */
 
     public Observable() {
-        observers = new ArrayList<>();
+        obs = new Vector<>();
     }
 
     /**
@@ -81,8 +81,8 @@
     public synchronized void addObserver(Observer o) {
         if (o == null)
             throw new NullPointerException();
-        if (!observers.contains(o)) {
-            observers.add(o);
+        if (!obs.contains(o)) {
+            obs.addElement(o);
         }
     }
 
@@ -92,7 +92,7 @@
      * @param   o   the observer to be deleted.
      */
     public synchronized void deleteObserver(Observer o) {
-        observers.remove(o);
+        obs.removeElement(o);
     }
 
     /**
@@ -134,38 +134,37 @@
          * a temporary array buffer, used as a snapshot of the state of
          * current Observers.
          */
-        Observer[] arrLocal;
+        Object[] arrLocal;
 
         synchronized (this) {
             /* We don't want the Observer doing callbacks into
-             * arbitrary Observables while holding its own Monitor.
+             * arbitrary code while holding its own Monitor.
              * The code where we extract each Observable from
-             * the ArrayList and store the state of the Observer
+             * the Vector and store the state of the Observer
              * needs synchronization, but notifying observers
              * does not (should not).  The worst result of any
              * potential race-condition here is that:
-             *
              * 1) a newly-added Observer will miss a
              *   notification in progress
              * 2) a recently unregistered Observer will be
              *   wrongly notified when it doesn't care
              */
+            // Android-changed: Call out to hasChanged() to figure out if something changes.
             if (!hasChanged())
                 return;
-
-            arrLocal = observers.toArray(new Observer[observers.size()]);
+            arrLocal = obs.toArray();
             clearChanged();
         }
 
         for (int i = arrLocal.length-1; i>=0; i--)
-            arrLocal[i].update(this, arg);
+            ((Observer)arrLocal[i]).update(this, arg);
     }
 
     /**
      * Clears the observer list so that this object no longer has any observers.
      */
     public synchronized void deleteObservers() {
-        observers.clear();
+        obs.removeAllElements();
     }
 
     /**
@@ -210,6 +209,6 @@
      * @return  the number of observers of this object.
      */
     public synchronized int countObservers() {
-        return observers.size();
+        return obs.size();
     }
 }
diff --git a/ojluni/src/main/java/java/util/Random.java b/ojluni/src/main/java/java/util/Random.java
index fb4ecfd..1061b59 100644
--- a/ojluni/src/main/java/java/util/Random.java
+++ b/ojluni/src/main/java/java/util/Random.java
@@ -312,9 +312,8 @@
      * Returns the next pseudorandom, uniformly distributed {@code int}
      * value from this random number generator's sequence. The general
      * contract of {@code nextInt} is that one {@code int} value is
-     * pseudorandomly generated and returned. All 2<font size="-1"><sup>32
-     * </sup></font> possible {@code int} values are produced with
-     * (approximately) equal probability.
+     * pseudorandomly generated and returned. All 2<sup>32</sup> possible
+     * {@code int} values are produced with (approximately) equal probability.
      *
      * <p>The method {@code nextInt} is implemented by class {@code Random}
      * as if by:
@@ -335,23 +334,23 @@
      * between 0 (inclusive) and the specified value (exclusive), drawn from
      * this random number generator's sequence.  The general contract of
      * {@code nextInt} is that one {@code int} value in the specified range
-     * is pseudorandomly generated and returned.  All {@code n} possible
+     * is pseudorandomly generated and returned.  All {@code bound} possible
      * {@code int} values are produced with (approximately) equal
-     * probability.  The method {@code nextInt(int n)} is implemented by
+     * probability.  The method {@code nextInt(int bound)} is implemented by
      * class {@code Random} as if by:
      *  <pre> {@code
-     * public int nextInt(int n) {
-     *   if (n <= 0)
-     *     throw new IllegalArgumentException("n must be positive");
+     * public int nextInt(int bound) {
+     *   if (bound <= 0)
+     *     throw new IllegalArgumentException("bound must be positive");
      *
-     *   if ((n & -n) == n)  // i.e., n is a power of 2
-     *     return (int)((n * (long)next(31)) >> 31);
+     *   if ((bound & -bound) == bound)  // i.e., bound is a power of 2
+     *     return (int)((bound * (long)next(31)) >> 31);
      *
      *   int bits, val;
      *   do {
      *       bits = next(31);
-     *       val = bits % n;
-     *   } while (bits - val + (n-1) < 0);
+     *       val = bits % bound;
+     *   } while (bits - val + (bound-1) < 0);
      *   return val;
      * }}</pre>
      *
@@ -377,28 +376,28 @@
      * greatly increases the length of the sequence of values returned by
      * successive calls to this method if n is a small power of two.
      *
-     * @param n the bound on the random number to be returned.  Must be
-     *        positive.
+     * @param bound the upper bound (exclusive).  Must be positive.
      * @return the next pseudorandom, uniformly distributed {@code int}
-     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
+     *         value between zero (inclusive) and {@code bound} (exclusive)
      *         from this random number generator's sequence
-     * @throws IllegalArgumentException if n is not positive
+     * @throws IllegalArgumentException if bound is not positive
      * @since 1.2
      */
+    public int nextInt(int bound) {
+        if (bound <= 0)
+            throw new IllegalArgumentException(BadBound);
 
-    public int nextInt(int n) {
-        if (n <= 0)
-            throw new IllegalArgumentException("n must be positive");
-
-        if ((n & -n) == n)  // i.e., n is a power of 2
-            return (int)((n * (long)next(31)) >> 31);
-
-        int bits, val;
-        do {
-            bits = next(31);
-            val = bits % n;
-        } while (bits - val + (n-1) < 0);
-        return val;
+        int r = next(31);
+        int m = bound - 1;
+        if ((bound & m) == 0)  // i.e., bound is a power of 2
+            r = (int)((bound * (long)r) >> 31);
+        else {
+            for (int u = r;
+                 u - (r = u % bound) + m < 0;
+                 u = next(31))
+                ;
+        }
+        return r;
     }
 
     /**
@@ -457,11 +456,9 @@
      * <p>The general contract of {@code nextFloat} is that one
      * {@code float} value, chosen (approximately) uniformly from the
      * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
-     * pseudorandomly generated and returned. All 2<font
-     * size="-1"><sup>24</sup></font> possible {@code float} values
-     * of the form <i>m&nbsp;x&nbsp</i>2<font
-     * size="-1"><sup>-24</sup></font>, where <i>m</i> is a positive
-     * integer less than 2<font size="-1"><sup>24</sup> </font>, are
+     * pseudorandomly generated and returned. All 2<sup>24</sup> possible
+     * {@code float} values of the form <i>m&nbsp;x&nbsp;</i>2<sup>-24</sup>,
+     * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are
      * produced with (approximately) equal probability.
      *
      * <p>The method {@code nextFloat} is implemented by class {@code Random}
@@ -532,8 +529,7 @@
      * @see Math#random
      */
     public double nextDouble() {
-        return (((long)(next(26)) << 27) + next(27))
-            / (double)(1L << 53);
+        return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
     }
 
     private double nextNextGaussian;
diff --git a/ojluni/src/main/java/java/util/Scanner.java b/ojluni/src/main/java/java/util/Scanner.java
index fdc787d..d2ac697 100644
--- a/ojluni/src/main/java/java/util/Scanner.java
+++ b/ojluni/src/main/java/java/util/Scanner.java
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -50,47 +50,51 @@
  *
  * <p>For example, this code allows a user to read a number from
  * <tt>System.in</tt>:
- * <blockquote><pre>
+ * <blockquote><pre>{@code
  *     Scanner sc = new Scanner(System.in);
  *     int i = sc.nextInt();
- * </pre></blockquote>
+ * }</pre></blockquote>
  *
  * <p>As another example, this code allows <code>long</code> types to be
  * assigned from entries in a file <code>myNumbers</code>:
- * <blockquote><pre>
+ * <blockquote><pre>{@code
  *      Scanner sc = new Scanner(new File("myNumbers"));
  *      while (sc.hasNextLong()) {
  *          long aLong = sc.nextLong();
- *      }</pre></blockquote>
+ *      }
+ * }</pre></blockquote>
  *
  * <p>The scanner can also use delimiters other than whitespace. This
  * example reads several items in from a string:
- *<blockquote><pre>
+ * <blockquote><pre>{@code
  *     String input = "1 fish 2 fish red fish blue fish";
  *     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
  *     System.out.println(s.nextInt());
  *     System.out.println(s.nextInt());
  *     System.out.println(s.next());
  *     System.out.println(s.next());
- *     s.close(); </pre></blockquote>
+ *     s.close();
+ * }</pre></blockquote>
  * <p>
  * prints the following output:
- * <blockquote><pre>
+ * <blockquote><pre>{@code
  *     1
  *     2
  *     red
- *     blue </pre></blockquote>
+ *     blue
+ * }</pre></blockquote>
  *
  * <p>The same output can be generated with this code, which uses a regular
  * expression to parse all four tokens at once:
- *<blockquote><pre>
+ * <blockquote><pre>{@code
  *     String input = "1 fish 2 fish red fish blue fish";
  *     Scanner s = new Scanner(input);
  *     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
  *     MatchResult result = s.match();
  *     for (int i=1; i<=result.groupCount(); i++)
  *         System.out.println(result.group(i));
- *     s.close(); </pre></blockquote>
+ *     s.close();
+ * }</pre></blockquote>
  *
  * <p>The <a name="default-delimiter">default whitespace delimiter</a> used
  * by a scanner is as recognized by {@link java.lang.Character}.{@link
@@ -146,13 +150,13 @@
  * {@link #reset} method will reset the value of the scanner's radix to
  * <code>10</code> regardless of whether it was previously changed.
  *
- * <a name="localized-numbers">
- * <h4> Localized numbers </h4>
+ * <h3> <a name="localized-numbers">Localized numbers</a> </h3>
  *
  * <p> An instance of this class is capable of scanning numbers in the standard
  * formats as well as in the formats of the scanner's locale. A scanner's
  * <a name="initial-locale">initial locale </a>is the value returned by the {@link
- * java.util.Locale#getDefault} method; it may be changed via the {@link
+ * java.util.Locale#getDefault(Locale.Category)
+ * Locale.getDefault(Locale.Category.FORMAT)} method; it may be changed via the {@link
  * #useLocale} method. The {@link #reset} method will reset the value of the
  * scanner's locale to the initial locale regardless of whether it was
  * previously changed.
@@ -163,186 +167,138 @@
  * {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} object,
  * <tt>dfs</tt>.
  *
- * <blockquote><table>
- * <tr><td valign="top"><i>LocalGroupSeparator&nbsp;&nbsp;</i></td>
- *     <td valign="top">The character used to separate thousands groups,
- *                      <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
- *                      java.text.DecimalFormatSymbols#getGroupingSeparator
- *                      getGroupingSeparator()}</td></tr>
- * <tr><td valign="top"><i>LocalDecimalSeparator&nbsp;&nbsp;</i></td>
- *     <td valign="top">The character used for the decimal point,
- *                      <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
- *                      java.text.DecimalFormatSymbols#getDecimalSeparator
- *                      getDecimalSeparator()}</td></tr>
- * <tr><td valign="top"><i>LocalPositivePrefix&nbsp;&nbsp;</i></td>
- *     <td valign="top">The string that appears before a positive number (may
- *                      be empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
- *                      java.text.DecimalFormat#getPositivePrefix
- *                      getPositivePrefix()}</td></tr>
- * <tr><td valign="top"><i>LocalPositiveSuffix&nbsp;&nbsp;</i></td>
- *     <td valign="top">The string that appears after a positive number (may be
- *                      empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
- *                      java.text.DecimalFormat#getPositiveSuffix
- *                      getPositiveSuffix()}</td></tr>
- * <tr><td valign="top"><i>LocalNegativePrefix&nbsp;&nbsp;</i></td>
- *     <td valign="top">The string that appears before a negative number (may
- *                      be empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
- *                      java.text.DecimalFormat#getNegativePrefix
- *                      getNegativePrefix()}</td></tr>
- * <tr><td valign="top"><i>LocalNegativeSuffix&nbsp;&nbsp;</i></td>
- *     <td valign="top">The string that appears after a negative number (may be
- *                      empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
- *                      java.text.DecimalFormat#getNegativeSuffix
- *                      getNegativeSuffix()}</td></tr>
- * <tr><td valign="top"><i>LocalNaN&nbsp;&nbsp;</i></td>
- *     <td valign="top">The string that represents not-a-number for
- *                      floating-point values,
- *                      <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
- *                      java.text.DecimalFormatSymbols#getNaN
- *                      getNaN()}</td></tr>
- * <tr><td valign="top"><i>LocalInfinity&nbsp;&nbsp;</i></td>
- *     <td valign="top">The string that represents infinity for floating-point
- *                      values, <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
- *                      java.text.DecimalFormatSymbols#getInfinity
- *                      getInfinity()}</td></tr>
- * </table></blockquote>
+ * <blockquote><dl>
+ *     <dt><i>LocalGroupSeparator&nbsp;&nbsp;</i>
+ *         <dd>The character used to separate thousands groups,
+ *         <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
+ *         java.text.DecimalFormatSymbols#getGroupingSeparator
+ *         getGroupingSeparator()}
+ *     <dt><i>LocalDecimalSeparator&nbsp;&nbsp;</i>
+ *         <dd>The character used for the decimal point,
+ *     <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
+ *     java.text.DecimalFormatSymbols#getDecimalSeparator
+ *     getDecimalSeparator()}
+ *     <dt><i>LocalPositivePrefix&nbsp;&nbsp;</i>
+ *         <dd>The string that appears before a positive number (may
+ *         be empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
+ *         java.text.DecimalFormat#getPositivePrefix
+ *         getPositivePrefix()}
+ *     <dt><i>LocalPositiveSuffix&nbsp;&nbsp;</i>
+ *         <dd>The string that appears after a positive number (may be
+ *         empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
+ *         java.text.DecimalFormat#getPositiveSuffix
+ *         getPositiveSuffix()}
+ *     <dt><i>LocalNegativePrefix&nbsp;&nbsp;</i>
+ *         <dd>The string that appears before a negative number (may
+ *         be empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
+ *         java.text.DecimalFormat#getNegativePrefix
+ *         getNegativePrefix()}
+ *     <dt><i>LocalNegativeSuffix&nbsp;&nbsp;</i>
+ *         <dd>The string that appears after a negative number (may be
+ *         empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
+ *     java.text.DecimalFormat#getNegativeSuffix
+ *     getNegativeSuffix()}
+ *     <dt><i>LocalNaN&nbsp;&nbsp;</i>
+ *         <dd>The string that represents not-a-number for
+ *         floating-point values,
+ *         <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
+ *         java.text.DecimalFormatSymbols#getNaN
+ *         getNaN()}
+ *     <dt><i>LocalInfinity&nbsp;&nbsp;</i>
+ *         <dd>The string that represents infinity for floating-point
+ *         values, <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
+ *         java.text.DecimalFormatSymbols#getInfinity
+ *         getInfinity()}
+ * </dl></blockquote>
  *
- * <a name="number-syntax">
- * <h4> Number syntax </h4>
+ * <h4> <a name="number-syntax">Number syntax</a> </h4>
  *
  * <p> The strings that can be parsed as numbers by an instance of this class
  * are specified in terms of the following regular-expression grammar, where
- * Rmax is the highest digit in the radix being used (for example, Rmax is 9
- * in base 10).
+ * Rmax is the highest digit in the radix being used (for example, Rmax is 9 in base 10).
  *
- * <p>
- * <table cellspacing=0 cellpadding=0 align=center>
- *
- *   <tr><td valign=top align=right><i>NonASCIIDigit</i>&nbsp;&nbsp;::</td>
- *       <td valign=top>= A non-ASCII character c for which
+ * <dl>
+ *   <dt><i>NonAsciiDigit</i>:
+ *       <dd>A non-ASCII character c for which
  *            {@link java.lang.Character#isDigit Character.isDigit}<tt>(c)</tt>
- *                        returns&nbsp;true</td></tr>
+ *                        returns&nbsp;true
  *
- *   <tr><td>&nbsp;</td></tr>
+ *   <dt><i>Non0Digit</i>:
+ *       <dd><tt>[1-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i>
  *
- *   <tr><td align=right><i>Non0Digit</i>&nbsp;&nbsp;::</td>
- *   <td><tt>= [1-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i></td></tr>
+ *   <dt><i>Digit</i>:
+ *       <dd><tt>[0-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i>
  *
- *   <tr><td>&nbsp;</td></tr>
+ *   <dt><i>GroupedNumeral</i>:
+ *       <dd><tt>(&nbsp;</tt><i>Non0Digit</i>
+ *                   <i>Digit</i><tt>?
+ *                   </tt><i>Digit</i><tt>?</tt>
+ *       <dd>&nbsp;&nbsp;&nbsp;&nbsp;<tt>(&nbsp;</tt><i>LocalGroupSeparator</i>
+ *                         <i>Digit</i>
+ *                         <i>Digit</i>
+ *                         <i>Digit</i><tt> )+ )</tt>
  *
- *   <tr><td align=right><i>Digit</i>&nbsp;&nbsp;::</td>
- *   <td><tt>= [0-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i></td></tr>
+ *   <dt><i>Numeral</i>:
+ *       <dd><tt>( ( </tt><i>Digit</i><tt>+ )
+ *               | </tt><i>GroupedNumeral</i><tt> )</tt>
  *
- *   <tr><td>&nbsp;</td></tr>
+ *   <dt><a name="Integer-regex"><i>Integer</i>:</a>
+ *       <dd><tt>( [-+]? ( </tt><i>Numeral</i><tt>
+ *                               ) )</tt>
+ *       <dd><tt>| </tt><i>LocalPositivePrefix</i> <i>Numeral</i>
+ *                      <i>LocalPositiveSuffix</i>
+ *       <dd><tt>| </tt><i>LocalNegativePrefix</i> <i>Numeral</i>
+ *                 <i>LocalNegativeSuffix</i>
  *
- *   <tr><td valign=top align=right><i>GroupedNumeral</i>&nbsp;&nbsp;::</td>
- *       <td valign=top>
- *         <table cellpadding=0 cellspacing=0>
- *           <tr><td><tt>= (&nbsp;</tt></td>
- *               <td><i>Non0Digit</i><tt>
- *                   </tt><i>Digit</i><tt>?
- *                   </tt><i>Digit</i><tt>?</tt></td></tr>
- *           <tr><td></td>
- *               <td><tt>(&nbsp;</tt><i>LocalGroupSeparator</i><tt>
- *                         </tt><i>Digit</i><tt>
- *                         </tt><i>Digit</i><tt>
- *                         </tt><i>Digit</i><tt> )+ )</tt></td></tr>
- *         </table></td></tr>
+ *   <dt><i>DecimalNumeral</i>:
+ *       <dd><i>Numeral</i>
+ *       <dd><tt>| </tt><i>Numeral</i>
+ *                 <i>LocalDecimalSeparator</i>
+ *                 <i>Digit</i><tt>*</tt>
+ *       <dd><tt>| </tt><i>LocalDecimalSeparator</i>
+ *                 <i>Digit</i><tt>+</tt>
  *
- *   <tr><td>&nbsp;</td></tr>
+ *   <dt><i>Exponent</i>:
+ *       <dd><tt>( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt>
  *
- *   <tr><td align=right><i>Numeral</i>&nbsp;&nbsp;::</td>
- *       <td><tt>= ( ( </tt><i>Digit</i><tt>+ )
- *               | </tt><i>GroupedNumeral</i><tt> )</tt></td></tr>
+ *   <dt><a name="Decimal-regex"><i>Decimal</i>:</a>
+ *       <dd><tt>( [-+]? </tt><i>DecimalNumeral</i>
+ *                         <i>Exponent</i><tt>? )</tt>
+ *       <dd><tt>| </tt><i>LocalPositivePrefix</i>
+ *                 <i>DecimalNumeral</i>
+ *                 <i>LocalPositiveSuffix</i>
+ *                 <i>Exponent</i><tt>?</tt>
+ *       <dd><tt>| </tt><i>LocalNegativePrefix</i>
+ *                 <i>DecimalNumeral</i>
+ *                 <i>LocalNegativeSuffix</i>
+ *                 <i>Exponent</i><tt>?</tt>
  *
- *   <tr><td>&nbsp;</td></tr>
+ *   <dt><i>HexFloat</i>:
+ *       <dd><tt>[-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
+ *                 ([pP][-+]?[0-9]+)?</tt>
  *
- *   <tr><td valign=top align=right>
- *         <a name="Integer-regex"><i>Integer</i>&nbsp;&nbsp;::</td>
- *       <td valign=top><tt>= ( [-+]? ( </tt><i>Numeral</i><tt>
- *                               ) )</tt></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalPositivePrefix</i><tt> </tt><i>Numeral</i><tt>
- *                      </tt><i>LocalPositiveSuffix</i></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalNegativePrefix</i><tt> </tt><i>Numeral</i><tt>
- *                 </tt><i>LocalNegativeSuffix</i></td></tr>
- *
- *   <tr><td>&nbsp;</td></tr>
- *
- *   <tr><td align=right><i>DecimalNumeral</i>&nbsp;&nbsp;::</td>
- *       <td><tt>= </tt><i>Numeral</i></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>Numeral</i><tt>
- *                 </tt><i>LocalDecimalSeparator</i><tt>
- *                 </tt><i>Digit</i><tt>*</tt></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalDecimalSeparator</i><tt>
- *                 </tt><i>Digit</i><tt>+</tt></td></tr>
- *
- *   <tr><td>&nbsp;</td></tr>
- *
- *   <tr><td align=right><i>Exponent</i>&nbsp;&nbsp;::</td>
- *       <td><tt>= ( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt></td></tr>
- *
- *   <tr><td>&nbsp;</td></tr>
- *
- *   <tr><td align=right>
- *         <a name="Decimal-regex"><i>Decimal</i>&nbsp;&nbsp;::</td>
- *       <td><tt>= ( [-+]? </tt><i>DecimalNumeral</i><tt>
- *                         </tt><i>Exponent</i><tt>? )</tt></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalPositivePrefix</i><tt>
- *                 </tt><i>DecimalNumeral</i><tt>
- *                 </tt><i>LocalPositiveSuffix</i>
- *                 </tt><i>Exponent</i><tt>?</td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalNegativePrefix</i><tt>
- *                 </tt><i>DecimalNumeral</i><tt>
- *                 </tt><i>LocalNegativeSuffix</i>
- *                 </tt><i>Exponent</i><tt>?</td></tr>
- *
- *   <tr><td>&nbsp;</td></tr>
- *
- *   <tr><td align=right><i>HexFloat</i>&nbsp;&nbsp;::</td>
- *       <td><tt>= [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
- *                 ([pP][-+]?[0-9]+)?</tt></td></tr>
- *
- *   <tr><td>&nbsp;</td></tr>
- *
- *   <tr><td align=right><i>NonNumber</i>&nbsp;&nbsp;::</td>
- *       <td valign=top><tt>= NaN
+ *   <dt><i>NonNumber</i>:
+ *       <dd><tt>NaN
  *                          | </tt><i>LocalNan</i><tt>
  *                          | Infinity
- *                          | </tt><i>LocalInfinity</i></td></tr>
+ *                          | </tt><i>LocalInfinity</i>
  *
- *   <tr><td>&nbsp;</td></tr>
+ *   <dt><i>SignedNonNumber</i>:
+ *       <dd><tt>( [-+]? </tt><i>NonNumber</i><tt> )</tt>
+ *       <dd><tt>| </tt><i>LocalPositivePrefix</i>
+ *                 <i>NonNumber</i>
+ *                 <i>LocalPositiveSuffix</i>
+ *       <dd><tt>| </tt><i>LocalNegativePrefix</i>
+ *                 <i>NonNumber</i>
+ *                 <i>LocalNegativeSuffix</i>
  *
- *   <tr><td align=right><i>SignedNonNumber</i>&nbsp;&nbsp;::</td>
- *       <td><tt>= ( [-+]? </tt><i>NonNumber</i><tt> )</tt></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalPositivePrefix</i><tt>
- *                 </tt><i>NonNumber</i><tt>
- *                 </tt><i>LocalPositiveSuffix</i></td></tr>
- *   <tr><td></td>
- *       <td><tt>| </tt><i>LocalNegativePrefix</i><tt>
- *                 </tt><i>NonNumber</i><tt>
- *                 </tt><i>LocalNegativeSuffix</i></td></tr>
+ *   <dt><a name="Float-regex"><i>Float</i></a>:
+ *       <dd><i>Decimal</i>
+ *           <tt>| </tt><i>HexFloat</i>
+ *           <tt>| </tt><i>SignedNonNumber</i>
  *
- *   <tr><td>&nbsp;</td></tr>
- *
- *   <tr><td valign=top align=right>
- *         <a name="Float-regex"><i>Float</i>&nbsp;&nbsp;::</td>
- *       <td valign=top><tt>= </tt><i>Decimal</i><tt></td></tr>
- *       <tr><td></td>
- *           <td><tt>| </tt><i>HexFloat</i><tt></td></tr>
- *       <tr><td></td>
- *           <td><tt>| </tt><i>SignedNonNumber</i><tt></td></tr>
- *
- * </table>
- * </center>
- *
- * <p> Whitespace is not significant in the above regular expressions.
+ * </dl>
+ * <p>Whitespace is not significant in the above regular expressions.
  *
  * @since   1.5
  */
@@ -466,7 +422,7 @@
     private int SIMPLE_GROUP_INDEX = 5;
     private String buildIntegerPatternString() {
         String radixDigits = digits.substring(0, radix);
-        // Android changed : Support non-decimal starting digits. (i.e, a-z are valid radix digits).
+        // Android-changed : Support non-decimal starting digits. (i.e, a-z are valid radix digits).
         String nonZeroRadixDigits = "((?i)[" + digits.substring(1, radix) + "]|(" + non0Digit + "))";
 
         // \\p{javaDigit} is not guaranteed to be appropriate
@@ -474,7 +430,7 @@
         // whatever parse method is invoked, so ultimately the
         // Scanner will do the right thing
         String digit = "((?i)["+radixDigits+"]|\\p{javaDigit})";
-        // Android changed : Support non-decimal starting digits.
+        // Android-changed : Support non-decimal starting digits.
         String groupedNumeral = "("+nonZeroRadixDigits+digit+"?"+digit+"?("+
                                 groupSeparator+digit+digit+digit+")+)";
         // digit++ is the possessive form which is necessary for reducing
@@ -573,9 +529,8 @@
      * Constructs a <code>Scanner</code> that returns values scanned
      * from the specified source delimited by the specified pattern.
      *
-     * @param  source A character source implementing the Readable interface
+     * @param source A character source implementing the Readable interface
      * @param pattern A delimiting pattern
-     * @return A scanner with the specified source and pattern
      */
     private Scanner(Readable source, Pattern pattern) {
         assert source != null : "source should not be null";
@@ -741,7 +696,7 @@
     public Scanner(Path source, String charsetName) throws IOException {
         this(Objects.requireNonNull(source), toCharset(charsetName));
     }
-    
+
     private Scanner(Path source, Charset charset)  throws IOException {
         this(makeReadable(Files.newInputStream(source), charset));
     }
@@ -1688,6 +1643,7 @@
      * <tt>findWithinHorizon(Pattern.compile(pattern, horizon))</tt>.
      *
      * @param pattern a string specifying the pattern to search for
+     * @param horizon the search horizon
      * @return the text that matched the specified pattern
      * @throws IllegalStateException if this scanner is closed
      * @throws IllegalArgumentException if horizon is negative
@@ -1722,6 +1678,7 @@
      * thrown.
      *
      * @param pattern the pattern to scan for
+     * @param horizon the search horizon
      * @return the text that matched the specified pattern
      * @throws IllegalStateException if this scanner is closed
      * @throws IllegalArgumentException if horizon is negative
@@ -2660,11 +2617,11 @@
      * <tt>scanner.reset()</tt> behaves in exactly the same way as the
      * invocation
      *
-     * <blockquote><pre>
+     * <blockquote><pre>{@code
      *   scanner.useDelimiter("\\p{javaWhitespace}+")
-     *          .useLocale(Locale.getDefault())
+     *          .useLocale(Locale.getDefault(Locale.Category.FORMAT))
      *          .useRadix(10);
-     * </pre></blockquote>
+     * }</pre></blockquote>
      *
      * @return this scanner
      *
diff --git a/ojluni/src/main/java/java/util/TimerTask.java b/ojluni/src/main/java/java/util/TimerTask.java
index b8c8340..5750486 100644
--- a/ojluni/src/main/java/java/util/TimerTask.java
+++ b/ojluni/src/main/java/java/util/TimerTask.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -130,14 +130,14 @@
      * <p>This method is typically invoked from within a task's run method, to
      * determine whether the current execution of the task is sufficiently
      * timely to warrant performing the scheduled activity:
-     * <pre>
+     * <pre>{@code
      *   public void run() {
      *       if (System.currentTimeMillis() - scheduledExecutionTime() >=
      *           MAX_TARDINESS)
      *               return;  // Too late; skip this execution.
      *       // Perform the task
      *   }
-     * </pre>
+     * }</pre>
      * This method is typically <i>not</i> used in conjunction with
      * <i>fixed-delay execution</i> repeating tasks, as their scheduled
      * execution times are allowed to drift over time, and so are not terribly
diff --git a/ojluni/src/main/java/javax/crypto/CipherOutputStream.java b/ojluni/src/main/java/javax/crypto/CipherOutputStream.java
index 15edd45..6b8d273 100644
--- a/ojluni/src/main/java/javax/crypto/CipherOutputStream.java
+++ b/ojluni/src/main/java/javax/crypto/CipherOutputStream.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -74,6 +74,9 @@
     // the buffer holding data ready to be written out
     private byte[] obuffer;
 
+    // stream status
+    private boolean closed = false;
+
     /**
      *
      * Constructs a CipherOutputStream from an OutputStream and a
@@ -198,11 +201,14 @@
      * @since      JCE1.2
      */
     public void close() throws IOException {
+        if (closed) {
+            return;
+        }
+
+        closed = true;
         try {
             obuffer = cipher.doFinal();
-        } catch (IllegalBlockSizeException e) {
-            obuffer = null;
-        } catch (BadPaddingException e) {
+        } catch (IllegalBlockSizeException | BadPaddingException e) {
             obuffer = null;
         }
         try {
diff --git a/ojluni/src/main/java/javax/net/ssl/SSLEngine.java b/ojluni/src/main/java/javax/net/ssl/SSLEngine.java
index f460d5f..47783a0 100644
--- a/ojluni/src/main/java/javax/net/ssl/SSLEngine.java
+++ b/ojluni/src/main/java/javax/net/ssl/SSLEngine.java
@@ -498,14 +498,14 @@
  *             <td>9&ndash;22</td>
  *             <td></td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>SSL_RSA_WITH_RC4_128_MD5</td>
- *             <td>9+</td>
+ *             <td>9&ndash;TBD</td>
  *             <td>9&ndash;19</td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>SSL_RSA_WITH_RC4_128_SHA</td>
- *             <td>9+</td>
+ *             <td>9&ndash;TBD</td>
  *             <td>9&ndash;23</td>
  *         </tr>
  *         <tr class="deprecated">
@@ -718,9 +718,9 @@
  *             <td>20&ndash;22</td>
  *             <td></td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>TLS_ECDHE_ECDSA_WITH_RC4_128_SHA</td>
- *             <td>20+</td>
+ *             <td>20&ndash;TBD</td>
  *             <td>20&ndash;23</td>
  *         </tr>
  *         <tr>
@@ -783,9 +783,9 @@
  *             <td>20&ndash;22</td>
  *             <td></td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>TLS_ECDHE_RSA_WITH_RC4_128_SHA</td>
- *             <td>20+</td>
+ *             <td>20&ndash;TBD</td>
  *             <td>20&ndash;23</td>
  *         </tr>
  *         <tr class="deprecated">
@@ -933,9 +933,9 @@
  *             <td>21+</td>
  *             <td>21+</td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>TLS_PSK_WITH_RC4_128_SHA</td>
- *             <td>21+</td>
+ *             <td>21&ndash;TBD</td>
  *             <td></td>
  *         </tr>
  *         <tr class="deprecated">
diff --git a/ojluni/src/main/java/javax/net/ssl/SSLSocket.java b/ojluni/src/main/java/javax/net/ssl/SSLSocket.java
index 7203166..4dd8b9e 100644
--- a/ojluni/src/main/java/javax/net/ssl/SSLSocket.java
+++ b/ojluni/src/main/java/javax/net/ssl/SSLSocket.java
@@ -158,7 +158,7 @@
  *         <tr>
  *             <td>SSLv3</td>
  *             <td>1+</td>
- *             <td>1+</td>
+ *             <td>1&ndash;22</td>
  *         </tr>
  *         <tr>
  *             <td>TLSv1</td>
@@ -313,14 +313,14 @@
  *             <td>9&ndash;22</td>
  *             <td></td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>SSL_RSA_WITH_RC4_128_MD5</td>
- *             <td>9+</td>
+ *             <td>9&ndash;TBD</td>
  *             <td>9&ndash;19</td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>SSL_RSA_WITH_RC4_128_SHA</td>
- *             <td>9+</td>
+ *             <td>9&ndash;TBD</td>
  *             <td>9&ndash;23</td>
  *         </tr>
  *         <tr class="deprecated">
@@ -458,9 +458,9 @@
  *             <td>11&ndash;22</td>
  *             <td></td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>TLS_ECDHE_ECDSA_WITH_RC4_128_SHA</td>
- *             <td>11+</td>
+ *             <td>11&ndash;TBD</td>
  *             <td>11&ndash;23</td>
  *         </tr>
  *         <tr>
@@ -518,9 +518,9 @@
  *             <td>11&ndash;22</td>
  *             <td></td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>TLS_ECDHE_RSA_WITH_RC4_128_SHA</td>
- *             <td>11+</td>
+ *             <td>11&ndash;TBD</td>
  *             <td>11&ndash;23</td>
  *         </tr>
  *         <tr class="deprecated">
@@ -663,9 +663,9 @@
  *             <td>21+</td>
  *             <td>21+</td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>TLS_PSK_WITH_RC4_128_SHA</td>
- *             <td>21+</td>
+ *             <td>21&ndash;TBD</td>
  *             <td></td>
  *         </tr>
  *         <tr>
@@ -848,16 +848,16 @@
  *             <td>1&ndash;8</td>
  *             <td>1&ndash;8</td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>RC4-MD5</td>
  *             <td>SSL_RSA_WITH_RC4_128_MD5</td>
- *             <td>1+</td>
+ *             <td>1&ndash;TBD</td>
  *             <td>1&ndash;19</td>
  *         </tr>
- *         <tr>
+ *         <tr class="deprecated">
  *             <td>RC4-SHA</td>
  *             <td>SSL_RSA_WITH_RC4_128_SHA</td>
- *             <td>1+</td>
+ *             <td>1&ndash;TBD</td>
  *             <td>1&ndash;23</td>
  *         </tr>
  *     </tbody>
diff --git a/ojluni/src/main/java/sun/invoke/util/VerifyAccess.java b/ojluni/src/main/java/sun/invoke/util/VerifyAccess.java
new file mode 100644
index 0000000..d2e5820
--- /dev/null
+++ b/ojluni/src/main/java/sun/invoke/util/VerifyAccess.java
@@ -0,0 +1,300 @@
+/*
+ * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.invoke.util;
+
+import java.lang.reflect.Modifier;
+import static java.lang.reflect.Modifier.*;
+import sun.reflect.Reflection;
+
+/**
+ * This class centralizes information about the JVM's linkage access control.
+ * @author jrose
+ */
+public class VerifyAccess {
+
+    private VerifyAccess() { }  // cannot instantiate
+
+    private static final int PACKAGE_ONLY = 0;
+    private static final int PACKAGE_ALLOWED = java.lang.invoke.MethodHandles.Lookup.PACKAGE;
+    private static final int PROTECTED_OR_PACKAGE_ALLOWED = (PACKAGE_ALLOWED|PROTECTED);
+    private static final int ALL_ACCESS_MODES = (PUBLIC|PRIVATE|PROTECTED|PACKAGE_ONLY);
+    private static final boolean ALLOW_NESTMATE_ACCESS = false;
+
+    /**
+     * Evaluate the JVM linkage rules for access to the given method
+     * on behalf of a caller class which proposes to perform the access.
+     * Return true if the caller class has privileges to invoke a method
+     * or access a field with the given properties.
+     * This requires an accessibility check of the referencing class,
+     * plus an accessibility check of the member within the class,
+     * which depends on the member's modifier flags.
+     * <p>
+     * The relevant properties include the defining class ({@code defc})
+     * of the member, and its modifier flags ({@code mods}).
+     * Also relevant is the class used to make the initial symbolic reference
+     * to the member ({@code refc}).  If this latter class is not distinguished,
+     * the defining class should be passed for both arguments ({@code defc == refc}).
+     * <h3>JVM Specification, 5.4.4 "Access Control"</h3>
+     * A field or method R is accessible to a class or interface D if
+     * and only if any of the following conditions is true:<ul>
+     * <li>R is public.
+     * <li>R is protected and is declared in a class C, and D is either
+     *     a subclass of C or C itself.  Furthermore, if R is not
+     *     static, then the symbolic reference to R must contain a
+     *     symbolic reference to a class T, such that T is either a
+     *     subclass of D, a superclass of D or D itself.
+     * <li>R is either protected or has default access (that is,
+     *     neither public nor protected nor private), and is declared
+     *     by a class in the same runtime package as D.
+     * <li>R is private and is declared in D.
+     * </ul>
+     * This discussion of access control omits a related restriction
+     * on the target of a protected field access or method invocation
+     * (the target must be of class D or a subtype of D). That
+     * requirement is checked as part of the verification process
+     * (5.4.1); it is not part of link-time access control.
+     * @param refc the class used in the symbolic reference to the proposed member
+     * @param defc the class in which the proposed member is actually defined
+     * @param mods modifier flags for the proposed member
+     * @param lookupClass the class for which the access check is being made
+     * @return true iff the the accessing class can access such a member
+     */
+    public static boolean isMemberAccessible(Class<?> refc,  // symbolic ref class
+                                             Class<?> defc,  // actual def class
+                                             int      mods,  // actual member mods
+                                             Class<?> lookupClass,
+                                             int      allowedModes) {
+        if (allowedModes == 0)  return false;
+        assert((allowedModes & PUBLIC) != 0 &&
+               (allowedModes & ~(ALL_ACCESS_MODES|PACKAGE_ALLOWED)) == 0);
+        // The symbolic reference class (refc) must always be fully verified.
+        if (!isClassAccessible(refc, lookupClass, allowedModes)) {
+            return false;
+        }
+        // Usually refc and defc are the same, but verify defc also in case they differ.
+        if (defc == lookupClass &&
+            (allowedModes & PRIVATE) != 0)
+            return true;        // easy check; all self-access is OK
+        switch (mods & ALL_ACCESS_MODES) {
+        case PUBLIC:
+            return true;  // already checked above
+        case PROTECTED:
+            assert !defc.isInterface(); // protected members aren't allowed in interfaces
+            if ((allowedModes & PROTECTED_OR_PACKAGE_ALLOWED) != 0 &&
+                isSamePackage(defc, lookupClass))
+                return true;
+            if ((allowedModes & PROTECTED) == 0)
+                return false;
+            // Protected members are accessible by subclasses, which does not include interfaces.
+            // Interfaces are types, not classes. They should not have access to
+            // protected members in j.l.Object, even though it is their superclass.
+            if ((mods & STATIC) != 0 &&
+                !isRelatedClass(refc, lookupClass))
+                return false;
+            if ((allowedModes & PROTECTED) != 0 &&
+                isSubClass(lookupClass, defc))
+                return true;
+            return false;
+        case PACKAGE_ONLY:  // That is, zero.  Unmarked member is package-only access.
+            assert !defc.isInterface(); // package-private members aren't allowed in interfaces
+            return ((allowedModes & PACKAGE_ALLOWED) != 0 &&
+                    isSamePackage(defc, lookupClass));
+        case PRIVATE:
+            // Loosened rules for privates follows access rules for inner classes.
+            return (ALLOW_NESTMATE_ACCESS &&
+                    (allowedModes & PRIVATE) != 0 &&
+                    isSamePackageMember(defc, lookupClass));
+        default:
+            throw new IllegalArgumentException("bad modifiers: "+Modifier.toString(mods));
+        }
+    }
+
+    static boolean isRelatedClass(Class<?> refc, Class<?> lookupClass) {
+        return (refc == lookupClass ||
+                isSubClass(refc, lookupClass) ||
+                isSubClass(lookupClass, refc));
+    }
+
+    static boolean isSubClass(Class<?> lookupClass, Class<?> defc) {
+        return defc.isAssignableFrom(lookupClass) &&
+               !lookupClass.isInterface(); // interfaces are types, not classes.
+    }
+
+    /**
+     * Evaluate the JVM linkage rules for access to the given class on behalf of caller.
+     * <h3>JVM Specification, 5.4.4 "Access Control"</h3>
+     * A class or interface C is accessible to a class or interface D
+     * if and only if either of the following conditions are true:<ul>
+     * <li>C is public.
+     * <li>C and D are members of the same runtime package.
+     * </ul>
+     * @param refc the symbolic reference class to which access is being checked (C)
+     * @param lookupClass the class performing the lookup (D)
+     */
+    public static boolean isClassAccessible(Class<?> refc, Class<?> lookupClass,
+                                            int allowedModes) {
+        if (allowedModes == 0)  return false;
+        // Android-changed: Use public APIs to figure out whether a class
+        // is public or not.
+        if (Modifier.isPublic(refc.getModifiers()))
+            return true;
+        if ((allowedModes & PACKAGE_ALLOWED) != 0 &&
+            isSamePackage(lookupClass, refc))
+            return true;
+        return false;
+    }
+
+    /**
+     * Decide if the given method type, attributed to a member or symbolic
+     * reference of a given reference class, is really visible to that class.
+     * @param type the supposed type of a member or symbolic reference of refc
+     * @param refc the class attempting to make the reference
+     */
+    public static boolean isTypeVisible(Class<?> type, Class<?> refc) {
+        if (type == refc)  return true;  // easy check
+        while (type.isArray())  type = type.getComponentType();
+        if (type.isPrimitive() || type == Object.class)  return true;
+        ClassLoader parent = type.getClassLoader();
+        if (parent == null)  return true;
+        ClassLoader child  = refc.getClassLoader();
+        if (child == null)  return false;
+        if (parent == child || loadersAreRelated(parent, child, true))
+            return true;
+        // Do it the hard way:  Look up the type name from the refc loader.
+        try {
+            Class<?> res = child.loadClass(type.getName());
+            return (type == res);
+        } catch (ClassNotFoundException ex) {
+            return false;
+        }
+    }
+
+    /**
+     * Decide if the given method type, attributed to a member or symbolic
+     * reference of a given reference class, is really visible to that class.
+     * @param type the supposed type of a member or symbolic reference of refc
+     * @param refc the class attempting to make the reference
+     */
+    public static boolean isTypeVisible(java.lang.invoke.MethodType type, Class<?> refc) {
+        for (int n = -1, max = type.parameterCount(); n < max; n++) {
+            Class<?> ptype = (n < 0 ? type.returnType() : type.parameterType(n));
+            if (!isTypeVisible(ptype, refc))
+                return false;
+        }
+        return true;
+    }
+
+    /**
+     * Test if two classes have the same class loader and package qualifier.
+     * @param class1 a class
+     * @param class2 another class
+     * @return whether they are in the same package
+     */
+    public static boolean isSamePackage(Class<?> class1, Class<?> class2) {
+        // Android-changed: Throw IAE (instead of asserting) if called with array classes.
+        if (class1.isArray() || class2.isArray()) {
+            throw new IllegalArgumentException();
+        }
+
+        if (class1 == class2)
+            return true;
+        if (class1.getClassLoader() != class2.getClassLoader())
+            return false;
+        String name1 = class1.getName(), name2 = class2.getName();
+        int dot = name1.lastIndexOf('.');
+        if (dot != name2.lastIndexOf('.'))
+            return false;
+        for (int i = 0; i < dot; i++) {
+            if (name1.charAt(i) != name2.charAt(i))
+                return false;
+        }
+        return true;
+    }
+
+    /** Return the package name for this class.
+     */
+    public static String getPackageName(Class<?> cls) {
+        assert(!cls.isArray());
+        String name = cls.getName();
+        int dot = name.lastIndexOf('.');
+        if (dot < 0)  return "";
+        return name.substring(0, dot);
+    }
+
+    /**
+     * Test if two classes are defined as part of the same package member (top-level class).
+     * If this is true, they can share private access with each other.
+     * @param class1 a class
+     * @param class2 another class
+     * @return whether they are identical or nested together
+     */
+    public static boolean isSamePackageMember(Class<?> class1, Class<?> class2) {
+        if (class1 == class2)
+            return true;
+        if (!isSamePackage(class1, class2))
+            return false;
+        if (getOutermostEnclosingClass(class1) != getOutermostEnclosingClass(class2))
+            return false;
+        return true;
+    }
+
+    private static Class<?> getOutermostEnclosingClass(Class<?> c) {
+        Class<?> pkgmem = c;
+        for (Class<?> enc = c; (enc = enc.getEnclosingClass()) != null; )
+            pkgmem = enc;
+        return pkgmem;
+    }
+
+    private static boolean loadersAreRelated(ClassLoader loader1, ClassLoader loader2,
+                                             boolean loader1MustBeParent) {
+        if (loader1 == loader2 || loader1 == null
+                || (loader2 == null && !loader1MustBeParent)) {
+            return true;
+        }
+        for (ClassLoader scan2 = loader2;
+                scan2 != null; scan2 = scan2.getParent()) {
+            if (scan2 == loader1)  return true;
+        }
+        if (loader1MustBeParent)  return false;
+        // see if loader2 is a parent of loader1:
+        for (ClassLoader scan1 = loader1;
+                scan1 != null; scan1 = scan1.getParent()) {
+            if (scan1 == loader2)  return true;
+        }
+        return false;
+    }
+
+    /**
+     * Is the class loader of parentClass identical to, or an ancestor of,
+     * the class loader of childClass?
+     * @param parentClass a class
+     * @param childClass another class, which may be a descendent of the first class
+     * @return whether parentClass precedes or equals childClass in class loader order
+     */
+    public static boolean classLoaderIsAncestor(Class<?> parentClass, Class<?> childClass) {
+        return loadersAreRelated(parentClass.getClassLoader(), childClass.getClassLoader(), true);
+    }
+}
diff --git a/ojluni/src/main/native/PlainDatagramSocketImpl.c b/ojluni/src/main/native/PlainDatagramSocketImpl.c
index f9b9deb..aad8c34 100644
--- a/ojluni/src/main/native/PlainDatagramSocketImpl.c
+++ b/ojluni/src/main/native/PlainDatagramSocketImpl.c
@@ -1284,11 +1284,11 @@
 
 /*
  * Class:     java_net_PlainDatagramSocketImpl
- * Method:    socketSetOption
+ * Method:    socketSetOption0
  * Signature: (ILjava/lang/Object;)V
  */
 JNIEXPORT void JNICALL
-PlainDatagramSocketImpl_socketSetOption(JNIEnv *env,
+PlainDatagramSocketImpl_socketSetOption0(JNIEnv *env,
                                                       jobject this,
                                                       jint opt,
                                                       jobject value) {
@@ -1942,7 +1942,7 @@
   NATIVE_METHOD(PlainDatagramSocketImpl, setTimeToLive, "(I)V"),
   NATIVE_METHOD(PlainDatagramSocketImpl, setTTL, "(B)V"),
   NATIVE_METHOD(PlainDatagramSocketImpl, socketGetOption, "(I)Ljava/lang/Object;"),
-  NATIVE_METHOD(PlainDatagramSocketImpl, socketSetOption, "(ILjava/lang/Object;)V"),
+  NATIVE_METHOD(PlainDatagramSocketImpl, socketSetOption0, "(ILjava/lang/Object;)V"),
   NATIVE_METHOD(PlainDatagramSocketImpl, datagramSocketClose, "()V"),
   NATIVE_METHOD(PlainDatagramSocketImpl, datagramSocketCreate, "()V"),
   NATIVE_METHOD(PlainDatagramSocketImpl, receive0, "(Ljava/net/DatagramPacket;)V"),
diff --git a/ojluni/src/main/native/PlainSocketImpl.c b/ojluni/src/main/native/PlainSocketImpl.c
index af856c5..0129eb5 100644
--- a/ojluni/src/main/native/PlainSocketImpl.c
+++ b/ojluni/src/main/native/PlainSocketImpl.c
@@ -845,11 +845,11 @@
 
 /*
  * Class:     java_net_PlainSocketImpl
- * Method:    socketSetOption
+ * Method:    socketSetOption0
  * Signature: (IZLjava/lang/Object;)V
  */
 JNIEXPORT void JNICALL
-PlainSocketImpl_socketSetOption(JNIEnv *env, jobject this,
+PlainSocketImpl_socketSetOption0(JNIEnv *env, jobject this,
                                               jint cmd, jboolean on,
                                               jobject value) {
     int fd;
@@ -1073,7 +1073,7 @@
 static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(PlainSocketImpl, socketSendUrgentData, "(I)V"),
   NATIVE_METHOD(PlainSocketImpl, socketGetOption, "(ILjava/lang/Object;)I"),
-  NATIVE_METHOD(PlainSocketImpl, socketSetOption, "(IZLjava/lang/Object;)V"),
+  NATIVE_METHOD(PlainSocketImpl, socketSetOption0, "(IZLjava/lang/Object;)V"),
   NATIVE_METHOD(PlainSocketImpl, socketShutdown, "(I)V"),
   NATIVE_METHOD(PlainSocketImpl, socketClose0, "()V"),
   NATIVE_METHOD(PlainSocketImpl, socketAccept, "(Ljava/net/SocketImpl;)V"),
diff --git a/ojluni/src/main/native/net_util_md.c b/ojluni/src/main/native/net_util_md.c
index f1d60fe..0dd6c09 100644
--- a/ojluni/src/main/native/net_util_md.c
+++ b/ojluni/src/main/native/net_util_md.c
@@ -911,12 +911,10 @@
 
     int i;
 
-    /*
-     * Different multicast options if IPv6 is enabled
-     */
 #ifdef AF_INET6
     if (ipv6_available()) {
         switch (cmd) {
+            // Different multicast options if IPv6 is enabled
             case java_net_SocketOptions_IP_MULTICAST_IF:
             case java_net_SocketOptions_IP_MULTICAST_IF2:
                 *level = IPPROTO_IPV6;
@@ -927,6 +925,13 @@
                 *level = IPPROTO_IPV6;
                 *optname = IPV6_MULTICAST_LOOP;
                 return 0;
+#if (defined(__solaris__) || defined(MACOSX))
+            // Map IP_TOS request to IPV6_TCLASS
+            case java_net_SocketOptions_IP_TOS:
+                *level = IPPROTO_IPV6;
+                *optname = IPV6_TCLASS;
+                return 0;
+#endif
         }
     }
 #endif
@@ -950,9 +955,6 @@
  * Wrapper for getsockopt system routine - does any necessary
  * pre/post processing to deal with OS specific oddities :-
  *
- * IP_TOS is a no-op with IPv6 sockets as it's setup when
- * the connection is established.
- *
  * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
  * to compensate for an incorrect value returned by the kernel.
  */
@@ -962,21 +964,6 @@
 {
     int rv;
 
-#ifdef AF_INET6
-    if ((level == IPPROTO_IP) && (opt == IP_TOS)) {
-        if (ipv6_available()) {
-
-            /*
-             * For IPv6 socket option implemented at Java-level
-             * so return -1.
-             */
-            int *tc = (int *)result;
-            *tc = -1;
-            return 0;
-        }
-    }
-#endif
-
 #ifdef __solaris__
     rv = getsockopt(fd, level, opt, result, len);
 #else
@@ -1027,8 +1014,7 @@
  *
  * For IP_TOS socket option need to mask off bits as this
  * aren't automatically masked by the kernel and results in
- * an error. In addition IP_TOS is a NOOP with IPv6 as it
- * should be setup as connection time.
+ * an error.
  */
 int
 NET_SetSockOpt(int fd, int level, int  opt, const void *arg,
@@ -1054,41 +1040,46 @@
 #else
     static long maxsockbuf = -1;
 #endif
-
-    int addopt;
-    struct linger *ling;
 #endif
 
     /*
      * IPPROTO/IP_TOS :-
-     * 1. IPv6 on Solaris/Mac OS: NOOP and will be set
-     *    in flowinfo field when connecting TCP socket,
-     *    or sending UDP packet.
+     * 1. IPv6 on Solaris/Mac OS:
+     *    Set the TOS OR Traffic Class value to cater for
+     *    IPv6 and IPv4 scenarios.
      * 2. IPv6 on Linux: By default Linux ignores flowinfo
      *    field so enable IPV6_FLOWINFO_SEND so that flowinfo
-     *    will be examined.
+     *    will be examined. We also set the IPv4 TOS option in this case.
      * 3. IPv4: set socket option based on ToS and Precedence
      *    fields (otherwise get invalid argument)
      */
     if (level == IPPROTO_IP && opt == IP_TOS) {
         int *iptos;
 
-#if defined(AF_INET6) && (defined(__solaris__) || defined(MACOSX))
-        if (ipv6_available()) {
-            return 0;
-        }
-#endif
-
 #if defined(AF_INET6) && defined(__linux__)
         if (ipv6_available()) {
             int optval = 1;
-            return setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
-                              (void *)&optval, sizeof(optval));
+            if (setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
+                           (void *)&optval, sizeof(optval)) < 0) {
+                return -1;
+            }
+           /*
+            * Let's also set the IPV6_TCLASS flag.
+            * Linux appears to allow both IP_TOS and IPV6_TCLASS to be set
+            * This helps in mixed environments where IPv4 and IPv6 sockets
+            * are connecting.
+            */
+           if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS,
+                           arg, len) < 0) {
+                return -1;
+            }
         }
 #endif
 
         iptos = (int *)arg;
-        *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
+        // Android-changed: This is out-dated RFC 1349 scheme. Modern Linux uses
+        // Diffsev/ECN, and this mask is no longer relavant.
+        // *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
     }
 
     /*
@@ -1153,7 +1144,7 @@
      * On Linux the receive buffer is used for both socket
      * structures and the the packet payload. The implication
      * is that if SO_RCVBUF is too small then small packets
-     * must be discard.
+     * must be discarded.
      */
 #ifdef __linux__
     if (level == SOL_SOCKET && opt == SO_RCVBUF) {
@@ -1222,8 +1213,7 @@
         }
 
         if (sotype == SOCK_DGRAM) {
-            addopt = SO_REUSEPORT;
-            setsockopt(fd, level, addopt, arg, len);
+            setsockopt(fd, level, SO_REUSEPORT, arg, len);
         }
     }
 
@@ -1336,7 +1326,7 @@
  * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
  *
  * The function will return when either the socket is ready for one
- * of the specified operation or the timeout expired.
+ * of the specified operations or the timeout expired.
  *
  * It returns the time left from the timeout (possibly 0), or -1 if it expired.
  */
diff --git a/openjdk_java_files.mk b/openjdk_java_files.mk
index fc7117a..e717cae 100644
--- a/openjdk_java_files.mk
+++ b/openjdk_java_files.mk
@@ -187,9 +187,11 @@
     ojluni/src/main/java/java/lang/reflect/InvocationTargetException.java \
     ojluni/src/main/java/java/lang/ReflectiveOperationException.java \
     ojluni/src/main/java/java/lang/reflect/MalformedParameterizedTypeException.java \
+    ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java \
     ojluni/src/main/java/java/lang/reflect/Member.java \
     ojluni/src/main/java/java/lang/reflect/Method.java \
     ojluni/src/main/java/java/lang/reflect/Modifier.java \
+    ojluni/src/main/java/java/lang/reflect/Parameter.java \
     ojluni/src/main/java/java/lang/reflect/ParameterizedType.java \
     ojluni/src/main/java/java/lang/reflect/Proxy.java \
     ojluni/src/main/java/java/lang/reflect/ReflectPermission.java \
@@ -760,6 +762,7 @@
     ojluni/src/main/java/java/util/ArrayPrefixHelpers.java \
     ojluni/src/main/java/java/util/Arrays.java \
     ojluni/src/main/java/java/util/ArraysParallelSortHelpers.java \
+    ojluni/src/main/java/java/util/Base64.java \
     ojluni/src/main/java/java/util/BitSet.java \
     ojluni/src/main/java/java/util/Calendar.java \
     ojluni/src/main/java/java/util/Collection.java \
@@ -1161,6 +1164,8 @@
     ojluni/src/main/java/sun/misc/FloatingDecimal.java \
     ojluni/src/main/java/java/lang/invoke/LambdaConversionException.java \
     ojluni/src/main/java/java/lang/invoke/MethodHandle.java \
+    ojluni/src/main/java/java/lang/invoke/MethodHandles.java \
+    ojluni/src/main/java/java/lang/invoke/MethodHandleImpl.java \
     ojluni/src/main/java/java/lang/invoke/MethodHandleInfo.java \
     ojluni/src/main/java/java/lang/invoke/MethodHandleStatics.java \
     ojluni/src/main/java/java/lang/invoke/MethodType.java \
@@ -1184,6 +1189,7 @@
     ojluni/src/main/java/sun/invoke/empty/Empty.java \
     ojluni/src/main/java/sun/invoke/util/BytecodeDescriptor.java \
     ojluni/src/main/java/sun/invoke/util/Wrapper.java \
+    ojluni/src/main/java/sun/invoke/util/VerifyAccess.java \
     ojluni/src/main/java/sun/misc/ASCIICaseInsensitiveComparator.java \
     ojluni/src/main/java/sun/misc/BASE64Decoder.java \
     ojluni/src/main/java/sun/misc/BASE64Encoder.java \
@@ -1622,9 +1628,9 @@
 openjdk_lambda_stub_files := \
     ojluni/src/lambda/java/java/lang/invoke/CallSite.java \
     ojluni/src/lambda/java/java/lang/invoke/LambdaMetafactory.java \
-    ojluni/src/lambda/java/java/lang/invoke/MethodHandles.java \
     ojluni/src/lambda/java/java/lang/invoke/SerializedLambda.java
 openjdk_lambda_duplicate_stub_files := \
+    ojluni/src/lambda/java/java/lang/invoke/MethodHandles.java \
     ojluni/src/lambda/java/java/lang/invoke/LambdaConversionException.java \
     ojluni/src/lambda/java/java/lang/invoke/MethodHandle.java \
     ojluni/src/lambda/java/java/lang/invoke/MethodType.java \
diff --git a/support/src/test/java/libcore/java/security/StandardNames.java b/support/src/test/java/libcore/java/security/StandardNames.java
index d414d4b..c9546e2 100644
--- a/support/src/test/java/libcore/java/security/StandardNames.java
+++ b/support/src/test/java/libcore/java/security/StandardNames.java
@@ -735,11 +735,7 @@
         addBoth(   "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");
         addBoth(   "TLS_RSA_WITH_AES_128_CBC_SHA");
         addBoth(   "TLS_DHE_RSA_WITH_AES_128_CBC_SHA");
-        addBoth(   "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA");
-        addBoth(   "TLS_ECDHE_RSA_WITH_RC4_128_SHA");
-        addBoth(   "SSL_RSA_WITH_RC4_128_SHA");
         addBoth(   "SSL_RSA_WITH_3DES_EDE_CBC_SHA");
-        addBoth(   "SSL_RSA_WITH_RC4_128_MD5");
 
         // TLSv1.2 cipher suites
         addBoth(   "TLS_RSA_WITH_AES_128_CBC_SHA256");
@@ -762,7 +758,6 @@
         addOpenSsl("TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256");
 
         // Pre-Shared Key (PSK) cipher suites
-        addOpenSsl("TLS_PSK_WITH_RC4_128_SHA");
         addOpenSsl("TLS_PSK_WITH_AES_128_CBC_SHA");
         addOpenSsl("TLS_PSK_WITH_AES_256_CBC_SHA");
         addOpenSsl("TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA");
@@ -801,6 +796,12 @@
         addRi(     "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256");
         addNeither("TLS_DHE_DSS_WITH_AES_256_GCM_SHA384");
 
+        // Android does not have RC4 support
+        addRi(     "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA");
+        addRi(     "TLS_ECDHE_RSA_WITH_RC4_128_SHA");
+        addRi(     "SSL_RSA_WITH_RC4_128_SHA");
+        addRi(     "SSL_RSA_WITH_RC4_128_MD5");
+
         // Dropped
         addNeither("SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA");
         addNeither("SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA");
@@ -988,6 +989,15 @@
             "TLS_PSK_WITH_AES_256_CBC_SHA"
             );
 
+    // Should be updated to match BoringSSL's defaults when they change.
+    // https://android.googlesource.com/platform/external/boringssl/+/master/src/ssl/t1_lib.c#305
+    public static final List<String> ELLIPTIC_CURVES_DEFAULT = Arrays.asList(
+            "x25519 (29)",
+            "secp256r1 (23)",
+            "secp384r1 (24)",
+            "secp521r1 (25)"
+            );
+
     private static final Set<String> PERMITTED_DEFAULT_KEY_EXCHANGE_ALGS =
             new HashSet<String>(Arrays.asList("RSA",
                                               "DHE_RSA",
@@ -1154,6 +1164,10 @@
         }
     }
 
+    public static void assertDefaultEllipticCurves(String[] curves) {
+        assertEquals(ELLIPTIC_CURVES_DEFAULT, Arrays.asList(curves));
+    }
+
     public static void assertSSLContextEnabledProtocols(String version, String[] protocols) {
         assertEquals("For protocol \"" + version + "\"",
                 Arrays.toString(SSL_CONTEXT_PROTOCOLS_ENABLED.get(version)),
diff --git a/support/src/test/java/libcore/tlswire/handshake/EllipticCurve.java b/support/src/test/java/libcore/tlswire/handshake/EllipticCurve.java
new file mode 100644
index 0000000..a409c41
--- /dev/null
+++ b/support/src/test/java/libcore/tlswire/handshake/EllipticCurve.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.tlswire.handshake;
+
+/**
+ * {@code EllipticCurve} enum from RFC 4492 section 5.1.1. Curves are assigned
+ * via the
+ * <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8">IANA registry</a>.
+ */
+public enum EllipticCurve {
+    SECT163K1(1, "sect163k1"),
+    SECT163R1(2, "sect163r1"),
+    SECT163R2(3, "sect163r2"),
+    SECT193R1(4, "sect193r1"),
+    SECT193R2(5, "sect193r2"),
+    SECT233K1(6, "sect233k1"),
+    SECT233R1(7, "sect233r1"),
+    SECT239K1(8, "sect239k1"),
+    SECT283K1(9, "sect283k1"),
+    SECT283R1(10, "sect283r1"),
+    SECT409K1(11, "sect409k1"),
+    SECT409R1(12, "sect409r1"),
+    SECT571K1(13, "sect571k1"),
+    SECT571R1(14, "sect571r1"),
+    SECP160K1(15, "secp160k1"),
+    SECP160R1(16, "secp160r1"),
+    SECP160R2(17, "secp160r2"),
+    SECP192K1(18, "secp192k1"),
+    SECP192R1(19, "secp192r1"),
+    SECP224K1(20, "secp224k1"),
+    SECP256K1(22, "secp256k1"),
+    SECP256R1(23, "secp256r1"),
+    SECP384R1(24, "secp384r1"),
+    SECP521R1(25, "secp521r1"),
+    BRAINPOOLP256R1(26, "brainpoolP256r1"),
+    BRAINPOOLP384R1(27, "brainpoolP384r1"),
+    BRAINPOOLP521R1(28, "brainpoolP521r1"),
+    X25519(29, "x25519"),
+    X448(30, "x448"),
+    ARBITRARY_PRIME(0xFF01, "arbitrary_explicit_prime_curves"),
+    ARBITRARY_CHAR2(0xFF02, "arbitrary_explicit_char2_curves");
+
+    public final int identifier;
+    public final String name;
+
+    private EllipticCurve(int identifier, String name) {
+        this.identifier = identifier;
+        this.name = name;
+    }
+
+    public static EllipticCurve fromIdentifier(int identifier) {
+        for (EllipticCurve curve : values()) {
+            if (curve.identifier == identifier) {
+                return curve;
+            }
+        }
+        throw new AssertionError("Unknown curve identifier " + identifier);
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder(name);
+        sb.append(" (");
+        sb.append(identifier);
+        sb.append(')');
+        return sb.toString();
+    }
+}
diff --git a/support/src/test/java/libcore/tlswire/handshake/EllipticCurvesHelloExtension.java b/support/src/test/java/libcore/tlswire/handshake/EllipticCurvesHelloExtension.java
new file mode 100644
index 0000000..19749a3
--- /dev/null
+++ b/support/src/test/java/libcore/tlswire/handshake/EllipticCurvesHelloExtension.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.tlswire.handshake;
+
+import libcore.tlswire.util.IoUtils;
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * {@code elliptic_curves} {@link HelloExtension} from RFC 4492 section 5.1.1.
+ */
+public class EllipticCurvesHelloExtension extends HelloExtension {
+    public List<EllipticCurve> supported;
+    public boolean wellFormed;
+
+    @Override
+    protected void parseData() throws IOException {
+        byte[] ellipticCurvesListBytes = IoUtils.readTlsVariableLengthByteVector(
+                new DataInputStream(new ByteArrayInputStream(data)), 0xffff);
+        ByteArrayInputStream ellipticCurvesListIn = new ByteArrayInputStream(ellipticCurvesListBytes);
+        DataInputStream in = new DataInputStream(ellipticCurvesListIn);
+        wellFormed = (ellipticCurvesListIn.available() % 2) == 0;
+        supported = new ArrayList<EllipticCurve>(ellipticCurvesListIn.available() / 2);
+        while (ellipticCurvesListIn.available() >= 2) {
+            int curve_id = in.readUnsignedShort();
+            supported.add(EllipticCurve.fromIdentifier(curve_id));
+        }
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("HelloExtension{type: elliptic_curves, wellFormed: ");
+        sb.append(wellFormed);
+        sb.append(", supported: ");
+        sb.append(supported);
+        sb.append('}');
+        return sb.toString();
+    }
+}
diff --git a/support/src/test/java/libcore/tlswire/handshake/HelloExtension.java b/support/src/test/java/libcore/tlswire/handshake/HelloExtension.java
index a648cdf..2a77687 100644
--- a/support/src/test/java/libcore/tlswire/handshake/HelloExtension.java
+++ b/support/src/test/java/libcore/tlswire/handshake/HelloExtension.java
@@ -29,6 +29,7 @@
 public class HelloExtension {
 
     public static final int TYPE_SERVER_NAME = 0;
+    public static final int TYPE_ELLIPTIC_CURVES = 10;
     public static final int TYPE_PADDING = 21;
     public static final int TYPE_SESSION_TICKET = 35;
     public static final int TYPE_RENEGOTIATION_INFO = 65281;
@@ -45,7 +46,7 @@
         TYPE_TO_NAME.put(7, "client_authz");
         TYPE_TO_NAME.put(8, "server_authz");
         TYPE_TO_NAME.put(9, "cert_type");
-        TYPE_TO_NAME.put(10, "elliptic_curves");
+        TYPE_TO_NAME.put(TYPE_ELLIPTIC_CURVES, "elliptic_curves");
         TYPE_TO_NAME.put(11, "ec_point_formats");
         TYPE_TO_NAME.put(12, "srp");
         TYPE_TO_NAME.put(13, "signature_algorithms");
@@ -75,6 +76,9 @@
             case TYPE_SERVER_NAME:
                 result = new ServerNameHelloExtension();
                 break;
+            case TYPE_ELLIPTIC_CURVES:
+                result = new EllipticCurvesHelloExtension();
+                break;
             default:
                 result = new HelloExtension();
                 break;