1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.luni.net; 19 20 import java.net.Proxy; 21 import java.net.ProxySelector; 22 import java.net.URI; 23 import java.security.AccessController; 24 import java.security.PrivilegedAction; 25 import java.util.List; 26 27 public class NetUtil { 28 29 /** 30 * Returns whether to use a SOCKS proxy. 31 * 32 * @param proxy java.net.Proxy <code>proxy</code> is used to determine 33 * whether using SOCKS proxy. 34 * @return true if only the type of <code>proxy</code> is 35 * Proxy.Type.SOCKS. 36 */ 37 public static boolean usingSocks(Proxy proxy) { 38 if (null != proxy && Proxy.Type.SOCKS == proxy.type()) { 39 return true; 40 } 41 return false; 42 } 43 44 /** 45 * Answer whether to prefer IPV6 address 46 * 47 * @return boolean 48 */ 49 public static boolean preferIPv6Addresses() { 50 final Action a = new Action("java.net.preferIPv6Addresses");//$NON-NLS-1$ 51 return AccessController.doPrivileged(a).booleanValue(); 52 } 53 54 /** 55 * Answer whether to prefer IPV4 stack 56 * 57 * @return boolean 58 */ 59 public static boolean preferIPv4Stack() { 60 final Action a = new Action("java.net.preferIPv4Stack");//$NON-NLS-1$ 61 return AccessController.doPrivileged(a).booleanValue(); 62 } 63 64 /** 65 * Gets proxy list according to the URI by system ProxySelector. 66 * 67 * @param uri 68 * @return a list of proxy for the URI. Returns null if no proxy is 69 * available. 70 */ 71 public static List<Proxy> getProxyList(URI uri) { 72 // use system default selector to get proxy list 73 ProxySelector selector = ProxySelector.getDefault(); 74 if (null == selector) { 75 return null; 76 } 77 return selector.select(uri); 78 } 79 80 private static final class Action implements PrivilegedAction<Boolean> { 81 private final String propertyName; 82 83 Action(String propertyName) { 84 super(); 85 this.propertyName = propertyName; 86 } 87 88 public Boolean run() { 89 if (Boolean.getBoolean(propertyName)) { 90 return Boolean.TRUE; 91 } 92 return Boolean.FALSE; 93 } 94 } 95 96 static void intToBytes(int value, byte bytes[], int start) { 97 /* 98 * Shift the int so the current byte is right-most Use a byte mask of 99 * 255 to single out the last byte. 100 */ 101 bytes[start] = (byte) ((value >> 24) & 255); 102 bytes[start + 1] = (byte) ((value >> 16) & 255); 103 bytes[start + 2] = (byte) ((value >> 8) & 255); 104 bytes[start + 3] = (byte) (value & 255); 105 } 106 107 static int bytesToInt(byte bytes[], int start) { 108 /* 109 * First mask the byte with 255, as when a negative signed byte converts 110 * to an integer, it has bits on in the first 3 bytes, we are only 111 * concerned about the right-most 8 bits. Then shift the rightmost byte 112 * to align with its position in the integer. 113 */ 114 int value = ((bytes[start + 3] & 255)) | ((bytes[start + 2] & 255) << 8) 115 | ((bytes[start + 1] & 255) << 16) | ((bytes[start] & 255) << 24); 116 return value; 117 } 118 } 119