View Javadoc
1   /*
2    * Copyright 2014 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.epoll;
17  
18  import io.netty.buffer.CompositeByteBuf;
19  import io.netty.channel.ChannelOutboundBuffer;
20  import io.netty.util.concurrent.FastThreadLocal;
21  
22  /**
23   * Allow to obtain {@link IovArray} instances.
24   */
25  final class IovArrayThreadLocal {
26  
27      private static final FastThreadLocal<IovArray> ARRAY = new FastThreadLocal<IovArray>() {
28          @Override
29          protected IovArray initialValue() throws Exception {
30              return new IovArray();
31          }
32  
33          @Override
34          protected void onRemoval(IovArray value) throws Exception {
35              // free the direct memory now
36              value.release();
37          }
38      };
39  
40      /**
41       * Returns a {@link IovArray} which is filled with the flushed messages of {@link ChannelOutboundBuffer}.
42       */
43      static IovArray get(ChannelOutboundBuffer buffer) throws Exception {
44          IovArray array = ARRAY.get();
45          array.clear();
46          buffer.forEachFlushedMessage(array);
47          return array;
48      }
49  
50      /**
51       * Returns a {@link IovArray} which is filled with the {@link CompositeByteBuf}.
52       */
53      static IovArray get(CompositeByteBuf buf) throws Exception {
54          IovArray array = ARRAY.get();
55          array.clear();
56          array.add(buf);
57          return array;
58      }
59  
60      private IovArrayThreadLocal() { }
61  }