blob: 108d6cfb20dab953b7dacefbaeef5bc558354210 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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.
Jordan Halterman2c83a102017-08-20 17:11:41 -070027 *
28 * @deprecated since 1.11 ("Loon")
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070029 */
Jordan Halterman2c83a102017-08-20 17:11:41 -070030@Deprecated
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070031public interface StoreSerializer extends Serializer {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070032
33 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070034 * Serializes the specified object into bytes.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070035 *
36 * @param obj object to be serialized
37 * @return serialized bytes
38 */
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070039 @Override
Sho SHIMIZU3310a342015-05-13 12:14:05 -070040 byte[] encode(final Object obj);
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070041
42 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070043 * Serializes the specified object into bytes.
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070044 *
45 * @param obj object to be serialized
46 * @param buffer to write serialized bytes
47 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070048 void encode(final Object obj, ByteBuffer buffer);
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070049
50 /**
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080051 * Serializes the specified object into bytes.
52 *
53 * @param obj object to be serialized
54 * @param stream to write serialized bytes
55 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070056 void encode(final Object obj, final OutputStream stream);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080057
58 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070059 * Deserializes the specified bytes into an object.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070060 *
61 * @param bytes bytes to be deserialized
62 * @return deserialized object
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080063 * @param <T> decoded type
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070064 */
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070065 @Override
Sho SHIMIZU3310a342015-05-13 12:14:05 -070066 <T> T decode(final byte[] bytes);
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070067
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070068 /**
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070069 * Deserializes the specified bytes into an object.
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070070 *
71 * @param buffer bytes to be deserialized
72 * @return deserialized object
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080073 * @param <T> decoded type
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070074 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070075 <T> T decode(final ByteBuffer buffer);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080076
77 /**
78 * Deserializes the specified bytes into an object.
79 *
80 * @param stream stream containing the bytes to be deserialized
81 * @return deserialized object
82 * @param <T> decoded type
83 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070084 <T> T decode(final InputStream stream);
Madan Jampanidc012972016-04-25 11:13:26 -070085
86 /**
87 * Returns a copy of the specfied object.
88 *
89 * @param object object to copy
90 * @return a copy of the object
91 * @param <T> object type
92 */
93 <T> T copy(final T object);
HIGUCHI Yutae7290652016-05-18 11:29:01 -070094
95 /**
96 * Creates a new StoreSerializer instance from a KryoNamespace.
97 *
98 * @param ns kryo namespace
99 * @return StoreSerializer instance
100 */
101 static StoreSerializer using(KryoNamespace ns) {
102 return new StoreSerializer() {
103
104 @Override
105 public void encode(Object obj, OutputStream stream) {
106 ns.serialize(obj, stream);
107 }
108
109 @Override
110 public void encode(Object obj, ByteBuffer buffer) {
111 ns.serialize(obj, buffer);
112 }
113
114 @Override
115 public byte[] encode(Object obj) {
116 return ns.serialize(obj);
117 }
118
119 @Override
120 public <T> T decode(InputStream stream) {
121 return ns.deserialize(stream);
122 }
123
124 @Override
125 public <T> T decode(ByteBuffer buffer) {
126 return ns.deserialize(buffer);
127 }
128
129 @Override
130 public <T> T decode(byte[] bytes) {
131 return ns.deserialize(bytes);
132 }
133
134 @Override
135 public <T> T copy(T object) {
136 return ns.run(kryo -> kryo.copy(object));
137 }
138 };
139 }
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -0700140}