Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.serializers; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 18 | import java.io.InputStream; |
| 19 | import java.io.OutputStream; |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 20 | import java.nio.ByteBuffer; |
| 21 | |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 23 | import org.onosproject.store.service.Serializer; |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 24 | |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Service to serialize Objects into byte array. |
Jordan Halterman | 2c83a10 | 2017-08-20 17:11:41 -0700 | [diff] [blame] | 27 | * |
| 28 | * @deprecated since 1.11 ("Loon") |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 29 | */ |
Jordan Halterman | 2c83a10 | 2017-08-20 17:11:41 -0700 | [diff] [blame] | 30 | @Deprecated |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 31 | public interface StoreSerializer extends Serializer { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 34 | * Serializes the specified object into bytes. |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 35 | * |
| 36 | * @param obj object to be serialized |
| 37 | * @return serialized bytes |
| 38 | */ |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 39 | @Override |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 40 | byte[] encode(final Object obj); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 43 | * Serializes the specified object into bytes. |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 44 | * |
| 45 | * @param obj object to be serialized |
| 46 | * @param buffer to write serialized bytes |
| 47 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 48 | void encode(final Object obj, ByteBuffer buffer); |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 51 | * Serializes the specified object into bytes. |
| 52 | * |
| 53 | * @param obj object to be serialized |
| 54 | * @param stream to write serialized bytes |
| 55 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 56 | void encode(final Object obj, final OutputStream stream); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 59 | * Deserializes the specified bytes into an object. |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 60 | * |
| 61 | * @param bytes bytes to be deserialized |
| 62 | * @return deserialized object |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 63 | * @param <T> decoded type |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 64 | */ |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 65 | @Override |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 66 | <T> T decode(final byte[] bytes); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 67 | |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 68 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 69 | * Deserializes the specified bytes into an object. |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 70 | * |
| 71 | * @param buffer bytes to be deserialized |
| 72 | * @return deserialized object |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 73 | * @param <T> decoded type |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 74 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 75 | <T> T decode(final ByteBuffer buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 76 | |
| 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 84 | <T> T decode(final InputStream stream); |
Madan Jampani | dc01297 | 2016-04-25 11:13:26 -0700 | [diff] [blame] | 85 | |
| 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 Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 94 | |
| 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 HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 140 | } |