Merge pull request #30233 from Captain1653/use-standart-charsets
Use StandardCharsets.UTF-8 in SerializationDocTest
This commit is contained in:
commit
240378f062
1 changed files with 10 additions and 18 deletions
|
|
@ -4,19 +4,19 @@
|
||||||
|
|
||||||
package jdocs.serialization;
|
package jdocs.serialization;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
|
|
||||||
import akka.actor.typed.javadsl.Behaviors;
|
import akka.actor.typed.javadsl.Behaviors;
|
||||||
import akka.cluster.Cluster;
|
import akka.cluster.Cluster;
|
||||||
import akka.testkit.javadsl.TestKit;
|
import akka.testkit.javadsl.TestKit;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
// #imports
|
// #imports
|
||||||
import akka.actor.*;
|
import akka.actor.*;
|
||||||
import akka.serialization.*;
|
import akka.serialization.*;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
// #imports
|
// #imports
|
||||||
|
|
||||||
public class SerializationDocTest {
|
public class SerializationDocTest {
|
||||||
|
|
@ -86,7 +86,7 @@ public class SerializationDocTest {
|
||||||
|
|
||||||
private static final String CUSTOMER_MANIFEST = "customer";
|
private static final String CUSTOMER_MANIFEST = "customer";
|
||||||
private static final String USER_MANIFEST = "user";
|
private static final String USER_MANIFEST = "user";
|
||||||
private static final String UTF_8 = StandardCharsets.UTF_8.name();
|
private static final Charset UTF_8 = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
// Pick a unique identifier for your Serializer,
|
// Pick a unique identifier for your Serializer,
|
||||||
// you've got a couple of billions to choose from,
|
// you've got a couple of billions to choose from,
|
||||||
|
|
@ -107,13 +107,9 @@ public class SerializationDocTest {
|
||||||
@Override
|
@Override
|
||||||
public byte[] toBinary(Object obj) {
|
public byte[] toBinary(Object obj) {
|
||||||
// Put the real code that serializes the object here
|
// Put the real code that serializes the object here
|
||||||
try {
|
if (obj instanceof Customer) return ((Customer) obj).name.getBytes(UTF_8);
|
||||||
if (obj instanceof Customer) return ((Customer) obj).name.getBytes(UTF_8);
|
else if (obj instanceof User) return ((User) obj).name.getBytes(UTF_8);
|
||||||
else if (obj instanceof User) return ((User) obj).name.getBytes(UTF_8);
|
else throw new IllegalArgumentException("Unknown type: " + obj);
|
||||||
else throw new IllegalArgumentException("Unknown type: " + obj);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// "fromBinary" deserializes the given array,
|
// "fromBinary" deserializes the given array,
|
||||||
|
|
@ -121,13 +117,9 @@ public class SerializationDocTest {
|
||||||
@Override
|
@Override
|
||||||
public Object fromBinary(byte[] bytes, String manifest) {
|
public Object fromBinary(byte[] bytes, String manifest) {
|
||||||
// Put the real code that deserializes here
|
// Put the real code that deserializes here
|
||||||
try {
|
if (manifest.equals(CUSTOMER_MANIFEST)) return new Customer(new String(bytes, UTF_8));
|
||||||
if (manifest.equals(CUSTOMER_MANIFEST)) return new Customer(new String(bytes, UTF_8));
|
else if (manifest.equals(USER_MANIFEST)) return new User(new String(bytes, UTF_8));
|
||||||
else if (manifest.equals(USER_MANIFEST)) return new User(new String(bytes, UTF_8));
|
else throw new IllegalArgumentException("Unknown manifest: " + manifest);
|
||||||
else throw new IllegalArgumentException("Unknown manifest: " + manifest);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// #my-own-serializer2
|
// #my-own-serializer2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue