blob: 761e4bc49bf92d6fbdf3b531b2de0db04642c27b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070017
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080018import java.io.InputStream;
19import java.io.OutputStream;
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070020import java.nio.ByteBuffer;
21
HIGUCHI Yutae7290652016-05-18 11:29:01 -070022import org.onlab.util.KryoNamespace;
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070023import org.onosproject.store.service.Serializer;
HIGUCHI Yutae7290652016-05-18 11:29:01 -070024
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070025/**
26 * Service to serialize Objects into byte array.
27 */
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070028public interface StoreSerializer extends Serializer {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070029
30 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070031 * Serializes the specified object into bytes.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070032 *
33 * @param obj object to be serialized
34 * @return serialized bytes
35 */
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070036 @Override
Sho SHIMIZU3310a342015-05-13 12:14:05 -070037 byte[] encode(final Object obj);
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070038
39 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070040 * Serializes the specified object into bytes.
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070041 *
42 * @param obj object to be serialized
43 * @param buffer to write serialized bytes
44 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070045 void encode(final Object obj, ByteBuffer buffer);
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070046
47 /**
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080048 * Serializes the specified object into bytes.
49 *
50 * @param obj object to be serialized
51 * @param stream to write serialized bytes
52 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070053 void encode(final Object obj, final OutputStream stream);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080054
55 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070056 * Deserializes the specified bytes into an object.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070057 *
58 * @param bytes bytes to be deserialized
59 * @return deserialized object
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080060 * @param <T> decoded type
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070061 */
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070062 @Override
Sho SHIMIZU3310a342015-05-13 12:14:05 -070063 <T> T decode(final byte[] bytes);
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070064
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070065 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070066 * Deserializes the specified bytes into an object.
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070067 *
68 * @param buffer bytes to be deserialized
69 * @return deserialized object
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080070 * @param <T> decoded type
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070071 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070072 <T> T decode(final ByteBuffer buffer);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080073
74 /**
75 * Deserializes the specified bytes into an object.
76 *
77 * @param stream stream containing the bytes to be deserialized
78 * @return deserialized object
79 * @param <T> decoded type
80 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070081 <T> T decode(final InputStream stream);
Madan Jampanidc012972016-04-25 11:13:26 -070082
83 /**
84 * Returns a copy of the specfied object.
85 *
86 * @param object object to copy
87 * @return a copy of the object
88 * @param <T> object type
89 */
90 <T> T copy(final T object);
HIGUCHI Yutae7290652016-05-18 11:29:01 -070091
92 /**
93 * Creates a new StoreSerializer instance from a KryoNamespace.
94 *
95 * @param ns kryo namespace
96 * @return StoreSerializer instance
97 */
98 static StoreSerializer using(KryoNamespace ns) {
99 return new StoreSerializer() {
100
101 @Override
102 public void encode(Object obj, OutputStream stream) {
103 ns.serialize(obj, stream);
104 }
105
106 @Override
107 public void encode(Object obj, ByteBuffer buffer) {
108 ns.serialize(obj, buffer);
109 }
110
111 @Override
112 public byte[] encode(Object obj) {
113 return ns.serialize(obj);
114 }
115
116 @Override
117 public <T> T decode(InputStream stream) {
118 return ns.deserialize(stream);
119 }
120
121 @Override
122 public <T> T decode(ByteBuffer buffer) {
123 return ns.deserialize(buffer);
124 }
125
126 @Override
127 public <T> T decode(byte[] bytes) {
128 return ns.deserialize(bytes);
129 }
130
131 @Override
132 public <T> T copy(T object) {
133 return ns.run(kryo -> kryo.copy(object));
134 }
135 };
136 }
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -0700137}