summaryrefslogtreecommitdiff
path: root/java/src/SOCKStream.java
blob: 638482d8e4077b2975e7bbad4c56a44ef492532e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*************************************************
 *
 * = PACKAGE
 *    JACE.SOCK_SAP
 *
 * = FILENAME
 *    SOCKStream.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.SOCK_SAP;

import java.io.*;
import java.net.*;
import JACE.OS.*;

/**
 * <hr>
 * <p><b>TITLE</b><br>
 *     Defines the methods in the ACE.SOCKStream abstraction.
 *     
 * <p><b>DESCRIPTION</b><br>
 *     This adds additional wrapper methods atop the java Socket class.
 */
public class SOCKStream
{
  /**
   * Create a default SOCK Stream. Do nothing constructor.
   */
  public SOCKStream ()
    {
    }

  /**
   * Create a SOCK Stream.
   *@param s Socket to initialize SOCK Stream with.
   */
  public SOCKStream (Socket s) throws IOException
    {
      this.socket (s);
    }

  /**
   * Set the socket and the underlying streams.
   *@param s Socket associated with the SOCK Stream.
   */
  public void socket (Socket s) throws IOException
  {
    this.socket_ = s;
    // Note that if s is not a valid socket or is null, the
    // following calls will throw exceptions

    // Create buffered, platform independent byte streams.  This hasn't been switched
    // to the newer character streams since the change would break cross talk with
    // non-Java sockets.

    this.iStream_ = new DataInputStream(new BufferedInputStream(s.getInputStream()));

    this.oStream_ = new PrintStream(new DataOutputStream(new BufferedOutputStream(s.getOutputStream())));
  }

  /* Get the underlying Socket.
   *@return the underlying socket
   */
  public Socket socket ()
    {
      return this.socket_;
    }

  /**
   * Close the streams and the underlying socket.
   */
  public void close () throws IOException
    {
      if (this.socket_ != null)
	this.socket_.close ();
      this.socket_ = null;
    }
  
  // = The following send and recv methods are overloaded to provide a
  // flexible interface

  /**
   * Send a StringBuffer. Note that the method blocks.
   *@param s the StringBuffer to send
   *@return the length of the StringBuffer
   */
  public int send (StringBuffer s) throws IOException
    {
      // Get the data out
      String buf = s.toString ();

      this.oStream_.println(buf);
      this.oStream_.flush ();

      return buf.length ();
    }

  /**
   * Send a String. Note that the method blocks.
   *@param s the String to send
   *@return the length of the String
   */
  public int send (String s) throws IOException
    {
      this.oStream_.println(s);
      this.oStream_.flush();

      return s.length ();
    }

  /** 
   * Send an array of bytes. Note that the method blocks.
   *@param b array of bytes to send
   *@param offset offset into the byte array to start sending from
   *@param length number of bytes to send
   *@return number of bytes sent
   */
  public int sendN (byte[] b, int offset, int length) throws IOException
    {
      this.oStream_.write (b, offset, length);
      this.oStream_.flush ();
      return length;
    }

  /**
   * Receive data and append it to the StringBuffer that was passed
   * in. Note that the method blocks.
   *@param s the StringBuffer to append the result of the recv to
   *@return the length of the String received
   */
  public int recv (StringBuffer s) throws IOException
    {
      String temp = this.iStream_.readLine ();
      s.append (temp);

      if (temp == null) // Possible if user sends just a line feed, but        
        return -1;      // not checking would cause a null ptr exception       
      else                    
        return temp.length ();  
    }

  /**
   * Receive an array of characters. This method blocks until either
   * all the bytes are read, the end of the stream is detected, or
   * an exception is thrown.
   *@param b byte array to receive the data in
   *@param offset the start offset of the data in the byte array.
   *@param n number of bytes to receive
   *@return n
   */
  public int recvN (byte[] b, int offset, int n) throws IOException
    {
      this.iStream_.readFully (b, offset, n);
      return n;
    }

  /**
   * Set the underlying input stream.
   *@param iStream the input stream
   */
  public void inputStream (InputStream iStream)
    {
      this.iStream_ = new DataInputStream(new BufferedInputStream(iStream));
    }

  /**
   * Get the underlying input stream.
   *@return the underlying input stream
   */
  public InputStream inputStream ()
    {
      return this.iStream_;
    }

  /**
   * Set the underlying output stream.
   *@param iStream the output stream
   */
  public void outputStream (OutputStream oStream)
    {    
      this.oStream_ = new PrintStream(new DataOutputStream(new BufferedOutputStream(oStream)));
    }

  /**
   * Get the underlying output stream.
   *@return the underlying output stream
   */
  public OutputStream outputStream ()
    {
      return this.oStream_;
    }

  /**
   * Cleanup when the SOCK Stream is garbage collected.
   */
  protected void finalize () throws Throwable
    {
      super.finalize ();
      this.close ();
    }
  
  private Socket socket_;

  // = The input and output streams (by default null)
  private DataInputStream iStream_;
  private PrintStream oStream_;
}