273
|
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: SFTPOutputStream.java 151 2014-04-28 10:03:39Z dkocher@sudo.ch $
|
|
12 */
|
|
13 public class SFTPOutputStream extends OutputStream {
|
|
14
|
|
15 private SFTPFileHandle handle;
|
|
16
|
|
17 /**
|
|
18 * Offset (in bytes) in the file to write
|
|
19 */
|
|
20 private long writeOffset = 0;
|
|
21
|
|
22 public SFTPOutputStream(SFTPFileHandle handle) {
|
|
23 this.handle = handle;
|
|
24 }
|
|
25
|
|
26 /**
|
|
27 * Writes <code>len</code> bytes from the specified byte array
|
|
28 * starting at offset <code>off</code> to this output stream.
|
|
29 * The general contract for <code>write(b, off, len)</code> is that
|
|
30 * some of the bytes in the array <code>b</code> are written to the
|
|
31 * output stream in order; element <code>b[off]</code> is the first
|
|
32 * byte written and <code>b[off+len-1]</code> is the last byte written
|
|
33 * by this operation.
|
|
34 *
|
|
35 * @see SFTPClient#write(SFTPFileHandle, long, byte[], int, int)
|
|
36 */
|
|
37 @Override
|
|
38 public void write(byte[] buffer, int offset, int len) throws IOException {
|
|
39 // We can just blindly write the whole buffer at once.
|
|
40 // if <code>len</code> > 32768, then the write operation will
|
|
41 // be split into multiple writes in SFTPv3Client#write.
|
|
42 handle.getClient().write(handle, writeOffset, buffer, offset, len);
|
|
43 writeOffset += len;
|
|
44 }
|
|
45
|
|
46 @Override
|
|
47 public void write(int b) throws IOException {
|
|
48 byte[] buffer = new byte[1];
|
|
49 buffer[0] = (byte) b;
|
|
50 handle.getClient().write(handle, writeOffset, buffer, 0, 1);
|
|
51 writeOffset += 1;
|
|
52 }
|
|
53
|
|
54 public long skip(long n) {
|
|
55 writeOffset += n;
|
|
56 return n;
|
|
57 }
|
|
58
|
|
59 @Override
|
|
60 public void close() throws IOException {
|
|
61 handle.getClient().closeFile(handle);
|
|
62 }
|
|
63 } |