|
| 1 | +package com.example.grpc; |
| 2 | + |
| 3 | +import com.google.protobuf.CodedInputStream; |
| 4 | +import com.google.protobuf.CodedOutputStream; |
| 5 | +import com.google.protobuf.WireFormat; |
| 6 | +import io.grpc.MethodDescriptor.Marshaller; |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.time.Instant; |
| 11 | + |
| 12 | +public record ChatMessage(String sender, String content, Instant sentAt) { |
| 13 | + public ChatMessage withSender(String sender) { |
| 14 | + return new ChatMessage(sender, content, sentAt); |
| 15 | + } |
| 16 | + |
| 17 | + public ChatMessage withContent(String content) { |
| 18 | + return new ChatMessage(sender, content, sentAt); |
| 19 | + } |
| 20 | + |
| 21 | + public ChatMessage withSentAt(Instant sentAt) { |
| 22 | + return new ChatMessage(sender, content, sentAt); |
| 23 | + } |
| 24 | + |
| 25 | + public static Marshaller<ChatMessage> MARSHALLER = |
| 26 | + new Marshaller<ChatMessage>() { |
| 27 | + @Override |
| 28 | + public InputStream stream(ChatMessage value) { |
| 29 | + var bytes = new byte[value.getSerializedSize()]; |
| 30 | + var cos = CodedOutputStream.newInstance(bytes); |
| 31 | + try { |
| 32 | + value.writeTo(cos); |
| 33 | + cos.flush(); |
| 34 | + } catch (IOException e) { |
| 35 | + throw new RuntimeException(e); |
| 36 | + } |
| 37 | + return new ByteArrayInputStream(bytes); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public ChatMessage parse(InputStream stream) { |
| 42 | + try { |
| 43 | + return ChatMessage.parseFrom(CodedInputStream.newInstance(stream)); |
| 44 | + } catch (IOException e) { |
| 45 | + throw new RuntimeException(e); |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + public static ChatMessage parseFrom(CodedInputStream input) throws IOException { |
| 51 | + String sender = ""; |
| 52 | + String content = ""; |
| 53 | + Instant sentAt = Instant.EPOCH; |
| 54 | + while (!input.isAtEnd()) { |
| 55 | + var tag = input.readTag(); |
| 56 | + if (WireFormat.getTagFieldNumber(tag) == 1) { |
| 57 | + sender = input.readString(); |
| 58 | + } else if (WireFormat.getTagFieldNumber(tag) == 2) { |
| 59 | + content = input.readString(); |
| 60 | + } else if (WireFormat.getTagFieldNumber(tag) == 3) { |
| 61 | + var _length = input.readRawVarint32(); |
| 62 | + var _oldLimit = input.pushLimit(_length); |
| 63 | + var _tsSeconds = 0L; |
| 64 | + var _tsNanos = 0; |
| 65 | + while (!input.isAtEnd()) { |
| 66 | + var _tsTag = input.readTag(); |
| 67 | + if (WireFormat.getTagFieldNumber(_tsTag) == 1) { |
| 68 | + _tsSeconds = input.readInt64(); |
| 69 | + } else if (WireFormat.getTagFieldNumber(_tsTag) == 2) { |
| 70 | + _tsNanos = input.readInt32(); |
| 71 | + } else { |
| 72 | + input.skipField(_tsTag); |
| 73 | + } |
| 74 | + ; |
| 75 | + } |
| 76 | + ; |
| 77 | + sentAt = Instant.ofEpochSecond(_tsSeconds, (long) (_tsNanos)); |
| 78 | + input.popLimit(_oldLimit); |
| 79 | + ; |
| 80 | + } else { |
| 81 | + input.skipField(tag); |
| 82 | + } |
| 83 | + ; |
| 84 | + } |
| 85 | + ; |
| 86 | + return new ChatMessage(sender, content, sentAt); |
| 87 | + } |
| 88 | + |
| 89 | + public Integer getSerializedSize() { |
| 90 | + Integer size = 0; |
| 91 | + size = size + CodedOutputStream.computeStringSize(1, this.sender()); |
| 92 | + size = size + CodedOutputStream.computeStringSize(2, this.content()); |
| 93 | + size = |
| 94 | + size |
| 95 | + + CodedOutputStream.computeTagSize(3) |
| 96 | + + CodedOutputStream.computeUInt32SizeNoTag( |
| 97 | + CodedOutputStream.computeInt64Size(1, this.sentAt().getEpochSecond()) |
| 98 | + + CodedOutputStream.computeInt32Size(2, this.sentAt().getNano())) |
| 99 | + + CodedOutputStream.computeInt64Size(1, this.sentAt().getEpochSecond()) |
| 100 | + + CodedOutputStream.computeInt32Size(2, this.sentAt().getNano()); |
| 101 | + return size; |
| 102 | + } |
| 103 | + |
| 104 | + public void writeTo(CodedOutputStream output) throws IOException { |
| 105 | + output.writeString(1, this.sender()); |
| 106 | + output.writeString(2, this.content()); |
| 107 | + output.writeTag(3, 2); |
| 108 | + output.writeUInt32NoTag( |
| 109 | + CodedOutputStream.computeInt64Size(1, this.sentAt().getEpochSecond()) |
| 110 | + + CodedOutputStream.computeInt32Size(2, this.sentAt().getNano())); |
| 111 | + output.writeInt64(1, this.sentAt().getEpochSecond()); |
| 112 | + output.writeInt32(2, this.sentAt().getNano()); |
| 113 | + } |
| 114 | +} |
0 commit comments