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  /**
19   * WriteBufferWaterMark is used to set low water mark and high water mark for the write buffer.
20   * <p>
21   * If the number of bytes queued in the write buffer exceeds the
22   * {@linkplain #high high water mark}, {@link Channel#isWritable()}
23   * will start to return {@code false}.
24   * <p>
25   * If the number of bytes queued in the write buffer exceeds the
26   * {@linkplain #high high water mark} and then
27   * dropped down below the {@linkplain #low low water mark},
28   * {@link Channel#isWritable()} will start to return
29   * {@code true} again.
30   */
31  public final class WriteBufferWaterMark {
32  
33      private static final int DEFAULT_LOW_WATER_MARK = 32 * 1024;
34      private static final int DEFAULT_HIGH_WATER_MARK = 64 * 1024;
35  
36      public static final WriteBufferWaterMark DEFAULT =
37              new WriteBufferWaterMark(DEFAULT_LOW_WATER_MARK, DEFAULT_HIGH_WATER_MARK, false);
38  
39      private final int low;
40      private final int high;
41  
42      /**
43       * Create a new instance.
44       *
45       * @param low low water mark for write buffer.
46       * @param high high water mark for write buffer
47       */
48      public WriteBufferWaterMark(int low, int high) {
49          this(low, high, true);
50      }
51  
52      /**
53       * This constructor is needed to keep backward-compatibility.
54       */
55      WriteBufferWaterMark(int low, int high, boolean validate) {
56          if (validate) {
57              if (low < 0) {
58                  throw new IllegalArgumentException("write buffer's low water mark must be >= 0");
59              }
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  }