001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.lib.file;
031:
032: import com.caucho.quercus.env.Env;
033: import com.caucho.quercus.env.EnvCleanup;
034: import com.caucho.vfs.ReadStream;
035: import com.caucho.vfs.SocketStream;
036: import com.caucho.vfs.WriteStream;
037:
038: import java.io.InputStream;
039: import java.io.IOException;
040: import java.net.Socket;
041: import java.net.SocketAddress;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * Represents read/write stream
047: */
048: public class SocketInputOutput extends AbstractBinaryInputOutput
049: implements EnvCleanup {
050: private static final Logger log = Logger
051: .getLogger(SocketInputOutput.class.getName());
052:
053: public enum Domain {
054: AF_INET, AF_INET6, AF_UNIX
055: };
056:
057: private int _lastErrorCode;
058: private String _lastErrorString;
059:
060: private Domain _domain;
061: private Socket _socket;
062:
063: public SocketInputOutput(Env env, Socket socket, Domain domain) {
064: super (env);
065:
066: env.addCleanup(this );
067:
068: _socket = socket;
069: _domain = domain;
070: }
071:
072: public void bind(SocketAddress address) throws IOException {
073: _socket.bind(address);
074: }
075:
076: public void connect(SocketAddress address) throws IOException {
077: _socket.connect(address);
078:
079: init();
080: }
081:
082: public void setError(int error) {
083: _lastErrorCode = error;
084: }
085:
086: public void init() {
087: SocketStream sock = new SocketStream(_socket);
088:
089: WriteStream os = new WriteStream(sock);
090: ReadStream is = new ReadStream(sock, os);
091:
092: init(is, os);
093: }
094:
095: public void setTimeout(long timeout) {
096: try {
097: if (_socket != null)
098: _socket.setSoTimeout((int) timeout);
099: } catch (Exception e) {
100: log.log(Level.FINER, e.toString(), e);
101: }
102: }
103:
104: public void write(int ch) throws IOException {
105: super .write(ch);
106:
107: flush();
108: }
109:
110: public void write(byte[] buffer, int offset, int length)
111: throws IOException {
112: super .write(buffer, offset, length);
113:
114: try {
115: flush();
116: } catch (IOException e) {
117: log.log(Level.FINE, e.toString(), e);
118: }
119: }
120:
121: /**
122: * Read length bytes of data from the InputStream
123: * argument and write them to this output stream.
124: */
125: public int write(InputStream is, int length) {
126: int result = super .write(is, length);
127:
128: try {
129: flush();
130: } catch (IOException e) {
131: log.log(Level.FINE, e.toString(), e);
132: }
133:
134: return result;
135: }
136:
137: /**
138: * Closes the stream.
139: */
140: public void close() {
141: getEnv().removeCleanup(this );
142:
143: cleanup();
144: }
145:
146: /**
147: * Implements the EnvCleanup interface.
148: */
149: public void cleanup() {
150: super .close();
151:
152: Socket s = _socket;
153: _socket = null;
154:
155: try {
156: if (s != null)
157: s.close();
158: } catch (IOException e) {
159: log.log(Level.FINE, e.toString(), e);
160: }
161: }
162:
163: public String toString() {
164: if (_socket != null)
165: return "SocketInputOutput[" + _socket.getInetAddress()
166: + "," + _socket.getPort() + "]";
167: else
168: return "SocketInputOutput[closed]";
169: }
170: }
|