Search in sources :

Example 26 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project graal by oracle.

the class TruffleFile method readAllBytes.

/**
 * Reads a file content as bytes.
 *
 * @return the created {@link BufferedReader}
 * @throws IOException in case of IO error
 * @throws OutOfMemoryError if an array of a file size cannot be allocated
 * @throws SecurityException if the {@link FileSystem} denied the operation
 * @since 1.0
 */
public byte[] readAllBytes() throws IOException {
    try (SeekableByteChannel channel = newByteChannel(Collections.emptySet())) {
        long sizel = channel.size();
        if (sizel > MAX_BUFFER_SIZE) {
            throw new OutOfMemoryError("File size is too large.");
        }
        try (InputStream in = Channels.newInputStream(channel)) {
            int size = (int) sizel;
            byte[] buf = new byte[size];
            int read = 0;
            while (true) {
                int n;
                while ((n = in.read(buf, read, size - read)) > 0) {
                    read += n;
                }
                if (n < 0 || (n = in.read()) < 0) {
                    break;
                }
                if (size << 1 <= MAX_BUFFER_SIZE) {
                    size = Math.max(size << 1, BUFFER_SIZE);
                } else if (size == MAX_BUFFER_SIZE) {
                    throw new OutOfMemoryError("Required array size too large");
                } else {
                    size = MAX_BUFFER_SIZE;
                }
                buf = Arrays.copyOf(buf, size);
                buf[read++] = (byte) n;
            }
            return size == read ? buf : Arrays.copyOf(buf, read);
        }
    } catch (IOException | OutOfMemoryError | SecurityException e) {
        throw e;
    } catch (Throwable t) {
        throw wrapHostException(t);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 27 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project FinalCrypt by ron-from-nl.

the class FinalCrypt method createTargetDestinToken.

private // Tested
ByteBuffer createTargetDestinToken(// Tested
Path cipherSourcePath) {
    ByteBuffer plainTextTokenBuffer = ByteBuffer.allocate(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    plainTextTokenBuffer.clear();
    ByteBuffer cipherBitTokenBuffer = ByteBuffer.allocate(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    cipherBitTokenBuffer.clear();
    ByteBuffer encryptedTokenBuffer = ByteBuffer.allocate(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    encryptedTokenBuffer.clear();
    ByteBuffer targetDstTokenBuffer = ByteBuffer.allocate(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length() * 2);
    targetDstTokenBuffer.clear();
    long readCipherSourceChannelTransfered = 0;
    // Create plaint text Buffer
    plainTextTokenBuffer.put(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.getBytes());
    // Create Cipher Buffer
    try (final SeekableByteChannel readCipherSourceChannel = Files.newByteChannel(cipherSourcePath, EnumSet.of(StandardOpenOption.READ))) {
        // readCipherSourceChannel.position(readCipherSourceChannelPosition);
        readCipherSourceChannelTransfered = readCipherSourceChannel.read(cipherBitTokenBuffer);
        cipherBitTokenBuffer.flip();
        readCipherSourceChannel.close();
    } catch (IOException ex) {
        ui.error("Error: getTargetDestinToken: readCipherSourceChannel " + ex.getMessage() + "\r\n");
    }
    // Create Encrypted Token Buffer
    encryptedTokenBuffer = encryptBuffer(plainTextTokenBuffer, cipherBitTokenBuffer);
    // Create Target Destin Token Buffer
    byte[] tokenArray = new byte[(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length() * 2)];
    for (int x = 0; x < plainTextTokenBuffer.capacity(); x++) {
        tokenArray[x] = plainTextTokenBuffer.array()[x];
    }
    for (int x = 0; x < encryptedTokenBuffer.capacity(); x++) {
        tokenArray[(FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length() + x)] = encryptedTokenBuffer.array()[x];
    }
    targetDstTokenBuffer.put(tokenArray);
    targetDstTokenBuffer.flip();
    return targetDstTokenBuffer;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 28 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project FinalCrypt by ron-from-nl.

the class MySimpleFCFileVisitor method targetHasAuthenticatedFCToken.

public static synchronized // Tested
boolean targetHasAuthenticatedFCToken(// Tested
UI ui, // Tested
Path targetSourcePath, // Tested
Path cipherSourcePath) {
    boolean readTargetSourceChannelError = false;
    boolean cipherAuthenticatedTargetSource = false;
    ByteBuffer targetSrcTokenBuffer = ByteBuffer.allocate(FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length() * 2);
    targetSrcTokenBuffer.clear();
    ByteBuffer targetEncryptedTokenBuffer = ByteBuffer.allocate(FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    targetEncryptedTokenBuffer.clear();
    ByteBuffer cipherSourceBuffer = ByteBuffer.allocate(FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    cipherSourceBuffer.clear();
    ByteBuffer cipherDecryptedTokenBuffer = ByteBuffer.allocate(FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    cipherDecryptedTokenBuffer.clear();
    long readTargetSourceChannelPosition = 0;
    long readTargetSourceChannelTransfered = 0;
    long readCipherSourceChannelPosition = 0;
    long readCipherSourceChannelTransfered = 0;
    // Create Target Source Token Buffer
    try (final SeekableByteChannel readTargetSourceChannel = Files.newByteChannel(targetSourcePath, EnumSet.of(StandardOpenOption.READ))) {
        // Fill up inputFileBuffer
        // readTargetSourceChannel.position(readTargetSourceChannelPosition);
        // readTargetSourceChannelTransfered = readTargetSourceChannel.read(targetSrcTokenBuffer); targetSrcTokenBuffer.flip();
        readTargetSourceChannel.read(targetSrcTokenBuffer);
        targetSrcTokenBuffer.flip();
        readTargetSourceChannel.close();
    } catch (IOException ex) {
        readTargetSourceChannelError = true;
        ui.error("Error: targetHasAuthenticatedToken: readTargetSourceChannel " + ex.getMessage() + "\r\n");
    }
    // Encrypted Token Buffer
    targetEncryptedTokenBuffer.put(targetSrcTokenBuffer.array(), FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length(), FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN.length());
    targetEncryptedTokenBuffer.flip();
    if (!readTargetSourceChannelError) {
        try (final SeekableByteChannel readCipherSourceChannel = Files.newByteChannel(cipherSourcePath, EnumSet.of(StandardOpenOption.READ))) {
            // Fill up cipherFileBuffer
            // readCipherSourceChannel.position(readCipherSourceChannelPosition);
            // readCipherSourceChannelTransfered = readCipherSourceChannel.read(cipherSourceBuffer);
            readCipherSourceChannel.read(cipherSourceBuffer);
            cipherSourceBuffer.flip();
            readCipherSourceChannel.close();
        } catch (IOException ex) {
            ui.error("Error: cipherAuthenticatedTargetSource readCipherSourceChannel " + ex.getMessage() + "\r\n");
        }
        // Create Encrypted Token Buffer
        cipherDecryptedTokenBuffer = FinalCrypt.encryptBuffer(targetEncryptedTokenBuffer, cipherSourceBuffer);
        String cipherDecryptedTokenBufferString = new String(cipherDecryptedTokenBuffer.array(), StandardCharsets.UTF_8);
        // Authenticate Cipher Token against Target Token
        if (cipherDecryptedTokenBufferString.equals(FinalCrypt.FINALCRYPT_PLAIN_IEXT_AUTHENTICATION_TOKEN)) {
            cipherAuthenticatedTargetSource = true;
        } else {
            cipherAuthenticatedTargetSource = false;
        }
    } else {
        cipherAuthenticatedTargetSource = false;
    }
    return cipherAuthenticatedTargetSource;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 29 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project FinalCrypt by ron-from-nl.

the class DeviceController method getDeviceSize1.

// Get size of device
public static synchronized long getDeviceSize1(UI ui, Path path) {
    long deviceSize = 0;
    try (final SeekableByteChannel deviceChannel = Files.newByteChannel(path, EnumSet.of(StandardOpenOption.READ))) {
        deviceSize = deviceChannel.size();
        deviceChannel.close();
    } catch (IOException ex) {
        ui.status(ex.getMessage(), true);
    }
    return deviceSize;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException)

Example 30 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project FinalCrypt by ron-from-nl.

the class DeviceController method readPos.

public static synchronized byte[] readPos(FCPath fcPath, long pos, long length) {
    long readInputDeviceChannelTransfered = 0;
    ByteBuffer inputDeviceBuffer = ByteBuffer.allocate((int) length);
    inputDeviceBuffer.clear();
    try (final SeekableByteChannel readInputDeviceChannel = Files.newByteChannel(fcPath.path, EnumSet.of(StandardOpenOption.READ))) {
        readInputDeviceChannel.position(pos);
        readInputDeviceChannelTransfered = readInputDeviceChannel.read(inputDeviceBuffer);
        inputDeviceBuffer.flip();
        readInputDeviceChannel.close();
    // ui.log("Read Pos " + pos + " Transfered: " + readInputDeviceChannelTransfered + "\r\n");
    } catch (IOException ex) {
        ui.status("Device().read(..) " + ex.getMessage(), true);
    }
    return inputDeviceBuffer.array();
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)130 ByteBuffer (java.nio.ByteBuffer)58 Path (java.nio.file.Path)48 IOException (java.io.IOException)42 Test (org.junit.Test)33 InputStream (java.io.InputStream)14 NoSuchFileException (java.nio.file.NoSuchFileException)12 Test (org.testng.annotations.Test)9 ReadableByteChannel (java.nio.channels.ReadableByteChannel)8 OpenOption (java.nio.file.OpenOption)7 StandardOpenOption (java.nio.file.StandardOpenOption)7 HashSet (java.util.HashSet)7 File (java.io.File)6 FileSystem (java.nio.file.FileSystem)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 URI (java.net.URI)5 RandomAccessData (org.apache.beam.runners.dataflow.util.RandomAccessData)5 OutputStream (java.io.OutputStream)4 FileChannel (java.nio.channels.FileChannel)4 WritableByteChannel (java.nio.channels.WritableByteChannel)4