KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > netbios > SocketInputStream


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.netbios;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 class SocketInputStream extends InputStream JavaDoc {
25
26     private static final int TMP_BUFFER_SIZE = 256;
27
28     private InputStream JavaDoc in;
29     private SessionServicePacket ssp;
30     private int tot, bip, n;
31     private byte[] header, tmp;
32
33     SocketInputStream( InputStream JavaDoc in ) {
34         this.in = in;
35         header = new byte[4];
36         tmp = new byte[TMP_BUFFER_SIZE];
37     }
38
39     public synchronized int read() throws IOException JavaDoc {
40         if( read( tmp, 0, 1 ) < 0 ) {
41             return -1;
42         }
43         return tmp[0] & 0xFF;
44     }
45     public synchronized int read( byte[] b ) throws IOException JavaDoc {
46         return read( b, 0, b.length );
47     }
48
49     /* This method will not return until len bytes have been read
50      * or the stream has been closed.
51      */

52
53     public synchronized int read( byte[] b, int off, int len ) throws IOException JavaDoc {
54         if( len == 0 ) {
55             return 0;
56         }
57         tot = 0;
58
59         while( true ) {
60             while( bip > 0 ) {
61                 n = in.read( b, off, Math.min( len, bip ));
62                 if( n == -1 ) {
63                     return tot > 0 ? tot : -1;
64                 }
65                 tot += n;
66                 off += n;
67                 len -= n;
68                 bip -= n;
69                 if( len == 0 ) {
70                     return tot;
71                 }
72             }
73
74             switch( SessionServicePacket.readPacketType( in, header, 0 )) {
75                 case SessionServicePacket.SESSION_KEEP_ALIVE:
76                     break;
77                 case SessionServicePacket.SESSION_MESSAGE:
78                     bip = SessionServicePacket.readLength( header, 0 );
79                     break;
80                 case -1:
81                     if( tot > 0 ) {
82                         return tot;
83                     }
84                     return -1;
85             }
86         }
87     }
88     public synchronized long skip( long numbytes ) throws IOException JavaDoc {
89         if( numbytes <= 0 ) {
90             return 0;
91         }
92         long n = numbytes;
93         while( n > 0 ) {
94             int r = read( tmp, 0, (int)Math.min( (long)TMP_BUFFER_SIZE, n ));
95             if (r < 0) {
96                 break;
97             }
98             n -= r;
99         }
100         return numbytes - n;
101     }
102     public int available() throws IOException JavaDoc {
103         if( bip > 0 ) {
104             return bip;
105         }
106         return in.available();
107     }
108     public void close() throws IOException JavaDoc {
109         in.close();
110     }
111 }
112
113
Popular Tags