KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jhttptunnel > MySocket


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /*
3 Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jhttptunnel;
31 import java.io.*;
32 import java.net.*;
33 import java.util.*;
34
35 public class MySocket {
36   Socket socket=null;
37   private DataInputStream dataInputStream=null;
38   private OutputStream os=null;
39
40   MySocket(Socket s) throws IOException{
41
42     try{ s.setTcpNoDelay(true); }
43     catch(Exception JavaDoc e){
44       System.out.println(e+" tcpnodelay");
45     }
46     socket=s;
47     BufferedInputStream bis=new BufferedInputStream(s.getInputStream());
48     dataInputStream=new DataInputStream(bis);
49     os=s.getOutputStream();
50   }
51
52   InputStream getInputStream(){
53     try{ return dataInputStream;}
54     catch(Exception JavaDoc e){}
55     return null;
56   }
57
58   public void close(){
59     try{
60       socket.shutdownOutput();
61       dataInputStream.close();
62       os.close();
63       socket.close();
64     }
65     catch(IOException e){ }
66   }
67   public int read(byte[] buf, int s, int len){
68     try{ return dataInputStream.read(buf, s, len); }
69     catch(IOException e){ return -1; }
70   }
71   public int readByte(){
72     try{ int r=dataInputStream.readByte(); return(r&0xff); }
73     catch(IOException e){ return(-1); }
74   }
75
76   public String JavaDoc readLine(){
77     try{ return(dataInputStream.readLine()); }
78     catch(IOException e){ return(null); }
79   }
80
81   public void write(byte[] foo, int start, int length) throws IOException{
82     os.write(foo, start, length);
83   }
84   public void p(String JavaDoc s) throws IOException{
85     os.write(s.getBytes());
86   }
87   public void print(String JavaDoc s) throws IOException{
88     os.write(s.getBytes());
89   }
90   public void p(byte[] s) throws IOException{
91     os.write(s);
92   }
93   public void print(byte[] s) throws IOException{
94     os.write(s);
95   }
96   public void p(char c) throws IOException{
97     os.write(c);
98   }
99   public void print(char c) throws IOException{
100     os.write(c);
101   }
102   public void p(int c) throws IOException{
103     os.write(Integer.toString(c).getBytes());
104   }
105   public void print(int c) throws IOException{
106     os.write(Integer.toString(c).getBytes());
107   }
108
109   static final private byte[] _rn="\r\n".getBytes();
110   public void pn(String JavaDoc s) throws IOException{
111     println(s);
112   }
113   public void println(String JavaDoc s) throws IOException{
114     print(s); print(_rn);
115   }
116   public void flush() throws IOException{
117     os.flush();
118   }
119 }
120
Popular Tags