Merge "MethodHandleStatics: Remove unused debugging code."
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
index 6e3e181..73cc60b 100644
--- a/libart/src/main/java/java/lang/reflect/AbstractMethod.java
+++ b/libart/src/main/java/java/lang/reflect/AbstractMethod.java
@@ -216,6 +216,10 @@
}
}
+ boolean hasGenericInformationInternal() {
+ return getSignatureAnnotation() != null;
+ }
+
/**
* Returns generic information associated with this method/constructor member.
*/
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/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/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..86d9c58 100644
--- a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
@@ -21,6 +21,7 @@
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.SocketException;
@@ -88,4 +89,31 @@
}
}
}
+
+ public void test_setTrafficClass() throws Exception {
+ DatagramSocket s = new DatagramSocket();
+
+ for (int i = 0; i <= 255; ++i) {
+ s.setTrafficClass(i);
+ assertEquals(i, s.getTrafficClass());
+ }
+ }
+
+ // Socket should become connected even if impl.connect() failed and threw exception.
+ public void test_b31218085() throws Exception {
+ final int port = 9999;
+
+ try (DatagramSocket s = new DatagramSocket()) {
+ // Set fd of DatagramSocket to null, forcing impl.connect() to throw.
+ 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);
+
+ s.connect(InetAddress.getLocalHost(), port);
+ assertTrue(s.isConnected());
+ }
+ }
}
diff --git a/luni/src/test/java/libcore/java/net/SocketTest.java b/luni/src/test/java/libcore/java/net/SocketTest.java
index 6f1ce29..f273f8e 100644
--- a/luni/src/test/java/libcore/java/net/SocketTest.java
+++ b/luni/src/test/java/libcore/java/net/SocketTest.java
@@ -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 {
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/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/nio/file/Files2Test.java b/luni/src/test/java/libcore/java/nio/file/Files2Test.java
index 61b8878..d4f50a7 100644
--- a/luni/src/test/java/libcore/java/nio/file/Files2Test.java
+++ b/luni/src/test/java/libcore/java/nio/file/Files2Test.java
@@ -217,10 +217,12 @@
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perm);
try {
Files.setPosixFilePermissions(null, perm);
+ fail();
} catch(NullPointerException expected) {}
try {
Files.setPosixFilePermissions(filesSetup.getDataFilePath(), null);
+ fail();
} catch(NullPointerException expected) {}
}
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/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
index 4bb0c07..1127559 100644
--- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java
+++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
@@ -3731,6 +3731,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 +3746,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 {
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/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..9a3ef03 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 \
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>'\u0001'</code> to
- * <code>'\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>'\u0000'</code> and characters in the
- * range <code>'\u0080'</code> to <code>'\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>'\u0800'</code> to
- * <code>'\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>'\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 << 8) | (b & 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 & 0xff) << 8) | (b & 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 << 8) | (b & 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 & 0xff) << 24) | ((b & 0xff) << 16) |
- *  ((c & 0xff) << 8) | (d & 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 & 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)))
- * </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  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>\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& 0x1F) << 6) | (b & 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 & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 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 <= b <= 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 << 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 <= b1, b2 <= 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 <= b1, b2 <= 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 <= b1, b2, b3, b4 <= 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 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
* </pre></blockquote>
* <p>
* then the result is equal to:
- * <p><blockquote><pre>
+ * <blockquote><pre>
* ((long)b1 << 56) + ((long)b2 << 48)
* + ((long)b3 << 40) + ((long)b4 << 32)
* + ((long)b5 << 24) + ((long)b6 << 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>'\r'</code>), a newline character (<code>'\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/reflect/Constructor.java b/ojluni/src/main/java/java/lang/reflect/Constructor.java
index 3e26cbb..a85d427 100644
--- a/ojluni/src/main/java/java/lang/reflect/Constructor.java
+++ b/ojluni/src/main/java/java/lang/reflect/Constructor.java
@@ -74,6 +74,12 @@
return new Constructor<T>(ctor, cl);
}
+ @Override
+ boolean hasGenericInformation() {
+ // Android-changed: Signature retrieval is handled in AbstractMethod.
+ return super.hasGenericInformationInternal();
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/ojluni/src/main/java/java/lang/reflect/Executable.java b/ojluni/src/main/java/java/lang/reflect/Executable.java
index 1840566..6c7f775 100644
--- a/ojluni/src/main/java/java/lang/reflect/Executable.java
+++ b/ojluni/src/main/java/java/lang/reflect/Executable.java
@@ -41,6 +41,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) {
@@ -256,6 +261,160 @@
}
/**
+ * 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.
+ * @hide Hidden pending tests
+ */
+ 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
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..fe41c5e 100644
--- a/ojluni/src/main/java/java/lang/reflect/Method.java
+++ b/ojluni/src/main/java/java/lang/reflect/Method.java
@@ -82,9 +82,15 @@
private Method() {
}
+ @Override
+ boolean hasGenericInformation() {
+ // Android-changed: Signature retrieval is handled in AbstractMethod.
+ return super.hasGenericInformationInternal();
+ }
+
/**
* {@inheritDoc}
- */
+ */
@Override
public Class<?> getDeclaringClass() {
// Android-changed: This is handled by AbstractMethod.
@@ -159,6 +165,7 @@
* @since 1.5
*/
public Type getGenericReturnType() {
+ // Android-changed: Modified implementation to use AbstractMethod.
return Types.getType(getMethodOrConstructorGenericInfoInternal().genericReturnType);
}
@@ -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.
@@ -425,7 +430,6 @@
return super.isSynthetic();
}
-
/**
* Returns {@code true} if this method is a default
* method; returns {@code false} otherwise.
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..89dd606
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/reflect/Parameter.java
@@ -0,0 +1,324 @@
+/*
+ * 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
+ * @hide Hidden pending tests
+ */
+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™ 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™ 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™ 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..4e03f8f 100755
--- a/ojluni/src/main/java/java/net/DatagramSocket.java
+++ b/ojluni/src/main/java/java/net/DatagramSocket.java
@@ -172,10 +172,11 @@
// connection will be emulated by DatagramSocket
connectState = ST_CONNECTED_NO_IMPL;
}*/
- getImpl().connect(address, port);
// socket is now connected by the impl
connectState = ST_CONNECTED;
+ getImpl().connect(address, port);
+
// Do we need to filter some packets?
int avail = getImpl().dataAvailable();
if (avail == -1) {
@@ -1219,7 +1220,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 "wrapping"
+ * 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 & 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/Collection.java b/ojluni/src/main/java/java/util/Collection.java
index 7db86aa..e4c6b4c 100644
--- a/ojluni/src/main/java/java/util/Collection.java
+++ b/ojluni/src/main/java/java/util/Collection.java
@@ -104,10 +104,23 @@
* the specified behavior of underlying {@link Object} methods wherever the
* implementor deems it appropriate.
*
+ * <p>Some collection operations which perform recursive traversal of the
+ * collection may fail with an exception for self-referential instances where
+ * the collection directly or indirectly contains itself. This includes the
+ * {@code clone()}, {@code equals()}, {@code hashCode()} and {@code toString()}
+ * methods. Implementations may optionally handle the self-referential scenario,
+ * however most current implementations do not do so.
+ *
* <p>This interface is a member of the
* <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
+ * @implSpec
+ * The default method implementations (inherited or otherwise) do not apply any
+ * synchronization protocol. If a {@code Collection} implementation has a
+ * specific synchronization protocol, then it must override default
+ * implementations to apply that protocol.
+ *
* @param <E> the type of elements in this collection
*
* @author Josh Bloch
@@ -226,6 +239,7 @@
* Note that <tt>toArray(new Object[0])</tt> is identical in function to
* <tt>toArray()</tt>.
*
+ * @param <T> the runtime type of the array to contain the collection
* @param a the array into which the elements of this collection are to be
* stored, if it is big enough; otherwise, a new array of the same
* runtime type is allocated for this purpose.
@@ -370,7 +384,6 @@
*/
boolean removeAll(Collection<?> c);
-
/**
* Removes all of the elements of this collection that satisfy the given
* predicate. Errors or runtime exceptions thrown during iteration or by
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/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 </i></td>
- * <td valign="top">The character used to separate thousands groups,
- * <i>i.e.,</i> <tt>dfs.</tt>{@link
- * java.text.DecimalFormatSymbols#getGroupingSeparator
- * getGroupingSeparator()}</td></tr>
- * <tr><td valign="top"><i>LocalDecimalSeparator </i></td>
- * <td valign="top">The character used for the decimal point,
- * <i>i.e.,</i> <tt>dfs.</tt>{@link
- * java.text.DecimalFormatSymbols#getDecimalSeparator
- * getDecimalSeparator()}</td></tr>
- * <tr><td valign="top"><i>LocalPositivePrefix </i></td>
- * <td valign="top">The string that appears before a positive number (may
- * be empty), <i>i.e.,</i> <tt>df.</tt>{@link
- * java.text.DecimalFormat#getPositivePrefix
- * getPositivePrefix()}</td></tr>
- * <tr><td valign="top"><i>LocalPositiveSuffix </i></td>
- * <td valign="top">The string that appears after a positive number (may be
- * empty), <i>i.e.,</i> <tt>df.</tt>{@link
- * java.text.DecimalFormat#getPositiveSuffix
- * getPositiveSuffix()}</td></tr>
- * <tr><td valign="top"><i>LocalNegativePrefix </i></td>
- * <td valign="top">The string that appears before a negative number (may
- * be empty), <i>i.e.,</i> <tt>df.</tt>{@link
- * java.text.DecimalFormat#getNegativePrefix
- * getNegativePrefix()}</td></tr>
- * <tr><td valign="top"><i>LocalNegativeSuffix </i></td>
- * <td valign="top">The string that appears after a negative number (may be
- * empty), <i>i.e.,</i> <tt>df.</tt>{@link
- * java.text.DecimalFormat#getNegativeSuffix
- * getNegativeSuffix()}</td></tr>
- * <tr><td valign="top"><i>LocalNaN </i></td>
- * <td valign="top">The string that represents not-a-number for
- * floating-point values,
- * <i>i.e.,</i> <tt>dfs.</tt>{@link
- * java.text.DecimalFormatSymbols#getNaN
- * getNaN()}</td></tr>
- * <tr><td valign="top"><i>LocalInfinity </i></td>
- * <td valign="top">The string that represents infinity for floating-point
- * values, <i>i.e.,</i> <tt>dfs.</tt>{@link
- * java.text.DecimalFormatSymbols#getInfinity
- * getInfinity()}</td></tr>
- * </table></blockquote>
+ * <blockquote><dl>
+ * <dt><i>LocalGroupSeparator </i>
+ * <dd>The character used to separate thousands groups,
+ * <i>i.e.,</i> <tt>dfs.</tt>{@link
+ * java.text.DecimalFormatSymbols#getGroupingSeparator
+ * getGroupingSeparator()}
+ * <dt><i>LocalDecimalSeparator </i>
+ * <dd>The character used for the decimal point,
+ * <i>i.e.,</i> <tt>dfs.</tt>{@link
+ * java.text.DecimalFormatSymbols#getDecimalSeparator
+ * getDecimalSeparator()}
+ * <dt><i>LocalPositivePrefix </i>
+ * <dd>The string that appears before a positive number (may
+ * be empty), <i>i.e.,</i> <tt>df.</tt>{@link
+ * java.text.DecimalFormat#getPositivePrefix
+ * getPositivePrefix()}
+ * <dt><i>LocalPositiveSuffix </i>
+ * <dd>The string that appears after a positive number (may be
+ * empty), <i>i.e.,</i> <tt>df.</tt>{@link
+ * java.text.DecimalFormat#getPositiveSuffix
+ * getPositiveSuffix()}
+ * <dt><i>LocalNegativePrefix </i>
+ * <dd>The string that appears before a negative number (may
+ * be empty), <i>i.e.,</i> <tt>df.</tt>{@link
+ * java.text.DecimalFormat#getNegativePrefix
+ * getNegativePrefix()}
+ * <dt><i>LocalNegativeSuffix </i>
+ * <dd>The string that appears after a negative number (may be
+ * empty), <i>i.e.,</i> <tt>df.</tt>{@link
+ * java.text.DecimalFormat#getNegativeSuffix
+ * getNegativeSuffix()}
+ * <dt><i>LocalNaN </i>
+ * <dd>The string that represents not-a-number for
+ * floating-point values,
+ * <i>i.e.,</i> <tt>dfs.</tt>{@link
+ * java.text.DecimalFormatSymbols#getNaN
+ * getNaN()}
+ * <dt><i>LocalInfinity </i>
+ * <dd>The string that represents infinity for floating-point
+ * values, <i>i.e.,</i> <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> ::</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 true</td></tr>
+ * returns true
*
- * <tr><td> </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> ::</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> </td></tr>
+ * <dt><i>GroupedNumeral</i>:
+ * <dd><tt>( </tt><i>Non0Digit</i>
+ * <i>Digit</i><tt>?
+ * </tt><i>Digit</i><tt>?</tt>
+ * <dd> <tt>( </tt><i>LocalGroupSeparator</i>
+ * <i>Digit</i>
+ * <i>Digit</i>
+ * <i>Digit</i><tt> )+ )</tt>
*
- * <tr><td align=right><i>Digit</i> ::</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> </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> ::</td>
- * <td valign=top>
- * <table cellpadding=0 cellspacing=0>
- * <tr><td><tt>= ( </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>( </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> </td></tr>
+ * <dt><i>Exponent</i>:
+ * <dd><tt>( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt>
*
- * <tr><td align=right><i>Numeral</i> ::</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> </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> ::</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> </td></tr>
- *
- * <tr><td align=right><i>DecimalNumeral</i> ::</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> </td></tr>
- *
- * <tr><td align=right><i>Exponent</i> ::</td>
- * <td><tt>= ( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt></td></tr>
- *
- * <tr><td> </td></tr>
- *
- * <tr><td align=right>
- * <a name="Decimal-regex"><i>Decimal</i> ::</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> </td></tr>
- *
- * <tr><td align=right><i>HexFloat</i> ::</td>
- * <td><tt>= [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
- * ([pP][-+]?[0-9]+)?</tt></td></tr>
- *
- * <tr><td> </td></tr>
- *
- * <tr><td align=right><i>NonNumber</i> ::</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> </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> ::</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> </td></tr>
- *
- * <tr><td valign=top align=right>
- * <a name="Float-regex"><i>Float</i> ::</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/SSLSocket.java b/ojluni/src/main/java/javax/net/ssl/SSLSocket.java
index 7203166..3ee7f8c 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–22</td>
* </tr>
* <tr>
* <td>TLSv1</td>
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 b0d574e..f1e425e 100644
--- a/openjdk_java_files.mk
+++ b/openjdk_java_files.mk
@@ -186,9 +186,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 \
@@ -759,6 +761,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 \
diff --git a/support/src/test/java/libcore/java/security/StandardNames.java b/support/src/test/java/libcore/java/security/StandardNames.java
index d414d4b..d2f8b67 100644
--- a/support/src/test/java/libcore/java/security/StandardNames.java
+++ b/support/src/test/java/libcore/java/security/StandardNames.java
@@ -988,6 +988,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 +1163,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;