1 /* 2 * Copyright (c) 2011 David Kocher. All rights reserved. 3 * Please refer to the LICENSE.txt for licensing details. 4 */ 5 package ch.ethz.ssh2; 6 7 import java.io.IOException; 8 import java.io.OutputStream; 9 10 /** 11 * @version $Id:$ 12 */ 13 public class SFTPOutputStream extends OutputStream 14 { 15 16 private SFTPv3FileHandle handle; 17 18 /** 19 * Offset (in bytes) in the file to write 20 */ 21 private long writeOffset = 0; 22 SFTPOutputStream(SFTPv3FileHandle handle)23 public SFTPOutputStream(SFTPv3FileHandle handle) { 24 this.handle = handle; 25 } 26 27 /** 28 * Writes <code>len</code> bytes from the specified byte array 29 * starting at offset <code>off</code> to this output stream. 30 * The general contract for <code>write(b, off, len)</code> is that 31 * some of the bytes in the array <code>b</code> are written to the 32 * output stream in order; element <code>b[off]</code> is the first 33 * byte written and <code>b[off+len-1]</code> is the last byte written 34 * by this operation. 35 * 36 * @see SFTPv3Client#write(SFTPv3FileHandle,long,byte[],int,int) 37 */ 38 @Override write(byte[] buffer, int offset, int len)39 public void write(byte[] buffer, int offset, int len) throws IOException 40 { 41 // We can just blindly write the whole buffer at once. 42 // if <code>len</code> > 32768, then the write operation will 43 // be split into multiple writes in SFTPv3Client#write. 44 handle.getClient().write(handle, writeOffset, buffer, offset, len); 45 46 writeOffset += len; 47 } 48 49 @Override write(int b)50 public void write(int b) throws IOException { 51 byte[] buffer = new byte[1]; 52 buffer[0] = (byte) b; 53 handle.getClient().write(handle, writeOffset, buffer, 0, 1); 54 55 writeOffset += 1; 56 } 57 skip(long n)58 public long skip(long n) { 59 writeOffset += n; 60 return n; 61 } 62 63 @Override close()64 public void close() throws IOException { 65 handle.getClient().closeFile(handle); 66 } 67 }