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");
}
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");
}
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++;
}
}
}
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;
}
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));
}
Aggregations