Mercurial > 510Connectbot
comparison app/src/main/java/ch/ethz/ssh2/SFTPInputStream.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/ch/ethz/ssh2/SFTPInputStream.java@071eccdff8ea |
children |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
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 | |
38 if (read > 0) { | |
39 readOffset += read; | |
40 } | |
41 | |
42 return read; | |
43 } | |
44 | |
45 /** | |
46 * Reads the next byte of data from the input stream. The value byte is | |
47 * returned as an <code>int</code> in the range <code>0</code> to | |
48 * <code>255</code>. If no byte is available because the end of the stream | |
49 * has been reached, the value <code>-1</code> is returned. This method | |
50 * blocks until input data is available, the end of the stream is detected, | |
51 * or an exception is thrown. | |
52 * <p/> | |
53 * <p> A subclass must provide an implementation of this method. | |
54 * | |
55 * @return the next byte of data, or <code>-1</code> if the end of the | |
56 * stream is reached. | |
57 * @throws IOException if an I/O error occurs. | |
58 */ | |
59 @Override | |
60 public int read() throws IOException { | |
61 byte[] buffer = new byte[1]; | |
62 int b = handle.getClient().read(handle, readOffset, buffer, 0, 1); | |
63 | |
64 if (b > 0) { | |
65 readOffset += 1; | |
66 } | |
67 | |
68 return b; | |
69 } | |
70 | |
71 /** | |
72 * Skips over and discards <code>n</code> bytes of data from this input | |
73 * stream. | |
74 * | |
75 * @param n the number of bytes to be skipped. | |
76 * @return the actual number of bytes skipped. | |
77 */ | |
78 @Override | |
79 public long skip(long n) { | |
80 readOffset += n; | |
81 return n; | |
82 } | |
83 | |
84 @Override | |
85 public void close() throws IOException { | |
86 handle.getClient().closeFile(handle); | |
87 } | |
88 } |