View Javadoc
1   /*
2    * Copyright 2022 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.netty.channel.unix;
17  
18  
19  import io.netty.util.internal.ObjectUtil;
20  
21  import java.nio.ByteBuffer;
22  
23  /**
24   * A {@link GenericUnixChannelOption} which uses an {@link ByteBuffer} as {@code optval}. The user is responsible
25   * to fill the {@link ByteBuffer} in a correct manner, so it works with the {@param level} and {@param optname}.
26   */
27  public final class RawUnixChannelOption extends GenericUnixChannelOption<ByteBuffer> {
28  
29      private final int length;
30  
31      /**
32       * Creates a new instance.
33       *
34       * @param name      the name that is used.
35       * @param level     the level.
36       * @param length    the expected length of the optvalue.
37       * @param optname   the optname.
38       */
39      public RawUnixChannelOption(String name, int level, int optname, int length) {
40          super(name, level, optname);
41          this.length = ObjectUtil.checkPositive(length, "length");
42      }
43  
44      /**
45       * The length of the optval.
46       *
47       * @return the length.
48       */
49      public int length() {
50          return length;
51      }
52  
53      @Override
54      public void validate(ByteBuffer value) {
55          super.validate(value);
56          if (value.remaining() != length) {
57              throw new IllegalArgumentException("Length of value does not match. Expected "
58                      + length + ", but got " + value.remaining());
59          }
60      }
61  }