View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a 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
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec.http2;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.buffer.api.BufferHolder;
20  import io.netty5.util.internal.StringUtil;
21  import io.netty5.util.internal.UnstableApi;
22  
23  import java.nio.charset.StandardCharsets;
24  
25  import static io.netty5.buffer.api.DefaultBufferAllocators.onHeapAllocator;
26  
27  @UnstableApi
28  public final class DefaultHttp2UnknownFrame extends BufferHolder<Http2UnknownFrame> implements Http2UnknownFrame {
29      private final byte frameType;
30      private final Http2Flags flags;
31      private Http2FrameStream stream;
32  
33      public DefaultHttp2UnknownFrame(byte frameType, Http2Flags flags) {
34          this(frameType, flags, onHeapAllocator().allocate(0));
35      }
36  
37      public DefaultHttp2UnknownFrame(byte frameType, Http2Flags flags, Buffer data) {
38          super(data);
39          this.frameType = frameType;
40          this.flags = flags;
41      }
42  
43      @Override
44      public Buffer content() {
45          return getBuffer();
46      }
47  
48      @Override
49      public Http2FrameStream stream() {
50          return stream;
51      }
52  
53      @Override
54      public DefaultHttp2UnknownFrame stream(Http2FrameStream stream) {
55          this.stream = stream;
56          return this;
57      }
58  
59      @Override
60      public byte frameType() {
61          return frameType;
62      }
63  
64      @Override
65      public Http2Flags flags() {
66          return flags;
67      }
68  
69      @Override
70      public String name() {
71          return "UNKNOWN";
72      }
73  
74      @Override
75      public DefaultHttp2UnknownFrame copy() {
76          return receive(getBuffer().copy());
77      }
78  
79      @Override
80      public String toString() {
81          return StringUtil.simpleClassName(this) + "(frameType=" + frameType + ", stream=" + stream +
82                 ", flags=" + flags + ", content=" + getBuffer().toString(StandardCharsets.UTF_8) + ')';
83      }
84  
85      @Override
86      protected DefaultHttp2UnknownFrame receive(Buffer buf) {
87          return new DefaultHttp2UnknownFrame(frameType, flags, buf).stream(stream);
88      }
89  
90      @Override
91      public boolean equals(Object o) {
92          if (!(o instanceof DefaultHttp2UnknownFrame)) {
93              return false;
94          }
95          DefaultHttp2UnknownFrame other = (DefaultHttp2UnknownFrame) o;
96          Http2FrameStream otherStream = other.stream();
97          return (stream == otherStream || otherStream != null && otherStream.equals(stream))
98                 && flags.equals(other.flags())
99                 && frameType == other.frameType()
100                && super.equals(other);
101     }
102 
103     @Override
104     public int hashCode() {
105         int hash = super.hashCode();
106         hash = hash * 31 + frameType;
107         hash = hash * 31 + flags.hashCode();
108         if (stream != null) {
109             hash = hash * 31 + stream.hashCode();
110         }
111 
112         return hash;
113     }
114 }