Pavlin Radoslavov | 17378ef | 2014-11-05 16:13:47 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.serializers; |
Pavlin Radoslavov | 17378ef | 2014-11-05 16:13:47 -0800 | [diff] [blame] | 17 | |
| 18 | import org.onlab.packet.Ip6Prefix; |
| 19 | |
| 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | import com.esotericsoftware.kryo.Serializer; |
| 22 | import com.esotericsoftware.kryo.io.Input; |
| 23 | import com.esotericsoftware.kryo.io.Output; |
| 24 | |
| 25 | /** |
| 26 | * Kryo Serializer for {@link Ip6Prefix}. |
| 27 | */ |
| 28 | public 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 Radoslavov | 17378ef | 2014-11-05 16:13:47 -0800 | [diff] [blame] | 42 | // 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 | } |