Skip to content

Commit

Permalink
Merge pull request #30233 from Captain1653/use-standart-charsets
Browse files Browse the repository at this point in the history
Use StandardCharsets.UTF-8 in SerializationDocTest
  • Loading branch information
patriknw committed May 12, 2021
2 parents 245527c + 0ee9532 commit 240378f
Showing 1 changed file with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

package jdocs.serialization;

import java.io.UnsupportedEncodingException;

import akka.actor.typed.javadsl.Behaviors;
import akka.cluster.Cluster;
import akka.testkit.javadsl.TestKit;
import org.junit.Test;
import static org.junit.Assert.*;
import java.nio.charset.StandardCharsets;

// #imports
import akka.actor.*;
import akka.serialization.*;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

// #imports

public class SerializationDocTest {
Expand Down Expand Up @@ -86,7 +86,7 @@ static class MyOwnSerializer2 extends SerializerWithStringManifest {

private static final String CUSTOMER_MANIFEST = "customer";
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,
// you've got a couple of billions to choose from,
Expand All @@ -107,27 +107,19 @@ public String manifest(Object obj) {
@Override
public byte[] toBinary(Object obj) {
// Put the real code that serializes the object here
try {
if (obj instanceof Customer) return ((Customer) obj).name.getBytes(UTF_8);
else if (obj instanceof User) return ((User) obj).name.getBytes(UTF_8);
else throw new IllegalArgumentException("Unknown type: " + obj);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
if (obj instanceof Customer) return ((Customer) obj).name.getBytes(UTF_8);
else if (obj instanceof User) return ((User) obj).name.getBytes(UTF_8);
else throw new IllegalArgumentException("Unknown type: " + obj);
}

// "fromBinary" deserializes the given array,
// using the type hint
@Override
public Object fromBinary(byte[] bytes, String manifest) {
// Put the real code that deserializes here
try {
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 throw new IllegalArgumentException("Unknown manifest: " + manifest);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
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 throw new IllegalArgumentException("Unknown manifest: " + manifest);
}
}
// #my-own-serializer2
Expand Down

0 comments on commit 240378f

Please sign in to comment.