KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > netbios > SessionServicePacket


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.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 public abstract class SessionServicePacket {
25
26     // session service packet types
27
static final int SESSION_MESSAGE = 0x00;
28     static final int SESSION_REQUEST = 0x81;
29     public static final int POSITIVE_SESSION_RESPONSE = 0x82;
30     public static final int NEGATIVE_SESSION_RESPONSE = 0x83;
31     static final int SESSION_RETARGET_RESPONSE = 0x84;
32     static final int SESSION_KEEP_ALIVE = 0x85;
33
34     static final int MAX_MESSAGE_SIZE = 0x0001FFFF;
35     static final int HEADER_LENGTH = 4;
36
37     static void writeInt2( int val, byte[] dst, int dstIndex ) {
38         dst[dstIndex++] = (byte)(( val >> 8 ) & 0xFF );
39         dst[dstIndex] = (byte)( val & 0xFF );
40     }
41     static void writeInt4( int val, byte[] dst, int dstIndex ) {
42         dst[dstIndex++] = (byte)(( val >> 24 ) & 0xFF );
43         dst[dstIndex++] = (byte)(( val >> 16 ) & 0xFF );
44         dst[dstIndex++] = (byte)(( val >> 8 ) & 0xFF );
45         dst[dstIndex] = (byte)( val & 0xFF );
46     }
47     static int readInt2( byte[] src, int srcIndex ) {
48         return (( src[srcIndex] & 0xFF ) << 8 ) +
49                 ( src[srcIndex + 1] & 0xFF );
50     }
51     static int readInt4( byte[] src, int srcIndex ) {
52         return (( src[srcIndex] & 0xFF ) << 24 ) +
53                 (( src[srcIndex + 1] & 0xFF ) << 16 ) +
54                 (( src[srcIndex + 2] & 0xFF ) << 8 ) +
55                 ( src[srcIndex + 3] & 0xFF );
56     }
57     static int readLength( byte[] src, int srcIndex ) {
58         srcIndex++;
59         return (( src[srcIndex++] & 0x01 ) << 16 ) +
60                 (( src[srcIndex++] & 0xFF ) << 8 ) +
61                 ( src[srcIndex++] & 0xFF );
62     }
63     static int readn( InputStream JavaDoc in,
64                 byte[] b,
65                 int off,
66                 int len ) throws IOException JavaDoc {
67         int i = 0, n;
68
69         while (i < len) {
70             n = in.read( b, off + i, len - i );
71             if (n <= 0) {
72                 break;
73             }
74             i += n;
75         }
76
77         return i;
78     }
79     static int readPacketType( InputStream JavaDoc in,
80                                     byte[] buffer,
81                                     int bufferIndex )
82                                     throws IOException JavaDoc {
83         int n;
84         if(( n = readn( in, buffer, bufferIndex, HEADER_LENGTH )) != HEADER_LENGTH ) {
85             if( n == -1 ) {
86                 return -1;
87             }
88             throw new IOException JavaDoc( "unexpected EOF reading netbios session header" );
89         }
90         int t = buffer[bufferIndex] & 0xFF;
91         return t;
92     }
93
94     int type, length;
95
96     public int writeWireFormat( byte[] dst, int dstIndex ) {
97         length = writeTrailerWireFormat( dst, dstIndex + HEADER_LENGTH );
98         writeHeaderWireFormat( dst, dstIndex );
99         return HEADER_LENGTH + length;
100     }
101     int readWireFormat( InputStream JavaDoc in, byte[] buffer, int bufferIndex ) throws IOException JavaDoc {
102         readHeaderWireFormat( in, buffer, bufferIndex );
103         return HEADER_LENGTH + readTrailerWireFormat( in, buffer, bufferIndex );
104     }
105     int writeHeaderWireFormat( byte[] dst, int dstIndex ) {
106         dst[dstIndex++] = (byte)type;
107         if( length > 0x0000FFFF ) {
108             dst[dstIndex] = (byte)0x01;
109         }
110         dstIndex++;
111         writeInt2( length, dst, dstIndex );
112         return HEADER_LENGTH;
113     }
114     int readHeaderWireFormat( InputStream JavaDoc in,
115                                 byte[] buffer,
116                                 int bufferIndex )
117                                 throws IOException JavaDoc {
118         type = buffer[bufferIndex++] & 0xFF;
119         length = (( buffer[bufferIndex] & 0x01 ) << 16 ) + readInt2( buffer, bufferIndex + 1 );
120         return HEADER_LENGTH;
121     }
122
123     abstract int writeTrailerWireFormat( byte[] dst, int dstIndex );
124     abstract int readTrailerWireFormat( InputStream JavaDoc in,
125                                 byte[] buffer,
126                                 int bufferIndex )
127                                 throws IOException JavaDoc;
128 }
129
Popular Tags