Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Android™) Chaining Asynchronous StreamsThe asynchronous functionality of Chilkat, combined with streams, can offer some interesting possibilities. This example demonstrates how to create two streams, call them streamA and streamB, where streamA feeds into streamB and both streams run in background threads. The examples demonstrates copying a file in this way: sourceFile --> streamA --> streamB --> outputFile This may seem pointless, but the possibilities become interesting when intermediate streams can perform tasks such as encryption or compression, or when a source or sink is a socket, a TLS connection, an SSH tunnel, etc. Or perhaps if a stream becomes the source for the file in an HTTP upload, or perhaps it is the source or sink for files in FTP, SFTP, etc. This is where Chilkat's Async standards in combination with Chilkat Stream can begin to offer powerful and simple-to-implement possibilities.
// 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); CkStream streamA = new CkStream(); CkStream streamB = new CkStream(); streamA.put_SourceFile("qa_data/hamlet.xml"); // streamA will feed into streamB. (streamA is the source for streamB) boolean success = streamB.SetSourceStream(streamA); streamB.put_SinkFile("qa_output/hamlet_copy.xml"); // Run each stream asynchronously in background threads: CkTask taskA = streamA.RunStreamAsync(); CkTask taskB = streamB.RunStreamAsync(); // Start each task. (The call to Run returns immediately. // each task begins running in its own background thread.) success = taskA.Run(); success = taskB.Run(); // Wait for each task to complete. // Note: The SleepMs method is a utility method that puts the caller to sleep // for a specified number of milliseconds. // It is the caller's thread that sleeps (i.e. this foreground thread). // It does not cause the background thread of the given task object to sleep. while (((taskA.get_Finished() != true) or (taskB.get_Finished() != true))) { taskB.SleepMs(20); } // At this point, each background task has finished. // Let's check to see if each stream succeeded. // Did streamA succeed in reading the entire file? if (taskA.get_TaskSuccess() != true) { Log.i(TAG, "streamA failed:"); Log.i(TAG, taskA.resultErrorText()); success = false; } // Did streamB succeed in writing the entire file? if (taskB.get_TaskSuccess() != true) { Log.i(TAG, "streamB failed:"); Log.i(TAG, taskB.resultErrorText()); success = false; } if (success != true) { return; } // Let's double-check to see that the files are equal in size and content: CkFileAccess fac = new CkFileAccess(); boolean bFilesEqual = fac.FileContentsEqual(streamA.sourceFile(),streamB.sinkFile()); if (bFilesEqual != true) { Log.i(TAG, "The output file is not equal to the input file!"); } else { Log.i(TAG, "The file was successfully copied in a very roundabout way!"); } } 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." } } |
© 2000-2023 Chilkat Software, Inc. All Rights Reserved.