如何在 Java 中反序列化 java.nio.ByteBuffer
How to deserialize a java.nio.ByteBuffer in Java
我将对象转换为 json 字符串并使用 ByteBuffer.wrap(attributes.getBytes())
将其作为 Blob 存储在 Datastax 中。如何将其转换回 String 并在我的测试中断言?我尝试使用
org.apache.commons.lang3.SerializationUtils.deserialize(entity.getAttributes().array())
但出现异常
org.apache.commons.lang3.SerializationException: java.io.StreamCorruptedException: invalid stream header: 7B226F70
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:197)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:223)
at com.homedepot.productassortment.fullfeed.util.InstallSkusToProductAssortmentConverterTest.convertInstallSkuToProductAssortmentEntity(InstallSkusToProductAssortmentConverterTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
你应该尝试这样的事情:
byte[] bytes = attributes.getBytes( StandardCharsets.UTF_8 );
ByteBuffer.wrap(bytes);
我添加了 StandardCharsets。UTF_8 因为 getBytes() 使用您平台的默认字符集。
然后你可以用类似的东西取回你的字节:
String original = new String( entity.getAttributes(), StandardCharsets.UTF_8 );
事实上我不知道你的 entity.getAttributes() 是什么类型,但如果它是 byte[] 它应该可以工作。
我将对象转换为 json 字符串并使用 ByteBuffer.wrap(attributes.getBytes())
将其作为 Blob 存储在 Datastax 中。如何将其转换回 String 并在我的测试中断言?我尝试使用
org.apache.commons.lang3.SerializationUtils.deserialize(entity.getAttributes().array())
但出现异常
org.apache.commons.lang3.SerializationException: java.io.StreamCorruptedException: invalid stream header: 7B226F70
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:197)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:223)
at com.homedepot.productassortment.fullfeed.util.InstallSkusToProductAssortmentConverterTest.convertInstallSkuToProductAssortmentEntity(InstallSkusToProductAssortmentConverterTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
你应该尝试这样的事情:
byte[] bytes = attributes.getBytes( StandardCharsets.UTF_8 );
ByteBuffer.wrap(bytes);
我添加了 StandardCharsets。UTF_8 因为 getBytes() 使用您平台的默认字符集。 然后你可以用类似的东西取回你的字节:
String original = new String( entity.getAttributes(), StandardCharsets.UTF_8 );
事实上我不知道你的 entity.getAttributes() 是什么类型,但如果它是 byte[] 它应该可以工作。