diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ch/ethz/ssh2/SFTPInputStream.java	Fri Jul 18 11:21:46 2014 -0700
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2011 David Kocher. All rights reserved.
+ * Please refer to the LICENSE.txt for licensing details.
+ */
+package ch.ethz.ssh2;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @version $Id: SFTPInputStream.java 151 2014-04-28 10:03:39Z dkocher@sudo.ch $
+ */
+public class SFTPInputStream extends InputStream {
+
+    private SFTPFileHandle handle;
+
+    /**
+     * Offset (in bytes) in the file to read
+     */
+    private long readOffset = 0;
+
+    public SFTPInputStream(SFTPFileHandle handle) {
+        this.handle = handle;
+    }
+
+    /**
+     * Reads up to <code>len</code> bytes of data from the input stream into
+     * an array of bytes.  An attempt is made to read as many as
+     * <code>len</code> bytes, but a smaller number may be read, possibly
+     * zero. The number of bytes actually read is returned as an integer.
+     *
+     * @see SFTPClient#read(SFTPFileHandle, long, byte[], int, int)
+     */
+    @Override
+    public int read(byte[] buffer, int offset, int len) throws IOException {
+        int read = handle.getClient().read(handle, readOffset, buffer, offset, len);
+        if(read > 0) {
+            readOffset += read;
+        }
+        return read;
+    }
+
+    /**
+     * Reads the next byte of data from the input stream. The value byte is
+     * returned as an <code>int</code> in the range <code>0</code> to
+     * <code>255</code>. If no byte is available because the end of the stream
+     * has been reached, the value <code>-1</code> is returned. This method
+     * blocks until input data is available, the end of the stream is detected,
+     * or an exception is thrown.
+     * <p/>
+     * <p> A subclass must provide an implementation of this method.
+     *
+     * @return the next byte of data, or <code>-1</code> if the end of the
+     * stream is reached.
+     * @throws IOException if an I/O error occurs.
+     */
+    @Override
+    public int read() throws IOException {
+        byte[] buffer = new byte[1];
+        int b = handle.getClient().read(handle, readOffset, buffer, 0, 1);
+        if(b > 0) {
+            readOffset += 1;
+        }
+        return b;
+    }
+
+    /**
+     * Skips over and discards <code>n</code> bytes of data from this input
+     * stream.
+     *
+     * @param n the number of bytes to be skipped.
+     * @return the actual number of bytes skipped.
+     */
+    @Override
+    public long skip(long n) {
+        readOffset += n;
+        return n;
+    }
+
+    @Override
+    public void close() throws IOException {
+        handle.getClient().closeFile(handle);
+    }
+}
\ No newline at end of file