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

Quick Search    Search Deep

Source code: com/act365/net/GeneralSocketInputStream.java


1   /*
2    * JSocket Wrench
3    * 
4    * Copyright (C) act365.com October 2003
5    * 
6    * Web site: http://www.act365.com/wrench
7    * E-mail: developers@act365.com
8    * 
9    * The JSocket Wrench library adds support for low-level Internet protocols
10   * to the Java programming language.
11   * 
12   * This program is free software; you can redistribute it and/or modify it 
13   * under the terms of the GNU General Public License as published by the Free 
14   * Software Foundation; either version 2 of the License, or (at your option) 
15   * any later version.
16   *  
17   * This program is distributed in the hope that it will be useful, 
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of 
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
20   * Public License for more details.
21   * 
22   * You should have received a copy of the GNU General Public License along with 
23   * this program; if not, write to the Free Software Foundation, Inc., 
24   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25   */
26  
27  package com.act365.net ;
28  
29  import java.io.*;
30  
31  /**
32   * <code>GeneralSocketInputStream</code> reads from a TCP socket. 
33   * The class should be used in conjunction with <code>GeneralSocketImpl</code>.
34   * @see GeneralSocketImpl
35   */
36  
37  public class GeneralSocketInputStream extends InputStream {
38  
39    int socketDescriptor ;
40  
41    /**
42     * Creates an input stream to read from the TCP socket with the given descriptor.
43     * @param socketDescriptor socket file descriptor
44     */
45    
46    public GeneralSocketInputStream( int socketDescriptor ){
47      this.socketDescriptor = socketDescriptor ;
48    }
49  
50    /**
51     * Reads a single character from the TCP stream.
52     */
53    
54    public int read() throws IOException {
55  
56      byte[] buffer = new byte[ 1 ];
57  
58      _read( socketDescriptor , buffer , 0 , 1 );
59  
60      return (int) buffer[ 0 ];
61    }
62  
63    /**
64     * Reads a string of characters from the TCP stream into a buffer.
65     * @param buffer the buffer the characters are to be written into
66     */
67    
68    public int read( byte[] buffer ) throws IOException {
69      
70      int ret = _read( socketDescriptor , buffer , 0 , buffer.length ); 
71  
72      if( ret == 0 ){
73        ret = -1 ;
74      }
75  
76      return ret ;
77    }
78  
79    /**
80     * Reads a string of characters from the TCP stream into a given
81     * location in a buffer.
82     * @param buffer the buffer the characters are to be read into
83     * @param offset the array location where the input is to be written
84     * @param count the number of characters to be read
85     */
86    
87    public int read( byte[] buffer , int offset , int count ) throws IOException {
88    
89      int ret = _read( socketDescriptor , buffer , offset , count );
90  
91      if( ret == 0 ){
92        ret = -1 ;
93      }
94  
95      return ret ;
96    }
97  
98    static native int _read( int socketDescriptor , byte[] buffer , int offset , int count );
99  
100   /**
101    * Closes the input stream.
102    */
103   
104   public void close() throws IOException {
105   
106     int ret = _close( socketDescriptor );
107   }
108 
109   static native int _close( int socketDescriptor );
110 }
111