Search in sources :

Example 16 with Channel

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

the class OpenFile method inFlow.

/**
 * {@inheritDoc}
 */
@Override
public Channel inFlow(Context context) throws BallerinaException {
    String pathUrl = context.getStringArgument(PATH_FIELD_INDEX);
    String accessMode = context.getStringArgument(FILE_ACCESS_MODE_INDEX);
    Path path = null;
    Channel channel;
    try {
        String accessLC = accessMode.toLowerCase(Locale.getDefault());
        path = Paths.get(pathUrl);
        Set<OpenOption> opts = new HashSet<>();
        if (accessLC.contains("r")) {
            if (!Files.exists(path)) {
                throw new BallerinaException("file not found: " + path);
            }
            if (!Files.isReadable(path)) {
                throw new BallerinaException("file is not readable: " + path);
            }
            opts.add(StandardOpenOption.READ);
        }
        boolean write = accessLC.contains("w");
        boolean append = accessLC.contains("a");
        if (write || append) {
            if (Files.exists(path) && !Files.isWritable(path)) {
                throw new BallerinaException("file is not writable: " + path);
            }
            createDirs(path);
            opts.add(StandardOpenOption.CREATE);
            if (append) {
                opts.add(StandardOpenOption.APPEND);
            } else {
                opts.add(StandardOpenOption.WRITE);
            }
        }
        FileChannel byteChannel = FileChannel.open(path, opts);
        // channel = new FileIOChannel(byteChannel, IOConstants.CHANNEL_BUFFER_SIZE);
        channel = new FileIOChannel(byteChannel);
    } catch (AccessDeniedException e) {
        throw new BallerinaException("Do not have access to write file: " + path, e);
    } catch (Throwable e) {
        throw new BallerinaException("failed to open file: " + e.getMessage(), e);
    }
    return channel;
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) AccessDeniedException(java.nio.file.AccessDeniedException) FileChannel(java.nio.channels.FileChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) AbstractNativeChannel(org.ballerinalang.nativeimpl.io.channels.AbstractNativeChannel) FileChannel(java.nio.channels.FileChannel) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) HashSet(java.util.HashSet)

Example 17 with Channel

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

the class Write method execute.

/**
 * Writes bytes to a given channel.
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
    byte[] content = context.getBlobArgument(CONTENT_INDEX);
    int offset = (int) context.getIntArgument(START_OFFSET_INDEX);
    Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.write(byteChannel, content, offset, eventContext, Write::writeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Example 18 with Channel

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

the class AbstractNativeChannel method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context) {
    Channel channel = inFlow(context);
    BStruct channelStruct = BLangConnectorSPIUtil.createBStruct(context, BYTE_CHANNEL_PACKAGE, STRUCT_TYPE);
    channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, channel);
    context.setReturnValues(channelStruct);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Example 19 with Channel

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

the class OpenChannel method inFlow.

/**
 * {@inheritDoc}
 */
@Override
public Channel inFlow(Context context) throws BallerinaException {
    BStruct fileStruct = (BStruct) context.getRefArgument(FILE_CHANNEL_INDEX);
    String accessMode = context.getStringArgument(FILE_ACCESS_MODE_INDEX);
    Path path = null;
    Channel channel;
    try {
        String accessLC = accessMode.toLowerCase(Locale.getDefault());
        path = Paths.get(fileStruct.getStringField(PATH_FIELD_INDEX));
        Set<OpenOption> opts = new HashSet<>();
        if (accessLC.contains("r")) {
            if (!Files.exists(path)) {
                throw new BallerinaException("file not found: " + path);
            }
            if (!Files.isReadable(path)) {
                throw new BallerinaException("file is not readable: " + path);
            }
            opts.add(StandardOpenOption.READ);
        }
        boolean write = accessLC.contains("w");
        boolean append = accessLC.contains("a");
        if (write || append) {
            if (Files.exists(path) && !Files.isWritable(path)) {
                throw new BallerinaException("file is not writable: " + path);
            }
            createDirs(path);
            opts.add(StandardOpenOption.CREATE);
            if (append) {
                opts.add(StandardOpenOption.APPEND);
            } else {
                opts.add(StandardOpenOption.WRITE);
            }
        }
        FileChannel byteChannel = FileChannel.open(path, opts);
        channel = new FileIOChannel(byteChannel);
    } catch (AccessDeniedException e) {
        throw new BallerinaException("Do not have access to write file: " + path, e);
    } catch (Throwable e) {
        throw new BallerinaException("failed to open file: " + e.getMessage(), e);
    }
    return channel;
}
Also used : Path(java.nio.file.Path) BStruct(org.ballerinalang.model.values.BStruct) AccessDeniedException(java.nio.file.AccessDeniedException) FileChannel(java.nio.channels.FileChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) AbstractNativeChannel(org.ballerinalang.nativeimpl.io.channels.AbstractNativeChannel) FileChannel(java.nio.channels.FileChannel) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) HashSet(java.util.HashSet)

Example 20 with Channel

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

the class EntityBodyHandler method constructJsonDataSource.

/**
 * Construct JsonDataSource from the underneath byte channel which is associated with the entity struct.
 *
 * @param entityStruct Represent an entity struct
 * @return BJSON data source which is kept in memory
 */
public static BJSON constructJsonDataSource(BStruct entityStruct) {
    try {
        Channel byteChannel = getByteChannel(entityStruct);
        if (byteChannel == null) {
            return null;
        }
        BJSON jsonData = new BJSON(byteChannel.getInputStream());
        byteChannel.close();
        return jsonData;
    } catch (IOException e) {
        throw new BallerinaIOException("Error occurred while closing connection", 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) BJSON(org.ballerinalang.model.values.BJSON) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException)

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