blob: 8ecd13afcf32b06c0dc8e8138a0c38a22003d9a6 [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 */
16package org.onlab.onos.store.serializers;
17
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();
42 // TODO: Writing (and reading) the number of octets is redundant:
43 // It is always Ip6Address.BYTE_LENGTH
44 output.writeInt(octs.length);
45 output.writeBytes(octs);
46 output.writeInt(object.prefixLength());
47 }
48
49 @Override
50 public Ip6Prefix read(Kryo kryo, Input input,
51 Class<Ip6Prefix> type) {
52 int octLen = input.readInt();
53 byte[] octs = new byte[octLen];
54 input.readBytes(octs);
55 int prefLen = input.readInt();
56 return Ip6Prefix.valueOf(octs, prefLen);
57 }
58}