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