Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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. |
| 27 | */ |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 28 | public interface StoreSerializer extends Serializer { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 31 | * Serializes the specified object into bytes. |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 32 | * |
| 33 | * @param obj object to be serialized |
| 34 | * @return serialized bytes |
| 35 | */ |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 36 | @Override |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 37 | byte[] encode(final Object obj); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 40 | * Serializes the specified object into bytes. |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 41 | * |
| 42 | * @param obj object to be serialized |
| 43 | * @param buffer to write serialized bytes |
| 44 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 45 | void encode(final Object obj, ByteBuffer buffer); |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 48 | * Serializes the specified object into bytes. |
| 49 | * |
| 50 | * @param obj object to be serialized |
| 51 | * @param stream to write serialized bytes |
| 52 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 53 | void encode(final Object obj, final OutputStream stream); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 56 | * Deserializes the specified bytes into an object. |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 57 | * |
| 58 | * @param bytes bytes to be deserialized |
| 59 | * @return deserialized object |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 60 | * @param <T> decoded type |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 61 | */ |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 62 | @Override |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 63 | <T> T decode(final byte[] bytes); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 64 | |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 65 | /** |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 66 | * Deserializes the specified bytes into an object. |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 67 | * |
| 68 | * @param buffer bytes to be deserialized |
| 69 | * @return deserialized object |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 70 | * @param <T> decoded type |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 71 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 72 | <T> T decode(final ByteBuffer buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 73 | |
| 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 81 | <T> T decode(final InputStream stream); |
Madan Jampani | dc01297 | 2016-04-25 11:13:26 -0700 | [diff] [blame] | 82 | |
| 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 Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 91 | |
| 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 HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 137 | } |