KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26
27 import com.knowgate.debug.*;
28
29 /**
30  * This <code>OutputStream</code> can write bytes to a file on an SMB file server.
31  */

32
33 public class SmbFileOutputStream extends OutputStream JavaDoc {
34
35     private SmbFile file;
36     private boolean append, useNTSmbs;
37     private int openFlags, writeSize;
38     private long fp;
39     private byte[] tmp = new byte[1];
40     private SmbComWriteAndX reqx;
41     private SmbComWriteAndXResponse rspx;
42     private SmbComWrite req;
43     private SmbComWriteResponse rsp;
44
45 /**
46  * Creates an {@link java.io.OutputStream} for writing to a file
47  * on an SMB server addressed by the URL parameter. See {@link
48  * jcifs.smb.SmbFile} for a detailed description and examples of
49  * the smb URL syntax.
50  *
51  * @param url An smb URL string representing the file to write to
52  * @return A new <code>OutputStream</code> for the specified file
53  */

54
55     public SmbFileOutputStream( String JavaDoc url ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
56         this( url, false );
57     }
58
59 /**
60  * Creates an {@link java.io.OutputStream} for writing bytes to a file on
61  * an SMB server represented by the {@link jcifs.smb.SmbFile} parameter. See
62  * {@link jcifs.smb.SmbFile} for a detailed description and examples of
63  * the smb URL syntax.
64  *
65  * @param url An <code>SmbFile</code> specifying the file to write to
66  * @return A new <code>OutputStream</code> for the specified <code>SmbFile</code>
67  */

68
69     public SmbFileOutputStream( SmbFile file ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
70         this( file, false );
71     }
72
73 /**
74  * Creates an {@link java.io.OutputStream} for writing bytes to a file on an
75  * SMB server addressed by the URL parameter. See {@link jcifs.smb.SmbFile}
76  * for a detailed description and examples of the smb URL syntax. If the
77  * second argument is <code>true</code>, then bytes will be written to the
78  * end of the file rather than the beginning.
79  *
80  * @param url An smb URL string representing the file to write to
81  * @return A new <code>OutputStream</code> for the specified <code>url</code>
82  */

83
84     public SmbFileOutputStream( String JavaDoc url, boolean append ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
85         this( new SmbFile( url ), append );
86     }
87
88 /**
89  * Creates an {@link java.io.OutputStream} for writing bytes to a file
90  * on an SMB server addressed by the <code>SmbFile</code> parameter. See
91  * {@link jcifs.smb.SmbFile} for a detailed description and examples of
92  * the smb URL syntax. If the second argument is <code>true</code>, then
93  * bytes will be written to the end of the file rather than the beginning.
94  *
95  * @param url An <code>SmbFile</code> representing the file to write to
96  * @return A new <code>OutputStream</code> for the specified <code>SmbFile</code>
97  */

98
99     public SmbFileOutputStream( SmbFile file, boolean append ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
100         this( file, append, append ? SmbFile.O_CREAT | SmbFile.O_WRONLY | SmbFile.O_APPEND :
101                                     SmbFile.O_CREAT | SmbFile.O_WRONLY | SmbFile.O_TRUNC );
102     }
103 /**
104  * Creates an {@link java.io.OutputStream} for writing bytes to a file
105  * on an SMB server addressed by the <code>SmbFile</code> parameter. See
106  * {@link jcifs.smb.SmbFile} for a detailed description and examples of
107  * the smb URL syntax.
108 <p>
109 The second parameter specifies how the file should be shared. If
110 <code>SmbFile.FILE_NO_SHARE</code> is specified the client will
111 have exclusive access to the file. An additional open command
112 from jCIFS or another application will fail with the "file is being
113 accessed by another process" error. The <code>FILE_SHARE_READ</code>,
114 <code>FILE_SHARE_WRITE</code>, and <code>FILE_SHARE_DELETE</code> may be
115 combined with the bitwise OR '|' to specify that other peocesses may read,
116 write, and/or delete the file while the jCIFS user has the file open.
117  *
118  * @param url An <code>SmbFile</code> representing the file to write to
119  * @return A new <code>OutputStream</code> for the specified <code>SmbFile</code>
120  */

121
122     public SmbFileOutputStream( String JavaDoc url, int shareAccess ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
123         this( new SmbFile( url, "", null, shareAccess ), false );
124     }
125
126     SmbFileOutputStream( SmbFile file, boolean append, int openFlags ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
127         this.file = file;
128         this.append = append;
129         this.openFlags = openFlags;
130         if( append ) {
131             try {
132                 fp = file.length();
133             } catch( SmbException se ) {
134                 fp = 0L;
135             }
136         }
137         if( file instanceof SmbNamedPipe && file.unc.startsWith( "\\pipe\\" )) {
138             file.unc = file.unc.substring( 5 );
139             file.sendTransaction( new TransWaitNamedPipe( "\\pipe" + file.unc ),
140                                         new TransWaitNamedPipeResponse() );
141         }
142         file.open( openFlags, SmbFile.ATTR_NORMAL, 0 );
143         writeSize = file.tree.session.transport.snd_buf_size - 70;
144
145         useNTSmbs = file.tree.session.transport.hasCapability( ServerMessageBlock.CAP_NT_SMBS );
146         if( useNTSmbs ) {
147             reqx = new SmbComWriteAndX();
148             rspx = new SmbComWriteAndXResponse();
149         } else {
150             req = new SmbComWrite();
151             rsp = new SmbComWriteResponse();
152         }
153     }
154
155 /**
156  * Closes this output stream and releases any system resources associated
157  * with it.
158  *
159  * @throws IOException if a network error occurs
160  */

161
162     public void close() throws IOException JavaDoc {
163         file.close();
164     }
165
166 /**
167  * Writes the specified byte to this file output stream.
168  *
169  * @throws IOException if a network error occurs
170  */

171
172     public void write( int b ) throws IOException JavaDoc {
173         tmp[0] = (byte)b;
174         write( tmp, 0, 1 );
175     }
176
177 /**
178  * Writes b.length bytes from the specified byte array to this
179  * file output stream.
180  *
181  * @throws IOException if a network error occurs
182  */

183
184     public void write( byte[] b ) throws IOException JavaDoc {
185         write( b, 0, b.length );
186     }
187
188 /**
189  * Writes len bytes from the specified byte array starting at
190  * offset off to this file output stream.
191  *
192  * @param b The array
193  * @throws IOException if a network error occurs
194  */

195
196     public void write( byte[] b, int off, int len ) throws IOException JavaDoc {
197         if( len <= 0 ) {
198             return;
199         }
200
201         // ensure file is open
202
if( file.isOpen() == false ) {
203             if( file instanceof SmbNamedPipe ) {
204                 file.sendTransaction( new TransWaitNamedPipe( "\\pipe" + file.unc ),
205                                         new TransWaitNamedPipeResponse() );
206             }
207             file.open( openFlags, SmbFile.ATTR_NORMAL, 0 );
208             if( append ) {
209                 fp = file.length();
210             }
211         }
212
213         if( DebugFile.trace )
214             DebugFile.writeln( "write: fid=" + file.fid + ",off=" + off + ",len=" + len );
215
216         int w;
217         do {
218             w = len > writeSize ? writeSize : len;
219             if( useNTSmbs ) {
220                 reqx.setParam( file.fid, fp, len - w, b, off, w );
221                 file.send( reqx, rspx );
222                 fp += rspx.count;
223                 len -= rspx.count;
224                 off += rspx.count;
225             } else {
226                 req.setParam( file.fid, fp, len - w, b, off, w );
227                 fp += rsp.count;
228                 len -= rsp.count;
229                 off += rsp.count;
230                 file.send( req, rsp );
231             }
232         } while( len > 0 );
233     }
234 }
235
Popular Tags