KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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     public static final int PIPE_TYPE_DCE_TRANSACT = 0x02 | 0x04;
120
121     InputStream JavaDoc pipeIn;
122     OutputStream JavaDoc pipeOut;
123     int pipeType;
124
125     /**
126      * Open the Named Pipe resource specified by the url
127      * parameter. The pipeType parameter should be at least one of
128      * the <code>PIPE_TYPE</code> flags combined with the bitwise OR
129      * operator <code>|</code>. See the examples listed above.
130      */

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

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

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