001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: * "Paul Walker" <jcifs at samba dot org>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package com.knowgate.jcifs.smb;
021:
022: import java.net.URL;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.net.MalformedURLException;
027: import java.net.UnknownHostException;
028:
029: /**
030: * This class will allow a Java program to read and write data to Named
031: * Pipes and Transact NamedPipes.
032: *
033: * <p>There are three Win32 function calls provided by the Windows SDK
034: * that are important in the context of using jCIFS. They are:
035: *
036: * <ul>
037: * <li> <code>CallNamedPipe</code> A message-type pipe call that opens,
038: * writes to, reads from, and closes the pipe in a single operation.
039: * <li> <code>TransactNamedPipe</code> A message-type pipe call that
040: * writes to and reads from an existing pipe descriptor in one operation.
041: * <li> <code>CreateFile</code>, <code>ReadFile</code>,
042: * <code>WriteFile</code>, and <code>CloseFile</code> A byte-type pipe can
043: * be opened, written to, read from and closed using the standard Win32
044: * file operations.
045: * </ul>
046: *
047: * <p>The jCIFS API maps all of these operations into the standard Java
048: * <code>XxxputStream</code> interface. A special <code>PIPE_TYPE</code>
049: * flags is necessary to distinguish which type of Named Pipe behavior
050: * is desired.
051: *
052: * <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
053: * <tr bgcolor="#ccccff">
054: * <td colspan="2"><b><code>SmbNamedPipe</code> Constructor Examples</b></td>
055: * <tr><td width="20%"><b>Code Sample</b></td><td><b>Description</b></td></tr>
056: * <tr><td width="20%"><pre>
057: * new SmbNamedPipe( "smb://server/IPC$/PIPE/foo",
058: * SmbNamedPipe.PIPE_TYPE_RDWR |
059: * SmbNamedPipe.PIPE_TYPE_CALL );
060: * </pre></td><td>
061: * Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>CallNamedPipe</code> interface.
062: * </td></tr>
063: * <tr><td width="20%"><pre>
064: * new SmbNamedPipe( "smb://server/IPC$/foo",
065: * SmbNamedPipe.PIPE_TYPE_RDWR |
066: * SmbNamedPipe.PIPE_TYPE_TRANSACT );
067: * </pre></td><td>
068: * Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>TransactNamedPipe</code> interface.
069: * </td></tr>
070: * <tr><td width="20%"><pre>
071: * new SmbNamedPipe( "smb://server/IPC$/foo",
072: * SmbNamedPipe.PIPE_TYPE_RDWR );
073: * </pre></td><td>
074: * Open the Named Pipe foo for reading and writing. The pipe will
075: * behave as though the <code>CreateFile</code>, <code>ReadFile</code>,
076: * <code>WriteFile</code>, and <code>CloseFile</code> interface was
077: * being used.
078: * </td></tr>
079: * </table>
080: *
081: * <p>See <a href="../../../pipes.html">Using jCIFS to Connect to Win32
082: * Named Pipes</a> for a detailed description of how to use jCIFS with
083: * Win32 Named Pipe server processes.
084: *
085: */
086:
087: public class SmbNamedPipe extends SmbFile {
088:
089: /**
090: * The pipe should be opened read-only.
091: */
092:
093: public static final int PIPE_TYPE_RDONLY = O_RDONLY;
094:
095: /**
096: * The pipe should be opened only for writing.
097: */
098:
099: 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 pipeIn;
120: OutputStream 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 url, int pipeType)
131: throws MalformedURLException, UnknownHostException {
132: super (url);
133: this .pipeType = pipeType;
134: type = TYPE_NAMED_PIPE;
135: }
136:
137: public SmbNamedPipe(String url, int pipeType,
138: NtlmPasswordAuthentication auth)
139: throws MalformedURLException, UnknownHostException {
140: super (url, auth);
141: this .pipeType = pipeType;
142: type = TYPE_NAMED_PIPE;
143: }
144:
145: public SmbNamedPipe(URL url, int pipeType,
146: NtlmPasswordAuthentication auth)
147: throws MalformedURLException, UnknownHostException {
148: super (url, auth);
149: this .pipeType = pipeType;
150: type = TYPE_NAMED_PIPE;
151: }
152:
153: /**
154: * Return the <code>InputStream</code> used to read information
155: * from this pipe instance. Presumably data would first be written
156: * to the <code>OutputStream</code> associated with this Named
157: * Pipe instance although this is not a requirement (e.g. a
158: * read-only named pipe would write data to this stream on
159: * connection). Reading from this stream may block. Therefore it
160: * may be necessary that an addition thread be used to read and
161: * write to a Named Pipe.
162: */
163:
164: public InputStream getNamedPipeInputStream() throws IOException {
165: if (pipeIn == null) {
166: if ((pipeType & PIPE_TYPE_CALL) == PIPE_TYPE_CALL
167: || (pipeType & PIPE_TYPE_TRANSACT) == PIPE_TYPE_TRANSACT) {
168: pipeIn = new TransactNamedPipeInputStream(this );
169: } else {
170: pipeIn = new SmbFileInputStream(this ,
171: (pipeType & 0xFF0000) | SmbFile.O_EXCL);
172: }
173: }
174: return pipeIn;
175: }
176:
177: /**
178: * Return the <code>OutputStream</code> used to write
179: * information to this pipe instance. The act of writing data
180: * to this stream will result in response data recieved in the
181: * <code>InputStream</code> associated with this Named Pipe
182: * instance (unless of course it does not elicite a response or the pipe is write-only).
183: */
184:
185: public OutputStream getNamedPipeOutputStream() throws IOException {
186: if (pipeOut == null) {
187: if ((pipeType & PIPE_TYPE_CALL) == PIPE_TYPE_CALL
188: || (pipeType & PIPE_TYPE_TRANSACT) == PIPE_TYPE_TRANSACT) {
189: pipeOut = new TransactNamedPipeOutputStream(this );
190: } else {
191: pipeOut = new SmbFileOutputStream(this , false,
192: (pipeType & 0xFF0000) | SmbFile.O_EXCL);
193: }
194: }
195: return pipeOut;
196: }
197: }
|