Search in sources :

Example 86 with FileChannel

use of java.nio.channels.FileChannel in project platform_frameworks_base by android.

the class Hyphenator method loadHyphenator.

private static Hyphenator loadHyphenator(String languageTag) {
    String patternFilename = "hyph-" + languageTag.toLowerCase(Locale.US) + ".hyb";
    File patternFile = new File(getSystemHyphenatorLocation(), patternFilename);
    try {
        RandomAccessFile f = new RandomAccessFile(patternFile, "r");
        try {
            FileChannel fc = f.getChannel();
            MappedByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            long nativePtr = StaticLayout.nLoadHyphenator(buf, 0);
            return new Hyphenator(nativePtr, buf);
        } finally {
            f.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "error loading hyphenation " + patternFile, e);
        return null;
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 87 with FileChannel

use of java.nio.channels.FileChannel in project cassandra by apache.

the class EncryptionUtilsTest method fullRoundTrip.

@Test
public void fullRoundTrip() throws IOException, BadPaddingException, ShortBufferException, IllegalBlockSizeException {
    // compress
    byte[] buf = new byte[(1 << 12) - 7];
    random.nextBytes(buf);
    ByteBuffer compressedBuffer = EncryptionUtils.compress(ByteBuffer.wrap(buf), ByteBuffer.allocate(0), true, compressor);
    // encrypt
    CipherFactory cipherFactory = new CipherFactory(tdeOptions);
    Cipher encryptor = cipherFactory.getEncryptor(tdeOptions.cipher, tdeOptions.key_alias);
    File f = File.createTempFile("commitlog-enc-utils-", ".tmp");
    f.deleteOnExit();
    FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
    EncryptionUtils.encryptAndWrite(compressedBuffer, channel, true, encryptor);
    // decrypt
    Cipher decryptor = cipherFactory.getDecryptor(tdeOptions.cipher, tdeOptions.key_alias, encryptor.getIV());
    ByteBuffer decryptedBuffer = EncryptionUtils.decrypt(RandomAccessReader.open(f), ByteBuffer.allocate(0), true, decryptor);
    // uncompress
    ByteBuffer uncompressedBuffer = EncryptionUtils.uncompress(decryptedBuffer, ByteBuffer.allocate(0), true, compressor);
    Assert.assertArrayEquals(buf, uncompressedBuffer.array());
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 88 with FileChannel

use of java.nio.channels.FileChannel in project cassandra by apache.

the class EncryptionUtilsTest method encrypt.

@Test
public void encrypt() throws BadPaddingException, ShortBufferException, IllegalBlockSizeException, IOException {
    byte[] buf = new byte[(1 << 12) - 7];
    random.nextBytes(buf);
    // encrypt
    CipherFactory cipherFactory = new CipherFactory(tdeOptions);
    Cipher encryptor = cipherFactory.getEncryptor(tdeOptions.cipher, tdeOptions.key_alias);
    File f = File.createTempFile("commitlog-enc-utils-", ".tmp");
    f.deleteOnExit();
    FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
    EncryptionUtils.encryptAndWrite(ByteBuffer.wrap(buf), channel, true, encryptor);
    channel.close();
    // decrypt
    Cipher decryptor = cipherFactory.getDecryptor(tdeOptions.cipher, tdeOptions.key_alias, encryptor.getIV());
    ByteBuffer decryptedBuffer = EncryptionUtils.decrypt(RandomAccessReader.open(f), ByteBuffer.allocate(0), true, decryptor);
    // normally, we'd just call BB.array(), but that gives you the *entire* backing array, not with any of the offsets (position,limit) applied.
    // thus, just for this test, we copy the array and perform an array-level comparison with those offsets
    decryptedBuffer.limit(buf.length);
    byte[] b = new byte[buf.length];
    System.arraycopy(decryptedBuffer.array(), 0, b, 0, buf.length);
    Assert.assertArrayEquals(buf, b);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) Cipher(javax.crypto.Cipher) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 89 with FileChannel

use of java.nio.channels.FileChannel in project cordova-android by apache.

the class CordovaResourceApi method copyResource.

// Copies the input to the output in the most efficient manner possible.
// Closes both streams.
public void copyResource(OpenForReadResult input, OutputStream outputStream) throws IOException {
    assertBackgroundThread();
    try {
        InputStream inputStream = input.inputStream;
        if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
            FileChannel inChannel = ((FileInputStream) input.inputStream).getChannel();
            FileChannel outChannel = ((FileOutputStream) outputStream).getChannel();
            long offset = 0;
            long length = input.length;
            if (input.assetFd != null) {
                offset = input.assetFd.getStartOffset();
            }
            // transferFrom()'s 2nd arg is a relative position. Need to set the absolute
            // position first.
            inChannel.position(offset);
            outChannel.transferFrom(inChannel, 0, length);
        } else {
            final int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];
            for (; ; ) {
                int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE);
                if (bytesRead <= 0) {
                    break;
                }
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    } finally {
        input.inputStream.close();
        if (outputStream != null) {
            outputStream.close();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Example 90 with FileChannel

use of java.nio.channels.FileChannel in project cassandra by apache.

the class HintsWriter method create.

// HintsWriter owns channel
@SuppressWarnings("resource")
static HintsWriter create(File directory, HintsDescriptor descriptor) throws IOException {
    File file = new File(directory, descriptor.fileName());
    FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
    int fd = CLibrary.getfd(channel);
    CRC32 crc = new CRC32();
    try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
        // write the descriptor
        descriptor.serialize(dob);
        ByteBuffer descriptorBytes = dob.buffer();
        updateChecksum(crc, descriptorBytes);
        channel.write(descriptorBytes);
    } catch (Throwable e) {
        channel.close();
        throw e;
    }
    if (descriptor.isEncrypted())
        return new EncryptedHintsWriter(directory, descriptor, file, channel, fd, crc);
    if (descriptor.isCompressed())
        return new CompressedHintsWriter(directory, descriptor, file, channel, fd, crc);
    return new HintsWriter(directory, descriptor, file, channel, fd, crc);
}
Also used : CRC32(java.util.zip.CRC32) FileChannel(java.nio.channels.FileChannel) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Aggregations

FileChannel (java.nio.channels.FileChannel)676 IOException (java.io.IOException)247 ByteBuffer (java.nio.ByteBuffer)215 File (java.io.File)199 FileInputStream (java.io.FileInputStream)177 FileOutputStream (java.io.FileOutputStream)167 RandomAccessFile (java.io.RandomAccessFile)151 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)82 Path (java.nio.file.Path)42 FileLock (java.nio.channels.FileLock)34 FileNotFoundException (java.io.FileNotFoundException)32 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 OutputStream (java.io.OutputStream)12 ReadableByteChannel (java.nio.channels.ReadableByteChannel)12 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9