Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/barteo/cldc/socket/Connection.java


1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2003 Bartek Teodorczyk <barteo@it.pl>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package com.barteo.cldc.socket;
21  
22  import java.io.DataInputStream;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.net.Socket;
28  
29  import javax.microedition.io.StreamConnection;
30  
31  import com.barteo.cldc.ClosedConnection;
32  
33  
34  public class Connection implements StreamConnection, ClosedConnection 
35  {
36    private Socket socket;
37  
38  
39    public javax.microedition.io.Connection open(String name)
40      throws IOException
41    {
42      int portSepIndex = name.lastIndexOf(':');
43      int port = Integer.parseInt(name.substring(portSepIndex + 1));    
44      String host = name.substring("socket://".length(), portSepIndex);
45  
46      socket = new Socket(host, port);
47      
48      return this;
49    }
50  
51  
52    public void close() 
53        throws IOException 
54    {
55      if (socket == null) {
56        return;
57      }
58      
59      socket.close();
60    }
61  
62  
63    public InputStream openInputStream() 
64        throws IOException 
65    {
66      if (socket == null) {
67        throw new IOException();
68      }
69      
70      return socket.getInputStream();
71    }
72  
73  
74    public DataInputStream openDataInputStream() 
75        throws IOException 
76    {
77      return new DataInputStream(openInputStream());
78    }
79  
80  
81    public OutputStream openOutputStream() 
82        throws IOException 
83    {
84      if (socket == null) {
85        throw new IOException();
86      }
87  
88      return socket.getOutputStream();
89    }
90  
91  
92    public DataOutputStream openDataOutputStream() 
93        throws IOException 
94    {
95      return new DataOutputStream(openOutputStream());
96    }
97  
98  }