blob: 2f7a7c094a10aa1b0c5d4a45d689d89690accc34 [file] [log] [blame]
Jordi Ortiz1b1a3b42017-03-03 12:36:54 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordi Ortiz1b1a3b42017-03-03 12:36:54 +01003 *
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.onosproject.store.serializers;
17
18import com.esotericsoftware.kryo.Kryo;
19import com.esotericsoftware.kryo.Serializer;
20import com.esotericsoftware.kryo.io.Input;
21import com.esotericsoftware.kryo.io.Output;
22
23import java.util.BitSet;
24
25/**
26 * Kryo serializer for {@link BitSet}.
27 */
28public class BitSetSerializer extends Serializer<BitSet> {
29
30 /**
31 * Creates {@link BitSet} serializer instance.
32 */
33 public BitSetSerializer() {
34 super(false, false);
35 }
36
37 @Override
38 public void write(Kryo kryo, Output output, BitSet bitSet) {
39 final int len = bitSet.length();
40
41 output.writeInt(len, true);
42
43 byte[] bytes = bitSet.toByteArray();
44 output.writeInt(bytes.length, true);
45 output.writeBytes(bitSet.toByteArray());
46 }
47
48 @Override
49 public BitSet read(Kryo kryo, Input input, Class<BitSet> aClass) {
50 final int len = input.readInt(true);
51 int bytesize = input.readInt(true);
52 byte[] bytes = input.readBytes(bytesize);
53 return BitSet.valueOf(bytes);
54
55 }
56}