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