Toggle navigation
Toggle navigation
This project
Loading...
Sign in
홍길동
/
onos
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Yuta HIGUCHI
2014-09-29 17:27:26 -0700
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f4b107e216f680ede5fa5e7d8878e3628fb5aa74
f4b107e2
1 parent
63573c49
Add ByteBuffer interface
Change-Id: I6a2d6c7110a34b77a2d5567405fbb273802b89d1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
utils/misc/src/main/java/org/onlab/util/KryoPool.java
utils/misc/src/main/java/org/onlab/util/KryoPool.java
View file @
f4b107e
package
org
.
onlab
.
util
;
import
java.nio.ByteBuffer
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ConcurrentLinkedQueue
;
...
...
@@ -8,6 +9,8 @@ import org.apache.commons.lang3.tuple.Pair;
import
com.esotericsoftware.kryo.Kryo
;
import
com.esotericsoftware.kryo.Serializer
;
import
com.esotericsoftware.kryo.io.ByteBufferInput
;
import
com.esotericsoftware.kryo.io.ByteBufferOutput
;
import
com.esotericsoftware.kryo.io.Input
;
import
com.esotericsoftware.kryo.io.Output
;
import
com.google.common.collect.ImmutableList
;
...
...
@@ -174,6 +177,22 @@ public final class KryoPool {
}
/**
* Serializes given object to byte buffer using Kryo instance in pool.
*
* @param obj Object to serialize
* @param buffer to write to
*/
public
void
serialize
(
final
Object
obj
,
final
ByteBuffer
buffer
)
{
ByteBufferOutput
out
=
new
ByteBufferOutput
(
buffer
);
Kryo
kryo
=
getKryo
();
try
{
kryo
.
writeClassAndObject
(
out
,
obj
);
}
finally
{
putKryo
(
kryo
);
}
}
/**
* Deserializes given byte array to Object using Kryo instance in pool.
*
* @param bytes serialized bytes
...
...
@@ -192,6 +211,24 @@ public final class KryoPool {
}
}
/**
* Deserializes given byte buffer to Object using Kryo instance in pool.
*
* @param buffer input with serialized bytes
* @param <T> deserialized Object type
* @return deserialized Object
*/
public
<
T
>
T
deserialize
(
final
ByteBuffer
buffer
)
{
ByteBufferInput
in
=
new
ByteBufferInput
(
buffer
);
Kryo
kryo
=
getKryo
();
try
{
@SuppressWarnings
(
"unchecked"
)
T
obj
=
(
T
)
kryo
.
readClassAndObject
(
in
);
return
obj
;
}
finally
{
putKryo
(
kryo
);
}
}
/**
* Creates a Kryo instance with {@link #registeredTypes} pre-registered.
...
...
Please
register
or
login
to post a comment