Updates: undid the changes needed to break the JRE into subset libraries. Replaced the XmlLoader class with Android's java.util.XMLUtils, which has fewer dependencies. PiperOrigin-RevId: 789061356
diff --git a/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodes.java b/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodes.java deleted file mode 100644 index 2bceb07..0000000 --- a/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodes.java +++ /dev/null
@@ -1,34 +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 com.google.j2objc.util; - -/** - * Defines a table of ISO 4217 codes and their numbers, to support - * Currency.getNumericCode(). An interface is used to avoid adding a - * large data table into jre_core. This interface and the implementation - * class should be removed when ICU currency support is available. - */ -public interface CurrencyNumericCodes { - - /** - * Returns the numeric code for a ISO 4217 currency code string. - * - * @returns the numeric code, or zero if the currency code isn't known. - */ - public int getNumericCode(String currencyCode); -}
diff --git a/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodesImpl.java b/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodesImpl.java index 99b34b0..13097df 100644 --- a/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodesImpl.java +++ b/jre_emul/Classes/com/google/j2objc/util/CurrencyNumericCodesImpl.java
@@ -18,18 +18,16 @@ package com.google.j2objc.util; /** - * Defines a table of ISO 4217 codes and their numbers, to support - * Currency.getNumericCode(). This class should be removed when - * ICU currency support is available. + * Defines a table of ISO 4217 codes and their numbers, to support Currency.getNumericCode(). This + * class should be removed when ICU currency support is available. */ -public final class CurrencyNumericCodesImpl implements CurrencyNumericCodes { +public final class CurrencyNumericCodesImpl { /** - * Returns the numeric code for a ISO 4217 currency code string. - * The iso4217 dictionary is extracted from the Active Codes - * table defined in https://en.wikipedia.org/wiki/ISO_4217. + * Returns the numeric code for a ISO 4217 currency code string. The iso4217 dictionary is + * extracted from the Active Codes table defined in https://en.wikipedia.org/wiki/ISO_4217. */ - public native int getNumericCode(String currencyCode) /*-[ + public static native int getNumericCode(String currencyCode) /*-[ static NSDictionary *iso4217; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
diff --git a/jre_emul/Classes/com/google/j2objc/util/PropertiesXmlLoader.java b/jre_emul/Classes/com/google/j2objc/util/PropertiesXmlLoader.java deleted file mode 100644 index 1839cc0..0000000 --- a/jre_emul/Classes/com/google/j2objc/util/PropertiesXmlLoader.java +++ /dev/null
@@ -1,73 +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 com.google.j2objc.util; - -import org.xml.sax.Attributes; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.DefaultHandler; -import org.xml.sax.helpers.XMLReaderFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.util.InvalidPropertiesFormatException; -import java.util.Properties; - -/** - * Dynamically loaded implementation for Properties.loadFromXML(). Public so that users can add an - * explicit dependency to force load this class. - */ -public class PropertiesXmlLoader implements XmlLoader { - - public void load(final Properties p, InputStream in) throws IOException, - InvalidPropertiesFormatException { - if (in == null) { - throw new NullPointerException("in == null"); - } - - try { - XMLReader reader = XMLReaderFactory.createXMLReader(); - reader.setContentHandler(new DefaultHandler() { - private String key; - - @Override - public void startElement(String uri, String localName, - String qName, Attributes attributes) throws SAXException { - key = null; - if (qName.equals("entry")) { - key = attributes.getValue("key"); - } - } - - @Override - public void characters(char[] ch, int start, int length) - throws SAXException { - if (key != null) { - String value = new String(ch, start, length); - p.put(key, value); - key = null; - } - } - }); - reader.parse(new InputSource(in)); - } catch (SAXException e) { - throw new InvalidPropertiesFormatException(e); - } - } -}
diff --git a/jre_emul/Classes/com/google/j2objc/util/XmlLoader.java b/jre_emul/Classes/com/google/j2objc/util/XmlLoader.java deleted file mode 100644 index f15cf94..0000000 --- a/jre_emul/Classes/com/google/j2objc/util/XmlLoader.java +++ /dev/null
@@ -1,31 +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 com.google.j2objc.util; - -import java.io.IOException; -import java.io.InputStream; -import java.util.InvalidPropertiesFormatException; -import java.util.Properties; - -/** - * Creates a dynamic dependency on XML support so that XML support can be excluded from the - * application binary without incurring link errors. - */ -public interface XmlLoader { - void load(Properties p, InputStream in) throws IOException, InvalidPropertiesFormatException; -}
diff --git a/jre_emul/Classes/java_lang_SystemClassLoader.m b/jre_emul/Classes/java_lang_SystemClassLoader.m index de2271f..003be3b 100644 --- a/jre_emul/Classes/java_lang_SystemClassLoader.m +++ b/jre_emul/Classes/java_lang_SystemClassLoader.m
@@ -13,7 +13,7 @@ */ #include "J2ObjC_source.h" -#include "com/google/j2objc/LibraryNotLinkedError.h" +#include "com/google/j2objc/net/ResourceDataStreamHandler.h" #include "java/io/BufferedInputStream.h" #include "java/io/ByteArrayInputStream.h" #include "java/io/FileInputStream.h" @@ -49,27 +49,10 @@ } static JavaNetURL *CreateResourceURL(NSString *name, IOSByteArray *data) { - @try { - IOSClass *resourceClass = - IOSClass_forName_(@"com.google.j2objc.net.ResourceDataStreamHandler"); - IOSObjectArray *paramTypes = - [IOSObjectArray arrayWithObjects:(id[]){ NSString_class_(), [data java_getClass]} - count:2 - type:IOSClass_class_()]; - JavaLangReflectMethod *m = [resourceClass getMethod:@"createResourceDataURL" - parameterTypes:paramTypes]; - IOSObjectArray *args = [IOSObjectArray arrayWithObjects:(id[]){ name, data } - count:2 - type:NSObject_class_()]; - return (JavaNetURL *)[m invokeWithId:nil withNSObjectArray:args]; - } - @catch (JavaLangException *e) { - @throw create_ComGoogleJ2objcLibraryNotLinkedError_initWithNSString_withNSString_withNSString_( - @"java.net", @"jre_net ", @"JavaLangSystemClassLoader"); - } + return ComGoogleJ2objcNetResourceDataStreamHandler_createResourceDataURLWithNSString_withByteArray_( + name, data); } - JNIEXPORT jclass Java_java_lang_SystemClassLoader_findClass( JNIEnv *env, jobject obj, jstring name) { return [IOSClass forName:name];
diff --git a/jre_emul/android/platform/libcore/luni/src/main/java/java/nio/NioUtils.java b/jre_emul/android/platform/libcore/luni/src/main/java/java/nio/NioUtils.java index 9ab5b0b..63b08c1 100644 --- a/jre_emul/android/platform/libcore/luni/src/main/java/java/nio/NioUtils.java +++ b/jre_emul/android/platform/libcore/luni/src/main/java/java/nio/NioUtils.java
@@ -16,24 +16,16 @@ package java.nio; -import dalvik.annotation.compat.UnsupportedAppUsage; - -import com.google.j2objc.LibraryNotLinkedError; -import java.io.Closeable; -import java.io.FileDescriptor; -import java.io.IOException; -import java.nio.channels.FileChannel; -import java.util.Set; - -import sun.misc.Cleaner; -import sun.nio.ch.DirectBuffer; -import sun.nio.ch.FileChannelImpl; - import static libcore.io.OsConstants.O_ACCMODE; -import static libcore.io.OsConstants.O_APPEND; import static libcore.io.OsConstants.O_RDONLY; import static libcore.io.OsConstants.O_WRONLY; +import dalvik.annotation.compat.UnsupportedAppUsage; +import java.io.Closeable; +import java.io.FileDescriptor; +import java.nio.channels.FileChannel; +import sun.nio.ch.FileChannelImpl; + /** * @hide internal use only */ @@ -68,39 +60,11 @@ * Helps bridge between io and nio. */ public static FileChannel newFileChannel(Closeable ioObject, FileDescriptor fd, int mode) { - ChannelFactory factory = ChannelFactory.INSTANCE; - if (factory == null) { - throw new LibraryNotLinkedError("Channel support", "jre_channels", - "JavaNioChannelFactoryImpl"); - } - return factory.newFileChannel(ioObject, fd, mode); + boolean readable = (mode & O_ACCMODE) != O_WRONLY; + boolean writable = (mode & O_ACCMODE) != O_RDONLY; + return FileChannelImpl.open(fd, null, readable, writable, ioObject); } - public static FileChannel newFileChannelSafe(Object stream, FileDescriptor fd, int mode) { - ChannelFactory factory = ChannelFactory.INSTANCE; - if (factory != null) { - return factory.newFileChannel(stream, fd, mode); - } else { - return null; - } - } - - static interface ChannelFactory { - FileChannel newFileChannel(Object stream, FileDescriptor fd, int mode); - - static final ChannelFactory INSTANCE = getChannelFactory(); - } - - // Native implementation avoids the use of Class.forName(). This code might end up invoked from - // within the internals of Class.forName(), and re-invoking it causes deadlock. - private static native ChannelFactory getChannelFactory() /*-[ - Class cls = NSClassFromString(@"JavaNioChannelFactoryImpl"); - if (cls) { - return AUTORELEASE([[cls alloc] init]); - } - return nil; - ]-*/; - /** * Exposes the array backing a non-direct ByteBuffer, even if the ByteBuffer is read-only. * Normally, attempting to access the array backing a read-only buffer throws.
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileChannelOpener.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileChannelOpener.java deleted file mode 100644 index eab3f2b..0000000 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileChannelOpener.java +++ /dev/null
@@ -1,68 +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 java.io; - -import com.google.j2objc.LibraryNotLinkedError; -import java.lang.reflect.Method; -import java.nio.channels.FileChannel; - -/** - * J2ObjC-specific bridge class so jre_core and jre_channels can remain separate. - */ -class FileChannelOpener { - - // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel() - public static FileChannel open( - FileDescriptor fd, String path, boolean readable, boolean writable, Object parent) { - try { - Class<?> cls = Class.forName("sun.nio.ch.FileChannelImpl"); - Method m = - cls.getDeclaredMethod( - "open", FileDescriptor.class, String.class, Boolean.TYPE, Boolean.TYPE, Object.class); - return (FileChannel) m.invoke(null, fd, path, readable, writable, parent); - } catch (ClassNotFoundException e) { - throw new LibraryNotLinkedError("Channel support", "jre_channels", "SunNioChFileChannelImpl"); - } catch (Exception e) { - throw new AssertionError("failed opening file channel", e); - } - } - - // Used by FileOutputStream.getChannel - public static FileChannel open(FileDescriptor fd, String path, - boolean readable, boolean writable, - boolean append, Object parent) - { - try { - Class<?> cls = Class.forName("sun.nio.ch.FileChannelImpl"); - Method m = - cls.getDeclaredMethod( - "open", - FileDescriptor.class, - String.class, - Boolean.TYPE, - Boolean.TYPE, - Boolean.TYPE, - Object.class); - return (FileChannel) m.invoke(null, fd, path, readable, writable, append, parent); - } catch (ClassNotFoundException e) { - throw new LibraryNotLinkedError("Channel support", "jre_channels", "SunNioChFileChannelImpl"); - } catch (Exception e) { - throw new AssertionError("failed opening file channel", e); - } - } -}
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileInputStream.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileInputStream.java index 7950687..eac60db 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileInputStream.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileInputStream.java
@@ -31,7 +31,6 @@ import dalvik.system.BlockGuard; import libcore.io.IoTracker; import libcore.io.IoUtils; -import sun.nio.ch.FileChannelImpl; */ import static libcore.io.OsConstants.ESPIPE; @@ -43,7 +42,7 @@ import java.nio.channels.FileChannel; import libcore.io.IoBridge; import libcore.io.Libcore; - +import sun.nio.ch.FileChannelImpl; /** * A <code>FileInputStream</code> obtains input bytes @@ -506,12 +505,35 @@ * @spec JSR-51 */ public FileChannel getChannel() { - synchronized (this) { - if (channel == null) { - channel = FileChannelOpener.open(fd, path, true, false, this); + FileChannel fc = this.channel; + if (fc == null) { + synchronized (this) { + fc = this.channel; + if (fc == null) { + this.channel = + fc = + FileChannelImpl.open( + fd, + path, + true, + // Android-changed: TODO: Remove this patch when direct flag is supported. + // This patch should cause no behavior change as direct is off by default. + // false, false, this); + false, + this); + if (closed) { + try { + // possible race with close(), benign since + // FileChannel.close is final and idempotent + fc.close(); + } catch (IOException ioe) { + throw new InternalError(ioe); // should not happen } - return channel; + } } + } + } + return fc; } // BEGIN Android-removed: Unused code.
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileOutputStream.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileOutputStream.java index 676296f..ef0b92a 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileOutputStream.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/FileOutputStream.java
@@ -31,7 +31,6 @@ import dalvik.system.BlockGuard; import libcore.io.IoTracker; import libcore.io.IoUtils; -import sun.nio.ch.FileChannelImpl; */ import static libcore.io.OsConstants.O_APPEND; @@ -42,6 +41,7 @@ import dalvik.system.CloseGuard; import java.nio.channels.FileChannel; import libcore.io.IoBridge; +import sun.nio.ch.FileChannelImpl; /** * A file output stream is an output stream for writing data to a @@ -486,12 +486,36 @@ * @spec JSR-51 */ public FileChannel getChannel() { - synchronized (this) { - if (channel == null) { - channel = FileChannelOpener.open(fd, path, false, true, append, this); + FileChannel fc = this.channel; + if (fc == null) { + synchronized (this) { + fc = this.channel; + if (fc == null) { + this.channel = + fc = + FileChannelImpl.open( + fd, + path, + false, + // Android-changed: TODO: remove patch when FileChannelImpl supports direct. + // This patch should cause no behavior change as direct is off by default. + // true, false, this); + true, + append, + this); + if (closed) { + try { + // possible race with close(), benign since + // FileChannel.close is final and idempotent + fc.close(); + } catch (IOException ioe) { + throw new InternalError(ioe); // should not happen } - return channel; + } } + } + } + return fc; } /**
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/ObjectStreamClass.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/ObjectStreamClass.java index db77cab..cad1507 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/ObjectStreamClass.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/ObjectStreamClass.java
@@ -51,10 +51,7 @@ import java.util.concurrent.ConcurrentMap; import sun.misc.Unsafe; import sun.reflect.CallerSensitive; -import sun.reflect.Reflection; -import sun.reflect.misc.ReflectUtil; //import dalvik.system.VMRuntime; -import com.google.j2objc.LibraryNotLinkedError; //J2ObjC: Begin change /*-[ @@ -1747,11 +1744,8 @@ return 0L; } //J2ObjC: Begin J2ObjC change - Digest digest = getDigest(); - ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { - //ByteArrayOutputStream bout = new ByteArrayOutputStream(); - //J2ObjC: End J2ObjC change + ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout); dout.writeUTF(cl.getName()); @@ -1894,55 +1888,28 @@ } dout.flush(); - //J2ObjC: removed - // MessageDigest md = MessageDigest.getInstance("SHA"); - // byte[] hashBytes = md.digest(bout.toByteArray()); - // long hash = 0; - // for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) { - // hash = (hash << 8) | (hashBytes[i] & 0xFF); - // } + MessageDigest md = MessageDigest.getInstance("SHA"); + byte[] hashBytes = md.digest(bout.toByteArray()); + long hash = 0; + for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) { + hash = (hash << 8) | (hashBytes[i] & 0xFF); + } + // J2ObjC: android change not supported + // BEGIN Android-added: Fix/log clinit serialization workaround. b/29064453 + // ObjectStreamClass instances are cached per Class and caches its default + // serialVersionUID so it will only log one message per class per app process + // irrespective of the number of times the class is serialized. + // if (warnIncompatibleSUIDChange) { + // suidCompatibilityListener.warnDefaultSUIDTargetVersionDependent(cl, hash); + // } + // END Android-added: Fix/log clinit serialization workaround. b/29064453 + return hash; } catch (IOException ex) { throw new InternalError(ex); - } - // J2ObjC: removed - // catch (NoSuchAlgorithmException ex) { - // throw new SecurityException(ex.getMessage()); - // } - // now compute the UID based on the SHA - byte[] hashBytes = digest.digest(bout.toByteArray()); - long hash = 0; - for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) { - hash = (hash << 8) | (hashBytes[i] & 0xFF); - } - // J2ObjC: android change not supported - - // BEGIN Android-added: Fix/log clinit serialization workaround. b/29064453 - // ObjectStreamClass instances are cached per Class and caches its default - // serialVersionUID so it will only log one message per class per app process - // irrespective of the number of times the class is serialized. - // if (warnIncompatibleSUIDChange) { - // suidCompatibilityListener.warnDefaultSUIDTargetVersionDependent(cl, hash); - // } - // END Android-added: Fix/log clinit serialization workaround. b/29064453 - return hash; - } - - // J2ObjC: dynamically load MessageDigest to avoid linking jre_security unnecessarily. - static interface Digest { - byte[] digest(byte[] input); - } - - private static Digest getDigest() { - try { - Class<?> digestClass = Class.forName("java.io.SerialVersionUIDDigest"); - return (Digest) digestClass.newInstance(); - } catch (Exception e) { - throw new LibraryNotLinkedError( - "SerialVersionUID hashing", "jre_security", "JavaIoSerialVersionUIDDigest", - "3) Add serialVersionUID fields to all Serializable classes."); + } catch (NoSuchAlgorithmException ex) { + throw new SecurityException(ex.getMessage()); } } - // BEGIN Android-changed: Fix/log clinit serialization workaround. b/29064453 /** * Created for testing as there is no nice way to detect when a message is logged.
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/RandomAccessFile.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/RandomAccessFile.java index dec719b..dc7f476 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/RandomAccessFile.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/RandomAccessFile.java
@@ -32,7 +32,6 @@ import libcore.io.IoTracker; import libcore.io.IoUtils; import static android.system.OsConstants.*; -import sun.nio.ch.FileChannelImpl; */ import static libcore.io.OsConstants.O_CREAT; @@ -40,12 +39,13 @@ import static libcore.io.OsConstants.O_RDWR; import static libcore.io.OsConstants.SEEK_CUR; import static libcore.io.OsConstants.SEEK_SET; -import java.nio.channels.FileChannel; + import android.system.ErrnoException; import dalvik.system.CloseGuard; +import java.nio.channels.FileChannel; import libcore.io.IoBridge; import libcore.io.Libcore; - +import sun.nio.ch.FileChannelImpl; /** * Instances of this class support both reading and writing to a @@ -359,7 +359,7 @@ public final FileChannel getChannel() { synchronized (this) { if (channel == null) { - channel = FileChannelOpener.open(fd, path, true, rw, this); + channel = FileChannelImpl.open(fd, path, true, rw, this); } return channel; }
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/net/URLImpl.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/net/URLImpl.java index 0cceb4c..932659e 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/net/URLImpl.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/net/URLImpl.java
@@ -26,8 +26,8 @@ package java.net; -import com.google.j2objc.LibraryNotLinkedError; import com.google.j2objc.net.IosHttpHandler; +import com.google.j2objc.net.IosHttpsHandler; import java.io.IOException; import java.util.Hashtable; import java.util.StringTokenizer; @@ -269,13 +269,7 @@ } else if (protocol.equals("http")) { handler = new IosHttpHandler(); } else if (protocol.equals("https")) { - try { - String name = "com.google.j2objc.net.IosHttpsHandler"; - handler = (URLStreamHandler) Class.forName(name).newInstance(); - } catch (Exception e) { - throw new LibraryNotLinkedError("Https support", "jre_ssl", - "JavaxNetSslHttpsURLConnection"); - } + handler = new IosHttpsHandler(); } } catch (Exception e) { throw new AssertionError(e);
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/nio/ChannelFactoryImpl.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/nio/ChannelFactoryImpl.java deleted file mode 100644 index 31165eb..0000000 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/nio/ChannelFactoryImpl.java +++ /dev/null
@@ -1,42 +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 java.nio; - -import com.google.j2objc.annotations.ReflectionSupport; -import java.io.FileDescriptor; -import java.nio.channels.FileChannel; -import sun.nio.ch.FileChannelImpl; - -import static com.google.j2objc.annotations.ReflectionSupport.Level.FULL; -import static libcore.io.OsConstants.*; - -/** - * J2ObjC split of NioUtils to move method to jre_channels subset library. - * This class is only referenced by reflection. - */ -@SuppressWarnings("unused") -@ReflectionSupport(FULL) -class ChannelFactoryImpl implements NioUtils.ChannelFactory { - - public FileChannel newFileChannel(Object ioObject, FileDescriptor fd, int mode) { - boolean readable = (mode & O_ACCMODE) != O_WRONLY; - boolean writable = (mode & O_ACCMODE) != O_RDONLY; - boolean append = (mode & O_APPEND) != 0; - - return new FileChannelImpl(fd, null, readable, writable, append, ioObject); - } -}
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Currency.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Currency.java index acf6a56..061921c 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Currency.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Currency.java
@@ -26,24 +26,10 @@ package java.util; -import com.google.j2objc.LibraryNotLinkedError; -import com.google.j2objc.util.CurrencyNumericCodes; - -import java.io.BufferedInputStream; -import java.io.DataInputStream; -import java.io.File; -import java.io.FileReader; -import java.io.InputStream; -import java.io.IOException; +import com.google.j2objc.util.CurrencyNumericCodesImpl; import java.io.Serializable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import java.util.regex.Pattern; -import java.util.regex.Matcher; -import java.util.stream.Collectors; - -import sun.util.logging.PlatformLogger; - import libcore.icu.ICU; import libcore.icu.LocaleData; @@ -524,14 +510,7 @@ * @since 1.7 */ public int getNumericCode() { - try { - String name = "com.google.j2objc.util.CurrencyNumericCodesImpl"; - CurrencyNumericCodes cnc = (CurrencyNumericCodes) Class.forName(name).newInstance(); - return cnc.getNumericCode(currencyCode); - } catch (Exception e) { - throw new LibraryNotLinkedError("java.util support", "jre_util", - "ComGoogleJ2objcUtilCurrencyNumericCodesImpl"); - } + return CurrencyNumericCodesImpl.getNumericCode(currencyCode); } /**
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Properties.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Properties.java index 2f875ba..688f3d5 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Properties.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/Properties.java
@@ -26,12 +26,8 @@ package java.util; -import com.google.j2objc.LibraryNotLinkedError; -import com.google.j2objc.util.XmlLoader; import java.io.BufferedWriter; import java.io.IOException; -import java.io.PrintStream; -import java.io.PrintWriter; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -40,8 +36,7 @@ import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; -import java.nio.charset.UnsupportedCharsetException; +import java.nio.charset.StandardCharsets; // Android-removed: Dead native2ascii links. // These links are also gone in OpenJDK 9. @@ -910,21 +905,16 @@ */ public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException { - XmlLoader loader = getXmlLoader(); - if (loader == null) { - throw new LibraryNotLinkedError( - "XML support", "jre_xml", "ComGoogleJ2objcUtilPropertiesXmlLoader"); - } - loader.load(this, in); - } - - private static XmlLoader getXmlLoader() { - try { - Class<?> loaderClass = Class.forName("com.google.j2objc.util.PropertiesXmlLoader"); - return (XmlLoader) loaderClass.newInstance(); - } catch (Exception e) { - return null; - } + // Android-changed: Keep OpenJDK7u40's XmlUtils. + // XmlSupport's system property based XmlPropertiesProvider + // selection does not make sense on Android and has too many + // dependencies on classes that are not available on Android. + // + // Objects.requireNonNull(in); + // PropertiesDefaultHandler handler = new PropertiesDefaultHandler(); + // handler.load(this, in); + XMLUtils.load(this, Objects.requireNonNull(in)); + in.close(); } /** @@ -950,7 +940,7 @@ public void storeToXML(OutputStream os, String comment) throws IOException { - storeToXML(os, comment, "UTF-8"); + storeToXML(os, comment, StandardCharsets.UTF_8); } /** @@ -994,62 +984,63 @@ */ public void storeToXML(OutputStream os, String comment, String encoding) throws IOException { - if (os == null) { - throw new NullPointerException("os == null"); - } else if (encoding == null) { - throw new NullPointerException("encoding == null"); - } + // Android-changed: Keep OpenJDK7u40's XmlUtils. + // XmlSupport's system property based XmlPropertiesProvider + // selection does not make sense on Android and has too many + // dependencies on classes that are not available on Android. + /* + Objects.requireNonNull(os); + Objects.requireNonNull(encoding); - /* - * We can write to XML file using encoding parameter but note that some - * aliases for encodings are not supported by the XML parser. Thus we - * have to know canonical name for encoding used to store data in XML - * since the XML parser must recognize encoding name used to store data. - */ + try { + Charset charset = Charset.forName(encoding); + storeToXML(os, comment, charset); + } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { + throw new UnsupportedEncodingException(encoding); + } + */ + XMLUtils.save(this, Objects.requireNonNull(os), comment, Objects.requireNonNull(encoding)); + } - String encodingCanonicalName; - try { - encodingCanonicalName = Charset.forName(encoding).name(); - } catch (IllegalCharsetNameException e) { - System.out.println("Warning: encoding name " + encoding - + " is illegal, using UTF-8 as default encoding"); - encodingCanonicalName = "UTF-8"; - } catch (UnsupportedCharsetException e) { - System.out.println("Warning: encoding " + encoding - + " is not supported, using UTF-8 as default encoding"); - encodingCanonicalName = "UTF-8"; - } - - PrintStream printStream = new PrintStream(os, false, - encodingCanonicalName); - - printStream.print("<?xml version=\"1.0\" encoding=\""); - printStream.print(encodingCanonicalName); - printStream.println("\"?>"); - - printStream.print("<!DOCTYPE properties SYSTEM \""); - printStream.print(PROP_DTD_NAME); - printStream.println("\">"); - - printStream.println("<properties>"); - - if (comment != null) { - printStream.print("<comment>"); - printStream.print(substitutePredefinedEntries(comment)); - printStream.println("</comment>"); - } - - for (Map.Entry<Object, Object> entry : entrySet()) { - String keyValue = (String) entry.getKey(); - String entryValue = (String) entry.getValue(); - printStream.print("<entry key=\""); - printStream.print(substitutePredefinedEntries(keyValue)); - printStream.print("\">"); - printStream.print(substitutePredefinedEntries(entryValue)); - printStream.println("</entry>"); - } - printStream.println("</properties>"); - printStream.flush(); + /** + * Emits an XML document representing all of the properties contained in this table, using the + * specified encoding. + * + * <p>The XML document will have the following DOCTYPE declaration: + * + * <pre> + * <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> + * </pre> + * + * <p>If the specified comment is {@code null} then no comment will be stored in the document. + * + * <p>An implementation is required to support writing of XML documents that use the "{@code + * UTF-8}" or "{@code UTF-16}" encoding. An implementation may support additional encodings. + * + * <p>Unmappable characters for the specified charset will be encoded as numeric character + * references. + * + * <p>The specified stream remains open after this method returns. + * + * @param os the output stream on which to emit the XML document. + * @param comment a description of the property list, or {@code null} if no comment is desired. + * @param charset the charset + * @throws IOException if writing to the specified output stream results in an {@code + * IOException}. + * @throws NullPointerException if {@code os} or {@code charset} is {@code null}. + * @throws ClassCastException if this {@code Properties} object contains any keys or values that + * are not {@code Strings}. + * @see #loadFromXML(InputStream) + * @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character Encoding in Entities</a> + * @since 10 + */ + public void storeToXML(OutputStream os, String comment, Charset charset) throws IOException { + Objects.requireNonNull(os, "OutputStream"); + Objects.requireNonNull(charset, "Charset"); + // Objects.requireNonNull(os, "OutputStream"); + // PropertiesDefaultHandler handler = new PropertiesDefaultHandler(); + // handler.store(this, os, comment, charset); + storeToXML(os, comment, charset.name()); } private String substitutePredefinedEntries(String s) {
diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/XMLUtils.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/XMLUtils.java new file mode 100644 index 0000000..4a4539f --- /dev/null +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/util/XMLUtils.java
@@ -0,0 +1,201 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * Copyright (c) 2003, 2011, 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.*; +import javax.xml.parsers.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import org.w3c.dom.*; +import org.xml.sax.*; +import org.xml.sax.helpers.*; + +/** + * A class used to aid in Properties load and save in XML. Keeping this code outside of Properties + * helps reduce the number of classes loaded when Properties is loaded. + * + * @author Michael McCloskey + * @since 1.3 + */ +class XMLUtils { + + // XML loading and saving methods for Properties + + // The required DTD URI for exported properties + private static final String PROPS_DTD_URI = "http://java.sun.com/dtd/properties.dtd"; + + private static final String PROPS_DTD = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<!-- DTD for properties -->" + + "<!ELEMENT properties ( comment?, entry* ) >" + + "<!ATTLIST properties" + + " version CDATA #FIXED \"1.0\">" + + "<!ELEMENT comment (#PCDATA) >" + + "<!ELEMENT entry (#PCDATA) >" + + "<!ATTLIST entry " + + " key CDATA #REQUIRED>"; + + /** Version number for the format of exported properties files. */ + private static final String EXTERNAL_XML_VERSION = "1.0"; + + static void load(Properties props, InputStream in) + throws IOException, InvalidPropertiesFormatException { + Document doc = null; + try { + doc = getLoadingDoc(in); + } catch (SAXException saxe) { + throw new InvalidPropertiesFormatException(saxe); + } + Element propertiesElement = doc.getDocumentElement(); + String xmlVersion = propertiesElement.getAttribute("version"); + if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) + throw new InvalidPropertiesFormatException( + "Exported Properties file format version " + + xmlVersion + + " is not supported. This java installation can read" + + " versions " + + EXTERNAL_XML_VERSION + + " or older. You" + + " may need to install a newer version of JDK."); + importProperties(props, propertiesElement); + } + + static Document getLoadingDoc(InputStream in) throws SAXException, IOException { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setIgnoringElementContentWhitespace(true); + // Android-changed: We don't currently have a validating document builder. + // Revert this if the situation changes. + // + // dbf.setValidating(true); + dbf.setCoalescing(true); + dbf.setIgnoringComments(true); + try { + DocumentBuilder db = dbf.newDocumentBuilder(); + db.setEntityResolver(new Resolver()); + db.setErrorHandler(new EH()); + InputSource is = new InputSource(in); + return db.parse(is); + } catch (ParserConfigurationException x) { + throw new Error(x); + } + } + + static void importProperties(Properties props, Element propertiesElement) { + NodeList entries = propertiesElement.getChildNodes(); + int numEntries = entries.getLength(); + int start = numEntries > 0 && entries.item(0).getNodeName().equals("comment") ? 1 : 0; + for (int i = start; i < numEntries; i++) { + // Android-changed: Exclude CDATA nodes and the like. + if (!(entries.item(i) instanceof Element)) { + continue; + } + Element entry = (Element) entries.item(i); + if (entry.hasAttribute("key")) { + Node n = entry.getFirstChild(); + String val = (n == null) ? "" : n.getNodeValue(); + props.setProperty(entry.getAttribute("key"), val); + } + } + } + + static void save(Properties props, OutputStream os, String comment, String encoding) + throws IOException { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + try { + db = dbf.newDocumentBuilder(); + } catch (ParserConfigurationException pce) { + assert (false); + } + Document doc = db.newDocument(); + Element properties = (Element) doc.appendChild(doc.createElement("properties")); + + if (comment != null) { + Element comments = (Element) properties.appendChild(doc.createElement("comment")); + comments.appendChild(doc.createTextNode(comment)); + } + + synchronized (props) { + for (String key : props.stringPropertyNames()) { + Element entry = (Element) properties.appendChild(doc.createElement("entry")); + entry.setAttribute("key", key); + entry.appendChild(doc.createTextNode(props.getProperty(key))); + } + } + emitDocument(doc, os, encoding); + } + + static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException { + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer t = null; + try { + t = tf.newTransformer(); + t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI); + t.setOutputProperty(OutputKeys.INDENT, "yes"); + t.setOutputProperty(OutputKeys.METHOD, "xml"); + t.setOutputProperty(OutputKeys.ENCODING, encoding); + } catch (TransformerConfigurationException tce) { + assert (false); + } + DOMSource doms = new DOMSource(doc); + StreamResult sr = new StreamResult(os); + try { + t.transform(doms, sr); + } catch (TransformerException te) { + IOException ioe = new IOException(); + ioe.initCause(te); + throw ioe; + } + } + + private static class Resolver implements EntityResolver { + public InputSource resolveEntity(String pid, String sid) throws SAXException { + if (sid.equals(PROPS_DTD_URI)) { + InputSource is; + is = new InputSource(new StringReader(PROPS_DTD)); + is.setSystemId(PROPS_DTD_URI); + return is; + } + throw new SAXException("Invalid system identifier: " + sid); + } + } + + private static class EH implements ErrorHandler { + public void error(SAXParseException x) throws SAXException { + throw x; + } + + public void fatalError(SAXParseException x) throws SAXException { + throw x; + } + + public void warning(SAXParseException x) throws SAXException { + throw x; + } + } +}
diff --git a/jre_emul/jre_sources.mk b/jre_emul/jre_sources.mk index 955c4eb..6bc227a 100644 --- a/jre_emul/jre_sources.mk +++ b/jre_emul/jre_sources.mk
@@ -80,8 +80,6 @@ android/system/Int32Ref.java \ android/system/Int64Ref.java \ com/google/j2objc/util/AutoreleasePool.java \ - com/google/j2objc/util/CurrencyNumericCodes.java \ - com/google/j2objc/util/XmlLoader.java \ com/google/j2objc/util/logging/IOSLogHandler.java \ java/io/BufferedInputStream.java \ java/io/BufferedOutputStream.java \ @@ -457,6 +455,7 @@ java/util/UnsafeArrayList.java \ java/util/Vector.java \ java/util/WeakHashMap.java \ + java/util/XMLUtils.java \ java/util/concurrent/AbstractExecutorService.java \ java/util/concurrent/BlockingQueue.java \ java/util/concurrent/Callable.java \ @@ -602,7 +601,6 @@ java/io/DefaultFileSystem.java \ java/io/DeleteOnExitHook.java \ java/io/ExpiringCache.java \ - java/io/FileChannelOpener.java \ java/io/FileSystem.java \ java/io/SerialCallbackContext.java \ java/io/UnixFileSystem.java \ @@ -973,7 +971,6 @@ JAVA_PRIVATE_SOURCES_CONCURRENT = JAVA_PUBLIC_SOURCES_CHANNELS = \ - java/nio/ChannelFactoryImpl.java \ java/nio/channels/AcceptPendingException.java \ java/nio/channels/AlreadyBoundException.java \ java/nio/channels/AlreadyConnectedException.java \ @@ -1253,7 +1250,6 @@ UnixNativeDispatcher.m JAVA_PUBLIC_SOURCES_SECURITY = \ - java/io/SerialVersionUIDDigest.java \ java/net/URLClassLoader.java \ java/security/AccessControlException.java \ java/security/AlgorithmConstraints.java \ @@ -1680,7 +1676,6 @@ com/google/j2objc/net/ssl/IosSslContextSpi.java JAVA_PUBLIC_SOURCES_XML = \ - com/google/j2objc/util/PropertiesXmlLoader.java \ javax/xml/XMLConstants.java \ javax/xml/datatype/DatatypeConfigurationException.java \ javax/xml/datatype/DatatypeConstants.java \