View Javadoc
1   /*
2    * Copyright 2016 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    *   http://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;
17  
18  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
19  
20  /**
21   * WriteBufferWaterMark is used to set low water mark and high water mark for the write buffer.
22   * <p>
23   * If the number of bytes queued in the write buffer exceeds the
24   * {@linkplain #high high water mark}, {@link Channel#isWritable()}
25   * will start to return {@code false}.
26   * <p>
27   * If the number of bytes queued in the write buffer exceeds the
28   * {@linkplain #high high water mark} and then
29   * dropped down below the {@linkplain #low low water mark},
30   * {@link Channel#isWritable()} will start to return
31   * {@code true} again.
32   */
33  public final class WriteBufferWaterMark {
34  
35      private static final int DEFAULT_LOW_WATER_MARK = 32 * 1024;
36      private static final int DEFAULT_HIGH_WATER_MARK = 64 * 1024;
37  
38      public static final WriteBufferWaterMark DEFAULT =
39              new WriteBufferWaterMark(DEFAULT_LOW_WATER_MARK, DEFAULT_HIGH_WATER_MARK, false);
40  
41      private final int low;
42      private final int high;
43  
44      /**
45       * Create a new instance.
46       *
47       * @param low low water mark for write buffer.
48       * @param high high water mark for write buffer
49       */
50      public WriteBufferWaterMark(int low, int high) {
51          this(low, high, true);
52      }
53  
54      /**
55       * This constructor is needed to keep backward-compatibility.
56       */
57      WriteBufferWaterMark(int low, int high, boolean validate) {
58          if (validate) {
59              checkPositiveOrZero(low, "low");
60              if (high < low) {
61                  throw new IllegalArgumentException(
62                          "write buffer's high water mark cannot be less than " +
63                                  " low water mark (" + low + "): " +
64                                  high);
65              }
66          }
67          this.low = low;
68          this.high = high;
69      }
70  
71      /**
72       * Returns the low water mark for the write buffer.
73       */
74      public int low() {
75          return low;
76      }
77  
78      /**
79       * Returns the high water mark for the write buffer.
80       */
81      public int high() {
82          return high;
83      }
84  
85      @Override
86      public String toString() {
87          StringBuilder builder = new StringBuilder(55)
88              .append("WriteBufferWaterMark(low: ")
89              .append(low)
90              .append(", high: ")
91              .append(high)
92              .append(")");
93          return builder.toString();
94      }
95  
96  }