comparison src/ch/ethz/ssh2/SFTPInputStream.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
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.InputStream;
9
10 /**
11 * @version $Id: SFTPInputStream.java 151 2014-04-28 10:03:39Z dkocher@sudo.ch $
12 */
13 public class SFTPInputStream extends InputStream {
14
15 private SFTPFileHandle handle;
16
17 /**
18 * Offset (in bytes) in the file to read
19 */
20 private long readOffset = 0;
21
22 public SFTPInputStream(SFTPFileHandle handle) {
23 this.handle = handle;
24 }
25
26 /**
27 * Reads up to <code>len</code> bytes of data from the input stream into
28 * an array of bytes. An attempt is made to read as many as
29 * <code>len</code> bytes, but a smaller number may be read, possibly
30 * zero. The number of bytes actually read is returned as an integer.
31 *
32 * @see SFTPClient#read(SFTPFileHandle, long, byte[], int, int)
33 */
34 @Override
35 public int read(byte[] buffer, int offset, int len) throws IOException {
36 int read = handle.getClient().read(handle, readOffset, buffer, offset, len);
37 if(read > 0) {
38 readOffset += read;
39 }
40 return read;
41 }
42
43 /**
44 * Reads the next byte of data from the input stream. The value byte is
45 * returned as an <code>int</code> in the range <code>0</code> to
46 * <code>255</code>. If no byte is available because the end of the stream
47 * has been reached, the value <code>-1</code> is returned. This method
48 * blocks until input data is available, the end of the stream is detected,
49 * or an exception is thrown.
50 * <p/>
51 * <p> A subclass must provide an implementation of this method.
52 *
53 * @return the next byte of data, or <code>-1</code> if the end of the
54 * stream is reached.
55 * @throws IOException if an I/O error occurs.
56 */
57 @Override
58 public int read() throws IOException {
59 byte[] buffer = new byte[1];
60 int b = handle.getClient().read(handle, readOffset, buffer, 0, 1);
61 if(b > 0) {
62 readOffset += 1;
63 }
64 return b;
65 }
66
67 /**
68 * Skips over and discards <code>n</code> bytes of data from this input
69 * stream.
70 *
71 * @param n the number of bytes to be skipped.
72 * @return the actual number of bytes skipped.
73 */
74 @Override
75 public long skip(long n) {
76 readOffset += n;
77 return n;
78 }
79
80 @Override
81 public void close() throws IOException {
82 handle.getClient().closeFile(handle);
83 }
84 }