diff options
Diffstat (limited to 'luni/src/main/java')
24 files changed, 119 insertions, 2716 deletions
diff --git a/luni/src/main/java/java/lang/Character.java b/luni/src/main/java/java/lang/Character.java index f088c24..8efd6cd 100644 --- a/luni/src/main/java/java/lang/Character.java +++ b/luni/src/main/java/java/lang/Character.java @@ -1526,426 +1526,6 @@ public final class Character implements Serializable, Comparable<Character> { private static native int unicodeBlockForCodePoint(int codePoint); - /** - * Represents a <a href="http://www.unicode.org/reports/tr24/">Unicode script</a>. - * Every Unicode code point is contained by a single {@code UnicodeScript}. Code points - * shared between scripts will be in {@code COMMON}. Code points for combining - * characters that can be applied to multiple scripts will be in {@code INHERITED} - * because they inherit the script of their base character. Code points whose scripts - * don't have a corresponding {@code UnicodeScript} will be in {@code UNKNOWN}. - * - * @since 1.7 - * @hide 1.7 - */ - public static enum UnicodeScript { - /** ISO 15924 English name "Arabic" */ - ARABIC, - /** ISO 15924 English name "Armenian" */ - ARMENIAN, - /** ISO 15924 English name "Avestan" */ - AVESTAN, - /** ISO 15924 English name "Balinese" */ - BALINESE, - /** ISO 15924 English name "Bamum" */ - BAMUM, - /** ISO 15924 English name "Batak" */ - BATAK, - /** ISO 15924 English name "Bengali" */ - BENGALI, - /** ISO 15924 English name "Bopomofo" */ - BOPOMOFO, - /** ISO 15924 English name "Brahmi" */ - BRAHMI, - /** ISO 15924 English name "Braille" */ - BRAILLE, - /** ISO 15924 English name "Buginese" */ - BUGINESE, - /** ISO 15924 English name "Buhid" */ - BUHID, - /** ISO 15924 English name "Unified Canadian Aboriginal Syllabics" */ - CANADIAN_ABORIGINAL, - /** ISO 15924 English name "Carian" */ - CARIAN, - /** ISO 15924 English name "Cham" */ - CHAM, - /** ISO 15924 English name "Cherokee" */ - CHEROKEE, - /** ISO 15924 English name "Common" */ - COMMON, - /** ISO 15924 English name "Coptic" */ - COPTIC, - /** ISO 15924 English name "Cuneiform" */ - CUNEIFORM, - /** ISO 15924 English name "Cypriot" */ - CYPRIOT, - /** ISO 15924 English name "Cyrillic" */ - CYRILLIC, - /** ISO 15924 English name "Deseret" */ - DESERET, - /** ISO 15924 English name "Devanagari" */ - DEVANAGARI, - /** ISO 15924 English name "Egyptian hieroglyphs" */ - EGYPTIAN_HIEROGLYPHS, - /** ISO 15924 English name "Ethiopic" */ - ETHIOPIC, - /** ISO 15924 English name "Georgian" */ - GEORGIAN, - /** ISO 15924 English name "Glagolitic" */ - GLAGOLITIC, - /** ISO 15924 English name "Gothic" */ - GOTHIC, - /** ISO 15924 English name "Greek" */ - GREEK, - /** ISO 15924 English name "Gujarati" */ - GUJARATI, - /** ISO 15924 English name "Gurmukhi" */ - GURMUKHI, - /** ISO 15924 English name "Han" */ - HAN, - /** ISO 15924 English name "Hangul" */ - HANGUL, - /** ISO 15924 English name "Hanunoo" */ - HANUNOO, - /** ISO 15924 English name "Hebrew" */ - HEBREW, - /** ISO 15924 English name "Hiragana" */ - HIRAGANA, - /** ISO 15924 English name "Imperial aramaic" */ - IMPERIAL_ARAMAIC, - /** ISO 15924 English name "Inherited" */ - INHERITED, - /** ISO 15924 English name "Inscriptional pahlavi" */ - INSCRIPTIONAL_PAHLAVI, - /** ISO 15924 English name "Inscriptional parthian" */ - INSCRIPTIONAL_PARTHIAN, - /** ISO 15924 English name "Javanese" */ - JAVANESE, - /** ISO 15924 English name "Kaithi" */ - KAITHI, - /** ISO 15924 English name "Kannada" */ - KANNADA, - /** ISO 15924 English name "Katakana" */ - KATAKANA, - /** ISO 15924 English name "Kayah li" */ - KAYAH_LI, - /** ISO 15924 English name "Kharoshthi" */ - KHAROSHTHI, - /** ISO 15924 English name "Khmer" */ - KHMER, - /** ISO 15924 English name "Lao" */ - LAO, - /** ISO 15924 English name "Latin" */ - LATIN, - /** ISO 15924 English name "Lepcha" */ - LEPCHA, - /** ISO 15924 English name "Limbu" */ - LIMBU, - /** ISO 15924 English name "Linear B" */ - LINEAR_B, - /** ISO 15924 English name "Lisu" */ - LISU, - /** ISO 15924 English name "Lycian" */ - LYCIAN, - /** ISO 15924 English name "Lydian" */ - LYDIAN, - /** ISO 15924 English name "Malayalam" */ - MALAYALAM, - /** ISO 15924 English name "Mandaic" */ - MANDAIC, - /** ISO 15924 English name "Meetei Mayek (Meithei, Meetei)" */ - MEETEI_MAYEK, - /** ISO 15924 English name "Mongolian" */ - MONGOLIAN, - /** ISO 15924 English name "Myanmar" */ - MYANMAR, - /** ISO 15924 English name "New Tai Lue" */ - NEW_TAI_LUE, - /** ISO 15924 English name "Nko" */ - NKO, - /** ISO 15924 English name "Ogham" */ - OGHAM, - /** ISO 15924 English name "Ol Chiki" */ - OL_CHIKI, - /** ISO 15924 English name "Old Italic" */ - OLD_ITALIC, - /** ISO 15924 English name "Old Persian" */ - OLD_PERSIAN, - /** ISO 15924 English name "Old South Arabian" */ - OLD_SOUTH_ARABIAN, - /** ISO 15924 English name "Old Turkic, Orkhon Runic" */ - OLD_TURKIC, - /** ISO 15924 English name "Oriya" */ - ORIYA, - /** ISO 15924 English name "Osmanya" */ - OSMANYA, - /** ISO 15924 English name "Phags-pa" */ - PHAGS_PA, - /** ISO 15924 English name "Phoenician" */ - PHOENICIAN, - /** ISO 15924 English name "Rejang" */ - REJANG, - /** ISO 15924 English name "Runic" */ - RUNIC, - /** ISO 15924 English name "Samaritan" */ - SAMARITAN, - /** ISO 15924 English name "Saurashtra" */ - SAURASHTRA, - /** ISO 15924 English name "Shavian" */ - SHAVIAN, - /** ISO 15924 English name "Sinhala" */ - SINHALA, - /** ISO 15924 English name "Sundanese" */ - SUNDANESE, - /** ISO 15924 English name "Syloti Nagri" */ - SYLOTI_NAGRI, - /** ISO 15924 English name "Syriac" */ - SYRIAC, - /** ISO 15924 English name "Tagalog" */ - TAGALOG, - /** ISO 15924 English name "Tagbanwa" */ - TAGBANWA, - /** ISO 15924 English name "Tai Le" */ - TAI_LE, - /** ISO 15924 English name "Tai Tham (Lanna)" */ - TAI_THAM, - /** ISO 15924 English name "Tai Viet" */ - TAI_VIET, - /** ISO 15924 English name "Tamil" */ - TAMIL, - /** ISO 15924 English name "Telugu" */ - TELUGU, - /** ISO 15924 English name "Thaana" */ - THAANA, - /** ISO 15924 English name "Thai" */ - THAI, - /** ISO 15924 English name "Tibetan" */ - TIBETAN, - /** ISO 15924 English name "Tifinagh" */ - TIFINAGH, - /** ISO 15924 English name "Ugaritic" */ - UGARITIC, - /** ISO 15924 English name "Unknown" */ - UNKNOWN, - /** ISO 15924 English name "Vai" */ - VAI, - /** ISO 15924 English name "Yi" */ - YI; - - private static final UnicodeScript[] SCRIPTS = { - COMMON, - INHERITED, - ARABIC, - ARMENIAN, - BENGALI, - BOPOMOFO, - CHEROKEE, - COPTIC, - CYRILLIC, - DESERET, - DEVANAGARI, - ETHIOPIC, - GEORGIAN, - GOTHIC, - GREEK, - GUJARATI, - GURMUKHI, - HAN, - HANGUL, - HEBREW, - HIRAGANA, - KANNADA, - KATAKANA, - KHMER, - LAO, - LATIN, - MALAYALAM, - MONGOLIAN, - MYANMAR, - OGHAM, - OLD_ITALIC, - ORIYA, - RUNIC, - SINHALA, - SYRIAC, - TAMIL, - TELUGU, - THAANA, - THAI, - TIBETAN, - CANADIAN_ABORIGINAL, - YI, - TAGALOG, - HANUNOO, - BUHID, - TAGBANWA, - BRAILLE, - CYPRIOT, - LIMBU, - LINEAR_B, - OSMANYA, - SHAVIAN, - TAI_LE, - UGARITIC, - null, // USCRIPT_KATAKANA_OR_HIRAGANA - BUGINESE, - GLAGOLITIC, - KHAROSHTHI, - SYLOTI_NAGRI, - NEW_TAI_LUE, - TIFINAGH, - OLD_PERSIAN, - BALINESE, - BATAK, - null, // USCRIPT_BLISSYMBOLS, - BRAHMI, - CHAM, - null, // USCRIPT_CIRTH, - null, // USCRIPT_OLD_CHURCH_SLAVONIC_CYRILLIC, - null, // USCRIPT_DEMOTIC_EGYPTIAN, - null, // USCRIPT_HIERATIC_EGYPTIAN, - EGYPTIAN_HIEROGLYPHS, - null, // USCRIPT_USCRIPT_KHUTSURI, - null, // USCRIPT_SIMPLIFIED_HAN, - null, // USCRIPT_TRADITIONAL_HAN, - null, // USCRIPT_PAHAWH_HMONG, - null, // USCRIPT_OLD_HUNGARIAN, - null, // USCRIPT_HARAPPAN_INDUS, - JAVANESE, - KAYAH_LI, - null, // USCRIPT_LATIN_FRAKTUR, - null, // USCRIPT_LATIN_GAELIC, - LEPCHA, - null, // USCRIPT_LINEAR_A, - MANDAIC, // == MANDAEAN - null, // USCRIPT_MAYAN_HIEROGLYPHS, - null, // USCRIPT_MEROITIC_HIEROGLYPHS == USCRIPT_MEROITIC - null, // USCRIPT_NKO, - OLD_TURKIC, // USCRIPT_ORKHON == OLD_TURKIC, - null, // USCRIPT_OLD_PERMIC, - PHAGS_PA, - PHOENICIAN, - null, // USCRIPT_PHONETIC_POLLARD === MIAO, - null, // USCRIPT_RONGORONGO, - null, // USCRIPT_SARATI, - null, // USCRIPT_ESTRANGELO_SYRIAC, - null, // USCRIPT_WESTERN_SYRIAC, - null, // USCRIPT_EASTERN_SYRIAC, - null, // USCRIPT_TENGWAR, - VAI, - null, // USCRIPT_VISIBLE_SPEECH, - CUNEIFORM, - null, // USCRIPT_UNWRITTEN_LANGUAGES, - UNKNOWN, - CARIAN, - null, // USCRIPT_JAPANESE, - TAI_THAM, // USCRIPT_LANNA (aka TAI_THAM), - LYCIAN, - LYDIAN, - OL_CHIKI, - REJANG, - SAURASHTRA, - null, // USCRIPT_SIGN_WRITING, - SUNDANESE, - null, // USCRIPT_MOON, - MEETEI_MAYEK, // USCRIPT_MEITEI_MAYEK (aka MEETEI, MEITHEI), - IMPERIAL_ARAMAIC, - AVESTAN, - null, // USCRIPT_CHAKMA, - null, // USCRIPT_KOREAN, - KAITHI, - null, // USCRIPT_MANICHAEAN, - INSCRIPTIONAL_PAHLAVI, - null, // USCRIPT_PSALTER_PAHLAVI, - null, // USCRIPT_BOOK_PAHLAVI, - INSCRIPTIONAL_PARTHIAN, - SAMARITAN, - TAI_VIET, - null, // USCRIPT_MATHEMATICAL_NOTATION, - null, // USCRIPT_SYMBOLS, - BAMUM, - LISU, - null, // USCRIPT_NAKHI_GEBA, - OLD_SOUTH_ARABIAN, - null, // USCRIPT_BASSA_VAH, - null, // USCRIPT_DUPLOYAN_SHORTAND, - null, // USCRIPT_ELBASAN, - null, // USCRIPT_GRANTHA, - null, // USCRIPT_KPELLE, - null, // USCRIPT_LOMA, - null, // USCRIPT_MENDE, - null, // USCRIPT_MEROITIC_CURSIVE, - null, // USCRIPT_OLD_NORTH_ARABIAN, - null, // USCRIPT_NABATAEAN, - null, // USCRIPT_PALMYRENE, - null, // USCRIPT_SINDHI, - null, // USCRIPT_WARANG_CITI, - null, // USCRIPT_AFAKA, - null, // USCRIPT_JURCHEN, - null, // USCRIPT_MRO, - null, // USCRIPT_NUSHU, - null, // USCRIPT_SHARADA, - null, // USCRIPT_SORA_SOMPENG, - null, // USCRIPT_TAKRI, - null, // USCRIPT_TANGUT, - null, // USCRIPT_WOLEAI, - null, // USCRIPT_ANATOLIAN_HIEROGLYPHS, - null, // USCRIPT_KHOJKI, - null, // USCRIPT_TIRHUTA, - }; - - /** - * Returns the {@link UnicodeScript} value identified by {@code scriptName}. - * {@code scriptName} can be a ISO-15924 English script name - * or an alias (ISO-15924 script code) for that name. - * {@see http://www.unicode.org/iso15924/iso15924-codes.html} - * Lookups are case insensitive. - * - * @throws NullPointerException if {@code scriptName} is null. - * @throws IllegalAccessException if {@code scriptName} in invalid. - * - * @since 1.7 - */ - public static UnicodeScript forName(String scriptName) { - if (scriptName == null) { - throw new NullPointerException("scriptName == null"); - } - - final int script = unicodeScriptForName(scriptName); - if (script == -1 || script >= SCRIPTS.length || - SCRIPTS[script] == null) { - throw new IllegalArgumentException("Unknown script: " + scriptName); - } - - return SCRIPTS[script]; - } - - /** - * Returns the {@link UnicodeScript} value that the given Unicode code - * point is assigned to. - * - * @throws IllegalArgumentException if {@code Point} is not a valid Unicode code point. - * - * @since 1.7 - */ - public static UnicodeScript of(int codePoint) { - checkValidCodePoint(codePoint); - int script = unicodeScriptForCodePoint(codePoint); - if (script == -1 || script >= SCRIPTS.length) { - // This signifies an ICU error. Complain loudly instead of swallowing - // the error up. - throw new IllegalArgumentException("Invalid codePoint: " + codePoint); - } - - // This happens when ICU maps the code point to a script known to ICU but - // not the Java API. - if (SCRIPTS[script] == null) { - return UNKNOWN; - } - - return SCRIPTS[script]; - } - } - private static native int unicodeScriptForName(String blockName); private static native int unicodeScriptForCodePoint(int codePoint); diff --git a/luni/src/main/java/java/net/HttpCookie.java b/luni/src/main/java/java/net/HttpCookie.java index 3748b9f..dd81fd6 100644 --- a/luni/src/main/java/java/net/HttpCookie.java +++ b/luni/src/main/java/java/net/HttpCookie.java @@ -560,28 +560,6 @@ public final class HttpCookie implements Cloneable { } /** - * Returns the {@code HttpOnly} attribute. If {@code true} the cookie should not be accessible - * to scripts in a browser. - * - * @since 1.7 - * @hide 1.7 - */ - public boolean isHttpOnly() { - return httpOnly; - } - - /** - * Returns the {@code HttpOnly} attribute. If {@code true} the cookie should not be accessible - * to scripts in a browser. - * - * @since 1.7 - * @hide 1.7 - */ - public void setHttpOnly(boolean httpOnly) { - this.httpOnly = httpOnly; - } - - /** * Returns the value of this cookie. */ public String getValue() { diff --git a/luni/src/main/java/java/net/SocketOption.java b/luni/src/main/java/java/net/SocketOption.java deleted file mode 100644 index 2bd4c6d..0000000 --- a/luni/src/main/java/java/net/SocketOption.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.net; - -/** - * An option associated with a socket. - * - * <p>See {@link java.nio.channels.NetworkChannel#setOption}, - * {@link java.nio.channels.NetworkChannel#getOption} and - * {@link java.nio.channels.NetworkChannel#supportedOptions} for methods that use SocketOption. - * - * <p>See {@link StandardSocketOptions} for valid SocketOptions. - * - * @param <T> the type of the value - * @since 1.7 - * @hide 1.7 - */ -public interface SocketOption<T> { - - /** - * Returns the name of the option. - */ - String name(); - - /** - * Returns the type of the value of the option. - */ - Class<T> type(); -} diff --git a/luni/src/main/java/java/net/StandardSocketOptions.java b/luni/src/main/java/java/net/StandardSocketOptions.java deleted file mode 100644 index d61d937..0000000 --- a/luni/src/main/java/java/net/StandardSocketOptions.java +++ /dev/null @@ -1,407 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.net; - -import java.io.FileDescriptor; -import java.io.IOException; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -import libcore.io.IoBridge; - -/** - * Defines the set standard of socket options that can be supported by network channels. - * - * <p>See {@link java.nio.channels.NetworkChannel} for more information, particularly - * {@link java.nio.channels.NetworkChannel#supportedOptions()} for the options that are supported - * for each type of socket. - * - * @since 1.7 - * @hide 1.7 - */ -public final class StandardSocketOptions { - - /** - * The outgoing interface for multicast packets. - * - * <p>See {@link SocketOptions#IP_MULTICAST_IF2} for further documentation. - */ - public static final SocketOption<NetworkInterface> IP_MULTICAST_IF = - new NetworkInterfaceSocketOption("IP_MULTICAST_IF", SocketOptions.IP_MULTICAST_IF2); - - /** - * Whether the local loopback of multicast packets is enabled (true) or disabled (false). This - * option is enabled by default. - * - * <p>See {@link SocketOptions#IP_MULTICAST_LOOP} for further documentation. - */ - public static final SocketOption<Boolean> IP_MULTICAST_LOOP = - new BooleanSocketOption("IP_MULTICAST_LOOP", SocketOptions.IP_MULTICAST_LOOP); - - /** - * The time-to-live (TTL) for multicast packets. The value must be between 0 and 255 inclusive. - * A 0 value restricts the packet to the originating host. See also {@link #IP_MULTICAST_LOOP}. - * The default value is 1. - * - * <p>See <a href="http://tools.ietf.org/rfc/rfc1112.txt">RFC 1112: Host Extensions for IP - * Multicasting</a> for more information about IP multicast. - */ - public static final SocketOption<Integer> IP_MULTICAST_TTL = - new ByteRangeSocketOption("IP_MULTICAST_TTL", IoBridge.JAVA_IP_MULTICAST_TTL); - - /** - * The value for the type-of-service field of the IPv4 header, or the traffic class field of the - * IPv6 header. These correspond to the IP_TOS and IPV6_TCLASS socket options. These may be - * ignored by the underlying OS. Values must be between 0 and 255 inclusive. - * - * <p>See {@link SocketOptions#IP_TOS} for further documentation. - */ - public static final SocketOption<Integer> IP_TOS = - new ByteRangeSocketOption("IP_TOS", SocketOptions.IP_TOS); - - /** - * Whether broadcasting on datagram sockets is enabled or disabled. This option must be enabled to - * send broadcast messages. The default value is false. - * - * <p>See {@link SocketOptions#SO_BROADCAST} for further documentation. - */ - public static final SocketOption<Boolean> SO_BROADCAST = - new BooleanSocketOption("SO_BROADCAST", SocketOptions.SO_BROADCAST); - - /** - * Whether the kernel sends keepalive messages on connection-oriented sockets. - * - * <p>See {@link SocketOptions#SO_KEEPALIVE} for further documentation. - */ - public static final SocketOption<Boolean> SO_KEEPALIVE = - new BooleanSocketOption("SO_KEEPALIVE", SocketOptions.SO_KEEPALIVE); - - /** - * Number of seconds to wait when closing a socket if there is still some buffered data to be - * sent. - * - * <p>If this option is negative this option is disabled. This is the default value. If the value - * is 0 or positive it is enabled. - * - * <p>See {@link SocketOptions#SO_LINGER} for further documentation. - * - */ - public static final SocketOption<Integer> SO_LINGER = - new SocketOptionImpl<Integer>("SO_LINGER", Integer.class, SocketOptions.SO_LINGER) { - @Override - protected Object validateAndConvertValueBeforeSet( - FileDescriptor fd, Integer value) { - Object objectValue = super.validateAndConvertValueBeforeSet(fd, value); - if (value != null && value < 0) { - // IoBridge requires a "false" object to disable linger. - objectValue = Boolean.FALSE; - } - return objectValue; - } - - @Override - protected Integer validateAndConvertValueAfterGet(FileDescriptor fd, Object value) { - // IoBridge returns a "false" object to indicate that linger is disabled. - if (value != null && value instanceof Boolean) { - value = -1; - } - return super.validateAndConvertValueAfterGet(fd, value); - } - }; - - /** - * The size in bytes of a socket's receive buffer. This must be an integer greater than 0. - * This is a hint to the kernel; the kernel may use a larger buffer. - * - * <p>See {@link SocketOptions#SO_RCVBUF} for further documentation. - */ - public static final SocketOption<Integer> SO_RCVBUF = - new PositiveIntegerSocketOption("SO_RCVBUF", SocketOptions.SO_RCVBUF); - - /** - * Whether a reuse of a local address is allowed when another socket has not yet been removed by - * the operating system. - * - * <p>See {@link SocketOptions#SO_REUSEADDR} for further documentation. - */ - public static final SocketOption<Boolean> SO_REUSEADDR = - new BooleanSocketOption("SO_REUSEADDR", SocketOptions.SO_REUSEADDR); - - /** - * The size in bytes of a socket's send buffer. This must be an integer greater than 0. - * This is a hint to the kernel; the kernel may use a larger buffer. - * - * <p>See {@link SocketOptions#SO_SNDBUF} for further documentation. - */ - public static final SocketOption<Integer> SO_SNDBUF = - new PositiveIntegerSocketOption("SO_SNDBUF", SocketOptions.SO_SNDBUF); - - /** - * Specifies whether data is sent immediately on this socket or buffered. - * - * <p>See {@link SocketOptions#TCP_NODELAY} for further documentation. - */ - public static final SocketOption<Boolean> TCP_NODELAY = - new BooleanSocketOption("TCP_NODELAY", SocketOptions.TCP_NODELAY); - - /** - * The set of supported options for UDP sockets. - * - * @hide internal use only - */ - public static final Set<SocketOption<?>> DATAGRAM_SOCKET_OPTIONS; - - static { - HashSet<SocketOption<?>> mutableSet = new HashSet<SocketOption<?>>(8); - mutableSet.add(IP_MULTICAST_IF); - mutableSet.add(IP_MULTICAST_LOOP); - mutableSet.add(IP_MULTICAST_TTL); - mutableSet.add(IP_TOS); - mutableSet.add(SO_BROADCAST); - mutableSet.add(SO_REUSEADDR); - mutableSet.add(SO_RCVBUF); - mutableSet.add(SO_SNDBUF); - DATAGRAM_SOCKET_OPTIONS = Collections.unmodifiableSet(mutableSet); - } - - /** - * The set of supported options for TCP sockets. - * - * @hide internal use only - */ - public static final Set<SocketOption<?>> SOCKET_OPTIONS; - - static { - HashSet<SocketOption<?>> mutableSet = new HashSet<SocketOption<?>>(7); - mutableSet.add(IP_TOS); - mutableSet.add(SO_KEEPALIVE); - mutableSet.add(SO_LINGER); - mutableSet.add(TCP_NODELAY); - mutableSet.add(SO_RCVBUF); - mutableSet.add(SO_REUSEADDR); - mutableSet.add(SO_SNDBUF); - SOCKET_OPTIONS = Collections.unmodifiableSet(mutableSet); - } - - /** - * The set of supported options for TCP server sockets. - * - * @hide internal use only - */ - public static final Set<SocketOption<?>> SERVER_SOCKET_OPTIONS; - - static { - HashSet<SocketOption<?>> mutableSet = new HashSet<SocketOption<?>>(2); - mutableSet.add(SO_RCVBUF); - mutableSet.add(SO_REUSEADDR); - SERVER_SOCKET_OPTIONS = Collections.unmodifiableSet(mutableSet); - } - - /** - * A base class for SocketOption objects that passes the values to/from {@link IoBridge} as they - * are. For use with simple types like Integer and Boolean, and can be extended for more - * validation / type conversion. - * - * @hide internal use only - */ - public static class SocketOptionImpl<T> implements SocketOption<T> { - - protected final String name; - - private final Class<T> type; - - protected final int socketOption; - - public SocketOptionImpl(String name, Class<T> type, int socketOption) { - this.name = name; - this.type = type; - this.socketOption = socketOption; - } - - @Override - public String name() { - return name; - } - - @Override - public Class<T> type() { - return type; - } - - /** - * Sets the socket option of the file descriptor to value using IoBridge. - * - * @hide internal method - */ - public final void setValue(FileDescriptor fd, T value) throws IOException { - // Sanity check required because of type erasure. - if (value != null && !type.isAssignableFrom(value.getClass())) { - throw new AssertionError("Invalid type " + value + " of value for " + name); - } - Object objectValue = validateAndConvertValueBeforeSet(fd, value); - IoBridge.setSocketOption(fd, socketOption, objectValue); - } - - /** - * Throws IllegalArgumentException if the value is outside of the acceptable range. - * Subclasses can override to apply option-specific validate, and may also convert the value - * to a different type or value. The default implementation prevents null values and returns - * the value unchanged. - */ - protected Object validateAndConvertValueBeforeSet(FileDescriptor fd, T value) { - if (value == null) { - throw new IllegalArgumentException("value for " + name + " must not be null"); - } - return value; - } - - /** - * Gets the value of the socket option. - * - * @hide internal method - */ - public final T getValue(FileDescriptor fd) throws IOException { - Object value = IoBridge.getSocketOption(fd, socketOption); - T typedValue = validateAndConvertValueAfterGet(fd, value); - if (typedValue != null && !type.isAssignableFrom(typedValue.getClass())) { - // Sanity check required because of type erasure. - throw new AssertionError("Unexpected type of value returned for " + name); - } - return typedValue; - } - - /** - * Throws AssertionError if the value is outside of the acceptable range. - * Implementations may also convert the value to a different type or - * value. The default implementation does nothing. - */ - @SuppressWarnings("unchecked") - protected T validateAndConvertValueAfterGet(FileDescriptor fd, Object value) { - return (T) value; - } - } - - /** - * A SocketOption capable of setting / getting an boolean value. - */ - private static class BooleanSocketOption extends SocketOptionImpl<Boolean> { - - public BooleanSocketOption(String name, int socketOption) { - super(name, Boolean.class, socketOption); - } - } - - /** - * A SocketOption capable of setting / getting an network interface value. - */ - private static class NetworkInterfaceSocketOption extends SocketOptionImpl<NetworkInterface> { - - public NetworkInterfaceSocketOption(String name, int socketOption) { - super(name, NetworkInterface.class, socketOption); - } - - @Override - public Integer validateAndConvertValueBeforeSet(FileDescriptor fd, NetworkInterface value) { - if (value == null) { - throw new IllegalArgumentException("value for " + name + " must not be null"); - } - int nicIndex = value.getIndex(); - if (nicIndex == -1) { - throw new IllegalArgumentException("The NetworkInterface must have a valid index"); - } - return nicIndex; - } - - @Override - public NetworkInterface validateAndConvertValueAfterGet(FileDescriptor fd, Object value) { - if (value == null) { - return null; - } else if (!(value instanceof Integer)) { - throw new AssertionError("Unexpected type of value returned for " + name); - } - - int nicIndex = (Integer) value; - try { - return NetworkInterface.getByIndex(nicIndex); - } catch (SocketException e) { - throw new IllegalArgumentException( - "Unable to resolve NetworkInterface index: " + nicIndex, e); - } - } - } - - /** - * A SocketOption capable of setting / getting an integer in the range 0-255. - */ - private static class ByteRangeSocketOption extends SocketOptionImpl<Integer> { - - public ByteRangeSocketOption(String name, int socketOption) { - super(name, Integer.class, socketOption); - } - - @Override - protected Object validateAndConvertValueBeforeSet(FileDescriptor fd, Integer value) { - if (value == null || value < 0 || value > 255) { - throw new IllegalArgumentException(name + " must be >= 0 and <= 255, is " + value); - } - return value; - } - - @Override - protected Integer validateAndConvertValueAfterGet(FileDescriptor fd, Object value) { - if (!(value instanceof Integer)) { - throw new AssertionError("Unexpected value for option " + name + ": " + value); - } - int intValue = (Integer) value; - if (intValue < 0 || intValue > 255) { - throw new AssertionError("Unexpected value for option " + name + ": " + value); - } - return intValue; - } - } - - /** - * A SocketOption capable of setting / getting an integer in the range 1.. - */ - private static class PositiveIntegerSocketOption extends SocketOptionImpl<Integer> { - - public PositiveIntegerSocketOption(String name, int socketOption) { - super(name, Integer.class, socketOption); - } - - @Override - protected Integer validateAndConvertValueBeforeSet(FileDescriptor fd, Integer value) { - if (value < 1) { - throw new IllegalArgumentException(name + " value must be > 0"); - } - return value; - } - - @Override - protected Integer validateAndConvertValueAfterGet(FileDescriptor fd, Object value) { - if (!(value instanceof Integer)) { - throw new AssertionError("Unexpected value for option " + name + ": " + value); - } - int intValue = (Integer) value; - if (intValue < 1) { - throw new AssertionError("Unexpected value for option " + name + ": " + value); - } - return intValue; - } - } -} diff --git a/luni/src/main/java/java/nio/DatagramChannelImpl.java b/luni/src/main/java/java/nio/DatagramChannelImpl.java index 343ef00..9008637 100644 --- a/luni/src/main/java/java/nio/DatagramChannelImpl.java +++ b/luni/src/main/java/java/nio/DatagramChannelImpl.java @@ -32,14 +32,10 @@ import java.net.NetworkInterface; import java.net.PlainDatagramSocketImpl; import java.net.SocketAddress; import java.net.SocketException; -import java.net.SocketOption; -import java.net.StandardSocketOptions; -import java.nio.channels.AlreadyBoundException; import java.nio.channels.AlreadyConnectedException; import java.nio.channels.ClosedChannelException; import java.nio.channels.DatagramChannel; import java.nio.channels.IllegalBlockingModeException; -import java.nio.channels.MembershipKey; import java.nio.channels.NotYetConnectedException; import java.nio.channels.spi.SelectorProvider; import java.nio.channels.UnresolvedAddressException; @@ -79,9 +75,6 @@ class DatagramChannelImpl extends DatagramChannel implements FileDescriptorChann private final Object readLock = new Object(); private final Object writeLock = new Object(); - // A helper to manage multicast group membership. Created as required. - private MulticastMembershipHandler multicastMembershipHandler; - /* * Constructor */ @@ -112,28 +105,6 @@ class DatagramChannelImpl extends DatagramChannel implements FileDescriptorChann return socket; } - @Override - synchronized public DatagramChannel bind(SocketAddress local) throws IOException { - checkOpen(); - if (isBound) { - throw new AlreadyBoundException(); - } - - if (local == null) { - local = new InetSocketAddress(Inet4Address.ANY, 0); - } else if (!(local instanceof InetSocketAddress)) { - throw new UnsupportedAddressTypeException(); - } - - InetSocketAddress localAddress = (InetSocketAddress) local; - if (localAddress.isUnresolved()) { - throw new UnresolvedAddressException(); - } - IoBridge.bind(fd, localAddress.getAddress(), localAddress.getPort()); - onBind(true /* updateSocketState */); - return this; - } - /** * Initialise the isBound, localAddress and localPort state from the file descriptor. Used when * some or all of the bound state has been left to the OS to decide, or when the Socket handled @@ -160,31 +131,6 @@ class DatagramChannelImpl extends DatagramChannel implements FileDescriptorChann } @Override - synchronized public SocketAddress getLocalAddress() throws IOException { - checkOpen(); - return isBound ? new InetSocketAddress(localAddress, localPort) : null; - } - - @Override - public <T> T getOption(SocketOption<T> option) throws IOException { - return NioUtils.getSocketOption( - this, StandardSocketOptions.DATAGRAM_SOCKET_OPTIONS, option); - } - - @Override - public <T> DatagramChannel setOption(SocketOption<T> option, T value) throws IOException { - checkOpen(); - NioUtils.setSocketOption( - this, StandardSocketOptions.DATAGRAM_SOCKET_OPTIONS, option, value); - return this; - } - - @Override - public Set<SocketOption<?>> supportedOptions() { - return StandardSocketOptions.DATAGRAM_SOCKET_OPTIONS; - } - - @Override synchronized public boolean isConnected() { return connected; } @@ -519,7 +465,6 @@ class DatagramChannelImpl extends DatagramChannel implements FileDescriptorChann // A closed channel is not connected. onDisconnect(true /* updateSocketState */); IoBridge.closeAndSignalBlockedThreads(fd); - multicastMembershipHandler = null; if (socket != null && !socket.isClosed()) { socket.onClose(); @@ -565,52 +510,6 @@ class DatagramChannelImpl extends DatagramChannel implements FileDescriptorChann return fd; } - @Override - synchronized public MembershipKey join(InetAddress groupAddress, - NetworkInterface networkInterface) throws IOException { - - checkOpen(); - ensureMembershipHandlerExists(); - return multicastMembershipHandler.addAnySourceMembership(networkInterface, groupAddress); - } - - @Override - synchronized public MembershipKey join( - InetAddress groupAddress, NetworkInterface networkInterface, InetAddress sourceAddress) - throws IOException { - checkOpen(); - ensureMembershipHandlerExists(); - return multicastMembershipHandler.addSourceSpecificMembership( - networkInterface, groupAddress, sourceAddress); - } - - synchronized void multicastDrop(MembershipKeyImpl membershipKey) { - ensureMembershipHandlerExists(); - multicastMembershipHandler.dropMembership(membershipKey); - } - - synchronized void multicastBlock(MembershipKeyImpl membershipKey, InetAddress sourceAddress) - throws SocketException { - - ensureMembershipHandlerExists(); - multicastMembershipHandler.block(membershipKey, sourceAddress); - } - - synchronized void multicastUnblock(MembershipKeyImpl membershipKey, InetAddress sourceAddress) { - ensureMembershipHandlerExists(); - multicastMembershipHandler.unblock(membershipKey, sourceAddress); - } - - /** - * Creates the {@code multicastMembershipHandler} if one doesn't already exist. Callers must - * handle synchronization. - */ - private void ensureMembershipHandlerExists() { - if (multicastMembershipHandler == null) { - multicastMembershipHandler = new MulticastMembershipHandler(this); - } - } - /* * The adapter class of DatagramSocket */ diff --git a/luni/src/main/java/java/nio/MembershipKeyImpl.java b/luni/src/main/java/java/nio/MembershipKeyImpl.java deleted file mode 100644 index 3d7e957..0000000 --- a/luni/src/main/java/java/nio/MembershipKeyImpl.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.nio; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.nio.channels.MembershipKey; -import java.nio.channels.MulticastChannel; - -/** - * An implementation of {@link MembershipKey}. - * - * To keep this class simple and keep all mutation operations in one place and easily synchronized, - * most socket logic is held in {@link java.nio.DatagramChannelImpl}. - */ -final class MembershipKeyImpl extends MembershipKey { - - private final DatagramChannelImpl channel; - private final InetAddress groupAddress; - private final NetworkInterface networkInterface; - private final InetAddress sourceAddress; - private volatile boolean isValid; - - public MembershipKeyImpl(DatagramChannelImpl channel, NetworkInterface networkInterface, - InetAddress groupAddress, InetAddress sourceAddress) { - - this.channel = channel; - this.networkInterface = networkInterface; - this.groupAddress = groupAddress; - this.sourceAddress = sourceAddress; - this.isValid = true; - } - - @Override - public boolean isValid() { - // invalidate() is called if the key is dropped, but for simplicity it is not - // invalidated when the channel is closed. Therefore, the channel must also be checked to see - // if it is still open. - return isValid && channel.isOpen(); - } - - void invalidate() { - this.isValid = false; - } - - @Override - public void drop() { - channel.multicastDrop(this); - } - - @Override - public MembershipKey block(InetAddress source) throws IOException { - channel.multicastBlock(this, source); - return this; - } - - @Override - synchronized public MembershipKey unblock(InetAddress source) { - channel.multicastUnblock(this, source); - return this; - } - - @Override - public MulticastChannel channel() { - return channel; - } - - @Override - public InetAddress group() { - return groupAddress; - } - - @Override - public NetworkInterface networkInterface() { - return networkInterface; - } - - @Override - public InetAddress sourceAddress() { - return sourceAddress; - } - - @Override - public String toString() { - return "MembershipKeyImpl{" + - "groupAddress=" + groupAddress + - ", networkInterface=" + networkInterface + - ", sourceAddress=" + sourceAddress + - '}'; - } -} diff --git a/luni/src/main/java/java/nio/MulticastMembershipHandler.java b/luni/src/main/java/java/nio/MulticastMembershipHandler.java deleted file mode 100644 index 2fe8130..0000000 --- a/luni/src/main/java/java/nio/MulticastMembershipHandler.java +++ /dev/null @@ -1,495 +0,0 @@ -package java.nio; - -import android.system.StructGroupReq; -import android.system.StructGroupSourceReq; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.nio.channels.MembershipKey; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import libcore.io.IoBridge; - -/** - * A helper class for {@link DatagramChannelImpl} that keeps track of multicast group - * memberships. This class is not threadsafe, and relies on the DatagramChannelImpl to synchronize. - * - * <p>See <a href="http://tools.ietf.org/html/rfc3678">RFC 3678</a> for context and terminology. - */ -final class MulticastMembershipHandler { - - private final DatagramChannelImpl channel; - private final Map<Id, Membership> memberships = new HashMap<Id, Membership>(); - - MulticastMembershipHandler(DatagramChannelImpl channel) { - this.channel = channel; - } - - /** - * The implementation for - * {@link java.nio.channels.MulticastChannel#join(InetAddress, NetworkInterface)}. - */ - public MembershipKeyImpl addAnySourceMembership( - NetworkInterface networkInterface, InetAddress groupAddress) throws SocketException { - - validateMulticastGroupArgs(groupAddress, networkInterface); - assertChannelOpen(); - - Id id = new Id(networkInterface, groupAddress); - Membership membership = memberships.get(id); - if (membership != null) { - return membership.getAnySourceMembershipKey(); - } - - // No existing membership found. Attempt to join. - StructGroupReq groupReq = makeGroupReq(groupAddress, networkInterface); - IoBridge.setSocketOption(channel.getFD(), IoBridge.JAVA_MCAST_JOIN_GROUP, groupReq); - - // Record the membership and return the key. - membership = Membership.createAnySource(channel, networkInterface, groupAddress); - memberships.put(id, membership); - return membership.getAnySourceMembershipKey(); - } - - /** - * The implementation for - * {@link java.nio.channels.MulticastChannel#join(InetAddress, NetworkInterface, InetAddress)}. - */ - public MembershipKeyImpl addSourceSpecificMembership( - NetworkInterface networkInterface, InetAddress groupAddress, InetAddress sourceAddress) - throws SocketException { - - validateMulticastGroupArgs(groupAddress, networkInterface); - validateSourceAddress(sourceAddress); - validateAddressProtocolTheSame(groupAddress, sourceAddress); - assertChannelOpen(); - - Id id = new Id(networkInterface, groupAddress); - Membership membership = memberships.get(id); - if (membership != null) { - MembershipKeyImpl existingMembershipKey = - membership.getSourceSpecificMembershipKey(sourceAddress); - if (existingMembershipKey != null) { - return existingMembershipKey; - } - } - - // No existing membership found. Attempt to join. - IoBridge.setSocketOption(channel.getFD(), IoBridge.JAVA_MCAST_JOIN_SOURCE_GROUP, - makeGroupSourceReq(groupAddress, networkInterface, sourceAddress)); - - if (membership == null) { - // Record the membership and return the key. - membership = Membership.createSourceSpecific( - channel, networkInterface, groupAddress, sourceAddress); - memberships.put(id, membership); - return membership.getSourceSpecificMembershipKey(sourceAddress); - } else { - // Add a new source to the existing membership. - return membership.addSource(sourceAddress); - } - } - - /** - * The implementation for {@link MembershipKey#drop()}. - */ - public void dropMembership(MembershipKeyImpl membershipKey) { - // For compatibility with the RI, this is one case where the membershipKey can no longer be - // valid. - if (!membershipKey.isValid()) { - return; - } - if (membershipKey.channel() != this.channel) { - throw new AssertionError("Bad membership key"); - } - assertChannelOpen(); - - Id id = createId(membershipKey); - Membership membership = memberships.get(id); - if (membership == null) { - throw new AssertionError("Bad membership key" + membershipKey); - } - - if (!membership.isSourceSpecific()) { - try { - StructGroupReq groupReq = - makeGroupReq(membershipKey.group(), membershipKey.networkInterface()); - IoBridge.setSocketOption(channel.getFD(), IoBridge.JAVA_MCAST_LEAVE_GROUP, groupReq); - } catch (SocketException e) { - // TODO: Obtain opinion on how to report this, throw this or if it is safe to ignore. - throw new IllegalStateException(e); - } - memberships.remove(id); - } else { - StructGroupSourceReq groupSourceReq = makeGroupSourceReq(membershipKey.group(), - membershipKey.networkInterface(), membershipKey.sourceAddress()); - - try { - IoBridge.setSocketOption( - channel.getFD(), IoBridge.JAVA_MCAST_LEAVE_SOURCE_GROUP, - groupSourceReq); - } catch (SocketException e) { - // TODO: Obtain opinion on how to report this, throw this or if it is safe to ignore. - throw new IllegalStateException(e); - } - - boolean isLast = membership.removeSource(membershipKey.sourceAddress()); - if (isLast) { - memberships.remove(id); - } - } - membershipKey.invalidate(); - } - - /** - * The implementation for {@link MembershipKey#block(java.net.InetAddress)}. - */ - public void block(MembershipKeyImpl membershipKey, InetAddress sourceAddress) - throws SocketException { - validateMembershipKey(membershipKey); - validateSourceAddress(sourceAddress); - validateAddressProtocolTheSame(membershipKey.group(), sourceAddress); - assertChannelOpen(); - - Membership membership = getMembershipForKey(membershipKey); - if (membership == null) { - throw new AssertionError("Bad membership key" + membershipKey); - } - - if (membership.isBlocked(sourceAddress)) { - return; - } - - IoBridge.setSocketOption(channel.getFD(), IoBridge.JAVA_MCAST_BLOCK_SOURCE, - makeGroupSourceReq( - membershipKey.group(), membershipKey.networkInterface(), sourceAddress)); - - membership.block(sourceAddress); - } - - /** - * The implementation for {@link MembershipKey#unblock(java.net.InetAddress)}. - */ - public void unblock(MembershipKeyImpl membershipKey, InetAddress sourceAddress) { - validateMembershipKey(membershipKey); - validateSourceAddress(sourceAddress); - validateAddressProtocolTheSame(membershipKey.group(), sourceAddress); - assertChannelOpen(); - - Membership membership = getMembershipForKey(membershipKey); - if (membership == null) { - throw new AssertionError("Bad membership key" + membershipKey); - } - - if (!membership.isBlocked(sourceAddress)) { - throw new IllegalStateException( - "sourceAddress " + sourceAddress + " is not blocked for " + membership.debugId()); - } - - try { - IoBridge.setSocketOption(channel.getFD(), IoBridge.JAVA_MCAST_UNBLOCK_SOURCE, - makeGroupSourceReq(membershipKey.group(), membershipKey.networkInterface(), - sourceAddress)); - } catch (SocketException e) { - throw new IllegalStateException(e); - } - - membership.unblock(sourceAddress); - } - - private Membership getMembershipForKey(MembershipKey membershipKey) { - Id id = createId(membershipKey); - Membership membership = memberships.get(id); - if (membership == null) { - throw new AssertionError("No membership found for id " + id); - } - return membership; - } - - private void assertChannelOpen() { - if (!channel.isOpen()) { - throw new AssertionError("Channel is closed"); - } - } - - private void validateMembershipKey(MembershipKeyImpl membershipKey) { - if (membershipKey.channel() != this.channel) { - throw new AssertionError("Invalid or bad membership key"); - } - if (!membershipKey.isValid()) { - throw new IllegalStateException("Membership key is no longer valid: " + membershipKey); - } - } - - private static Id createId(MembershipKey membershipKey) { - return new Id(membershipKey.networkInterface(), membershipKey.group()); - } - - private static void validateSourceAddress(InetAddress sourceAddress) { - if (sourceAddress.isAnyLocalAddress()) { - throw new IllegalArgumentException( - "sourceAddress must not be a wildcard address, is " + sourceAddress); - } - if (sourceAddress.isMulticastAddress()) { - throw new IllegalArgumentException( - "sourceAddress must be a unicast address, is " + sourceAddress); - } - } - - private static void validateMulticastGroupArgs( - InetAddress groupAddress, NetworkInterface networkInterface) throws SocketException { - - if (groupAddress == null) { - // RI throws NullPointerException. - throw new NullPointerException("groupAddress == null"); - } - if (networkInterface == null) { - // RI throws NullPointerException. - throw new NullPointerException("networkInterface == null"); - } - if (!networkInterface.isLoopback() && !networkInterface.supportsMulticast()) { - throw new IllegalArgumentException( - "networkInterface " + networkInterface + " does not support multicast"); - } - if (!groupAddress.isMulticastAddress()) { - throw new IllegalArgumentException("Not a multicast group: " + groupAddress); - } - } - - private static void validateAddressProtocolTheSame( - InetAddress groupAddress, InetAddress sourceAddress) { - - if (groupAddress.getClass() != sourceAddress.getClass()) { - throw new IllegalArgumentException("Mixed address types not permitted: groupAddress: " + - groupAddress + ", sourceAddress: " + sourceAddress); - } - } - - private static StructGroupSourceReq makeGroupSourceReq( - InetAddress gsr_group, NetworkInterface networkInterface, InetAddress gsr_source) { - int gsr_interface = (networkInterface != null) ? networkInterface.getIndex() : 0; - return new StructGroupSourceReq(gsr_interface, gsr_group, gsr_source); - } - - private static StructGroupReq makeGroupReq(InetAddress gr_group, - NetworkInterface networkInterface) { - int gr_interface = (networkInterface != null) ? networkInterface.getIndex() : 0; - return new StructGroupReq(gr_interface, gr_group); - } - - /** - * Membership information associated with an {@link Id}. A membership can be one of two types: - * "source-specific" and "any-source". The two types a mutually exclusive for a given Id. - */ - static final class Membership { - - private final DatagramChannelImpl channel; - private final InetAddress groupAddress; - private final NetworkInterface networkInterface; - - // Any-source membership key. Mutually exclusive with sourceSpecificMembershipKeys. - private final MembershipKeyImpl anySourceMembershipKey; - // Blocked source addresses for any-source memberships. Assigned when required. - private Set<InetAddress> blockedSourceAddresses; - - // Source-specific membership keys. Mutually exclusive with anySourceMembershipKey. - private final Map<InetAddress, MembershipKeyImpl> sourceSpecificMembershipKeys; - - /** Use {@link #createSourceSpecific} or {@link #createAnySource} to construct. */ - private Membership( - DatagramChannelImpl channel, - InetAddress groupAddress, - NetworkInterface networkInterface, - MembershipKeyImpl anySourceMembershipKey, - Map<InetAddress, MembershipKeyImpl> sourceSpecificMembershipKeys) { - - this.channel = channel; - this.groupAddress = groupAddress; - this.networkInterface = networkInterface; - this.anySourceMembershipKey = anySourceMembershipKey; - this.sourceSpecificMembershipKeys = sourceSpecificMembershipKeys; - } - - /** Creates an any-source membership. */ - public static Membership createAnySource(DatagramChannelImpl channel, - NetworkInterface networkInterface, InetAddress groupAddress) { - - MembershipKeyImpl withoutSourceAddressKey = - new MembershipKeyImpl(channel, networkInterface, groupAddress, null /* sourceAddress */); - return new Membership( - channel, groupAddress, networkInterface, withoutSourceAddressKey, - null /* sourceSpecificMembershipKeys */); - } - - /** - * Creates a source-specific membership. See {@link #addSource} to add additional source - * addresses. - */ - public static Membership createSourceSpecific(DatagramChannelImpl channel, - NetworkInterface networkInterface, InetAddress groupAddress, InetAddress sourceAddress) { - - Map<InetAddress, MembershipKeyImpl> withSourceKeys = - new HashMap<InetAddress, MembershipKeyImpl>(); - Membership membership = new Membership( - channel, groupAddress, networkInterface, null /* anySourceMembershipKey */, - withSourceKeys); - membership.addSource(sourceAddress); - return membership; - } - - /** - * Adds a new source address filter to an existing membership, returning the associated - * {@link MembershipKeyImpl}. Throws an {@code IllegalStateException} if this is an - * any-source membership. - */ - public MembershipKeyImpl addSource(InetAddress sourceAddress) { - if (sourceSpecificMembershipKeys == null) { - throw new IllegalStateException( - "Can only add sources to source-specific memberships: " + debugId()); - } - - MembershipKeyImpl membershipKey = - new MembershipKeyImpl(channel, networkInterface, groupAddress, sourceAddress); - sourceSpecificMembershipKeys.put(sourceAddress, membershipKey); - return membershipKey; - } - - /** - * Removes the specified {@code sourceAddress} from the set of filters. Returns {@code true} if - * the set of filters is now empty. Throws an {@code IllegalStateException} if this is an - * any-source membership. - */ - public boolean removeSource(InetAddress sourceAddress) { - if (sourceSpecificMembershipKeys == null) { - throw new IllegalStateException( - "Can only remove sources from source-specific memberships: " + debugId()); - } - sourceSpecificMembershipKeys.remove(sourceAddress); - return sourceSpecificMembershipKeys.isEmpty(); - } - - /** - * Returns {@code true} if the membership source-specific, false if it is any-source. - */ - public boolean isSourceSpecific() { - return sourceSpecificMembershipKeys != null; - } - - /** - * Returns the {@link MembershipKeyImpl} for this membership. Throws an - * {@code IllegalStateException} if this is not an any-source membership. - */ - public MembershipKeyImpl getAnySourceMembershipKey() { - if (sourceSpecificMembershipKeys != null) { - throw new IllegalStateException( - "There an existing source-specific membership for " + debugId()); - } - return anySourceMembershipKey; - } - - /** - * Returns the {@link MembershipKeyImpl} for the specified {@code sourceAddress}. Throws an - * {@code IllegalStateException} if this is not a source-specific membership. - */ - public MembershipKeyImpl getSourceSpecificMembershipKey(InetAddress sourceAddress) { - if (anySourceMembershipKey != null) { - throw new IllegalStateException("There an existing any-source membership for " + debugId()); - } - return sourceSpecificMembershipKeys.get(sourceAddress); - } - - /** - * Returns {@code true} if there is an existing block for the specified address. Throws an - * {@code IllegalStateException} if this is not an any-source membership. - */ - public boolean isBlocked(InetAddress sourceAddress) { - if (anySourceMembershipKey == null) { - throw new IllegalStateException( - "block()/unblock() are only applicable for any-source memberships: " + debugId()); - } - return blockedSourceAddresses != null && blockedSourceAddresses.contains(sourceAddress); - } - - /** - * Adds a blocked address to this membership. Throws an {@code IllegalStateException} if - * the address is already blocked. Throws an {@code IllegalStateException} if this is not an - * any-source membership. - */ - public void block(InetAddress sourceAddress) { - if (anySourceMembershipKey == null) { - throw new IllegalStateException( - "block() is not supported for source-specific group memberships: " + debugId()); - } - if (blockedSourceAddresses == null) { - blockedSourceAddresses = new HashSet<InetAddress>(); - } - if (!blockedSourceAddresses.add(sourceAddress)) { - throw new IllegalStateException( - "Could not block " + sourceAddress + ": it was already blocked for " + debugId()); - } - } - - /** - * Removes a blocked address from this membership. Throws an {@code IllegalStateException} if - * the address is not blocked. Throws an {@code IllegalStateException} if this is not an - * any-source membership. - */ - public void unblock(InetAddress sourceAddress) { - if (anySourceMembershipKey == null) { - throw new IllegalStateException( - "unblock() is not supported for source-specific group memberships: " + debugId()); - } - if (blockedSourceAddresses == null || !blockedSourceAddresses.remove(sourceAddress)) { - throw new IllegalStateException( - "Could not unblock " + sourceAddress + ": it was not blocked for " + debugId()); - } - } - - public String debugId() { - return "<" + networkInterface + ":" + groupAddress + ">"; - } - - } - - /** An identifier for a multicast group membership, independent of membership type. */ - private static final class Id { - - private final InetAddress groupAddress; - private final NetworkInterface networkInterface; - - public Id(NetworkInterface networkInterface, InetAddress groupAddress) { - this.groupAddress = groupAddress; - this.networkInterface = networkInterface; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof Id)) { - return false; - } - - Id id = (Id) o; - - if (!groupAddress.equals(id.groupAddress)) { - return false; - } - if (!networkInterface.equals(id.networkInterface)) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - int result = groupAddress.hashCode(); - result = 31 * result + networkInterface.hashCode(); - return result; - } - } -} diff --git a/luni/src/main/java/java/nio/NioUtils.java b/luni/src/main/java/java/nio/NioUtils.java index 51adddb..f2a0b10 100644 --- a/luni/src/main/java/java/nio/NioUtils.java +++ b/luni/src/main/java/java/nio/NioUtils.java @@ -19,8 +19,6 @@ package java.nio; import java.io.Closeable; import java.io.FileDescriptor; import java.io.IOException; -import java.net.SocketOption; -import java.net.StandardSocketOptions; import java.nio.channels.ClosedChannelException; import java.nio.channels.FileChannel; import java.util.Set; @@ -68,66 +66,4 @@ public final class NioUtils { public static int unsafeArrayOffset(ByteBuffer b) { return ((ByteArrayBuffer) b).arrayOffset; } - - /** - * Sets the supplied option on the channel to have the value if option is a member of - * allowedOptions. - * - * @throws IOException - * if the value could not be set due to IO errors. - * @throws IllegalArgumentException - * if the socket option or the value is invalid. - * @throws UnsupportedOperationException - * if the option is not a member of allowedOptions. - * @throws ClosedChannelException - * if the channel is closed - */ - public static <T> void setSocketOption( - FileDescriptorChannel channel, Set<SocketOption<?>> allowedOptions, - SocketOption<T> option, T value) - throws IOException { - - if (!(option instanceof StandardSocketOptions.SocketOptionImpl)) { - throw new IllegalArgumentException("SocketOption must come from StandardSocketOptions"); - } - if (!allowedOptions.contains(option)) { - throw new UnsupportedOperationException( - option + " is not supported for this type of socket"); - } - if (!channel.getFD().valid()) { - throw new ClosedChannelException(); - } - ((StandardSocketOptions.SocketOptionImpl<T>) option).setValue(channel.getFD(), value); - } - - /** - * Gets the supplied option from the channel if option is a member of allowedOptions. - * - * @throws IOException - * if the value could not be read due to IO errors. - * @throws IllegalArgumentException - * if the socket option is invalid. - * @throws UnsupportedOperationException - * if the option is not a member of allowedOptions. - * @throws ClosedChannelException - * if the channel is closed - */ - public static <T> T getSocketOption( - FileDescriptorChannel channel, Set<SocketOption<?>> allowedOptions, - SocketOption<T> option) - throws IOException { - - if (!(option instanceof StandardSocketOptions.SocketOptionImpl)) { - throw new IllegalArgumentException("SocketOption must come from StandardSocketOptions"); - } - if (!allowedOptions.contains(option)) { - throw new UnsupportedOperationException( - option + " is not supported for this type of socket"); - } - if (!channel.getFD().valid()) { - throw new ClosedChannelException(); - } - return ((StandardSocketOptions.SocketOptionImpl<T>) option).getValue(channel.getFD()); - } - } diff --git a/luni/src/main/java/java/nio/ServerSocketChannelImpl.java b/luni/src/main/java/java/nio/ServerSocketChannelImpl.java index 1b84797..ae33672 100644 --- a/luni/src/main/java/java/nio/ServerSocketChannelImpl.java +++ b/luni/src/main/java/java/nio/ServerSocketChannelImpl.java @@ -24,10 +24,7 @@ import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketAddress; -import java.net.SocketOption; import java.net.SocketTimeoutException; -import java.net.StandardSocketOptions; -import java.nio.channels.AlreadyBoundException; import java.nio.channels.ClosedChannelException; import java.nio.channels.IllegalBlockingModeException; import java.nio.channels.NotYetBoundException; @@ -59,52 +56,6 @@ final class ServerSocketChannelImpl extends ServerSocketChannel implements FileD } @Override - public final ServerSocketChannel bind(SocketAddress localAddr, int backlog) throws IOException { - if (!isOpen()) { - throw new ClosedChannelException(); - } - if (socket.isBound()) { - throw new AlreadyBoundException(); - } - if (localAddr != null) { - if (!(localAddr instanceof InetSocketAddress)) { - throw new UnsupportedAddressTypeException(); - } - InetSocketAddress localInetAddress = (InetSocketAddress) localAddr; - if (localInetAddress.isUnresolved()) { - throw new UnresolvedAddressException(); - } - } - - socket.bind(localAddr, backlog); - return this; - } - - @Override - public SocketAddress getLocalAddress() throws IOException { - if (!isOpen()) { - throw new ClosedChannelException(); - } - return socket.getLocalSocketAddress(); - } - - @Override - public <T> T getOption(SocketOption<T> option) throws IOException { - return NioUtils.getSocketOption(this, StandardSocketOptions.SERVER_SOCKET_OPTIONS, option); - } - - @Override - public <T> ServerSocketChannel setOption(SocketOption<T> option, T value) throws IOException { - NioUtils.setSocketOption(this, StandardSocketOptions.SERVER_SOCKET_OPTIONS, option, value); - return this; - } - - @Override - public Set<SocketOption<?>> supportedOptions() { - return StandardSocketOptions.SERVER_SOCKET_OPTIONS; - } - - @Override public SocketChannel accept() throws IOException { if (!isOpen()) { throw new ClosedChannelException(); diff --git a/luni/src/main/java/java/nio/SocketChannelImpl.java b/luni/src/main/java/java/nio/SocketChannelImpl.java index 9064f29..d5cb716 100644 --- a/luni/src/main/java/java/nio/SocketChannelImpl.java +++ b/luni/src/main/java/java/nio/SocketChannelImpl.java @@ -32,10 +32,7 @@ import java.net.PlainSocketImpl; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketException; -import java.net.SocketOption; import java.net.SocketUtils; -import java.net.StandardSocketOptions; -import java.nio.channels.AlreadyBoundException; import java.nio.channels.AlreadyConnectedException; import java.nio.channels.ClosedChannelException; import java.nio.channels.ConnectionPendingException; @@ -139,30 +136,6 @@ class SocketChannelImpl extends SocketChannel implements FileDescriptorChannel { return socket; } - @Override - synchronized public final SocketChannel bind(SocketAddress local) throws IOException { - if (!isOpen()) { - throw new ClosedChannelException(); - } - if (isBound) { - throw new AlreadyBoundException(); - } - - if (local == null) { - local = new InetSocketAddress(Inet4Address.ANY, 0); - } else if (!(local instanceof InetSocketAddress)) { - throw new UnsupportedAddressTypeException(); - } - - InetSocketAddress localAddress = (InetSocketAddress) local; - if (localAddress.isUnresolved()) { - throw new UnresolvedAddressException(); - } - IoBridge.bind(fd, localAddress.getAddress(), localAddress.getPort()); - onBind(true /* updateSocketState */); - return this; - } - /** * Initialise the isBound, localAddress and localPort state from the file descriptor. Used when * some or all of the bound state has been left to the OS to decide, or when the Socket handled @@ -189,30 +162,6 @@ class SocketChannelImpl extends SocketChannel implements FileDescriptorChannel { } @Override - synchronized public SocketAddress getLocalAddress() throws IOException { - if (!isOpen()) { - throw new ClosedChannelException(); - } - return isBound ? new InetSocketAddress(localAddress, localPort) : null; - } - - @Override - public <T> T getOption(SocketOption<T> option) throws IOException { - return NioUtils.getSocketOption(this, StandardSocketOptions.SOCKET_OPTIONS, option); - } - - @Override - public <T> SocketChannel setOption(SocketOption<T> option, T value) throws IOException { - NioUtils.setSocketOption(this, StandardSocketOptions.SOCKET_OPTIONS, option, value); - return this; - } - - @Override - public Set<SocketOption<?>> supportedOptions() { - return StandardSocketOptions.SOCKET_OPTIONS; - } - - @Override synchronized public boolean isConnected() { return status == SOCKET_STATUS_CONNECTED; } diff --git a/luni/src/main/java/java/nio/channels/AlreadyBoundException.java b/luni/src/main/java/java/nio/channels/AlreadyBoundException.java deleted file mode 100644 index e76b574..0000000 --- a/luni/src/main/java/java/nio/channels/AlreadyBoundException.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.nio.channels; - -/** - * An {@code AlreadyBoundException} is thrown when an attempt is made to bind a NetworkChannel that - * is already bound. - * - * @since 1.7 - * @hide 1.7 - */ -public class AlreadyBoundException extends IllegalStateException { - - private static final long serialVersionUID = 6796072983322737592L; - - public AlreadyBoundException() { - } -} diff --git a/luni/src/main/java/java/nio/channels/DatagramChannel.java b/luni/src/main/java/java/nio/channels/DatagramChannel.java index 7278655..2cff7f0 100644 --- a/luni/src/main/java/java/nio/channels/DatagramChannel.java +++ b/luni/src/main/java/java/nio/channels/DatagramChannel.java @@ -22,7 +22,6 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketAddress; -import java.net.SocketOption; import java.nio.ByteBuffer; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; @@ -44,7 +43,7 @@ import java.util.Set; * same time. */ public abstract class DatagramChannel extends AbstractSelectableChannel - implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, MulticastChannel { + implements ByteChannel, ScatteringByteChannel, GatheringByteChannel { /** * Constructs a new {@code DatagramChannel}. @@ -93,85 +92,6 @@ public abstract class DatagramChannel extends AbstractSelectableChannel public abstract DatagramSocket socket(); /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public DatagramChannel bind(SocketAddress local) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public SocketAddress getLocalAddress() throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public <T> T getOption(SocketOption<T> option) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public <T> DatagramChannel setOption(SocketOption<T> option, T value) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public Set<SocketOption<?>> supportedOptions() { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public MembershipKey join(InetAddress groupAddress, NetworkInterface networkInterface) - throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public MembershipKey join(InetAddress groupAddress, NetworkInterface networkInterface, - InetAddress sourceAddress) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** * Returns whether this channel's socket is connected or not. * * @return <code>true</code> if this channel's socket is connected; diff --git a/luni/src/main/java/java/nio/channels/FileChannel.java b/luni/src/main/java/java/nio/channels/FileChannel.java index 59dc26e..d6c140b 100644 --- a/luni/src/main/java/java/nio/channels/FileChannel.java +++ b/luni/src/main/java/java/nio/channels/FileChannel.java @@ -77,7 +77,7 @@ import java.nio.channels.spi.AbstractInterruptibleChannel; * content, size, etc. */ public abstract class FileChannel extends AbstractInterruptibleChannel - implements ByteChannel, GatheringByteChannel, ScatteringByteChannel, SeekableByteChannel { + implements ByteChannel, GatheringByteChannel, ScatteringByteChannel { /** * {@code MapMode} defines file mapping mode constants. @@ -281,21 +281,65 @@ public abstract class FileChannel extends AbstractInterruptibleChannel long position, long size) throws IOException; /** - * {@inheritDoc} + * Returns the current position as a positive integer number of bytes from + * the start of the file. + * + * @throws ClosedChannelException + * if this channel is closed. + * @throws IOException + * if another I/O error occurs. */ - @Override public abstract long position() throws IOException; /** - * {@inheritDoc} + * Sets the file position pointer to a new value. + * <p> + * The argument is the number of bytes counted from the start of the file. + * The position cannot be set to a value that is negative. The new position + * can be set beyond the current file size. If set beyond the current file + * size, attempts to read will return end of file. Write operations will + * succeed but they will fill the bytes between the current end of file and + * the new position with the required number of (unspecified) byte values. + * + * @return this. + * @throws IllegalArgumentException + * if the new position is negative. + * @throws ClosedChannelException + * if this channel is closed. + * @throws IOException + * if another I/O error occurs. */ - @Override public abstract FileChannel position(long newPosition) throws IOException; /** - * {@inheritDoc} + * Reads bytes from this file channel into the given buffer. + * <p> + * The maximum number of bytes that will be read is the remaining number of + * bytes in the buffer when the method is invoked. The bytes will be copied + * into the buffer starting at the buffer's current position. + * <p> + * The call may block if other threads are also attempting to read from this + * channel. + * <p> + * Upon completion, the buffer's position is set to the end of the bytes + * that have been read. The buffer's limit is not changed. + * + * @param buffer + * the byte buffer to receive the bytes. + * @return the number of bytes actually read. + * @throws AsynchronousCloseException + * if another thread closes the channel during the read. + * @throws ClosedByInterruptException + * if another thread interrupts the calling thread during the + * read. + * @throws ClosedChannelException + * if this channel is closed. + * @throws IOException + * if another I/O error occurs, details are in the message. + * @throws NonReadableChannelException + * if the channel has not been opened in a mode that permits + * reading. */ - @Override public abstract int read(ByteBuffer buffer) throws IOException; /** @@ -410,9 +454,13 @@ public abstract class FileChannel extends AbstractInterruptibleChannel throws IOException; /** - * {@inheritDoc} + * Returns the size of the file underlying this channel in bytes. + * + * @throws ClosedChannelException + * if this channel is closed. + * @throws IOException + * if an I/O error occurs while getting the size of the file. */ - @Override public abstract long size() throws IOException; /** @@ -490,9 +538,25 @@ public abstract class FileChannel extends AbstractInterruptibleChannel WritableByteChannel target) throws IOException; /** - * {@inheritDoc} + * Truncates the file underlying this channel to a given size. Any bytes + * beyond the given size are removed from the file. If there are no bytes + * beyond the given size then the file contents are unmodified. + * <p> + * If the file position is currently greater than the given size, then it is + * set to the new size. + * + * @param size + * the maximum size of the underlying file. + * @throws IllegalArgumentException + * if the requested size is negative. + * @throws ClosedChannelException + * if this channel is closed. + * @throws NonWritableChannelException + * if the channel cannot be written to. + * @throws IOException + * if another I/O error occurs. + * @return this channel. */ - @Override public abstract FileChannel truncate(long size) throws IOException; /** @@ -553,9 +617,30 @@ public abstract class FileChannel extends AbstractInterruptibleChannel throws IOException; /** - * {@inheritDoc} + * Writes bytes from the given byte buffer to this file channel. + * <p> + * The bytes are written starting at the current file position, and after + * some number of bytes are written (up to the remaining number of bytes in + * the buffer) the file position is increased by the number of bytes + * actually written. + * + * @param src + * the byte buffer containing the bytes to be written. + * @return the number of bytes actually written. + * @throws NonWritableChannelException + * if the channel was not opened for writing. + * @throws ClosedChannelException + * if the channel was already closed. + * @throws AsynchronousCloseException + * if another thread closes the channel during the write. + * @throws ClosedByInterruptException + * if another thread interrupts the calling thread while this + * operation is in progress. The interrupt state of the calling + * thread is set and the channel is closed. + * @throws IOException + * if another I/O error occurs, details are in the message. + * @see java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer) */ - @Override public abstract int write(ByteBuffer src) throws IOException; /** diff --git a/luni/src/main/java/java/nio/channels/FileLock.java b/luni/src/main/java/java/nio/channels/FileLock.java index 986527c..5b26475 100644 --- a/luni/src/main/java/java/nio/channels/FileLock.java +++ b/luni/src/main/java/java/nio/channels/FileLock.java @@ -114,17 +114,6 @@ public abstract class FileLock implements AutoCloseable { } /** - * Returns the {@link Channel} that holds this lock. - * - * @since 1.7 - * @hide 1.7 - */ - // Add "This method has been superseded by {@link #acquiredBy()}." to channel when unhiding. - public Channel acquiredBy() { - return channel; - } - - /** * Returns the lock's starting position in the file. * * @return the lock position. diff --git a/luni/src/main/java/java/nio/channels/MembershipKey.java b/luni/src/main/java/java/nio/channels/MembershipKey.java deleted file mode 100644 index ed8caad..0000000 --- a/luni/src/main/java/java/nio/channels/MembershipKey.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.nio.channels; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.NetworkInterface; - -/** - * A token produced as the result of joining a multicast group with - * {@link DatagramChannel#join(java.net.InetAddress, java.net.NetworkInterface)} or - * {@link DatagramChannel#join(java.net.InetAddress, java.net.NetworkInterface, - * java.net.InetAddress)}. - * - * <p>A multicast group membership can be source-specific or any-source. Source-specific memberships - * only allow datagrams from a single source address to be received. Any-source memberships - * initially allow datagrams from any source address to be received, but may have individual unicast - * IP addresses blocked via {@link #block(java.net.InetAddress)}. Any-source membership keys return - * {@code null} from {@link #sourceAddress()}. - * - * <p>See <a href="http://tools.ietf.org/html/rfc3678">RFC 3678: Socket Interface Extensions for - * Multicast Source Filters</a> for concepts and terminology associated with multicast membership. - * - * @since 1.7 - * @hide 1.7 - */ -public abstract class MembershipKey { - - protected MembershipKey() {} - - /** - * Returns {@code true} until the membership is dropped with {@link #drop()} or the associated - * channel is closed. - */ - public abstract boolean isValid(); - - /** - * Drops this membership from the multicast group, invalidating this key. - */ - public abstract void drop(); - - /** - * Blocks datagrams from the specified source address; after this call any datagrams received from - * the address will be discarded. Blocking an already-blocked source address has no effect. A - * blocked address can be unblocked by calling {@link #unblock(java.net.InetAddress)}. - * - * <p>The block may not take effect instantaneously: datagrams that are already buffered by the - * underlying OS may still be delivered. - * - * <p>There is an OS-level limit on the number of source addresses that can be block for a given - * {@code groupAddress}, {@code networkInterface} pair. This is typically 10. Attempts to add - * more than this result in a {@code SocketException}. - * - * <p>If this membership key is source-specific an {@link IllegalStateException} is thrown. - * - * @throws IllegalStateException - * if this membership key is no longer valid or is of the wrong type - * @throws IllegalArgumentException - * if the source address is not unicast address of the same type as the multicast group - * address supplied when the group was joined - * @throws IOException - * if an I/O error occurs. - */ - public abstract MembershipKey block(InetAddress source) throws IOException; - - /** - * Unblocks datagrams from the specified source address that were previously blocked with a call - * to {@link #block(java.net.InetAddress)}; after this call any datagrams received from the - * address will be received. Unblocking an address that is not currently blocked throws an - * {@code IllegalStateException}. - * - * <p>If this membership key is source-specific an {@link IllegalStateException} is thrown. - * - * @throws IllegalStateException - * if this membership key is no longer valid or is of the wrong type, or the address is - * not currently blocked - * @throws IllegalArgumentException - * if the source address is not unicast address of the same type as the multicast group - * address supplied when the group was joined - */ - public abstract MembershipKey unblock(InetAddress source); - - /** - * Returns the {@code MulticastChannel} associated with this key. Continues returning the value - * even when the key has been invalidated. - */ - public abstract MulticastChannel channel(); - - /** - * Returns the multicast group address associated with this key. Continues returning the value - * even when the key has been invalidated. - */ - public abstract InetAddress group(); - - /** - * Returns the network interface associated with this key. Continues returning the value - * even when the key has been invalidated. - */ - public abstract NetworkInterface networkInterface(); - - /** - * Returns the source address associated with this key if the membership is source-specific. - * Returns {@code null} if the membership is any-source. Continues returning the value - * even when the key has been invalidated. - */ - public abstract InetAddress sourceAddress(); -} diff --git a/luni/src/main/java/java/nio/channels/MulticastChannel.java b/luni/src/main/java/java/nio/channels/MulticastChannel.java deleted file mode 100644 index b8e6c8b..0000000 --- a/luni/src/main/java/java/nio/channels/MulticastChannel.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.nio.channels; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.NetworkInterface; - -/** - * A type of {@link NetworkChannel} that supports IP multicasting. IP multicasting allows for - * efficient routing of an IP datagram to multiple hosts. Hosts wishing to receive multicast - * datagrams join a multicast group identified by a multicast IP address. - * - * <p>Any datagram socket can be used to <em>send</em> to a multicast group: senders <em>do not</em> - * have to be a member of the group. - * - * <p>See <a href="http://www.ietf.org/rfc/rfc2236.txt">RFC 2236: Internet Group Management - * Protocol, Version 2</a> and <a href="http://www.ietf.org/rfc/rfc3376.txt">RFC 3376: Internet - * Group Management Protocol, Version 3</a> for network-level information regarding IPv4 group - * membership. See <a href="http://www.ietf.org/rfc/rfc2710.txt">RFC 2710: Multicast Listener - * Discovery (MLD) for IPv6</a> and <a href="http://www.ietf.org/rfc/rfc3810.txt">RFC 3810: - * Multicast Listener Discovery Version 2 (MLDv2) for IPv6</a> for equivalent IPv6 information. - * - * <p>See <a href="http://tools.ietf.org/html/rfc3678">RFC 3678: Socket Interface Extensions for - * Multicast Source Filters</a> for concepts and terminology associated with multicast membership. - * - * <p>IP multicast requires support from network infrastructure; networks may not support - * all features of IP multicast. - * - * <p>A channel can be restricted to send multicast datagrams through a specific - * {@link NetworkInterface} by using {@link #setOption(java.net.SocketOption, Object)} with - * {@link java.net.StandardSocketOptions#IP_MULTICAST_IF}. - * - * <p>A channel may or may not receive multicast datagrams sent from this host, determined by the - * {@link java.net.StandardSocketOptions#IP_MULTICAST_LOOP} option. - * - * <p>The time-to-live for multicast datagrams can be set using the - * {@link java.net.StandardSocketOptions#IP_MULTICAST_TTL} option. - * - * <p>Usually multicast channels should have {@link java.net.StandardSocketOptions#SO_REUSEADDR} - * set to {@code true} before binding to enable multiple sockets on this host to be members of - * the same multicast group. - * - * <p>Typically multicast channels are {@link NetworkChannel#bind bound} to a wildcard address - * such as "0.0.0.0" (IPv4) or "::" (IPv6). They may also be bound to a multicast group address. - * Binding to a multicast group address restricts datagrams received to only those sent to the - * multicast group. When the wildcard address is used the socket may join multiple groups and also - * receive non-multicast datagrams sent directly to the host. The port the channel is bound to is - * important: only datagrams sent to the group on that port will be received. - * - * <p>Having bound a channel, the group can be joined. Memberships are either "any-source" or - * "source-specific". The type of membership is determined by the variant of {@code join} that is - * used. See {@link #join(java.net.InetAddress, java.net.NetworkInterface)} and - * {@link #join(java.net.InetAddress, java.net.NetworkInterface, java.net.InetAddress)} for more - * information. - * - * @since 1.7 - * @hide 1.7 - */ -public interface MulticastChannel extends NetworkChannel { - - /** - * {@inheritDoc} - * - * If the channel is currently part of one or more multicast groups then the memberships are - * dropped and any associated {@code MembershipKey} objects are invalidated. - */ - void close() throws IOException; - - /** - * Creates an any-source membership to the {@code groupAddress} on the specified - * {@code networkInterface}. Returns a {@code MembershipKey} that can be used to modify or - * {@link MembershipKey#drop()} the membership. See {@link MembershipKey#block(InetAddress)} and - * {@link MembershipKey#unblock(InetAddress)} for methods to modify source-address - * filtering. - * - * <p>A channel may join several groups. Each membership is network interface-specific: an - * application must join the group for each applicable network interface to receive datagrams. - * - * <p>Any-source and source-specific memberships cannot be mixed for a group address on a given - * network interface. An {@code IllegalStateException} will be thrown if joins of different types - * are attempted for a given {@code groupAddress}, {@code networkInterface} pair. Joining a - * multicast group with the same arguments as an existing, valid membership returns the same - * {@code MembershipKey}. - * - * <p>There is an OS-level limit on the number of multicast groups a process can join. - * This is typically 20. Attempts to join more than this result in a {@code SocketException}. - * - * @param groupAddress the multicast group address to join - * @param networkInterface the network address to join with - * @throws IllegalArgumentException - * if the group address is not a multicast address or the network interface does not - * support multicast - * @throws IllegalStateException - * if the channel already has a source-specific membership for the group/network interface - * @throws ClosedChannelException - * if the channel is closed - * @throws IOException - * if some other I/O error occurs - * @since 1.7 - * @hide 1.7 - */ - MembershipKey join(InetAddress groupAddress, NetworkInterface networkInterface) - throws IOException; - - /** - * Creates a source-specific membership to the {@code groupAddress} on the specified - * {@code networkInterface} filtered by the {@code sourceAddress}. Returns a - * {@code MembershipKey} that can be used to {@link MembershipKey#drop()} the membership. - * - * <p>A channel may join several groups. Each membership is network interface-specific: an - * application must join the group for each applicable network interface to receive datagrams. - * - * <p>Any-source and source-specific memberships cannot be mixed for a group address on a given - * network interface. An {@code IllegalStateException} will be thrown if joins of different types - * are attempted for a given {@code groupAddress}, {@code networkInterface} pair. Joining a - * multicast group with the same arguments as an existing, valid membership returns the same - * {@code MembershipKey}. - * - * <p>There is an OS-level limit on the number of multicast groups a process can join. - * This is typically 20. Attempts to join more than this result in a {@code SocketException}. - * - * <p>There is an OS-level limit on the number of source addresses that can be joined for a given - * {@code groupAddress}, {@code networkInterface} pair. This is typically 10. Attempts to add - * more than this result in a {@code SocketException}. - * - * @param groupAddress the multicast group address to join - * @param networkInterface the network address to join with - * @param sourceAddress the source address to restrict datagrams to - * @throws IllegalArgumentException - * if the group address is not a multicast address, the network interface does not - * support multicast, or the {@code groupAddress} and {@code sourceAddress} are not of - * compatible types - * @throws IllegalStateException - * if the channel already has a source-specific membership for the group/network interface - * @throws ClosedChannelException - * if the channel is closed - * @throws IOException - * if some other I/O error occurs - * @since 1.7 - * @hide 1.7 - */ - MembershipKey join( - InetAddress groupAddress, NetworkInterface networkInterface, InetAddress sourceAddress) - throws IOException; - -} diff --git a/luni/src/main/java/java/nio/channels/NetworkChannel.java b/luni/src/main/java/java/nio/channels/NetworkChannel.java deleted file mode 100644 index 31b81d5..0000000 --- a/luni/src/main/java/java/nio/channels/NetworkChannel.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.nio.channels; - -import java.io.Closeable; -import java.io.IOException; -import java.net.SocketAddress; -import java.net.SocketOption; -import java.util.Set; - -/** - * A common interface for channels that are backed by network sockets. - * - * @since 1.7 - * @hide 1.7 - */ -public interface NetworkChannel extends AutoCloseable, Channel, Closeable { - - /** - * Binds this channel to the given local socket address. If the {@code localAddr} is set - * to {@code null} the socket will be bound to an available local address on any free port of - * the system. - * - * @param local - * the local machine address and port to bind on. - * @return this channel. - * @throws UnsupportedAddressTypeException - * if the {@code SocketAddress} is not supported. - * @throws ClosedChannelException - * if the channel is closed. - * @throws AlreadyBoundException - * if the channel is already bound. - * @throws IOException - * if another I/O error occurs. - * @hide 1.7 - */ - NetworkChannel bind(SocketAddress local) throws IOException; - - /** - * Returns the local socket address the channel is bound to. The socket may be bound explicitly - * with {@code bind} or as a side-effect when other methods are called, depending on the - * implementation. If the channel is not bound {@code null} is returned. - * - * <p>If IP is being used, the returned object will be a subclass of - * {@link java.net.InetSocketAddress} - * - * @return the local socket address, or {@code null} if the socket is not bound - * @throws ClosedChannelException - * if the channel is closed. - * @throws IOException - * if another I/O error occurs. - * @hide 1.7 - */ - SocketAddress getLocalAddress() throws IOException; - - /** - * Returns the value for the socket option. - * - * @throws UnsupportedOperationException - * if the option is not supported by the socket. - * @throws java.nio.channels.ClosedChannelException - * if the socket is closed - * @throws IOException - * if the value cannot be read. - * @see java.net.StandardSocketOptions - * @hide 1.7 - */ - <T> T getOption(SocketOption<T> option) throws IOException; - - /** - * Sets the value for the socket option. - * - * @return this NetworkChannel - * @throws UnsupportedOperationException - * if the option is not supported by the socket. - * @throws IllegalArgumentException - * if the value is not valid for the option. - * @throws java.nio.channels.ClosedChannelException - * if the socket is closed - * @throws IOException - * if the value cannot be written. - * @see java.net.StandardSocketOptions - * @hide 1.7 - */ - <T> NetworkChannel setOption(SocketOption<T> option, T value) throws IOException; - - /** - * Returns the set of socket options supported by this channel. - * @hide 1.7 - */ - Set<SocketOption<?>> supportedOptions(); -} diff --git a/luni/src/main/java/java/nio/channels/SeekableByteChannel.java b/luni/src/main/java/java/nio/channels/SeekableByteChannel.java deleted file mode 100644 index edd2645..0000000 --- a/luni/src/main/java/java/nio/channels/SeekableByteChannel.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package java.nio.channels; - -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * An interface for channels that keep a pointer to a current position within an underlying - * byte-based data source such as a file. - * - * <p>SeekableByteChannels have a pointer into the underlying data source which is referred to as a - * <em>position</em>. The position can be manipulated by moving it within the data source, and the - * current position can be queried. - * - * <p>SeekableByteChannels also have an associated <em>size</em>. The size of the channel is the - * number of bytes that the data source currently contains. The size of the data source can be - * manipulated by adding more bytes to the end or by removing bytes from the end. See - * {@link #truncate}, {@link #position} and {@link #write} for details. The current size can also - * be queried. - * - * @since 1.7 - * @hide 1.7 - */ -public interface SeekableByteChannel extends ByteChannel { - - /** - * Returns the current position as a positive number of bytes from the start of the underlying - * data source. - * - * @throws ClosedChannelException - * if this channel is closed. - * @throws IOException - * if another I/O error occurs. - */ - long position() throws IOException; - - /** - * Sets the channel's position to {@code newPosition}. - * - * <p>The argument is the number of bytes counted from the start of the data source. The position - * cannot be set to a value that is negative. The new position can be set beyond the current - * size. If set beyond the current size, attempts to read will return end-of-file. Write - * operations will succeed but they will fill the bytes between the current end of the data - * source - * and the new position with the required number of (unspecified) byte values. - * - * @return the channel. - * @throws IllegalArgumentException - * if the new position is negative. - * @throws ClosedChannelException - * if this channel is closed. - * @throws IOException - * if another I/O error occurs. - */ - SeekableByteChannel position(long newPosition) throws IOException; - - /** - * Returns the size of the data source underlying this channel in bytes. - * - * @throws ClosedChannelException - * if this channel is closed. - * @throws IOException - * if an I/O error occurs. - */ - long size() throws IOException; - - /** - * Truncates the data source underlying this channel to a given size. Any bytes beyond the given - * size are removed. If there are no bytes beyond the given size then the contents are - * unmodified. - * - * <p>If the position is currently greater than the given size, then it is set to the new size. - * - * @return this channel. - * @throws IllegalArgumentException - * if the requested size is negative. - * @throws ClosedChannelException - * if this channel is closed. - * @throws NonWritableChannelException - * if the channel cannot be written to. - * @throws IOException - * if another I/O error occurs. - */ - SeekableByteChannel truncate(long size) throws IOException; - - /** - * Writes bytes from the given byte buffer to this channel. - * - * <p>The bytes are written starting at the channel's current position, and after some number of - * bytes are written (up to the {@link java.nio.Buffer#remaining() remaining} number of bytes in - * the buffer) the channel's position is increased by the number of bytes actually written. - * - * <p>If the channel's position is beyond the current end of the underlying data source, then the - * data source is first extended up to the given position by the required number of unspecified - * byte values. - * - * @param buffer - * the byte buffer containing the bytes to be written. - * @return the number of bytes actually written. - * @throws NonWritableChannelException - * if the channel was not opened for writing. - * @throws ClosedChannelException - * if the channel was already closed. - * @throws AsynchronousCloseException - * if another thread closes the channel during the write. - * @throws ClosedByInterruptException - * if another thread interrupts the calling thread while this operation is in progress. The - * interrupt state of the calling thread is set and the channel is closed. - * @throws IOException - * if another I/O error occurs, details are in the message. - */ - @Override - int write(ByteBuffer buffer) throws IOException; - - /** - * Reads bytes from this channel into the given buffer. - * - * <p>If the channels position is beyond the current end of the underlying data source then - * end-of-file (-1) is returned. - * - * <p>The bytes are read starting at the channel's current position, and after some number of - * bytes are read (up to the {@link java.nio.Buffer#remaining() remaining} number of bytes in the - * buffer) the channel's position is increased by the number of bytes actually read. The bytes - * will be read into the buffer starting at the buffer's current - * {@link java.nio.Buffer#position() position}. The buffer's {@link java.nio.Buffer#limit() - * limit} is not changed. - * - * <p>The call may block if other threads are also attempting to read from the same channel. - * - * @param buffer - * the byte buffer to receive the bytes. - * @return the number of bytes actually read, or -1 if the end of the data has been reached - * @throws AsynchronousCloseException - * if another thread closes the channel during the read. - * @throws ClosedByInterruptException - * if another thread interrupts the calling thread while the operation is in progress. The - * interrupt state of the calling thread is set and the channel is closed. - * @throws ClosedChannelException - * if the channel is closed. - * @throws IOException - * another I/O error occurs, details are in the message. - * @throws NonReadableChannelException - * if the channel was not opened for reading. - */ - @Override - int read(ByteBuffer buffer) throws IOException; - -} diff --git a/luni/src/main/java/java/nio/channels/ServerSocketChannel.java b/luni/src/main/java/java/nio/channels/ServerSocketChannel.java index 01adbc7..ef50155 100644 --- a/luni/src/main/java/java/nio/channels/ServerSocketChannel.java +++ b/luni/src/main/java/java/nio/channels/ServerSocketChannel.java @@ -20,7 +20,6 @@ package java.nio.channels; import java.io.IOException; import java.net.ServerSocket; import java.net.SocketAddress; -import java.net.SocketOption; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; import java.util.Set; @@ -37,9 +36,7 @@ import java.util.Set; * {@link NotYetBoundException}. It can be bound by calling the bind method of a * related {@code ServerSocket} instance. */ -public abstract class ServerSocketChannel extends AbstractSelectableChannel - implements NetworkChannel { - +public abstract class ServerSocketChannel extends AbstractSelectableChannel { /** * Constructs a new {@link ServerSocketChannel}. * @@ -86,89 +83,6 @@ public abstract class ServerSocketChannel extends AbstractSelectableChannel public abstract ServerSocket socket(); /** - * {@inheritDoc} - * - * <p>This is equivalent to {@code bind(local, 0)}. - * @since 1.7 - * @hide 1.7 - */ - @Override - public final ServerSocketChannel bind(SocketAddress local) throws IOException { - return bind(local, 0); - } - - /** - * Binds this server channel to the given local socket address. If the {@code localAddr} is set - * to {@code null} the socket will be bound to an available local address on any free port of - * the system. - * - * @param localAddr - * the local machine address and port to bind on. - * @param backlog the maximum number of unaccepted connections. Passing 0 or - * a negative value yields the default backlog of 50. - * @return this {@code ServerSocketChannel}. - * @throws UnsupportedAddressTypeException - * if the {@code SocketAddress} is not supported. - * @throws ClosedChannelException - * if the channel is closed. - * @throws AlreadyBoundException - * if the channel is already bound. - * @throws IOException - * if another I/O error occurs. - * @since 1.7 - * @hide 1.7 - */ - public ServerSocketChannel bind(SocketAddress localAddr, int backlog) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public SocketAddress getLocalAddress() throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public <T> T getOption(SocketOption<T> option) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public <T> ServerSocketChannel setOption(SocketOption<T> option, T value) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public Set<SocketOption<?>> supportedOptions() { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** * Accepts a connection to this server-socket channel. * <p> * This method returns {@code null} when this channel is non-blocking and no diff --git a/luni/src/main/java/java/nio/channels/SocketChannel.java b/luni/src/main/java/java/nio/channels/SocketChannel.java index 9788c99..a91fccd 100644 --- a/luni/src/main/java/java/nio/channels/SocketChannel.java +++ b/luni/src/main/java/java/nio/channels/SocketChannel.java @@ -20,7 +20,6 @@ package java.nio.channels; import java.io.IOException; import java.net.Socket; import java.net.SocketAddress; -import java.net.SocketOption; import java.nio.ByteBuffer; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; @@ -64,7 +63,7 @@ import java.util.Set; * processing, calls to {@link #read} and {@link #write} will block. */ public abstract class SocketChannel extends AbstractSelectableChannel implements - ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel { + ByteChannel, ScatteringByteChannel, GatheringByteChannel { /** * Constructs a new {@code SocketChannel}. @@ -143,61 +142,6 @@ public abstract class SocketChannel extends AbstractSelectableChannel implements public abstract Socket socket(); /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public SocketChannel bind(SocketAddress local) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public SocketAddress getLocalAddress() throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public <T> T getOption(SocketOption<T> option) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public <T> SocketChannel setOption(SocketOption<T> option, T value) throws IOException { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** - * @since 1.7 - * @hide 1.7 - */ - @Override - public Set<SocketOption<?>> supportedOptions() { - // This method was added for interoperability with Java 7, where it is abstract. It is - // concrete here to avoid breaking existing Android applications that extend this class. - throw new UnsupportedOperationException("Subclasses must override this method"); - } - - /** * Indicates whether this channel's socket is connected. * * @return {@code true} if this channel's socket is connected, {@code false} diff --git a/luni/src/main/java/java/util/Currency.java b/luni/src/main/java/java/util/Currency.java index 0dea411..0b7d227 100644 --- a/luni/src/main/java/java/util/Currency.java +++ b/luni/src/main/java/java/util/Currency.java @@ -127,17 +127,6 @@ public final class Currency implements Serializable { } /** - * Returns the ISO 4217 numeric code for this currency. If there is no standard numeric code a - * zero is returned. - * - * @since 1.7 - * @hide 1.7 - */ - public int getNumericCode() { - return ICU.getCurrencyNumericCode(currencyCode); - } - - /** * Equivalent to {@code getSymbol(Locale.getDefault())}. * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>". */ diff --git a/luni/src/main/java/java/util/zip/ZipFile.java b/luni/src/main/java/java/util/zip/ZipFile.java index 9894152..b44156e 100644 --- a/luni/src/main/java/java/util/zip/ZipFile.java +++ b/luni/src/main/java/java/util/zip/ZipFile.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.ByteOrder; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Enumeration; import java.util.Iterator; @@ -98,8 +97,6 @@ public class ZipFile implements Closeable, ZipConstants { private final String filename; - private final Charset charset; - private File fileToDeleteOnClose; private RandomAccessFile raf; @@ -119,23 +116,7 @@ public class ZipFile implements Closeable, ZipConstants { * @throws IOException if an {@code IOException} occurs. */ public ZipFile(File file) throws ZipException, IOException { - this(file, OPEN_READ, StandardCharsets.UTF_8); - } - - /** - * Constructs a new {@code ZipFile} allowing read access to the contents of the given file. - * - * <p>The {@code charset} is used to decode the file comment if one exists. If the character - * encoding for entry names and comments is not explicitly marked as UTF-8 by the zip file - * then {@code charset} is used to decode them. - * - * @throws ZipException if a zip error occurs. - * @throws IOException if an {@code IOException} occurs. - * @since 1.7 - * @hide 1.7 - */ - public ZipFile(File file, Charset charset) throws ZipException, IOException { - this(file, OPEN_READ, charset); + this(file, OPEN_READ); } /** @@ -146,7 +127,7 @@ public class ZipFile implements Closeable, ZipConstants { * @throws IOException if an IOException occurs. */ public ZipFile(String name) throws IOException { - this(new File(name), OPEN_READ, StandardCharsets.UTF_8); + this(new File(name), OPEN_READ); } /** @@ -162,31 +143,7 @@ public class ZipFile implements Closeable, ZipConstants { * @throws IOException if an {@code IOException} occurs. */ public ZipFile(File file, int mode) throws IOException { - this(file, mode, StandardCharsets.UTF_8); - } - - /** - * Constructs a new {@code ZipFile} allowing access to the given file. - * - * <p>The {@code mode} must be either {@code OPEN_READ} or {@code OPEN_READ|OPEN_DELETE}. - * If the {@code OPEN_DELETE} flag is supplied, the file will be deleted at or before the - * time that the {@code ZipFile} is closed (the contents will remain accessible until - * this {@code ZipFile} is closed); it also calls {@code File.deleteOnExit}. - * - * <p>The {@code charset} is used to decode the file comment if one exists. If the character - * encoding for entry names and comments is not explicitly marked as UTF-8 by the zip file - * then {@code charset} is used to decode them. - * - * @throws IOException if an {@code IOException} occurs. - * @since 1.7 - * @hide 1.7 - */ - public ZipFile(File file, int mode, Charset charset) throws IOException { filename = file.getPath(); - if (charset == null) { - throw new NullPointerException("charset == null"); - } - this.charset = charset; if (mode != OPEN_READ && mode != (OPEN_READ | OPEN_DELETE)) { throw new IllegalArgumentException("Bad mode: " + mode); } @@ -467,7 +424,7 @@ public class ZipFile implements Closeable, ZipConstants { if (commentLength > 0) { byte[] commentBytes = new byte[commentLength]; raf.readFully(commentBytes); - comment = new String(commentBytes, 0, commentBytes.length, charset); + comment = new String(commentBytes, 0, commentBytes.length, StandardCharsets.UTF_8); } // Seek to the first CDE and read all entries. @@ -478,7 +435,7 @@ public class ZipFile implements Closeable, ZipConstants { BufferedInputStream bufferedStream = new BufferedInputStream(rafStream, 4096); byte[] hdrBuf = new byte[CENHDR]; // Reuse the same buffer for each entry. for (int i = 0; i < numEntries; ++i) { - ZipEntry newEntry = new ZipEntry(hdrBuf, bufferedStream, charset); + ZipEntry newEntry = new ZipEntry(hdrBuf, bufferedStream, StandardCharsets.UTF_8); if (newEntry.localHeaderRelOffset >= centralDirOffset) { throw new ZipException("Local file header offset is after central directory"); } diff --git a/luni/src/main/java/java/util/zip/ZipInputStream.java b/luni/src/main/java/java/util/zip/ZipInputStream.java index f1a5208..4c0034e 100644 --- a/luni/src/main/java/java/util/zip/ZipInputStream.java +++ b/luni/src/main/java/java/util/zip/ZipInputStream.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; import java.nio.ByteOrder; -import java.nio.charset.Charset; import java.nio.charset.ModifiedUtf8; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -90,35 +89,16 @@ public class ZipInputStream extends InflaterInputStream implements ZipConstants private char[] stringCharBuf = new char[256]; - private final Charset charset; - /** * Constructs a new {@code ZipInputStream} to read zip entries from the given input stream. * * <p>UTF-8 is used to decode all strings in the file. */ public ZipInputStream(InputStream stream) { - this(stream, StandardCharsets.UTF_8); - } - - /** - * Constructs a new {@code ZipInputStream} to read zip entries from the given input stream. - * - * <p>If the character encoding for entry names and comments is not explicitly marked as UTF-8 - * by the zip file then {@code charset} is used to decode them. - * - * @since 1.7 - * @hide 1.7 - */ - public ZipInputStream(InputStream stream, Charset charset) { super(new PushbackInputStream(stream, BUF_SIZE), new Inflater(true)); if (stream == null) { throw new NullPointerException("stream == null"); } - if (charset == null) { - throw new NullPointerException("charset == null"); - } - this.charset = charset; } /** @@ -272,12 +252,7 @@ public class ZipInputStream extends InflaterInputStream implements ZipConstants } int extraLength = peekShort(LOCEXT - LOCVER); - // Determine the character set to use to decode strings. - Charset charset = this.charset; - if ((flags & ZipFile.GPBF_UTF8_FLAG) != 0) { - charset = StandardCharsets.UTF_8; - } - String name = readString(nameLength, charset); + String name = readString(nameLength); currentEntry = createZipEntry(name); currentEntry.time = ceLastModifiedTime; currentEntry.modDate = ceLastModifiedDate; @@ -298,21 +273,17 @@ public class ZipInputStream extends InflaterInputStream implements ZipConstants /** * Reads bytes from the current stream position returning the string representation. */ - private String readString(int byteLength, Charset charset) throws IOException { + private String readString(int byteLength) throws IOException { if (byteLength > stringBytesBuf.length) { stringBytesBuf = new byte[byteLength]; } Streams.readFully(in, stringBytesBuf, 0, byteLength); - if (charset == StandardCharsets.UTF_8) { - // The number of chars will always be less than or equal to the number of bytes. It's - // fine if this buffer is too long. - if (byteLength > stringCharBuf.length) { - stringCharBuf = new char[byteLength]; - } - return ModifiedUtf8.decode(stringBytesBuf, stringCharBuf, 0, byteLength); - } else { - return new String(stringBytesBuf, 0, byteLength, charset); + // The number of chars will always be less than or equal to the number of bytes. It's + // fine if this buffer is too long. + if (byteLength > stringCharBuf.length) { + stringCharBuf = new char[byteLength]; } + return ModifiedUtf8.decode(stringBytesBuf, stringCharBuf, 0, byteLength); } private int peekShort(int offset) { diff --git a/luni/src/main/java/java/util/zip/ZipOutputStream.java b/luni/src/main/java/java/util/zip/ZipOutputStream.java index aeb77e9..8278355 100644 --- a/luni/src/main/java/java/util/zip/ZipOutputStream.java +++ b/luni/src/main/java/java/util/zip/ZipOutputStream.java @@ -20,7 +20,6 @@ package java.util.zip; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashSet; @@ -76,8 +75,6 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant private final HashSet<String> entries = new HashSet<String>(); - private final Charset charset; - private int defaultCompressionMethod = DEFLATED; private int compressionLevel = Deflater.DEFAULT_COMPRESSION; @@ -103,25 +100,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant * <p>UTF-8 will be used to encode the file comment, entry names and comments. */ public ZipOutputStream(OutputStream os) { - this(os, StandardCharsets.UTF_8); - } - - /** - * Constructs a new {@code ZipOutputStream} that writes a zip file to the given - * {@code OutputStream}. - * - * <p>The specified character set will be used to encode the file comment, entry names and - * comments. - * - * @since 1.7 - * @hide 1.7 - */ - public ZipOutputStream(OutputStream os, Charset charset) { super(os, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); - if (charset == null) { - throw new NullPointerException("charset == null"); - } - this.charset = charset; } /** @@ -183,9 +162,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant // Since gingerbread, we always set the UTF-8 flag on individual files if appropriate. // Some tools insist that the central directory have the UTF-8 flag. // http://code.google.com/p/android/issues/detail?id=20214 - if (charset == StandardCharsets.UTF_8) { - flags |= ZipFile.GPBF_UTF8_FLAG; - } + flags |= ZipFile.GPBF_UTF8_FLAG; writeLong(cDir, CENSIG); writeShort(cDir, ZIP_VERSION_2_0); // Version this file was made by. writeShort(cDir, ZIP_VERSION_2_0); // Minimum version needed to extract. @@ -319,11 +296,11 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant // TODO: support Zip64. throw new ZipException("Too many entries for the zip file format's 16-bit entry count"); } - nameBytes = ze.name.getBytes(charset); + nameBytes = ze.name.getBytes(StandardCharsets.UTF_8); checkSizeIsWithinShort("Name", nameBytes); entryCommentBytes = EmptyArray.BYTE; if (ze.comment != null) { - entryCommentBytes = ze.comment.getBytes(charset); + entryCommentBytes = ze.comment.getBytes(StandardCharsets.UTF_8); // The comment is not written out until the entry is finished, but it is validated here // to fail-fast. checkSizeIsWithinShort("Comment", entryCommentBytes); @@ -340,9 +317,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant int flags = (method == STORED) ? 0 : ZipFile.GPBF_DATA_DESCRIPTOR_FLAG; // Java always outputs UTF-8 filenames. (Before Java 7, the RI didn't set this flag and used // modified UTF-8. From Java 7, when using UTF_8 it sets this flag and uses normal UTF-8.) - if (charset == StandardCharsets.UTF_8) { - flags |= ZipFile.GPBF_UTF8_FLAG; - } + flags |= ZipFile.GPBF_UTF8_FLAG; writeLong(out, LOCSIG); // Entry header writeShort(out, ZIP_VERSION_2_0); // Minimum version needed to extract. writeShort(out, flags); @@ -384,7 +359,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant return; } - byte[] newCommentBytes = comment.getBytes(charset); + byte[] newCommentBytes = comment.getBytes(StandardCharsets.UTF_8); checkSizeIsWithinShort("Comment", newCommentBytes); this.commentBytes = newCommentBytes; } @@ -455,8 +430,8 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant private void checkSizeIsWithinShort(String property, byte[] bytes) { if (bytes.length > 0xffff) { - throw new IllegalArgumentException( - property + " too long in " + charset + ":" + bytes.length + " bytes"); + throw new IllegalArgumentException(property + " too long in UTF-8:" + bytes.length + + " bytes"); } } } |