KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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 import jcifs.util.LogStream;
27
28 /**
29  * This <code>OutputStream</code> can write bytes to a file on an SMB file server.
30  */

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

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

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

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

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

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

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

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

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

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