use of org.ballerinalang.model.values.BBlob in project ballerina by ballerina-lang.
the class RequestNativeFunctionSuccessTest method testSetBinaryPayload.
@Test(description = "Test setBinaryPayload() function")
public void testSetBinaryPayload() {
BBlob value = new BBlob("Ballerina".getBytes());
BValue[] inputArg = { value };
BValue[] returnVals = BRunUtil.invoke(result, "testSetBinaryPayload", inputArg);
Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
Assert.assertTrue(returnVals[0] instanceof BStruct);
BStruct entity = (BStruct) ((BStruct) returnVals[0]).getNativeData(MESSAGE_ENTITY);
MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entity);
Assert.assertEquals(messageDataSource.getMessageAsString(), "Ballerina", "Payload is not set properly");
}
use of org.ballerinalang.model.values.BBlob in project ballerina by ballerina-lang.
the class NativeFunctionsTestCase method testPingPong.
@Test
public void testPingPong() {
byte[] bytes = { 1, 2, 3, 4, 5 };
// Test ping
BValue[] inputBValuesForPing = { wsConnection, new BBlob(bytes) };
BRunUtil.invoke(compileResult, "testPing", inputBValuesForPing);
ByteBuffer pingBuffer = session.getBufferReceived();
Assert.assertTrue(session.isPing());
Assert.assertEquals(pingBuffer.array(), bytes);
// Test pong
BValue[] inputBValuesForPong = { wsConnection, new BBlob(bytes) };
BRunUtil.invoke(compileResult, "testPong", inputBValuesForPong);
ByteBuffer pongBuffer = session.getBufferReceived();
Assert.assertTrue(session.isPong());
Assert.assertEquals(pongBuffer.array(), bytes);
}
use of org.ballerinalang.model.values.BBlob in project ballerina by ballerina-lang.
the class SecureClientSocketTest method testWriteReadContent.
@Test(dependsOnMethods = "testOpenSecureClientSocket", description = "Test content read/write")
public void testWriteReadContent() {
final String newline = System.lineSeparator();
String content = "Hello World" + newline;
final 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 CPU method execTypeCastOpcodes.
@SuppressWarnings("rawtypes")
private static void execTypeCastOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
int i;
int j;
// Index of the constant pool
int cpIndex;
BRefType bRefTypeValue;
TypeRefCPEntry typeRefCPEntry;
switch(opcode) {
case InstructionCodes.I2ANY:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BInteger(sf.longRegs[i]);
break;
case InstructionCodes.F2ANY:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BFloat(sf.doubleRegs[i]);
break;
case InstructionCodes.S2ANY:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BString(sf.stringRegs[i]);
break;
case InstructionCodes.B2ANY:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BBoolean(sf.intRegs[i] == 1);
break;
case InstructionCodes.L2ANY:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BBlob(sf.byteRegs[i]);
break;
case InstructionCodes.ANY2I:
i = operands[0];
j = operands[1];
sf.longRegs[j] = ((BInteger) sf.refRegs[i]).intValue();
break;
case InstructionCodes.ANY2F:
i = operands[0];
j = operands[1];
sf.doubleRegs[j] = ((BFloat) sf.refRegs[i]).floatValue();
break;
case InstructionCodes.ANY2S:
i = operands[0];
j = operands[1];
sf.stringRegs[j] = sf.refRegs[i].stringValue();
break;
case InstructionCodes.ANY2B:
i = operands[0];
j = operands[1];
sf.intRegs[j] = ((BBoolean) sf.refRegs[i]).booleanValue() ? 1 : 0;
break;
case InstructionCodes.ANY2L:
i = operands[0];
j = operands[1];
sf.byteRegs[j] = ((BBlob) sf.refRegs[i]).blobValue();
break;
case InstructionCodes.ANY2JSON:
handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeJSON);
break;
case InstructionCodes.ANY2XML:
handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeXML);
break;
case InstructionCodes.ANY2MAP:
handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeMap);
break;
case InstructionCodes.ANY2TYPE:
handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeDesc);
break;
case InstructionCodes.ANY2DT:
handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeTable);
break;
case InstructionCodes.ANY2STM:
handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeStream);
break;
case InstructionCodes.ANY2E:
case InstructionCodes.ANY2T:
case InstructionCodes.ANY2C:
case InstructionCodes.CHECKCAST:
i = operands[0];
cpIndex = operands[1];
j = operands[2];
typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
bRefTypeValue = sf.refRegs[i];
if (checkCast(bRefTypeValue, typeRefCPEntry.getType())) {
sf.refRegs[j] = sf.refRegs[i];
} else {
handleTypeCastError(ctx, sf, j, bRefTypeValue != null ? bRefTypeValue.getType() : BTypes.typeNull, typeRefCPEntry.getType());
}
break;
case InstructionCodes.IS_ASSIGNABLE:
i = operands[0];
cpIndex = operands[1];
j = operands[2];
typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
bRefTypeValue = sf.refRegs[i];
if (isAssignable(bRefTypeValue, typeRefCPEntry.getType())) {
sf.intRegs[j] = 1;
} else {
sf.intRegs[j] = 0;
}
break;
case InstructionCodes.NULL2JSON:
j = operands[1];
sf.refRegs[j] = new BJSON("null");
break;
case InstructionCodes.B2JSON:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BJSON(sf.intRegs[i] == 1 ? "true" : "false");
break;
case InstructionCodes.JSON2I:
castJSONToInt(ctx, operands, sf);
break;
case InstructionCodes.JSON2F:
castJSONToFloat(ctx, operands, sf);
break;
case InstructionCodes.JSON2S:
castJSONToString(ctx, operands, sf);
break;
case InstructionCodes.JSON2B:
castJSONToBoolean(ctx, operands, sf);
break;
case InstructionCodes.NULL2S:
j = operands[1];
sf.stringRegs[j] = null;
break;
case InstructionCodes.CHECK_CONVERSION:
i = operands[0];
j = operands[1];
bRefTypeValue = sf.refRegs[i];
sf.refRegs[j] = JSONUtils.convertUnionTypeToJSON(bRefTypeValue);
break;
default:
throw new UnsupportedOperationException();
}
}
use of org.ballerinalang.model.values.BBlob in project ballerina by ballerina-lang.
the class Read method readResponse.
/*
* Function which will be notified on the response obtained after the async operation.
*
* @param result context of the callback.
* @return Once the callback is processed we further return back the result.
*/
private static EventResult readResponse(EventResult<Integer, EventContext> result) {
BStruct errorStruct;
BRefValueArray contentTuple = new BRefValueArray(readTupleType);
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
Throwable error = eventContext.getError();
Integer numberOfBytes = result.getResponse();
CallableUnitCallback callback = eventContext.getCallback();
byte[] content = (byte[]) eventContext.getProperties().get(ReadBytesEvent.CONTENT_PROPERTY);
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
contentTuple.add(0, new BBlob(content));
contentTuple.add(1, new BInteger(numberOfBytes));
context.setReturnValues(contentTuple);
}
callback.notifySuccess();
return result;
}
Aggregations