/
SmbFileOutputStream.java
261 lines (231 loc) · 9.2 KB
/
SmbFileOutputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* jcifs smb client library in Java
* Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package jcifs.smb1.smb1;
import java.net.URL;
import java.io.OutputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import jcifs.smb1.util.LogStream;
import java.net.MalformedURLException;
/**
* This <code>OutputStream</code> can write bytes to a file on an SMB file server.
*/
public class SmbFileOutputStream extends OutputStream {
private SmbFile file;
private boolean append, useNTSmbs;
private int openFlags, access, writeSize;
private long fp;
private byte[] tmp = new byte[1];
private SmbComWriteAndX reqx;
private SmbComWriteAndXResponse rspx;
private SmbComWrite req;
private SmbComWriteResponse rsp;
/**
* Creates an {@link java.io.OutputStream} for writing to a file
* on an SMB server addressed by the URL parameter. See {@link
* jcifs.smb1.smb1.SmbFile} for a detailed description and examples of
* the smb URL syntax.
*
* @param url An smb URL string representing the file to write to
*/
public SmbFileOutputStream( String url ) throws SmbException, MalformedURLException, UnknownHostException {
this( url, false );
}
/**
* Creates an {@link java.io.OutputStream} for writing bytes to a file on
* an SMB server represented by the {@link jcifs.smb1.smb1.SmbFile} parameter. See
* {@link jcifs.smb1.smb1.SmbFile} for a detailed description and examples of
* the smb URL syntax.
*
* @param file An <code>SmbFile</code> specifying the file to write to
*/
public SmbFileOutputStream( SmbFile file ) throws SmbException, MalformedURLException, UnknownHostException {
this( file, false );
}
/**
* Creates an {@link java.io.OutputStream} for writing bytes to a file on an
* SMB server addressed by the URL parameter. See {@link jcifs.smb1.smb1.SmbFile}
* for a detailed description and examples of the smb URL syntax. If the
* second argument is <code>true</code>, then bytes will be written to the
* end of the file rather than the beginning.
*
* @param url An smb URL string representing the file to write to
* @param append Append to the end of file
*/
public SmbFileOutputStream( String url, boolean append ) throws SmbException, MalformedURLException, UnknownHostException {
this( new SmbFile( url ), append );
}
/**
* Creates an {@link java.io.OutputStream} for writing bytes to a file
* on an SMB server addressed by the <code>SmbFile</code> parameter. See
* {@link jcifs.smb1.smb1.SmbFile} for a detailed description and examples of
* the smb URL syntax. If the second argument is <code>true</code>, then
* bytes will be written to the end of the file rather than the beginning.
*
* @param file An <code>SmbFile</code> representing the file to write to
* @param append Append to the end of file
*/
public SmbFileOutputStream( SmbFile file, boolean append ) throws SmbException, MalformedURLException, UnknownHostException {
this( file, append, append ? SmbFile.O_CREAT | SmbFile.O_WRONLY | SmbFile.O_APPEND :
SmbFile.O_CREAT | SmbFile.O_WRONLY | SmbFile.O_TRUNC );
}
/**
* Creates an {@link java.io.OutputStream} for writing bytes to a file
* on an SMB server addressed by the <code>SmbFile</code> parameter. See
* {@link jcifs.smb1.smb1.SmbFile} for a detailed description and examples of
* the smb URL syntax.
<p>
The second parameter specifies how the file should be shared. If
<code>SmbFile.FILE_NO_SHARE</code> is specified the client will
have exclusive access to the file. An additional open command
from jCIFS or another application will fail with the "file is being
accessed by another process" error. The <code>FILE_SHARE_READ</code>,
<code>FILE_SHARE_WRITE</code>, and <code>FILE_SHARE_DELETE</code> may be
combined with the bitwise OR '|' to specify that other peocesses may read,
write, and/or delete the file while the jCIFS user has the file open.
*
* @param url An smb URL representing the file to write to
* @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>
*/
public SmbFileOutputStream( String url, int shareAccess ) throws SmbException, MalformedURLException, UnknownHostException {
this( new SmbFile( url, "", null, shareAccess ), false );
}
SmbFileOutputStream( SmbFile file, boolean append, int openFlags ) throws SmbException, MalformedURLException, UnknownHostException {
this.file = file;
this.append = append;
this.openFlags = openFlags;
this.access = (openFlags >>> 16) & 0xFFFF;
if( append ) {
try {
fp = file.length();
} catch( SmbAuthException sae ) {
throw sae;
} catch( SmbException se ) {
fp = 0L;
}
}
if( file instanceof SmbNamedPipe && file.unc.startsWith( "\\pipe\\" )) {
file.unc = file.unc.substring( 5 );
file.send( new TransWaitNamedPipe( "\\pipe" + file.unc ),
new TransWaitNamedPipeResponse() );
}
file.open( openFlags, access | SmbConstants.FILE_WRITE_DATA, SmbFile.ATTR_NORMAL, 0 );
this.openFlags &= ~(SmbFile.O_CREAT | SmbFile.O_TRUNC); /* in case close and reopen */
writeSize = file.tree.session.transport.snd_buf_size - 70;
useNTSmbs = file.tree.session.transport.hasCapability( ServerMessageBlock.CAP_NT_SMBS );
if( useNTSmbs ) {
reqx = new SmbComWriteAndX();
rspx = new SmbComWriteAndXResponse();
} else {
req = new SmbComWrite();
rsp = new SmbComWriteResponse();
}
}
/**
* Closes this output stream and releases any system resources associated
* with it.
*
* @throws IOException if a network error occurs
*/
public void close() throws IOException {
file.close();
tmp = null;
}
/**
* Writes the specified byte to this file output stream.
*
* @throws IOException if a network error occurs
*/
public void write( int b ) throws IOException {
tmp[0] = (byte)b;
write( tmp, 0, 1 );
}
/**
* Writes b.length bytes from the specified byte array to this
* file output stream.
*
* @throws IOException if a network error occurs
*/
public void write( byte[] b ) throws IOException {
write( b, 0, b.length );
}
public boolean isOpen()
{
return file.isOpen();
}
void ensureOpen() throws IOException {
// ensure file is open
if( file.isOpen() == false ) {
file.open( openFlags, access | SmbConstants.FILE_WRITE_DATA, SmbFile.ATTR_NORMAL, 0 );
if( append ) {
fp = file.length();
}
}
}
/**
* Writes len bytes from the specified byte array starting at
* offset off to this file output stream.
*
* @param b The array
* @throws IOException if a network error occurs
*/
public void write( byte[] b, int off, int len ) throws IOException {
if( file.isOpen() == false && file instanceof SmbNamedPipe ) {
file.send( new TransWaitNamedPipe( "\\pipe" + file.unc ),
new TransWaitNamedPipeResponse() );
}
writeDirect( b, off, len, 0 );
}
/**
* Just bypasses TransWaitNamedPipe - used by DCERPC bind.
*/
public void writeDirect( byte[] b, int off, int len, int flags ) throws IOException {
if( len <= 0 ) {
return;
}
if( tmp == null ) {
throw new IOException( "Bad file descriptor" );
}
ensureOpen();
if( file.log.level >= 4 )
file.log.println( "write: fid=" + file.fid + ",off=" + off + ",len=" + len );
int w;
do {
w = len > writeSize ? writeSize : len;
if( useNTSmbs ) {
reqx.setParam( file.fid, fp, len - w, b, off, w );
if ((flags & 1) != 0) {
reqx.setParam( file.fid, fp, len, b, off, w );
reqx.writeMode = 0x8;
} else {
reqx.writeMode = 0;
}
file.send( reqx, rspx );
fp += rspx.count;
len -= rspx.count;
off += rspx.count;
} else {
req.setParam( file.fid, fp, len - w, b, off, w );
fp += rsp.count;
len -= rsp.count;
off += rsp.count;
file.send( req, rsp );
}
} while( len > 0 );
}
}