source: trunk/src/gcc/libjava/java/net/PlainSocketImpl.java@ 819

Last change on this file since 819 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.2 KB
Line 
1// PlainSocketImpl.java - Implementation of SocketImpl.
2
3/* Copyright (C) 1999 , 2002 Free Software Foundation
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11package java.net;
12import java.io.*;
13
14
15/**
16 * The standard GCJ socket implementation.
17 * Written using on-line Java Platform 1.2 API Specification, as well
18 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
19 * Status: Believed complete and correct.
20 *
21 * @author Per Bothner <[email protected]>
22 * @author Nic Ferrier <[email protected]>
23 */
24class PlainSocketImpl extends SocketImpl
25{
26 // These fields are mirrored for use in native code to avoid cpp conflicts
27 // when the #defines in system header files are the same as the public fields.
28 static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
29 _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
30 _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
31 _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
32 _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
33 _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
34 _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
35 _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
36
37 /**
38 * The OS file handle representing the socket.
39 * This is used for reads and writes to/from the socket and
40 * to close it.
41 *
42 * When the socket is closed this is reset to -1.
43 */
44 int fnum = -1;
45
46 // This value is set/read by setOption/getOption.
47 int timeout = 0;
48
49 // localAddress cache
50 InetAddress localAddress;
51
52 public native void setOption(int optID, Object value) throws SocketException;
53
54 public native Object getOption(int optID) throws SocketException;
55
56 protected native void create (boolean stream) throws IOException;
57
58 protected void connect (String host, int port) throws IOException
59 {
60 connect(InetAddress.getByName(host), port);
61 }
62
63 protected native void connect (InetAddress host, int port)
64 throws IOException;
65
66 protected native void bind (InetAddress host, int port) throws IOException;
67
68 protected native void listen (int backlog) throws IOException;
69
70 private native void accept (PlainSocketImpl s) throws IOException;
71
72 protected void accept (SocketImpl s) throws IOException
73 {
74 accept((PlainSocketImpl) s);
75 }
76
77 protected native int available() throws IOException;
78
79 protected native void close () throws IOException;
80
81
82 // Stream handling.
83
84 /** A cached copy of the in stream for reading from the socket. */
85 private InputStream in;
86
87 /** A cached copy of the out stream for writing to the socket. */
88 private OutputStream out;
89
90
91 // The native read methods.
92
93 private native int read() throws IOException;
94
95 private native int read(byte[] buffer, int offset, int count)
96 throws IOException;
97
98
99 // The native write methods.
100
101 private native void write(int c) throws IOException;
102
103 private native void write(byte[] buffer, int offset, int count)
104 throws IOException;
105
106 protected void finalize() throws Throwable
107 {
108 synchronized (this)
109 {
110 if (fnum != -1)
111 try
112 {
113 close();
114 }
115 catch (IOException ex)
116 {
117 // ignore
118 }
119 }
120 super.finalize();
121 }
122
123 /** @return the input stream attached to the socket.
124 */
125 protected InputStream getInputStream() throws IOException
126 {
127 if (in == null)
128 in = new SocketInputStream();
129 return in;
130 }
131
132 /** @return the output stream attached to the socket.
133 */
134 protected OutputStream getOutputStream() throws IOException
135 {
136 if (out == null)
137 out = new SocketOutputStream();
138 return out;
139 }
140
141 /**
142 * A stream which reads from the socket implementation.
143 *
144 * @author Nic Ferrier <[email protected]>
145 */
146 class SocketInputStream
147 extends InputStream
148 {
149 SocketInputStream()
150 {
151 }
152
153 public final void close() throws IOException
154 {
155 PlainSocketImpl.this.close();
156 }
157
158 public final int available() throws IOException
159 {
160 return PlainSocketImpl.this.available();
161 }
162
163 public final int read() throws IOException
164 {
165 return PlainSocketImpl.this.read();
166 }
167
168 public final int read(byte[] buffer, int offset, int length)
169 throws IOException
170 {
171 return PlainSocketImpl.this.read(buffer, offset, length);
172 }
173
174 public final int read(byte[] buffer)
175 throws IOException
176 {
177 return PlainSocketImpl.this.read(buffer, 0, buffer.length);
178 }
179 }
180
181 /** A stream which writes to the socket implementation.
182 *
183 * @author Nic Ferrier <[email protected]>
184 */
185 class SocketOutputStream
186 extends OutputStream
187 {
188 public final void close() throws IOException
189 {
190 PlainSocketImpl.this.close();
191 }
192
193 public final void write(int c) throws IOException
194 {
195 PlainSocketImpl.this.write(c);
196 }
197
198 public final void write(byte[] buffer, int offset, int length)
199 throws IOException
200 {
201 PlainSocketImpl.this.write(buffer, offset, length);
202 }
203
204 public final void write(byte[] buffer)
205 throws IOException
206 {
207 PlainSocketImpl.this.write(buffer, 0, buffer.length);
208 }
209 }
210}
Note: See TracBrowser for help on using the repository browser.