Search in sources :

Example 6 with BBlob

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

the class IOTest method testReadBytes.

@Test(description = "Test 'readBytes' function in ballerina.io package")
public void testReadBytes() throws URISyntaxException {
    int numberOfBytesToRead = 3;
    String resourceToRead = "datafiles/io/text/6charfile.txt";
    BBlob readBytes;
    // Will initialize the channel
    BValue[] args = { new BString(getAbsoluteFilePath(resourceToRead)), new BString("r") };
    BRunUtil.invoke(bytesInputOutputProgramFile, "initFileChannel", args);
    // Reads the 1st three bytes "123"
    byte[] expectedBytes = "123".getBytes();
    args = new BValue[] { new BInteger(numberOfBytesToRead) };
    BValue[] returns = BRunUtil.invoke(bytesInputOutputProgramFile, "readBytes", args);
    readBytes = (BBlob) returns[0];
    Assert.assertEquals(expectedBytes, readBytes.blobValue());
    // Reads the next three bytes "456"
    expectedBytes = "456".getBytes();
    args = new BValue[] { new BInteger(numberOfBytesToRead) };
    returns = BRunUtil.invoke(bytesInputOutputProgramFile, "readBytes", args);
    readBytes = (BBlob) returns[0];
    Assert.assertEquals(expectedBytes, readBytes.blobValue());
    // Request for a get, the bytes will be empty
    expectedBytes = new byte[0];
    args = new BValue[] { new BInteger(numberOfBytesToRead) };
    returns = BRunUtil.invoke(bytesInputOutputProgramFile, "readBytes", args);
    readBytes = (BBlob) returns[0];
    Assert.assertEquals(expectedBytes, readBytes.blobValue());
    BRunUtil.invoke(bytesInputOutputProgramFile, "close");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 7 with BBlob

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

the class ClientSocketTest method testWriteReadContent.

@Test(dependsOnMethods = "testOpenClientSocket", description = "Test content read/write")
public void testWriteReadContent() {
    String content = "Hello World\n";
    byte[] contentBytes = content.getBytes();
    BValue[] args = { new BBlob(contentBytes) };
    final BValue[] writeReturns = BRunUtil.invoke(socketClient, "write", args);
    BInteger returnedSize = (BInteger) writeReturns[0];
    Assert.assertEquals(returnedSize.intValue(), content.length(), "Write content size is not match.");
    args = new BValue[] { new BInteger(content.length()) };
    final BValue[] readReturns = BRunUtil.invoke(socketClient, "read", args);
    final BBlob readContent = (BBlob) readReturns[0];
    returnedSize = (BInteger) readReturns[1];
    Assert.assertEquals(readContent.stringValue(), content, "Return content are not match with written content.");
    Assert.assertEquals(returnedSize.intValue(), content.length(), "Read size not match with the request size");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 8 with BBlob

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

the class AbstractSQLAction method createProcessedStatement.

private void createProcessedStatement(Connection conn, PreparedStatement stmt, BRefValueArray params, String dataSourceName) {
    if (params == null) {
        return;
    }
    int paramCount = (int) params.size();
    int currentOrdinal = 0;
    for (int index = 0; index < paramCount; index++) {
        BStruct paramStruct = (BStruct) params.get(index);
        if (paramStruct != null) {
            String sqlType = getSQLType(paramStruct);
            BValue value = paramStruct.getRefField(1);
            int direction = getParameterDirection(paramStruct);
            // If the parameter is an array and sql type is not "array" then treat it as an array of parameters
            if (value != null && value.getType().getTag() == TypeTags.ARRAY_TAG && !Constants.SQLDataTypes.ARRAY.equalsIgnoreCase(sqlType)) {
                int arrayLength = (int) ((BNewArray) value).size();
                int typeTag = ((BArrayType) value.getType()).getElementType().getTag();
                for (int i = 0; i < arrayLength; i++) {
                    BValue paramValue;
                    switch(typeTag) {
                        case TypeTags.INT_TAG:
                            paramValue = new BInteger(((BIntArray) value).get(i));
                            break;
                        case TypeTags.FLOAT_TAG:
                            paramValue = new BFloat(((BFloatArray) value).get(i));
                            break;
                        case TypeTags.STRING_TAG:
                            paramValue = new BString(((BStringArray) value).get(i));
                            break;
                        case TypeTags.BOOLEAN_TAG:
                            paramValue = new BBoolean(((BBooleanArray) value).get(i) > 0);
                            break;
                        case TypeTags.BLOB_TAG:
                            paramValue = new BBlob(((BBlobArray) value).get(i));
                            break;
                        default:
                            throw new BallerinaException("unsupported array type for parameter index " + index);
                    }
                    if (Constants.SQLDataTypes.REFCURSOR.equals(sqlType)) {
                        setParameter(conn, stmt, sqlType, paramValue, direction, currentOrdinal, dataSourceName);
                    } else {
                        setParameter(conn, stmt, sqlType, paramValue, direction, currentOrdinal);
                    }
                    currentOrdinal++;
                }
            } else {
                if (Constants.SQLDataTypes.REFCURSOR.equals(sqlType)) {
                    setParameter(conn, stmt, sqlType, value, direction, currentOrdinal, dataSourceName);
                } else {
                    setParameter(conn, stmt, sqlType, value, direction, currentOrdinal);
                }
                currentOrdinal++;
            }
        } else {
            SQLDatasourceUtils.setNullObject(stmt, index);
            currentOrdinal++;
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BBlobArray(org.ballerinalang.model.values.BBlobArray) BFloat(org.ballerinalang.model.values.BFloat) BFloatArray(org.ballerinalang.model.values.BFloatArray) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BBlob(org.ballerinalang.model.values.BBlob)

Example 9 with BBlob

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

the class HttpDispatcher method populateAndGetEntityBody.

private static BValue populateAndGetEntityBody(HttpResource httpResource, BStruct inRequest, BStruct inRequestEntity, BType entityBodyType) throws IOException {
    HttpUtil.populateEntityBody(null, inRequest, inRequestEntity, true);
    switch(entityBodyType.getTag()) {
        case TypeTags.STRING_TAG:
            StringDataSource stringDataSource = EntityBodyHandler.constructStringDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, stringDataSource);
            return stringDataSource != null ? new BString(stringDataSource.getMessageAsString()) : null;
        case TypeTags.JSON_TAG:
            BJSON bjson = EntityBodyHandler.constructJsonDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, bjson);
            return bjson;
        case TypeTags.XML_TAG:
            BXML bxml = EntityBodyHandler.constructXmlDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, bxml);
            return bxml;
        case TypeTags.BLOB_TAG:
            BlobDataSource blobDataSource = EntityBodyHandler.constructBlobDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, blobDataSource);
            return new BBlob(blobDataSource != null ? blobDataSource.getValue() : new byte[0]);
        case TypeTags.STRUCT_TAG:
            bjson = EntityBodyHandler.constructJsonDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, bjson);
            try {
                return JSONUtils.convertJSONToStruct(bjson, (BStructType) entityBodyType);
            } catch (NullPointerException ex) {
                throw new BallerinaConnectorException("cannot convert payload to struct type: " + entityBodyType.getName());
            }
    }
    return null;
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BString(org.ballerinalang.model.values.BString) BXML(org.ballerinalang.model.values.BXML) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BJSON(org.ballerinalang.model.values.BJSON) BBlob(org.ballerinalang.model.values.BBlob)

Example 10 with BBlob

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

the class Decode method execute.

@Override
public void execute(Context context) {
    byte[] encodedContent = context.getBlobArgument(BLOB_INDEX);
    Base64.Decoder decoder = Base64.getMimeDecoder();
    byte[] decodedContent = decoder.decode(encodedContent);
    context.setReturnValues(new BBlob(decodedContent));
}
Also used : Base64(java.util.Base64) 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