/* 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 com.knowgate.jcifs.smb; import java.net.URL; import java.io.OutputStream; import java.io.IOException; import java.net.UnknownHostException; import java.net.MalformedURLException; import com.knowgate.debug.*; /** * 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, 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.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 * @return A new <code>OutputStream</code> for the specified file */ 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.smb.SmbFile} parameter. See * {@link jcifs.smb.SmbFile} for a detailed description and examples of * the smb URL syntax. * * @param url An <code>SmbFile</code> specifying the file to write to * @return A new <code>OutputStream</code> for the specified <code>SmbFile</code> */ 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.smb.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 * @return A new <code>OutputStream</code> for the specified <code>url</code> */ 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.smb.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 <code>SmbFile</code> representing the file to write to * @return A new <code>OutputStream</code> for the specified <code>SmbFile</code> */ 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.smb.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 <code>SmbFile</code> representing the file to write to * @return A new <code>OutputStream</code> for the specified <code>SmbFile</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; if( append ) { try { fp = file.length(); } catch( SmbException se ) { fp = 0L; } } if( file instanceof SmbNamedPipe && file.unc.startsWith( "\\pipe\\" )) { file.unc = file.unc.substring( 5 ); file.sendTransaction( new TransWaitNamedPipe( "\\pipe" + file.unc ), new TransWaitNamedPipeResponse() ); } file.open( openFlags, SmbFile.ATTR_NORMAL, 0 ); 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(); } /** * 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 ); } /** * 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( len <= 0 ) { return; } // ensure file is open if( file.isOpen() == false ) { if( file instanceof SmbNamedPipe ) { file.sendTransaction( new TransWaitNamedPipe( "\\pipe" + file.unc ), new TransWaitNamedPipeResponse() ); } file.open( openFlags, SmbFile.ATTR_NORMAL, 0 ); if( append ) { fp = file.length(); } } if( DebugFile.trace ) DebugFile.writeln( "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 ); 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 ); } }