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.unix;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.util.internal.ObjectUtil;
20  import java.nio.channels.WritableByteChannel;
21  
22  public abstract class SocketWritableByteChannel implements WritableByteChannel {
23      private final FileDescriptor fd;
24  
25      protected SocketWritableByteChannel(FileDescriptor fd) {
26          this.fd = ObjectUtil.checkNotNull(fd, "fd");
27      }
28  
29      @Override
30      public final int write(java.nio.ByteBuffer src) throws java.io.IOException {
31          final int written;
32          int position = src.position();
33          int limit = src.limit();
34          if (src.isDirect()) {
35              written = fd.write(src, position, src.limit());
36          } else {
37              final int readableBytes = limit - position;
38              io.netty.buffer.ByteBuf buffer = null;
39              try {
40                  if (readableBytes == 0) {
41                      buffer = io.netty.buffer.Unpooled.EMPTY_BUFFER;
42                  } else {
43                      final ByteBufAllocator alloc = alloc();
44                      if (alloc.isDirectBufferPooled()) {
45                          buffer = alloc.directBuffer(readableBytes);
46                      } else {
47                          buffer = io.netty.buffer.ByteBufUtil.threadLocalDirectBuffer();
48                          if (buffer == null) {
49                              buffer = io.netty.buffer.Unpooled.directBuffer(readableBytes);
50                          }
51                      }
52                  }
53                  buffer.writeBytes(src.duplicate());
54                  java.nio.ByteBuffer nioBuffer = buffer.internalNioBuffer(buffer.readerIndex(), readableBytes);
55                  written = fd.write(nioBuffer, nioBuffer.position(), nioBuffer.limit());
56              } finally {
57                  if (buffer != null) {
58                      buffer.release();
59                  }
60              }
61          }
62          if (written > 0) {
63              src.position(position + written);
64          }
65          return written;
66      }
67  
68      @Override
69      public final boolean isOpen() {
70          return fd.isOpen();
71      }
72  
73      @Override
74      public final void close() throws java.io.IOException {
75          fd.close();
76      }
77  
78      protected abstract ByteBufAllocator alloc();
79  }