blob: 91ba5a2933074dcfeb9b3a80b737c2ff5ce5de05 [file] [log] [blame]
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080017
18import org.onlab.packet.Ip6Prefix;
19
20import com.esotericsoftware.kryo.Kryo;
21import com.esotericsoftware.kryo.Serializer;
22import com.esotericsoftware.kryo.io.Input;
23import com.esotericsoftware.kryo.io.Output;
24
25/**
26 * Kryo Serializer for {@link Ip6Prefix}.
27 */
28public final class Ip6PrefixSerializer extends Serializer<Ip6Prefix> {
29
30 /**
31 * Creates {@link Ip6Prefix} serializer instance.
32 */
33 public Ip6PrefixSerializer() {
34 // non-null, immutable
35 super(false, true);
36 }
37
38 @Override
39 public void write(Kryo kryo, Output output,
40 Ip6Prefix object) {
41 byte[] octs = object.address().toOctets();
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080042 // It is always Ip6Address.BYTE_LENGTH
43 output.writeInt(octs.length);
44 output.writeBytes(octs);
45 output.writeInt(object.prefixLength());
46 }
47
48 @Override
49 public Ip6Prefix read(Kryo kryo, Input input,
50 Class<Ip6Prefix> type) {
51 int octLen = input.readInt();
52 byte[] octs = new byte[octLen];
53 input.readBytes(octs);
54 int prefLen = input.readInt();
55 return Ip6Prefix.valueOf(octs, prefLen);
56 }
57}