Android™
Android™
Check Whether the Last SFTP Read Failed
See more SFTP Examples
Demonstrates the Chilkat SFtp.LastReadFailed method, which returns whether the most recent read for a handle failed. The only argument is the handle. A normal end-of-file is not a failure.
Background: A chunked read loop needs to tell three outcomes apart: more data, a clean end-of-file, and an actual error such as a dropped connection or a revoked handle.
LastReadFailed isolates the error case — it is false on a normal EOF read (which succeeds with zero bytes) and true for an empty, malformed, or already-closed handle — so the loop can stop cleanly on EOF but bail out with an error message on a genuine failure.Chilkat Android™ Downloads
// 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.LastReadFailed method, which returns whether the most recent read for a
// handle failed. The only argument is the handle. A normal end-of-file is NOT a failure.
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;
}
boolean reading = true;
int chunkSize = 4096;
CkBinData bd = new CkBinData();
while (reading) {
success = sftp.ReadFileBd(handle,chunkSize,bd);
// Distinguish an actual read failure from a normal EOF. On EOF the read succeeds, returns
// zero bytes, and LastReadFailed is false.
if (sftp.LastReadFailed(handle)) {
Log.i(TAG, "Read failed: " + 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 with no read failures.");
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."
}
}