1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.shared; 18 19 import android.os.Parcel; 20 21 import java.net.InetAddress; 22 import java.net.UnknownHostException; 23 24 /** 25 * Collection of utilities to interact with {@link InetAddress} 26 * @hide 27 */ 28 public class InetAddressUtils { 29 30 /** 31 * Writes an InetAddress to a parcel. The address may be null. This is likely faster than 32 * calling writeSerializable. 33 * @hide 34 */ parcelInetAddress(Parcel parcel, InetAddress address, int flags)35 public static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) { 36 byte[] addressArray = (address != null) ? address.getAddress() : null; 37 parcel.writeByteArray(addressArray); 38 } 39 40 /** 41 * Reads an InetAddress from a parcel. Returns null if the address that was written was null 42 * or if the data is invalid. 43 * @hide 44 */ unparcelInetAddress(Parcel in)45 public static InetAddress unparcelInetAddress(Parcel in) { 46 byte[] addressArray = in.createByteArray(); 47 if (addressArray == null) { 48 return null; 49 } 50 try { 51 return InetAddress.getByAddress(addressArray); 52 } catch (UnknownHostException e) { 53 return null; 54 } 55 } 56 InetAddressUtils()57 private InetAddressUtils() {} 58 } 59