FileDocCategorySizeDatePackage
SmbFileOutputStream.javaAPI DocJCIFS 1.3.17 API9631Tue Oct 18 15:26:24 BST 2011jcifs.smb

SmbFileOutputStream

public class SmbFileOutputStream extends OutputStream
This OutputStream can write bytes to a file on an SMB file server.

Fields Summary
private SmbFile
file
private boolean
append
private boolean
useNTSmbs
private int
openFlags
private int
access
private int
writeSize
private long
fp
private byte[]
tmp
private SmbComWriteAndX
reqx
private SmbComWriteAndXResponse
rspx
private SmbComWrite
req
private SmbComWriteResponse
rsp
Constructors Summary
public SmbFileOutputStream(String url)
Creates an {@link java.io.OutputStream} for writing to a file on an SMB server addressed by the URL parameter. See {@link jcifs.smb.SmbFile} for a detailed description and examples of the smb URL syntax.

param
url An smb URL string representing the file to write to


                                             

             
        this( url, false );
    
public SmbFileOutputStream(SmbFile file)
Creates an {@link java.io.OutputStream} for writing bytes to a file on an SMB server represented by the {@link jcifs.smb.SmbFile} parameter. See {@link jcifs.smb.SmbFile} for a detailed description and examples of the smb URL syntax.

param
file An SmbFile specifying the file to write to

        this( file, false );
    
public SmbFileOutputStream(String url, boolean append)
Creates an {@link java.io.OutputStream} for writing bytes to a file on an SMB server addressed by the URL parameter. See {@link jcifs.smb.SmbFile} for a detailed description and examples of the smb URL syntax. If the second argument is true, 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

        this( new SmbFile( url ), append );
    
public SmbFileOutputStream(SmbFile file, boolean append)
Creates an {@link java.io.OutputStream} for writing bytes to a file on an SMB server addressed by the SmbFile parameter. See {@link jcifs.smb.SmbFile} for a detailed description and examples of the smb URL syntax. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

param
file An SmbFile representing the file to write to
param
append Append to the end of file

        this( file, append, append ? SmbFile.O_CREAT | SmbFile.O_WRONLY | SmbFile.O_APPEND :
                                    SmbFile.O_CREAT | SmbFile.O_WRONLY | SmbFile.O_TRUNC );
    
public SmbFileOutputStream(String url, int shareAccess)
Creates an {@link java.io.OutputStream} for writing bytes to a file on an SMB server addressed by the SmbFile parameter. See {@link jcifs.smb.SmbFile} for a detailed description and examples of the smb URL syntax.

The second parameter specifies how the file should be shared. If SmbFile.FILE_NO_SHARE 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 FILE_SHARE_READ, FILE_SHARE_WRITE, and FILE_SHARE_DELETE 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: SmbFile.FILE_NOSHARE or any combination of SmbFile.FILE_READ, SmbFile.FILE_WRITE, and SmbFile.FILE_DELETE

        this( new SmbFile( url, "", null, shareAccess ), false );
    
SmbFileOutputStream(SmbFile file, boolean append, int openFlags)

        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();    
        }
    
Methods Summary
public voidclose()
Closes this output stream and releases any system resources associated with it.

throws
IOException if a network error occurs

        file.close();
        tmp = null;
    
voidensureOpen()

        // 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();
            }
        }
    
public booleanisOpen()

        return file.isOpen();
    
public voidwrite(byte[] b, int off, int len)
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

        if( file.isOpen() == false && file instanceof SmbNamedPipe ) {
            file.send( new TransWaitNamedPipe( "\\pipe" + file.unc ),
                                    new TransWaitNamedPipeResponse() );
        }
        writeDirect( b, off, len, 0 );
    
public voidwrite(int b)
Writes the specified byte to this file output stream.

throws
IOException if a network error occurs

        tmp[0] = (byte)b;
        write( tmp, 0, 1 );
    
public voidwrite(byte[] b)
Writes b.length bytes from the specified byte array to this file output stream.

throws
IOException if a network error occurs

        write( b, 0, b.length );
    
public voidwriteDirect(byte[] b, int off, int len, int flags)
Just bypasses TransWaitNamedPipe - used by DCERPC bind.

        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 );