001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.foundation.network;
022:
023: import java.io.*;
024: import java.net.*;
025:
026: import com.db4o.*;
027: import com.db4o.config.*;
028: import com.db4o.internal.*;
029:
030: public class NetworkSocket implements Socket4 {
031:
032: private Socket _socket;
033: private OutputStream _out;
034: private InputStream _in;
035: private String _hostName;
036: private NativeSocketFactory _factory;
037:
038: public NetworkSocket(NativeSocketFactory factory, String hostName,
039: int port) throws Db4oIOException {
040: _factory = factory;
041: try {
042: Socket socket = _factory.createSocket(hostName, port);
043: initSocket(socket);
044: } catch (IOException e) {
045: throw new Db4oIOException(e);
046: }
047: _hostName = hostName;
048: }
049:
050: public NetworkSocket(NativeSocketFactory factory, Socket socket)
051: throws IOException {
052: _factory = factory;
053: initSocket(socket);
054: }
055:
056: private void initSocket(Socket socket) throws IOException {
057: _socket = socket;
058: _out = _socket.getOutputStream();
059: _in = _socket.getInputStream();
060: }
061:
062: public void close() throws Db4oIOException {
063: try {
064: _socket.close();
065: } catch (IOException e) {
066: throw new Db4oIOException(e);
067: }
068: }
069:
070: public void flush() throws Db4oIOException {
071: try {
072: _out.flush();
073: } catch (IOException e) {
074: throw new Db4oIOException(e);
075: }
076: }
077:
078: public boolean isConnected() {
079: return Platform4.isConnected(_socket);
080: }
081:
082: public int read() throws Db4oIOException {
083: try {
084: int ret = _in.read();
085: checkEOF(ret);
086: return ret;
087: } catch (IOException e) {
088: throw new Db4oIOException(e);
089: }
090: }
091:
092: public int read(byte[] a_bytes, int a_offset, int a_length)
093: throws Db4oIOException {
094: try {
095: int ret = _in.read(a_bytes, a_offset, a_length);
096: checkEOF(ret);
097: return ret;
098: } catch (IOException e) {
099: throw new Db4oIOException(e);
100: }
101: }
102:
103: private void checkEOF(int ret) {
104: if (ret == -1) {
105: throw new Db4oIOException();
106: }
107: }
108:
109: public void setSoTimeout(int timeout) {
110: try {
111: _socket.setSoTimeout(timeout);
112: } catch (SocketException e) {
113: e.printStackTrace();
114: }
115: }
116:
117: public void write(byte[] bytes) throws Db4oIOException {
118: try {
119: _out.write(bytes);
120: } catch (IOException e) {
121: throw new Db4oIOException(e);
122: }
123: }
124:
125: public void write(byte[] bytes, int off, int len)
126: throws Db4oIOException {
127: try {
128: _out.write(bytes, off, len);
129: } catch (IOException e) {
130: throw new Db4oIOException(e);
131: }
132: }
133:
134: public void write(int i) throws Db4oIOException {
135: try {
136: _out.write(i);
137: } catch (IOException e) {
138: throw new Db4oIOException(e);
139: }
140: }
141:
142: public Socket4 openParalellSocket() throws Db4oIOException {
143: if (_hostName == null) {
144: throw new IllegalStateException();
145: }
146: return new NetworkSocket(_factory, _hostName, _socket.getPort());
147: }
148: }
|