001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.net;
019:
020: import java.net.Proxy;
021: import java.net.ProxySelector;
022: import java.net.URI;
023: import java.security.AccessController;
024: import java.security.PrivilegedAction;
025: import java.util.List;
026:
027: public class NetUtil {
028:
029: /**
030: * Answers whether to use a SOCKS proxy.
031: *
032: * @param proxy java.net.Proxy <code>proxy</code> is used to determine
033: * whether using SOCKS proxy.
034: * @return true if only the type of <code>proxy</code> is
035: * Proxy.Type.SOCKS.
036: */
037: public static boolean usingSocks(Proxy proxy) {
038: if (null != proxy && Proxy.Type.SOCKS == proxy.type()) {
039: return true;
040: }
041: return false;
042: }
043:
044: /**
045: * Answer whether to prefer IPV6 address
046: *
047: * @return boolean
048: */
049: public static boolean preferIPv6Addresses() {
050: final Action a = new Action("java.net.preferIPv6Addresses");//$NON-NLS-1$
051: return AccessController.doPrivileged(a).booleanValue();
052: }
053:
054: /**
055: * Answer whether to prefer IPV4 stack
056: *
057: * @return boolean
058: */
059: public static boolean preferIPv4Stack() {
060: final Action a = new Action("java.net.preferIPv4Stack");//$NON-NLS-1$
061: return AccessController.doPrivileged(a).booleanValue();
062: }
063:
064: /**
065: * Gets proxy list according to the URI by system ProxySelector.
066: *
067: * @param uri
068: * @return a list of proxy for the URI. Returns null if no proxy is
069: * available.
070: */
071: public static List<Proxy> getProxyList(URI uri) {
072: // use system default selector to get proxy list
073: ProxySelector selector = ProxySelector.getDefault();
074: if (null == selector) {
075: return null;
076: }
077: return selector.select(uri);
078: }
079:
080: private static final class Action implements
081: PrivilegedAction<Boolean> {
082: private final String propertyName;
083:
084: Action(String propertyName) {
085: super ();
086: this .propertyName = propertyName;
087: }
088:
089: public Boolean run() {
090: if (Boolean.getBoolean(propertyName)) {
091: return Boolean.TRUE;
092: }
093: return Boolean.FALSE;
094: }
095: }
096:
097: static void intToBytes(int value, byte bytes[], int start) {
098: /*
099: * Shift the int so the current byte is right-most Use a byte mask of
100: * 255 to single out the last byte.
101: */
102: bytes[start] = (byte) ((value >> 24) & 255);
103: bytes[start + 1] = (byte) ((value >> 16) & 255);
104: bytes[start + 2] = (byte) ((value >> 8) & 255);
105: bytes[start + 3] = (byte) (value & 255);
106: }
107:
108: static int bytesToInt(byte bytes[], int start) {
109: /*
110: * First mask the byte with 255, as when a negative signed byte converts
111: * to an integer, it has bits on in the first 3 bytes, we are only
112: * concerned about the right-most 8 bits. Then shift the rightmost byte
113: * to align with its position in the integer.
114: */
115: int value = ((bytes[start + 3] & 255))
116: | ((bytes[start + 2] & 255) << 8)
117: | ((bytes[start + 1] & 255) << 16)
118: | ((bytes[start] & 255) << 24);
119: return value;
120: }
121: }
|