KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > network > YapSocketReal


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation.network;
22
23 import java.io.*;
24 import java.net.*;
25
26 import com.db4o.*;
27
28 public class YapSocketReal implements YapSocket {
29
30     private Socket _socket;
31     private OutputStream _out;
32     private InputStream _in;
33     private String JavaDoc _hostName;
34     
35     public YapSocketReal(String JavaDoc hostName, int port) throws IOException {
36         this(new Socket(hostName, port));
37         _hostName=hostName;
38     }
39
40     public YapSocketReal(Socket socket) throws IOException {
41         _socket = socket;
42         _out = _socket.getOutputStream();
43         _in = _socket.getInputStream();
44     }
45
46     public void close() throws IOException {
47         _socket.close();
48     }
49
50     public void flush() throws IOException {
51         _out.flush();
52     }
53     
54     public boolean isConnected() {
55         return Platform4.isConnected(_socket);
56     }
57     
58     public int read() throws IOException {
59         return _in.read();
60     }
61
62     public int read(byte[] a_bytes, int a_offset, int a_length) throws IOException {
63         return _in.read(a_bytes, a_offset, a_length);
64     }
65
66     public void setSoTimeout(int timeout) {
67         try {
68             _socket.setSoTimeout(timeout);
69         } catch (SocketException e) {
70             e.printStackTrace();
71         }
72     }
73
74     public void write(byte[] bytes) throws IOException {
75         _out.write(bytes);
76     }
77
78     public void write(byte[] bytes,int off,int len) throws IOException {
79         _out.write(bytes,off,len);
80     }
81
82     public void write(int i) throws IOException {
83         _out.write(i);
84     }
85     
86     public YapSocket openParalellSocket() throws IOException {
87         if(_hostName==null) {
88             throw new IOException("Cannot open parallel socket - invalid state.");
89         }
90         return new YapSocketReal(_hostName,_socket.getPort());
91     }
92 }
93
Popular Tags