Sample code for 30+ languages & platforms
Android™

Detect End-of-File on a Remote SFTP File

See more SFTP Examples

Demonstrates the Chilkat SFtp.Eof method, which returns whether the most recent read for a handle received the SFTP end-of-file status. The only argument is the handle. This example reads until Eof is true.

Background: The timing here is worth understanding: reading exactly through the final byte does not immediately set EOF. The next read succeeds, returns zero bytes, sets the status to SSH_FX_EOF, and only then does Eof report true. That is why the loop tests Eof after each read rather than assuming a short read means the end — a short read can also just be the server returning less than requested.

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.Eof method, which returns whether the most recent read for a handle
    //  received the SFTP end-of-file status.  The only argument is the handle.

    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/data.txt","readOnly","openExisting");
    if (sftp.get_LastMethodSuccess() == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Read until Eof reports the end of the file.  Note that reading exactly through the final byte
    //  does not set EOF; the next read returns zero bytes and then Eof becomes true.
    CkStringBuilder sbContent = new CkStringBuilder();
    int chunkSize = 4096;
    boolean reading = true;
    while (reading) {
        String chunk = sftp.readFileText(handle,chunkSize,"utf-8");
        if (sftp.LastReadFailed(handle)) {
            Log.i(TAG, sftp.lastErrorText());
            return;
            }

        sbContent.Append(chunk);
        reading = !sftp.Eof(handle);
        }

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

    Log.i(TAG, sbContent.getAsString());

    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."
  }
}