KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package com.knowgate.jcifs.smb;
21
22 import java.net.URL JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28
29 /**
30  * This class will allow a Java program to read and write data to Named
31  * Pipes and Transact NamedPipes.
32  *
33  * <p>There are three Win32 function calls provided by the Windows SDK
34  * that are important in the context of using jCIFS. They are:
35  *
36  * <ul>
37  * <li> <code>CallNamedPipe</code> A message-type pipe call that opens,
38  * writes to, reads from, and closes the pipe in a single operation.
39  * <li> <code>TransactNamedPipe</code> A message-type pipe call that
40  * writes to and reads from an existing pipe descriptor in one operation.
41  * <li> <code>CreateFile</code>, <code>ReadFile</code>,
42  * <code>WriteFile</code>, and <code>CloseFile</code> A byte-type pipe can
43  * be opened, written to, read from and closed using the standard Win32
44  * file operations.
45  * </ul>
46  *
47  * <p>The jCIFS API maps all of these operations into the standard Java
48  * <code>XxxputStream</code> interface. A special <code>PIPE_TYPE</code>
49  * flags is necessary to distinguish which type of Named Pipe behavior
50  * is desired.
51  *
52  * <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
53  * <tr bgcolor="#ccccff">
54  * <td colspan="2"><b><code>SmbNamedPipe</code> Constructor Examples</b></td>
55  * <tr><td width="20%"><b>Code Sample</b></td><td><b>Description</b></td></tr>
56  * <tr><td width="20%"><pre>
57  * new SmbNamedPipe( "smb://server/IPC$/PIPE/foo",
58  * SmbNamedPipe.PIPE_TYPE_RDWR |
59  * SmbNamedPipe.PIPE_TYPE_CALL );
60  * </pre></td><td>
61  * Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>CallNamedPipe</code> interface.
62  * </td></tr>
63  * <tr><td width="20%"><pre>
64  * new SmbNamedPipe( "smb://server/IPC$/foo",
65  * SmbNamedPipe.PIPE_TYPE_RDWR |
66  * SmbNamedPipe.PIPE_TYPE_TRANSACT );
67  * </pre></td><td>
68  * Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>TransactNamedPipe</code> interface.
69  * </td></tr>
70  * <tr><td width="20%"><pre>
71  * new SmbNamedPipe( "smb://server/IPC$/foo",
72  * SmbNamedPipe.PIPE_TYPE_RDWR );
73  * </pre></td><td>
74  * Open the Named Pipe foo for reading and writing. The pipe will
75  * behave as though the <code>CreateFile</code>, <code>ReadFile</code>,
76  * <code>WriteFile</code>, and <code>CloseFile</code> interface was
77  * being used.
78  * </td></tr>
79  * </table>
80  *
81  * <p>See <a HREF="../../../pipes.html">Using jCIFS to Connect to Win32
82  * Named Pipes</a> for a detailed description of how to use jCIFS with
83  * Win32 Named Pipe server processes.
84  *
85  */

86
87 public class SmbNamedPipe extends SmbFile {
88
89     /**
90      * The pipe should be opened read-only.
91      */

92
93     public static final int PIPE_TYPE_RDONLY = O_RDONLY;
94
95     /**
96      * The pipe should be opened only for writing.
97      */

98
99     public static final int PIPE_TYPE_WRONLY = O_WRONLY;
100
101     /**
102      * The pipe should be opened for both reading and writing.
103      */

104
105     public static final int PIPE_TYPE_RDWR = O_RDWR;
106
107     /**
108      * Pipe operations should behave like the <code>CallNamedPipe</code> Win32 Named Pipe function.
109      */

110
111     public static final int PIPE_TYPE_CALL = 0x01;
112
113     /**
114      * Pipe operations should behave like the <code>TransactNamedPipe</code> Win32 Named Pipe function.
115      */

116
117     public static final int PIPE_TYPE_TRANSACT = 0x02;
118
119     InputStream JavaDoc pipeIn;
120     OutputStream JavaDoc pipeOut;
121     int pipeType;
122
123     /**
124      * Open the Named Pipe resource specified by the url
125      * parameter. The pipeType parameter should be at least one of
126      * the <code>PIPE_TYPE</code> flags combined with the bitwise OR
127      * operator <code>|</code>. See the examples listed above.
128      */

129
130     public SmbNamedPipe( String JavaDoc url, int pipeType )
131                             throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
132         super( url );
133         this.pipeType = pipeType;
134         type = TYPE_NAMED_PIPE;
135     }
136     public SmbNamedPipe( String JavaDoc url, int pipeType, NtlmPasswordAuthentication auth )
137                             throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
138         super( url, auth );
139         this.pipeType = pipeType;
140         type = TYPE_NAMED_PIPE;
141     }
142     public SmbNamedPipe( URL JavaDoc url, int pipeType, NtlmPasswordAuthentication auth )
143                             throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
144         super( url, auth );
145         this.pipeType = pipeType;
146         type = TYPE_NAMED_PIPE;
147     }
148
149     /**
150      * Return the <code>InputStream</code> used to read information
151      * from this pipe instance. Presumably data would first be written
152      * to the <code>OutputStream</code> associated with this Named
153      * Pipe instance although this is not a requirement (e.g. a
154      * read-only named pipe would write data to this stream on
155      * connection). Reading from this stream may block. Therefore it
156      * may be necessary that an addition thread be used to read and
157      * write to a Named Pipe.
158      */

159
160     public InputStream JavaDoc getNamedPipeInputStream() throws IOException JavaDoc {
161         if( pipeIn == null ) {
162             if(( pipeType & PIPE_TYPE_CALL ) == PIPE_TYPE_CALL ||
163                     ( pipeType & PIPE_TYPE_TRANSACT ) == PIPE_TYPE_TRANSACT ) {
164                 pipeIn = new TransactNamedPipeInputStream( this );
165             } else {
166                 pipeIn = new SmbFileInputStream( this,
167                                             ( pipeType & 0xFF0000 ) | SmbFile.O_EXCL );
168             }
169         }
170         return pipeIn;
171     }
172
173     /**
174      * Return the <code>OutputStream</code> used to write
175      * information to this pipe instance. The act of writing data
176      * to this stream will result in response data recieved in the
177      * <code>InputStream</code> associated with this Named Pipe
178      * instance (unless of course it does not elicite a response or the pipe is write-only).
179      */

180
181     public OutputStream JavaDoc getNamedPipeOutputStream() throws IOException JavaDoc {
182         if( pipeOut == null ) {
183             if(( pipeType & PIPE_TYPE_CALL ) == PIPE_TYPE_CALL ||
184                     ( pipeType & PIPE_TYPE_TRANSACT ) == PIPE_TYPE_TRANSACT ) {
185                 pipeOut = new TransactNamedPipeOutputStream( this );
186             } else {
187                 pipeOut = new SmbFileOutputStream( this, false,
188                                             ( pipeType & 0xFF0000 ) | SmbFile.O_EXCL );
189             }
190         }
191         return pipeOut;
192     }
193 }
194
Popular Tags