1 18 19 package jcifs.smb; 20 21 import java.net.URL ; 22 import java.io.OutputStream ; 23 import java.io.IOException ; 24 import java.net.UnknownHostException ; 25 import java.net.MalformedURLException ; 26 import jcifs.util.LogStream; 27 28 31 32 public class SmbFileOutputStream extends OutputStream { 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 52 53 public SmbFileOutputStream( String url ) throws SmbException, MalformedURLException , UnknownHostException { 54 this( url, false ); 55 } 56 57 65 66 public SmbFileOutputStream( SmbFile file ) throws SmbException, MalformedURLException , UnknownHostException { 67 this( file, false ); 68 } 69 70 80 81 public SmbFileOutputStream( String url, boolean append ) throws SmbException, MalformedURLException , UnknownHostException { 82 this( new SmbFile( url ), append ); 83 } 84 85 95 96 public SmbFileOutputStream( SmbFile file, boolean append ) throws SmbException, MalformedURLException , UnknownHostException { 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 118 119 public SmbFileOutputStream( String url, int shareAccess ) throws SmbException, MalformedURLException , UnknownHostException { 120 this( new SmbFile( url, "", null, shareAccess ), false ); 121 } 122 123 SmbFileOutputStream( SmbFile file, boolean append, int openFlags ) throws SmbException, MalformedURLException , UnknownHostException { 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); 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 159 160 public void close() throws IOException { 161 file.close(); 162 tmp = null; 163 } 164 165 170 171 public void write( int b ) throws IOException { 172 tmp[0] = (byte)b; 173 write( tmp, 0, 1 ); 174 } 175 176 182 183 public void write( byte[] b ) throws IOException { 184 write( b, 0, b.length ); 185 } 186 187 194 195 public void write( byte[] b, int off, int len ) throws IOException { 196 if( len <= 0 ) { 197 return; 198 } 199 200 if( tmp == null ) { 201 throw new IOException ( "Bad file descriptor" ); 202 } 203 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 |