blob: 4752734b8ecb8e9f7ca29a9f67a2cfa18199dcdb [file] [log] [blame]
Carmelo Cascone804c0012016-06-23 13:05:40 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.store.serializers;
18
19import com.esotericsoftware.kryo.Kryo;
20import com.esotericsoftware.kryo.Serializer;
21import com.esotericsoftware.kryo.io.Input;
22import com.esotericsoftware.kryo.io.Output;
23import org.onlab.util.ImmutableByteSequence;
24
25/**
26 * Kryo serializer for {@link ImmutableByteSequence}.
27 */
28public class ImmutableByteSequenceSerializer extends Serializer<ImmutableByteSequence> {
29
30 /**
31 * Creates a new {@link ImmutableByteSequence} serializer instance.
32 */
33 public ImmutableByteSequenceSerializer() {
34 // non-null, immutable.
35 super(false, true);
36 }
37
38 @Override
39 public void write(Kryo kryo, Output output, ImmutableByteSequence object) {
40 byte[] data = object.asArray();
41 output.writeInt(data.length);
42 output.write(data);
43 }
44
45 @Override
46 public ImmutableByteSequence read(Kryo kryo, Input input, Class<ImmutableByteSequence> type) {
47 int length = input.readInt();
48 byte[] data = new byte[length];
49 input.read(data);
50 return ImmutableByteSequence.copyFrom(data);
51 }
52}