Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 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 | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 20 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 23 | |
| 24 | import org.apache.commons.lang3.tuple.Pair; |
| 25 | |
| 26 | import com.esotericsoftware.kryo.Kryo; |
| 27 | import com.esotericsoftware.kryo.Serializer; |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 28 | import com.esotericsoftware.kryo.io.ByteBufferInput; |
| 29 | import com.esotericsoftware.kryo.io.ByteBufferOutput; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 30 | import com.esotericsoftware.kryo.io.Input; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 31 | import com.esotericsoftware.kryo.pool.KryoCallback; |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 32 | import com.esotericsoftware.kryo.pool.KryoFactory; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 33 | import com.esotericsoftware.kryo.pool.KryoPool; |
| 34 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 35 | import com.google.common.collect.ImmutableList; |
| 36 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Pool of Kryo instances, with classes pre-registered. |
| 39 | */ |
| 40 | //@ThreadSafe |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 41 | public final class KryoNamespace implements KryoFactory, KryoPool { |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Default buffer size used for serialization. |
| 45 | * |
| 46 | * @see #serialize(Object) |
| 47 | */ |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 48 | public static final int DEFAULT_BUFFER_SIZE = 4096; |
Yuta HIGUCHI | d2a3882 | 2014-11-06 19:05:04 -0800 | [diff] [blame] | 49 | public static final int MAX_BUFFER_SIZE = 100 * 1000 * 1000; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 50 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 51 | /** |
| 52 | * ID to use if this KryoNamespace does not define registration id. |
| 53 | */ |
| 54 | public static final int FLOATING_ID = -1; |
| 55 | |
| 56 | /** |
| 57 | * Smallest ID free to use for user defined registrations. |
| 58 | */ |
| 59 | public static final int INITIAL_ID = 11; |
| 60 | |
| 61 | |
| 62 | private final KryoPool pool = new KryoPool.Builder(this) |
| 63 | .softReferences() |
| 64 | .build(); |
| 65 | |
| 66 | private final ImmutableList<RegistrationBlock> registeredBlocks; |
| 67 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 68 | private final boolean registrationRequired; |
| 69 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 70 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 71 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 72 | * KryoNamespace builder. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 73 | */ |
| 74 | //@NotThreadSafe |
| 75 | public static final class Builder { |
| 76 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 77 | private int blockHeadId = INITIAL_ID; |
| 78 | private List<Pair<Class<?>, Serializer<?>>> types = new ArrayList<>(); |
| 79 | private List<RegistrationBlock> blocks = new ArrayList<>(); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 80 | private boolean registrationRequired = true; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 83 | * Builds a {@link KryoNamespace} instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 84 | * |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 85 | * @return KryoNamespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 86 | */ |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 87 | public KryoNamespace build() { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 88 | if (!types.isEmpty()) { |
| 89 | blocks.add(new RegistrationBlock(this.blockHeadId, types)); |
| 90 | } |
| 91 | return new KryoNamespace(blocks, registrationRequired).populate(1); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets the next Kryo registration Id for following register entries. |
| 96 | * |
| 97 | * @param id Kryo registration Id |
| 98 | * @return this |
| 99 | * |
| 100 | * @see Kryo#register(Class, Serializer, int) |
| 101 | */ |
| 102 | public Builder nextId(final int id) { |
| 103 | if (!types.isEmpty()) { |
| 104 | blocks.add(new RegistrationBlock(this.blockHeadId, types)); |
| 105 | types = new ArrayList<>(); |
| 106 | } |
| 107 | this.blockHeadId = id; |
| 108 | return this; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Registers classes to be serialized using Kryo default serializer. |
| 113 | * |
| 114 | * @param expectedTypes list of classes |
| 115 | * @return this |
| 116 | */ |
| 117 | public Builder register(final Class<?>... expectedTypes) { |
| 118 | for (Class<?> clazz : expectedTypes) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 119 | types.add(Pair.of(clazz, null)); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 120 | } |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Registers a class and it's serializer. |
| 126 | * |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 127 | * @param classes list of classes to register |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 128 | * @param serializer serializer to use for the class |
| 129 | * @return this |
| 130 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 131 | public Builder register(Serializer<?> serializer, final Class<?>... classes) { |
| 132 | for (Class<?> clazz : classes) { |
| 133 | types.add(Pair.of(clazz, serializer)); |
| 134 | } |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | private Builder register(RegistrationBlock block) { |
| 139 | if (block.begin() != FLOATING_ID) { |
| 140 | // flush pending types |
| 141 | nextId(block.begin()); |
| 142 | blocks.add(block); |
| 143 | nextId(block.begin() + block.types().size()); |
| 144 | } else { |
| 145 | // flush pending types |
| 146 | final int addedBlockBegin = blockHeadId + types.size(); |
| 147 | nextId(addedBlockBegin); |
| 148 | blocks.add(new RegistrationBlock(addedBlockBegin, block.types())); |
| 149 | nextId(addedBlockBegin + block.types().size()); |
| 150 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 151 | return this; |
| 152 | } |
| 153 | |
| 154 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 155 | * Registers all the class registered to given KryoNamespace. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 156 | * |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 157 | * @param ns KryoNamespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 158 | * @return this |
| 159 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 160 | public Builder register(final KryoNamespace ns) { |
| 161 | for (RegistrationBlock block : ns.registeredBlocks) { |
| 162 | this.register(block); |
| 163 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 164 | return this; |
| 165 | } |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 166 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Sets the registrationRequired flag. |
| 169 | * |
| 170 | * @param registrationRequired Kryo's registrationRequired flag |
| 171 | * @return this |
| 172 | * |
| 173 | * @see Kryo#setRegistrationRequired(boolean) |
| 174 | */ |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 175 | public Builder setRegistrationRequired(boolean registrationRequired) { |
| 176 | this.registrationRequired = registrationRequired; |
| 177 | return this; |
| 178 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 182 | * Creates a new {@link KryoNamespace} builder. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 183 | * |
| 184 | * @return builder |
| 185 | */ |
| 186 | public static Builder newBuilder() { |
| 187 | return new Builder(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Creates a Kryo instance pool. |
| 192 | * |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 193 | * @param registeredTypes types to register |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 194 | * @param registrationRequired |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 195 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 196 | private KryoNamespace(final List<RegistrationBlock> registeredTypes, boolean registrationRequired) { |
| 197 | this.registeredBlocks = ImmutableList.copyOf(registeredTypes); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 198 | this.registrationRequired = registrationRequired; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Populates the Kryo pool. |
| 203 | * |
| 204 | * @param instances to add to the pool |
| 205 | * @return this |
| 206 | */ |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 207 | public KryoNamespace populate(int instances) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 208 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 209 | for (int i = 0; i < instances; ++i) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 210 | release(create()); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 211 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 212 | return this; |
| 213 | } |
| 214 | |
| 215 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 216 | * Serializes given object to byte array using Kryo instance in pool. |
| 217 | * <p> |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 218 | * Note: Serialized bytes must be smaller than {@link #MAX_BUFFER_SIZE}. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 219 | * |
| 220 | * @param obj Object to serialize |
| 221 | * @return serialized bytes |
| 222 | */ |
| 223 | public byte[] serialize(final Object obj) { |
| 224 | return serialize(obj, DEFAULT_BUFFER_SIZE); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Serializes given object to byte array using Kryo instance in pool. |
| 229 | * |
| 230 | * @param obj Object to serialize |
| 231 | * @param bufferSize maximum size of serialized bytes |
| 232 | * @return serialized bytes |
| 233 | */ |
| 234 | public byte[] serialize(final Object obj, final int bufferSize) { |
Yuta HIGUCHI | d2a3882 | 2014-11-06 19:05:04 -0800 | [diff] [blame] | 235 | ByteBufferOutput out = new ByteBufferOutput(bufferSize, MAX_BUFFER_SIZE); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 236 | try { |
Madan Jampani | 22fa5cb | 2015-04-13 15:53:44 -0700 | [diff] [blame] | 237 | Kryo kryo = borrow(); |
| 238 | try { |
| 239 | kryo.writeClassAndObject(out, obj); |
| 240 | out.flush(); |
| 241 | return out.toBytes(); |
| 242 | } finally { |
| 243 | release(kryo); |
| 244 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 245 | } finally { |
Madan Jampani | 22fa5cb | 2015-04-13 15:53:44 -0700 | [diff] [blame] | 246 | out.release(); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 251 | * Serializes given object to byte buffer using Kryo instance in pool. |
| 252 | * |
| 253 | * @param obj Object to serialize |
| 254 | * @param buffer to write to |
| 255 | */ |
| 256 | public void serialize(final Object obj, final ByteBuffer buffer) { |
| 257 | ByteBufferOutput out = new ByteBufferOutput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 258 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 259 | try { |
| 260 | kryo.writeClassAndObject(out, obj); |
Yuta HIGUCHI | cac919c | 2014-10-20 22:17:20 -0700 | [diff] [blame] | 261 | out.flush(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 262 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 263 | release(kryo); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 269 | * |
| 270 | * @param obj Object to serialize |
| 271 | * @param stream to write to |
| 272 | */ |
| 273 | public void serialize(final Object obj, final OutputStream stream) { |
| 274 | serialize(obj, stream, DEFAULT_BUFFER_SIZE); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 279 | * |
| 280 | * @param obj Object to serialize |
| 281 | * @param stream to write to |
| 282 | * @param bufferSize size of the buffer in front of the stream |
| 283 | */ |
| 284 | public void serialize(final Object obj, final OutputStream stream, final int bufferSize) { |
| 285 | ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize); |
| 286 | Kryo kryo = borrow(); |
| 287 | try { |
| 288 | kryo.writeClassAndObject(out, obj); |
| 289 | out.flush(); |
| 290 | } finally { |
| 291 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 296 | * Deserializes given byte array to Object using Kryo instance in pool. |
| 297 | * |
| 298 | * @param bytes serialized bytes |
| 299 | * @param <T> deserialized Object type |
| 300 | * @return deserialized Object |
| 301 | */ |
| 302 | public <T> T deserialize(final byte[] bytes) { |
| 303 | Input in = new Input(bytes); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 304 | Kryo kryo = borrow(); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 305 | try { |
| 306 | @SuppressWarnings("unchecked") |
| 307 | T obj = (T) kryo.readClassAndObject(in); |
| 308 | return obj; |
| 309 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 310 | release(kryo); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Deserializes given byte buffer to Object using Kryo instance in pool. |
| 316 | * |
| 317 | * @param buffer input with serialized bytes |
| 318 | * @param <T> deserialized Object type |
| 319 | * @return deserialized Object |
| 320 | */ |
| 321 | public <T> T deserialize(final ByteBuffer buffer) { |
| 322 | ByteBufferInput in = new ByteBufferInput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 323 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 324 | try { |
| 325 | @SuppressWarnings("unchecked") |
| 326 | T obj = (T) kryo.readClassAndObject(in); |
| 327 | return obj; |
| 328 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 329 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 332 | |
| 333 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 334 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 335 | * |
| 336 | * @param stream input stream |
| 337 | * @param <T> deserialized Object type |
| 338 | * @return deserialized Object |
| 339 | */ |
| 340 | public <T> T deserialize(final InputStream stream) { |
| 341 | return deserialize(stream, DEFAULT_BUFFER_SIZE); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 346 | * |
| 347 | * @param stream input stream |
| 348 | * @param <T> deserialized Object type |
| 349 | * @return deserialized Object |
| 350 | * @param bufferSize size of the buffer in front of the stream |
| 351 | */ |
| 352 | public <T> T deserialize(final InputStream stream, final int bufferSize) { |
| 353 | ByteBufferInput in = new ByteBufferInput(stream, bufferSize); |
| 354 | Kryo kryo = borrow(); |
| 355 | try { |
| 356 | @SuppressWarnings("unchecked") |
| 357 | T obj = (T) kryo.readClassAndObject(in); |
| 358 | return obj; |
| 359 | } finally { |
| 360 | release(kryo); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Creates a Kryo instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 366 | * |
| 367 | * @return Kryo instance |
| 368 | */ |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 369 | @Override |
| 370 | public Kryo create() { |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 371 | Kryo kryo = new Kryo(); |
| 372 | kryo.setRegistrationRequired(registrationRequired); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 373 | for (RegistrationBlock block : registeredBlocks) { |
| 374 | int id = block.begin(); |
| 375 | if (id == FLOATING_ID) { |
| 376 | id = kryo.getNextRegistrationId(); |
| 377 | } |
| 378 | for (Pair<Class<?>, Serializer<?>> entry : block.types()) { |
| 379 | final Serializer<?> serializer = entry.getRight(); |
| 380 | if (serializer == null) { |
| 381 | kryo.register(entry.getLeft(), id++); |
| 382 | } else { |
| 383 | kryo.register(entry.getLeft(), serializer, id++); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 384 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | return kryo; |
| 388 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 389 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 390 | @Override |
| 391 | public Kryo borrow() { |
| 392 | return pool.borrow(); |
| 393 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 394 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 395 | @Override |
| 396 | public void release(Kryo kryo) { |
| 397 | pool.release(kryo); |
| 398 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 399 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 400 | @Override |
| 401 | public <T> T run(KryoCallback<T> callback) { |
| 402 | return pool.run(callback); |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public String toString() { |
| 407 | return MoreObjects.toStringHelper(getClass()) |
| 408 | .add("registeredBlocks", registeredBlocks) |
| 409 | .toString(); |
| 410 | } |
| 411 | |
| 412 | static final class RegistrationBlock { |
| 413 | private final int begin; |
| 414 | private final ImmutableList<Pair<Class<?>, Serializer<?>>> types; |
| 415 | |
| 416 | public RegistrationBlock(int begin, List<Pair<Class<?>, Serializer<?>>> types) { |
| 417 | this.begin = begin; |
| 418 | this.types = ImmutableList.copyOf(types); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 421 | public int begin() { |
| 422 | return begin; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 425 | public ImmutableList<Pair<Class<?>, Serializer<?>>> types() { |
| 426 | return types; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public String toString() { |
| 431 | return MoreObjects.toStringHelper(getClass()) |
| 432 | .add("begin", begin) |
| 433 | .add("types", types) |
| 434 | .toString(); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 437 | } |