1 /* 2 * Copyright (C) 2011 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 com.android.server.net; 18 19 import android.net.NetworkIdentity; 20 21 import java.io.DataInputStream; 22 import java.io.DataOutputStream; 23 import java.io.IOException; 24 import java.util.HashSet; 25 26 /** 27 * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity} 28 * active on that interface. 29 * 30 * @hide 31 */ 32 public class NetworkIdentitySet extends HashSet<NetworkIdentity> { 33 private static final int VERSION_INIT = 1; 34 private static final int VERSION_ADD_ROAMING = 2; 35 private static final int VERSION_ADD_NETWORK_ID = 3; 36 37 public NetworkIdentitySet() { 38 } 39 40 public NetworkIdentitySet(DataInputStream in) throws IOException { 41 final int version = in.readInt(); 42 final int size = in.readInt(); 43 for (int i = 0; i < size; i++) { 44 if (version <= VERSION_INIT) { 45 final int ignored = in.readInt(); 46 } 47 final int type = in.readInt(); 48 final int subType = in.readInt(); 49 final String subscriberId = readOptionalString(in); 50 final String networkId; 51 if (version >= VERSION_ADD_NETWORK_ID) { 52 networkId = readOptionalString(in); 53 } else { 54 networkId = null; 55 } 56 final boolean roaming; 57 if (version >= VERSION_ADD_ROAMING) { 58 roaming = in.readBoolean(); 59 } else { 60 roaming = false; 61 } 62 63 add(new NetworkIdentity(type, subType, subscriberId, networkId, false)); 64 } 65 } 66 67 public void writeToStream(DataOutputStream out) throws IOException { 68 out.writeInt(VERSION_ADD_NETWORK_ID); 69 out.writeInt(size()); 70 for (NetworkIdentity ident : this) { 71 out.writeInt(ident.getType()); 72 out.writeInt(ident.getSubType()); 73 writeOptionalString(out, ident.getSubscriberId()); 74 writeOptionalString(out, ident.getNetworkId()); 75 out.writeBoolean(ident.getRoaming()); 76 } 77 } 78 79 private static void writeOptionalString(DataOutputStream out, String value) throws IOException { 80 if (value != null) { 81 out.writeByte(1); 82 out.writeUTF(value); 83 } else { 84 out.writeByte(0); 85 } 86 } 87 88 private static String readOptionalString(DataInputStream in) throws IOException { 89 if (in.readByte() != 0) { 90 return in.readUTF(); 91 } else { 92 return null; 93 } 94 } 95 } 96