Add ByteBuffer interface
Change-Id: I6a2d6c7110a34b77a2d5567405fbb273802b89d1
Showing
1 changed file
with
37 additions
and
0 deletions
1 | package org.onlab.util; | 1 | package org.onlab.util; |
2 | 2 | ||
3 | +import java.nio.ByteBuffer; | ||
3 | import java.util.ArrayList; | 4 | import java.util.ArrayList; |
4 | import java.util.List; | 5 | import java.util.List; |
5 | import java.util.concurrent.ConcurrentLinkedQueue; | 6 | import java.util.concurrent.ConcurrentLinkedQueue; |
... | @@ -8,6 +9,8 @@ import org.apache.commons.lang3.tuple.Pair; | ... | @@ -8,6 +9,8 @@ import org.apache.commons.lang3.tuple.Pair; |
8 | 9 | ||
9 | import com.esotericsoftware.kryo.Kryo; | 10 | import com.esotericsoftware.kryo.Kryo; |
10 | import com.esotericsoftware.kryo.Serializer; | 11 | import com.esotericsoftware.kryo.Serializer; |
12 | +import com.esotericsoftware.kryo.io.ByteBufferInput; | ||
13 | +import com.esotericsoftware.kryo.io.ByteBufferOutput; | ||
11 | import com.esotericsoftware.kryo.io.Input; | 14 | import com.esotericsoftware.kryo.io.Input; |
12 | import com.esotericsoftware.kryo.io.Output; | 15 | import com.esotericsoftware.kryo.io.Output; |
13 | import com.google.common.collect.ImmutableList; | 16 | import com.google.common.collect.ImmutableList; |
... | @@ -174,6 +177,22 @@ public final class KryoPool { | ... | @@ -174,6 +177,22 @@ public final class KryoPool { |
174 | } | 177 | } |
175 | 178 | ||
176 | /** | 179 | /** |
180 | + * Serializes given object to byte buffer using Kryo instance in pool. | ||
181 | + * | ||
182 | + * @param obj Object to serialize | ||
183 | + * @param buffer to write to | ||
184 | + */ | ||
185 | + public void serialize(final Object obj, final ByteBuffer buffer) { | ||
186 | + ByteBufferOutput out = new ByteBufferOutput(buffer); | ||
187 | + Kryo kryo = getKryo(); | ||
188 | + try { | ||
189 | + kryo.writeClassAndObject(out, obj); | ||
190 | + } finally { | ||
191 | + putKryo(kryo); | ||
192 | + } | ||
193 | + } | ||
194 | + | ||
195 | + /** | ||
177 | * Deserializes given byte array to Object using Kryo instance in pool. | 196 | * Deserializes given byte array to Object using Kryo instance in pool. |
178 | * | 197 | * |
179 | * @param bytes serialized bytes | 198 | * @param bytes serialized bytes |
... | @@ -192,6 +211,24 @@ public final class KryoPool { | ... | @@ -192,6 +211,24 @@ public final class KryoPool { |
192 | } | 211 | } |
193 | } | 212 | } |
194 | 213 | ||
214 | + /** | ||
215 | + * Deserializes given byte buffer to Object using Kryo instance in pool. | ||
216 | + * | ||
217 | + * @param buffer input with serialized bytes | ||
218 | + * @param <T> deserialized Object type | ||
219 | + * @return deserialized Object | ||
220 | + */ | ||
221 | + public <T> T deserialize(final ByteBuffer buffer) { | ||
222 | + ByteBufferInput in = new ByteBufferInput(buffer); | ||
223 | + Kryo kryo = getKryo(); | ||
224 | + try { | ||
225 | + @SuppressWarnings("unchecked") | ||
226 | + T obj = (T) kryo.readClassAndObject(in); | ||
227 | + return obj; | ||
228 | + } finally { | ||
229 | + putKryo(kryo); | ||
230 | + } | ||
231 | + } | ||
195 | 232 | ||
196 | /** | 233 | /** |
197 | * Creates a Kryo instance with {@link #registeredTypes} pre-registered. | 234 | * Creates a Kryo instance with {@link #registeredTypes} pre-registered. | ... | ... |
-
Please register or login to post a comment