KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 jcifs.smb;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25
26 class TransactNamedPipeInputStream extends SmbFileInputStream {
27
28     private static final int INIT_PIPE_SIZE = 4096;
29
30     private byte[] pipe_buf = new byte[INIT_PIPE_SIZE];
31     private int beg_idx, nxt_idx, used;
32     private boolean dcePipe;
33
34     Object JavaDoc lock;
35
36     TransactNamedPipeInputStream( SmbNamedPipe pipe ) throws SmbException,
37                 MalformedURLException JavaDoc, UnknownHostException JavaDoc {
38         super( pipe, ( pipe.pipeType & 0xFFFF0000 ) | SmbFile.O_EXCL );
39         this.dcePipe = ( pipe.pipeType & SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT ) != SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT;
40         lock = new Object JavaDoc();
41     }
42     public int read() throws IOException JavaDoc {
43         int result = -1;
44
45         synchronized( lock ) {
46             try {
47                 while( used == 0 ) {
48                     lock.wait();
49                 }
50             } catch( InterruptedException JavaDoc ie ) {
51                 throw new IOException JavaDoc( ie.getMessage() );
52             }
53             result = pipe_buf[beg_idx] & 0xFF;
54             beg_idx = ( beg_idx + 1 ) % pipe_buf.length;
55         }
56         return result;
57     }
58     public int read( byte[] b ) throws IOException JavaDoc {
59         return read( b, 0, b.length );
60     }
61     public int read( byte[] b, int off, int len ) throws IOException JavaDoc {
62         int result = -1;
63         int i;
64
65         if( len <= 0 ) {
66             return 0;
67         }
68         synchronized( lock ) {
69             try {
70                 while( used == 0 ) {
71                     lock.wait();
72                 }
73             } catch( InterruptedException JavaDoc ie ) {
74                 throw new IOException JavaDoc( ie.getMessage() );
75             }
76             i = pipe_buf.length - beg_idx;
77             result = len > used ? used : len;
78             if( used > i && result > i ) {
79                 System.arraycopy( pipe_buf, beg_idx, b, off, i );
80                 off += i;
81                 System.arraycopy( pipe_buf, 0, b, off, result - i );
82             } else {
83                 System.arraycopy( pipe_buf, beg_idx, b, off, result );
84             }
85             used -= result;
86             beg_idx = ( beg_idx + result ) % pipe_buf.length;
87         }
88
89         return result;
90     }
91     public int available() throws IOException JavaDoc {
92         if( file.log.level > 2 )
93             file.log.println( "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 ) { /* 2 chunks */
111                 System.arraycopy( tmp, beg_idx, pipe_buf, 0, i );
112                 System.arraycopy( tmp, 0, pipe_buf, i, used - i );
113             } else {
114                 System.arraycopy( tmp, beg_idx, pipe_buf, 0, used );
115             }
116             beg_idx = 0;
117             nxt_idx = used;
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     public int dce_read( byte[] b, int off, int len ) throws IOException JavaDoc {
134         return super.read(b, off, len);
135     }
136 }
137
Popular Tags