Sample code for 30+ languages & platforms
Java

Append Encoded Binary Data to StringBuilder

Demonstrates how to append encoded binary data to the contenets of a StringBuilder.

Chilkat Java Downloads

Java
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[])
  {
    boolean success = false;

    CkBinData bd = new CkBinData();

    success = bd.LoadFile("qa_data/jpg/starfish.jpg");
    if (success == false) {
        System.out.println("Failed to load file.");
        return;
        }

    //  For example, let's say we want construct simple JSON containing the base64 representation of the above JPG file.
    CkStringBuilder sb = new CkStringBuilder();
    sb.Append("{ \"jpg\": \"");
    //  GetEncodedSb appends the enocded representation of the binary data to the StringBuiler passed in the 2nd arg.
    bd.GetEncodedSb("base64",sb);
    sb.Append("\" }");

    System.out.println(sb.getAsString());

    //  Output looks like this:
    //   { "jpg": "/9j/4AAQSkZJRgABAg...rcQ+vo//2Q==" }
  }
}