View Javadoc
1   /*
2    * Copyright 2021 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty5.handler.codec.http;
16  
17  import io.netty5.buffer.api.Buffer;
18  import io.netty5.buffer.api.BufferAllocator;
19  import io.netty5.util.Send;
20  import io.netty5.handler.codec.DecoderResult;
21  
22  import java.util.Objects;
23  
24  public final class EmptyLastHttpContent implements LastHttpContent<EmptyLastHttpContent> {
25      private final Buffer payload;
26      private final BufferAllocator allocator;
27  
28      public EmptyLastHttpContent(BufferAllocator allocator) {
29          payload = allocator.allocate(0);
30          this.allocator = allocator;
31      }
32  
33      @Override
34      public Buffer payload() {
35          return payload;
36      }
37  
38      @Override
39      public EmptyLastHttpContent copy() {
40          return new EmptyLastHttpContent(allocator);
41      }
42  
43      @Override
44      public Send<EmptyLastHttpContent> send() {
45          return Send.sending(EmptyLastHttpContent.class, () -> new EmptyLastHttpContent(allocator));
46      }
47  
48      @Override
49      public void close() {
50          payload.close();
51      }
52  
53      @Override
54      public boolean isAccessible() {
55          return payload.isAccessible();
56      }
57  
58      @Override
59      public EmptyLastHttpContent touch(Object hint) {
60          payload.touch(hint);
61          return this;
62      }
63  
64      @Override
65      public HttpHeaders trailingHeaders() {
66          return EmptyHttpHeaders.INSTANCE;
67      }
68  
69      @Override
70      public DecoderResult decoderResult() {
71          return DecoderResult.success();
72      }
73  
74      @Override
75      public void setDecoderResult(DecoderResult result) {
76          throw new UnsupportedOperationException("read only");
77      }
78  
79      @Override
80      public String toString() {
81          return "EmptyLastHttpContent";
82      }
83  
84      @Override
85      public boolean equals(Object o) {
86          if (this == o) {
87              return true;
88          }
89          if (o == null || getClass() != o.getClass()) {
90              return false;
91          }
92  
93          EmptyLastHttpContent that = (EmptyLastHttpContent) o;
94  
95          return Objects.equals(payload, that.payload) && Objects.equals(allocator, that.allocator);
96      }
97  
98      @Override
99      public int hashCode() {
100         int result = payload != null ? payload.hashCode() : 0;
101         result = 31 * result + (allocator != null ? allocator.hashCode() : 0);
102         return result;
103     }
104 }