1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.string;
17
18 import io.netty5.buffer.api.Buffer;
19 import io.netty5.channel.ChannelHandlerContext;
20 import io.netty5.channel.ChannelPipeline;
21 import io.netty5.handler.codec.LineBasedFrameDecoder;
22 import io.netty5.handler.codec.MessageToMessageEncoder;
23 import io.netty5.util.CharsetUtil;
24 import io.netty5.util.internal.SilentDispose;
25 import io.netty5.util.internal.logging.InternalLogger;
26 import io.netty5.util.internal.logging.InternalLoggerFactory;
27
28 import java.nio.ByteBuffer;
29 import java.nio.CharBuffer;
30 import java.nio.charset.Charset;
31 import java.nio.charset.CharsetEncoder;
32 import java.nio.charset.CoderResult;
33 import java.util.List;
34
35 import static java.util.Objects.requireNonNull;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class LineEncoder extends MessageToMessageEncoder<CharSequence> {
59 private static final InternalLogger logger = InternalLoggerFactory.getInstance(LineEncoder.class);
60
61 private final Charset charset;
62 private final byte[] lineSeparator;
63
64
65
66
67 public LineEncoder() {
68 this(LineSeparator.DEFAULT, CharsetUtil.UTF_8);
69 }
70
71
72
73
74 public LineEncoder(LineSeparator lineSeparator) {
75 this(lineSeparator, CharsetUtil.UTF_8);
76 }
77
78
79
80
81 public LineEncoder(Charset charset) {
82 this(LineSeparator.DEFAULT, charset);
83 }
84
85
86
87
88 public LineEncoder(LineSeparator lineSeparator, Charset charset) {
89 this.charset = requireNonNull(charset, "charset");
90 this.lineSeparator = requireNonNull(lineSeparator, "lineSeparator").value().getBytes(charset);
91 }
92
93 @Override
94 public boolean isSharable() {
95 return true;
96 }
97
98 @Override
99 protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception {
100 CharsetEncoder encoder = CharsetUtil.encoder(charset);
101 int size = (int) (msg.length() * encoder.maxBytesPerChar() + lineSeparator.length);
102 Buffer buf = ctx.bufferAllocator().allocate(size);
103 assert buf.countWritableComponents() == 1;
104 boolean release = true;
105 CharBuffer chars = CharBuffer.wrap(msg);
106 try (var iterator = buf.forEachWritable()) {
107 var component = requireNonNull(iterator.first(), "writable component");
108 ByteBuffer byteBuffer = component.writableBuffer();
109 int start = byteBuffer.position();
110 CoderResult result = encoder.encode(chars, byteBuffer, true);
111 if (!result.isUnderflow()) {
112 result.throwException();
113 }
114 encoder.flush(byteBuffer);
115 if (!result.isUnderflow()) {
116 result.throwException();
117 }
118 component.skipWritableBytes(byteBuffer.position() - start);
119 release = false;
120 } finally {
121 if (release) {
122 SilentDispose.dispose(buf, logger);
123 }
124 }
125 buf.writeBytes(lineSeparator);
126 out.add(buf);
127 }
128 }