Sample code for 30+ languages & platforms
Android™

Read Bytes from a Remote SFTP File (BinData)

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadFileBd method, which reads up to a number of bytes from the current position of an open handle and appends them to a BinData. The arguments are the handle, the maximum number of bytes, and the BinData. This example reads in chunks until end-of-file.

Background: For binary files, reading into a BinData preserves the bytes exactly, unlike a text read that decodes through a charset. Because it appends, the chunked read loop accumulates the whole file in the BinData; each pass reads up to the chunk size and the loop repeats until Eof. Fewer bytes than requested can come back near the end of the file, which is normal.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the SFtp.ReadFileBd method, which reads up to a number of bytes from the current
    //  position of an open handle and appends them to a BinData.  The 1st argument is the handle, the
    //  2nd is the maximum number of bytes, and the 3rd is the BinData.

    CkSFtp sftp = new CkSFtp();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    int port = 22;
    success = sftp.Connect("sftp.example.com",port);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    String password = "mySshPassword";

    success = sftp.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    success = sftp.InitializeSftp();
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    String handle = sftp.openFile("subdir/image.png","readOnly","openExisting");
    if (sftp.get_LastMethodSuccess() == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Read the file in chunks until end-of-file, accumulating the bytes in a BinData.
    CkBinData bd = new CkBinData();
    int chunkSize = 8192;
    boolean reading = true;
    while (reading) {
        success = sftp.ReadFileBd(handle,chunkSize,bd);
        if (sftp.LastReadFailed(handle)) {
            Log.i(TAG, sftp.lastErrorText());
            return;
            }

        reading = !sftp.Eof(handle);
        }

    success = sftp.CloseHandle(handle);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Read " + String.valueOf(bd.get_NumBytes()) + " bytes.");

    sftp.Disconnect();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}