View Javadoc

1   /*
2    * Copyright 2012 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 org.jboss.netty.channel.socket.oio;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.io.PushbackInputStream;
21  import java.net.InetSocketAddress;
22  import java.net.Socket;
23  import java.net.SocketException;
24  
25  import org.jboss.netty.channel.Channel;
26  import org.jboss.netty.channel.ChannelSink;
27  import org.jboss.netty.channel.ChannelException;
28  import org.jboss.netty.channel.ChannelFactory;
29  import org.jboss.netty.channel.ChannelPipeline;
30  import org.jboss.netty.channel.socket.DefaultSocketChannelConfig;
31  import org.jboss.netty.channel.socket.SocketChannel;
32  import org.jboss.netty.channel.socket.SocketChannelConfig;
33  
34  abstract class OioSocketChannel extends AbstractOioChannel
35                                  implements SocketChannel {
36  
37      final Socket socket;
38      private final SocketChannelConfig config;
39  
40      OioSocketChannel(
41              Channel parent,
42              ChannelFactory factory,
43              ChannelPipeline pipeline,
44              ChannelSink sink,
45              Socket socket) {
46  
47          super(parent, factory, pipeline, sink);
48  
49          this.socket = socket;
50          try {
51              socket.setSoTimeout(1000);
52          } catch (SocketException e) {
53              throw new ChannelException(
54                      "Failed to configure the OioSocketChannel socket timeout.", e);
55          }
56          config = new DefaultSocketChannelConfig(socket);
57      }
58  
59      public SocketChannelConfig getConfig() {
60          return config;
61      }
62  
63      abstract PushbackInputStream getInputStream();
64      abstract OutputStream getOutputStream();
65  
66      @Override
67      boolean isSocketBound() {
68          return socket.isBound();
69      }
70  
71      @Override
72      boolean isSocketConnected() {
73          return socket.isConnected();
74      }
75  
76      @Override
77      InetSocketAddress getLocalSocketAddress() throws Exception {
78          return (InetSocketAddress) socket.getLocalSocketAddress();
79      }
80  
81      @Override
82      InetSocketAddress getRemoteSocketAddress() throws Exception {
83          return (InetSocketAddress) socket.getRemoteSocketAddress();
84      }
85  
86      @Override
87      void closeSocket() throws IOException {
88          socket.close();
89      }
90  
91      @Override
92      boolean isSocketClosed() {
93          return socket.isClosed();
94      }
95  }