1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import io.netty.util.internal.StringUtil;
21
22 import java.util.Map.Entry;
23
24 import static io.netty.handler.codec.http.DefaultHttpHeadersFactory.trailersFactory;
25 import static io.netty.util.internal.ObjectUtil.checkNotNull;
26
27
28
29
30 public class DefaultLastHttpContent extends DefaultHttpContent implements LastHttpContent {
31 private final HttpHeaders trailingHeaders;
32
33
34
35
36 public DefaultLastHttpContent() {
37 this(Unpooled.buffer(0));
38 }
39
40
41
42
43 public DefaultLastHttpContent(ByteBuf content) {
44 this(content, trailersFactory());
45 }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Deprecated
63 public DefaultLastHttpContent(ByteBuf content, boolean validateHeaders) {
64 this(content, trailersFactory().withValidation(validateHeaders));
65 }
66
67
68
69
70 public DefaultLastHttpContent(ByteBuf content, HttpHeadersFactory trailersFactory) {
71 super(content);
72 trailingHeaders = trailersFactory.newHeaders();
73 }
74
75
76
77
78 public DefaultLastHttpContent(ByteBuf content, HttpHeaders trailingHeaders) {
79 super(content);
80 this.trailingHeaders = checkNotNull(trailingHeaders, "trailingHeaders");
81 }
82
83 @Override
84 public LastHttpContent copy() {
85 return replace(content().copy());
86 }
87
88 @Override
89 public LastHttpContent duplicate() {
90 return replace(content().duplicate());
91 }
92
93 @Override
94 public LastHttpContent retainedDuplicate() {
95 return replace(content().retainedDuplicate());
96 }
97
98 @Override
99 public LastHttpContent replace(ByteBuf content) {
100 return new DefaultLastHttpContent(content, trailingHeaders().copy());
101 }
102
103 @Override
104 public LastHttpContent retain(int increment) {
105 super.retain(increment);
106 return this;
107 }
108
109 @Override
110 public LastHttpContent retain() {
111 super.retain();
112 return this;
113 }
114
115 @Override
116 public LastHttpContent touch() {
117 super.touch();
118 return this;
119 }
120
121 @Override
122 public LastHttpContent touch(Object hint) {
123 super.touch(hint);
124 return this;
125 }
126
127 @Override
128 public HttpHeaders trailingHeaders() {
129 return trailingHeaders;
130 }
131
132 @Override
133 public String toString() {
134 StringBuilder buf = new StringBuilder(super.toString());
135 buf.append(StringUtil.NEWLINE);
136 appendHeaders(buf);
137
138
139 buf.setLength(buf.length() - StringUtil.NEWLINE.length());
140 return buf.toString();
141 }
142
143 private void appendHeaders(StringBuilder buf) {
144 for (Entry<String, String> e : trailingHeaders()) {
145 buf.append(e.getKey());
146 buf.append(": ");
147 buf.append(e.getValue());
148 buf.append(StringUtil.NEWLINE);
149 }
150 }
151 }