KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > file > SocketReadWrite


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.file;
31
32 import com.caucho.quercus.env.Env;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.SocketStream;
35 import com.caucho.vfs.WriteStream;
36
37 import java.io.IOException JavaDoc;
38 import java.net.Socket JavaDoc;
39 import java.net.SocketAddress JavaDoc;
40
41 /**
42  * Represents read/write stream
43  */

44 public class SocketReadWrite extends AbstractBinaryInputOutput
45 {
46   public enum Domain { AF_INET, AF_INET6, AF_UNIX };
47
48   private int _lastError;
49   private Domain _domain;
50   private Socket JavaDoc _socket;
51
52   public SocketReadWrite(Env env, Socket JavaDoc socket, Domain domain)
53   {
54     env.addClose(this);
55     
56     _socket = socket;
57     _domain = domain;
58   }
59
60   public void bind(SocketAddress JavaDoc address)
61     throws IOException JavaDoc
62   {
63     _socket.bind(address);
64   }
65
66   public void connect(SocketAddress JavaDoc address)
67     throws IOException JavaDoc
68   {
69     _socket.connect(address);
70
71     init();
72   }
73
74   public void setError(int error)
75   {
76     _lastError = error;
77   }
78
79   public void init()
80   {
81     SocketStream sock = new SocketStream(_socket);
82
83     WriteStream os = new WriteStream(sock);
84     ReadStream is = new ReadStream(sock, os);
85
86     init(is, os);
87   }
88
89   /**
90    * Closes the stream.
91    */

92   public void close()
93   {
94     super.close();
95     
96     Socket JavaDoc s = _socket;
97     _socket = null;
98
99     try {
100       if (s != null)
101         s.close();
102     } catch (IOException JavaDoc e) {
103     }
104   }
105
106   public String JavaDoc toString()
107   {
108     if (_socket != null)
109       return "SocketReadWrite[" + _socket.getInetAddress() + "," + _socket.getPort() + "]";
110     else
111       return "SocketReadWrite[closed]";
112   }
113 }
114
115
Popular Tags