|
|
@@ -0,0 +1,102 @@
|
|
|
+package space.anyi.chatCommom;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.nio.ByteBuffer;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: chat-gwng
|
|
|
+ * @FileName: Encoder
|
|
|
+ * @Author: 杨逸
|
|
|
+ * @Data:2025/9/23 11:21
|
|
|
+ * @Description: 自定义的数据编码器
|
|
|
+ */
|
|
|
+public class Encoder {
|
|
|
+ //防止实例化
|
|
|
+ private Encoder() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param message 信息
|
|
|
+ * @return {@code byte[] }
|
|
|
+ * @description: 消息编码
|
|
|
+ * @author: 杨逸
|
|
|
+ * @data:2025/09/23 11:33:20
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+ public static byte[] encode(Message message) {
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ ObjectOutputStream objectOutputStream = null;
|
|
|
+ try {
|
|
|
+ objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
|
|
|
+ objectOutputStream.writeObject(message);
|
|
|
+ objectOutputStream.flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (Objects.nonNull(objectOutputStream))
|
|
|
+ objectOutputStream.close();
|
|
|
+ if (Objects.nonNull(byteArrayOutputStream))
|
|
|
+ byteArrayOutputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ byte[] bytes = byteArrayOutputStream.toByteArray();
|
|
|
+ int size = bytes.length;
|
|
|
+
|
|
|
+ ByteBuffer buffer = ByteBuffer.allocate(size + 4);
|
|
|
+ buffer.putInt(size);
|
|
|
+ buffer.put(bytes);
|
|
|
+ buffer.flip();
|
|
|
+
|
|
|
+ return buffer.array();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param bytes 数据
|
|
|
+ * @return {@code Message }
|
|
|
+ * @description: 消息解码
|
|
|
+ * @author: 杨逸
|
|
|
+ * @data:2025/09/23 11:38:03
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+ public static Message decode(byte[] bytes) {
|
|
|
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
|
|
+ ObjectInputStream objectInputStream = null;
|
|
|
+ Object object = null;
|
|
|
+ try {
|
|
|
+ objectInputStream = new ObjectInputStream(byteArrayInputStream);
|
|
|
+ //字节转对象
|
|
|
+ object = objectInputStream.readObject();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ //关闭资源
|
|
|
+ try {
|
|
|
+ if (Objects.nonNull(objectInputStream))
|
|
|
+ byteArrayInputStream.close();
|
|
|
+ if (Objects.nonNull(objectInputStream))
|
|
|
+ objectInputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (Message) object;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param byteBuffer
|
|
|
+ * @return {@code Message }
|
|
|
+ * @description: 信息解码
|
|
|
+ * @author: 杨逸
|
|
|
+ * @data:2025/09/23 11:44:05
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+ public static Message decode(ByteBuffer byteBuffer) {
|
|
|
+ return decode(byteBuffer.array());
|
|
|
+ }
|
|
|
+}
|