tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.serializers; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 2 | |
| 3 | import org.onlab.packet.IpPrefix; |
| 4 | |
| 5 | import com.esotericsoftware.kryo.Kryo; |
| 6 | import com.esotericsoftware.kryo.Serializer; |
| 7 | import com.esotericsoftware.kryo.io.Input; |
| 8 | import com.esotericsoftware.kryo.io.Output; |
| 9 | |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame] | 10 | /** |
| 11 | * Kryo Serializer for {@link IpPrefix}. |
| 12 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 13 | public final class IpPrefixSerializer extends Serializer<IpPrefix> { |
| 14 | |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame] | 15 | /** |
Yuta HIGUCHI | 03fec1f | 2014-10-03 09:13:50 -0700 | [diff] [blame^] | 16 | * Creates {@link IpPrefix} serializer instance. |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame] | 17 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 18 | public IpPrefixSerializer() { |
| 19 | // non-null, immutable |
| 20 | super(false, true); |
| 21 | } |
| 22 | |
| 23 | @Override |
| 24 | public void write(Kryo kryo, Output output, |
| 25 | IpPrefix object) { |
| 26 | byte[] octs = object.toOctets(); |
| 27 | output.writeInt(octs.length); |
| 28 | output.writeBytes(octs); |
| 29 | output.writeInt(object.prefixLength()); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public IpPrefix read(Kryo kryo, Input input, |
| 34 | Class<IpPrefix> type) { |
| 35 | int octLen = input.readInt(); |
| 36 | byte[] octs = new byte[octLen]; |
| 37 | input.read(octs); |
| 38 | int prefLen = input.readInt(); |
| 39 | return IpPrefix.valueOf(octs, prefLen); |
| 40 | } |
| 41 | } |