Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Java Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Java) Chaining Asynchronous Streams

The 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.

Chilkat Java Downloads

Java Libs for Windows, Linux, Alpine Linux,
MAC OS X, Solaris, FreeBSD, OpenBSD,
Raspberry Pi and other single board computers

Java Libs for Android

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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) || (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) {
        System.out.println("streamA failed:");
        System.out.println(taskA.resultErrorText());
        success = false;
    }

    //  Did streamB succeed in writing the entire file?
    if (taskB.get_TaskSuccess() != true) {
        System.out.println("streamB failed:");
        System.out.println(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) {
        System.out.println("The output file is not equal to the input file!");
    }
    else {
        System.out.println("The file was successfully copied in a very roundabout way!");
    }


  }
}

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.