Search in sources :

Example 1 with FileIOChannel

use of org.ballerinalang.nativeimpl.io.channels.FileIOChannel 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 2 with FileIOChannel

use of org.ballerinalang.nativeimpl.io.channels.FileIOChannel 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 3 with FileIOChannel

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

the class LoadToTable method getDelimitedRecordChannel.

private DelimitedRecordChannel getDelimitedRecordChannel(Context context, FileChannel sourceChannel) {
    final String recordSeparator = context.getStringArgument(1);
    final String fieldSeparator = context.getStringArgument(2);
    final String encoding = context.getStringArgument(3);
    FileIOChannel fileIOChannel = new FileIOChannel(sourceChannel);
    CharacterChannel characterChannel = new CharacterChannel(fileIOChannel, Charset.forName(encoding).name());
    return new DelimitedRecordChannel(characterChannel, recordSeparator, fieldSeparator);
}
Also used : FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Aggregations

FileIOChannel (org.ballerinalang.nativeimpl.io.channels.FileIOChannel)3 FileChannel (java.nio.channels.FileChannel)2 AccessDeniedException (java.nio.file.AccessDeniedException)2 OpenOption (java.nio.file.OpenOption)2 Path (java.nio.file.Path)2 StandardOpenOption (java.nio.file.StandardOpenOption)2 HashSet (java.util.HashSet)2 AbstractNativeChannel (org.ballerinalang.nativeimpl.io.channels.AbstractNativeChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 BStruct (org.ballerinalang.model.values.BStruct)1 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)1 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)1