Search in sources :

Example 46 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class EntityBodyHandler method writeByteChannelToOutputStream.

/**
 * Write byte channel stream directly into outputstream without converting it to a data source.
 *
 * @param entityStruct        Represent a ballerina entity
 * @param messageOutputStream Represent the outputstream that the message should be written to
 * @throws IOException When an error occurs while writing inputstream to outputstream
 */
public static void writeByteChannelToOutputStream(BStruct entityStruct, OutputStream messageOutputStream) throws IOException {
    Channel byteChannel = EntityBodyHandler.getByteChannel(entityStruct);
    if (byteChannel != null) {
        MimeUtil.writeInputToOutputStream(byteChannel.getInputStream(), messageOutputStream);
        byteChannel.close();
    }
}
Also used : Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) TempFileIOChannel(org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel) FileChannel(java.nio.channels.FileChannel)

Example 47 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class EntityBodyHandler method constructStringDataSource.

/**
 * Construct StringDataSource from the underneath byte channel which is associated with the entity struct.
 *
 * @param entityStruct Represent an entity struct
 * @return StringDataSource which represent the entity body which is kept in memory
 */
public static StringDataSource constructStringDataSource(BStruct entityStruct) {
    try {
        Channel byteChannel = getByteChannel(entityStruct);
        if (byteChannel == null) {
            throw new BallerinaIOException("Payload is null");
        }
        String textContent = StringUtils.getStringFromInputStream(byteChannel.getInputStream());
        byteChannel.close();
        return new StringDataSource(textContent);
    } catch (IOException e) {
        throw new BallerinaIOException("Error occurred while closing the channel", e);
    }
}
Also used : Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) TempFileIOChannel(org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException)

Example 48 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class EntityBodyHandler method constructBlobDataSource.

/**
 * Construct BlobDataSource from the underneath byte channel which is associated with the entity struct.
 *
 * @param entityStruct Represent an entity struct
 * @return BlobDataSource Data source for binary data which is kept in memory
 * @throws IOException In case an error occurred while creating blob data source
 */
public static BlobDataSource constructBlobDataSource(BStruct entityStruct) throws IOException {
    Channel byteChannel = getByteChannel(entityStruct);
    if (byteChannel == null) {
        return null;
    }
    byte[] byteData = MimeUtil.getByteArray(byteChannel.getInputStream());
    byteChannel.close();
    return new BlobDataSource(byteData);
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) TempFileIOChannel(org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel) FileChannel(java.nio.channels.FileChannel)

Example 49 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class GetBodyParts method execute.

@Override
public void execute(Context context) {
    BRefValueArray partsArray = new BRefValueArray();
    try {
        BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
        // Get the body parts from entity's multipart data field, if they've been already been decoded
        partsArray = EntityBodyHandler.getBodyPartArray(entityStruct);
        if (partsArray == null || partsArray.size() < 1) {
            Channel byteChannel = EntityBodyHandler.getByteChannel(entityStruct);
            if (byteChannel != null) {
                EntityBodyHandler.decodeEntityBody(context, entityStruct, byteChannel);
                // Check the body part availability for the second time, since the parts will be by this
                // time populated from bytechannel
                partsArray = EntityBodyHandler.getBodyPartArray(entityStruct);
                // Set byte channel that belongs to parent entity to null, once the message body parts have
                // been decoded
                entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
            }
        }
    } catch (Throwable e) {
        context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while extracting body parts from entity: " + e.getMessage()));
    }
    context.setReturnValues(partsArray);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Aggregations

Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)49 ByteChannel (java.nio.channels.ByteChannel)33 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)31 Test (org.testng.annotations.Test)31 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)19 BStruct (org.ballerinalang.model.values.BStruct)12 IOException (java.io.IOException)7 FileChannel (java.nio.channels.FileChannel)7 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)7 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)6 TempFileIOChannel (org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel)5 InputStream (java.io.InputStream)4 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 BallerinaIOException (org.ballerinalang.nativeimpl.io.BallerinaIOException)3 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 InputStreamReader (java.io.InputStreamReader)2