1 18 19 package com.knowgate.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 27 import com.knowgate.debug.*; 28 29 32 33 public class SmbFileOutputStream extends OutputStream { 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 54 55 public SmbFileOutputStream( String url ) throws SmbException, MalformedURLException , UnknownHostException { 56 this( url, false ); 57 } 58 59 68 69 public SmbFileOutputStream( SmbFile file ) throws SmbException, MalformedURLException , UnknownHostException { 70 this( file, false ); 71 } 72 73 83 84 public SmbFileOutputStream( String url, boolean append ) throws SmbException, MalformedURLException , UnknownHostException { 85 this( new SmbFile( url ), append ); 86 } 87 88 98 99 public SmbFileOutputStream( SmbFile file, boolean append ) throws SmbException, MalformedURLException , UnknownHostException { 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 121 122 public SmbFileOutputStream( String url, int shareAccess ) throws SmbException, MalformedURLException , UnknownHostException { 123 this( new SmbFile( url, "", null, shareAccess ), false ); 124 } 125 126 SmbFileOutputStream( SmbFile file, boolean append, int openFlags ) throws SmbException, MalformedURLException , UnknownHostException { 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 161 162 public void close() throws IOException { 163 file.close(); 164 } 165 166 171 172 public void write( int b ) throws IOException { 173 tmp[0] = (byte)b; 174 write( tmp, 0, 1 ); 175 } 176 177 183 184 public void write( byte[] b ) throws IOException { 185 write( b, 0, b.length ); 186 } 187 188 195 196 public void write( byte[] b, int off, int len ) throws IOException { 197 if( len <= 0 ) { 198 return; 199 } 200 201 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 |