Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 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 | |
| 10 | // TODO move to util, etc. |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame^] | 11 | /** |
| 12 | * Kryo Serializer for {@link IpPrefix}. |
| 13 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 14 | public final class IpPrefixSerializer extends Serializer<IpPrefix> { |
| 15 | |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame^] | 16 | /** |
| 17 | * Default constructor. |
| 18 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 19 | public IpPrefixSerializer() { |
| 20 | // non-null, immutable |
| 21 | super(false, true); |
| 22 | } |
| 23 | |
| 24 | @Override |
| 25 | public void write(Kryo kryo, Output output, |
| 26 | IpPrefix object) { |
| 27 | byte[] octs = object.toOctets(); |
| 28 | output.writeInt(octs.length); |
| 29 | output.writeBytes(octs); |
| 30 | output.writeInt(object.prefixLength()); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public IpPrefix read(Kryo kryo, Input input, |
| 35 | Class<IpPrefix> type) { |
| 36 | int octLen = input.readInt(); |
| 37 | byte[] octs = new byte[octLen]; |
| 38 | input.read(octs); |
| 39 | int prefLen = input.readInt(); |
| 40 | return IpPrefix.valueOf(octs, prefLen); |
| 41 | } |
| 42 | } |