Search in sources :

Example 31 with BBlob

use of org.ballerinalang.model.values.BBlob in project ballerina by ballerina-lang.

the class ToBlob method execute.

public void execute(Context ctx) {
    try {
        String string = ctx.getStringArgument(0);
        String encoding = ctx.getStringArgument(1);
        byte[] arr = string.getBytes(encoding);
        ctx.setReturnValues(new BBlob(arr));
    } catch (UnsupportedEncodingException e) {
        throw new BallerinaException("Unsupported Encoding", e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BBlob(org.ballerinalang.model.values.BBlob)

Example 32 with BBlob

use of org.ballerinalang.model.values.BBlob in project ballerina by ballerina-lang.

the class GetBlob method execute.

@Override
public void execute(Context context) {
    BlobDataSource result = null;
    try {
        BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
        MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
        if (messageDataSource != null) {
            result = (BlobDataSource) messageDataSource;
        } else {
            result = EntityBodyHandler.constructBlobDataSource(entityStruct);
            EntityBodyHandler.addMessageDataSource(entityStruct, result);
            // Set byte channel to null, once the message data source has been constructed
            entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
        }
    } catch (Throwable e) {
        context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while extracting blob data from entity : " + e.getMessage()));
    }
    context.setReturnValues(new BBlob(result != null ? result.getValue() : new byte[0]));
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BStruct(org.ballerinalang.model.values.BStruct) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) BBlob(org.ballerinalang.model.values.BBlob)

Aggregations

BBlob (org.ballerinalang.model.values.BBlob)32 BValue (org.ballerinalang.model.values.BValue)23 Test (org.testng.annotations.Test)19 BString (org.ballerinalang.model.values.BString)16 BInteger (org.ballerinalang.model.values.BInteger)11 BBoolean (org.ballerinalang.model.values.BBoolean)6 BFloat (org.ballerinalang.model.values.BFloat)6 BStruct (org.ballerinalang.model.values.BStruct)5 BType (org.ballerinalang.model.types.BType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)3 ByteBuffer (java.nio.ByteBuffer)2 Base64 (java.util.Base64)2 BJSON (org.ballerinalang.model.values.BJSON)2 BRefType (org.ballerinalang.model.values.BRefType)2 BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)2 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CRC32 (java.util.zip.CRC32)1 Checksum (java.util.zip.Checksum)1 Context (org.ballerinalang.bre.Context)1