KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > smb > TransactNamedPipeInputStream


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 com.knowgate.jcifs.smb;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import com.knowgate.debug.*;
25
26 class TransactNamedPipeInputStream extends InputStream JavaDoc {
27
28     private static final int INIT_PIPE_SIZE = 4096;
29
30     private SmbNamedPipe pipe;
31     private byte[] pipe_buf = new byte[INIT_PIPE_SIZE];
32     private int beg_idx, nxt_idx, used;
33
34     Object JavaDoc lock;
35
36     TransactNamedPipeInputStream( SmbNamedPipe pipe ) {
37         this.pipe = pipe;
38         lock = new Object JavaDoc();
39     }
40     public void close() throws IOException JavaDoc {
41         pipe.close();
42     }
43     public int read() throws IOException JavaDoc {
44         int result = -1;
45
46         synchronized( lock ) {
47             try {
48                 while( used == 0 ) {
49                     lock.wait();
50                 }
51             } catch( InterruptedException JavaDoc ie ) {
52                 throw new IOException JavaDoc( ie.getMessage() );
53             }
54             result = pipe_buf[beg_idx] & 0xFF;
55             beg_idx = ( beg_idx + 1 ) % pipe_buf.length;
56         }
57         return result;
58     }
59     public int read( byte[] b ) throws IOException JavaDoc {
60         return read( b, 0, b.length );
61     }
62     public int read( byte[] b, int off, int len ) throws IOException JavaDoc {
63         int result = -1;
64         int i;
65
66         if( len <= 0 ) {
67             return 0;
68         }
69         synchronized( lock ) {
70             try {
71                 while( used == 0 ) {
72                     lock.wait();
73                 }
74             } catch( InterruptedException JavaDoc ie ) {
75                 throw new IOException JavaDoc( ie.getMessage() );
76             }
77             i = pipe_buf.length - beg_idx;
78             result = len > used ? used : len;
79             if( used > i && result > i ) {
80                 System.arraycopy( pipe_buf, beg_idx, b, off, i );
81                 off += i;
82                 System.arraycopy( pipe_buf, 0, b, off, result - i );
83             } else {
84                 System.arraycopy( pipe_buf, beg_idx, b, off, result );
85             }
86             used -= result;
87             beg_idx = ( beg_idx + result ) % pipe_buf.length;
88         }
89         return result;
90     }
91     public int available() throws IOException JavaDoc {
92         if( DebugFile.trace )
93             DebugFile.writeln( "Named Pipe available() does not apply to TRANSACT Named Pipes" );
94         return 0;
95     }
96     int receive( byte[] b, int off, int len ) {
97         int i;
98
99         if( len > ( pipe_buf.length - used )) {
100             byte[] tmp;
101             int new_size;
102
103             new_size = pipe_buf.length * 2;
104             if( len > ( new_size - used )) {
105                 new_size = len + used;
106             }
107             tmp = pipe_buf;
108             pipe_buf = new byte[new_size];
109             i = tmp.length - beg_idx;
110             if( used > i ) {
111                 System.arraycopy( tmp, beg_idx, pipe_buf, 0, i );
112                 System.arraycopy( tmp, 0, pipe_buf, i, used - i );
113                 nxt_idx = used;
114             } else {
115                 System.arraycopy( tmp, beg_idx, pipe_buf, 0, used );
116             }
117             beg_idx = 0;
118             tmp = null;
119         }
120
121         i = pipe_buf.length - nxt_idx;
122         if( len > i ) {
123             System.arraycopy( b, off, pipe_buf, nxt_idx, i );
124             off += i;
125             System.arraycopy( b, off, pipe_buf, 0, len - i );
126         } else {
127             System.arraycopy( b, off, pipe_buf, nxt_idx, len );
128         }
129         nxt_idx = ( nxt_idx + len ) % pipe_buf.length;
130         used += len;
131         return len;
132     }
133 }
134
Popular Tags