StringSerializer.java 783 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.yiidata.intergration.web.modules.sys.cache.serde;
  2. import java.nio.charset.Charset;
  3. import java.nio.charset.StandardCharsets;
  4. /**
  5. * <pre>
  6. *
  7. * Created by zhaopx.
  8. * User: zhaopx
  9. * Date: 2019/11/14
  10. * Time: 11:47
  11. *
  12. * </pre>
  13. *
  14. * @author zhaopx
  15. */
  16. public class StringSerializer implements Serializer<String> {
  17. /**
  18. * 默认的编码
  19. */
  20. final Charset defaultCharset;
  21. public StringSerializer() {
  22. this(StandardCharsets.UTF_8);
  23. }
  24. public StringSerializer(Charset charset) {
  25. this.defaultCharset = charset;
  26. }
  27. @Override
  28. public byte[] serialize(String s) {
  29. return s.getBytes(defaultCharset);
  30. }
  31. @Override
  32. public String deserialize(byte[] bytes) {
  33. return new String(bytes);
  34. }
  35. }