Search in sources :

Example 81 with FileChannel

use of java.nio.channels.FileChannel in project gephi by gephi.

the class SimpleHTMLReport method copy.

public void copy(File source, File dest) throws IOException {
    FileChannel in = null, out = null;
    try {
        if (dest.isDirectory()) {
            dest = new File(dest, source.getName());
        }
        in = new FileInputStream(source).getChannel();
        out = new FileOutputStream(dest).getChannel();
        long size = in.size();
        MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
        out.write(buf);
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 82 with FileChannel

use of java.nio.channels.FileChannel in project xUtils3 by wyouflf.

the class MD5 method md5.

public static String md5(File file) throws IOException {
    MessageDigest messagedigest = null;
    FileInputStream in = null;
    FileChannel ch = null;
    byte[] encodeBytes = null;
    try {
        messagedigest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        messagedigest.update(byteBuffer);
        encodeBytes = messagedigest.digest();
    } catch (NoSuchAlgorithmException neverHappened) {
        throw new RuntimeException(neverHappened);
    } finally {
        IOUtil.closeQuietly(in);
        IOUtil.closeQuietly(ch);
    }
    return toHexString(encodeBytes);
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 83 with FileChannel

use of java.nio.channels.FileChannel in project hazelcast by hazelcast.

the class NearCachePreloaderLockTest method setUp.

@Before
public void setUp() throws Exception {
    preloaderLock = new NearCachePreloaderLock(logger, preloaderLockFile.getAbsolutePath());
    FileChannel realChannel = new RandomAccessFile(lockFile, "rw").getChannel();
    channel = spy(realChannel);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) Before(org.junit.Before)

Example 84 with FileChannel

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

the class SegmentReaderTest method underlyingEncryptedSegmenterTest.

public void underlyingEncryptedSegmenterTest(BiFunction<FileDataInput, Integer, ByteBuffer> readFun) throws IOException {
    EncryptionContext context = EncryptionContextGenerator.createContext(true);
    CipherFactory cipherFactory = new CipherFactory(context.getTransparentDataEncryptionOptions());
    int plainTextLength = (1 << 13) - 137;
    ByteBuffer plainTextBuffer = ByteBuffer.allocate(plainTextLength);
    random.nextBytes(plainTextBuffer.array());
    ByteBuffer compressedBuffer = EncryptionUtils.compress(plainTextBuffer, null, true, context.getCompressor());
    Cipher cipher = cipherFactory.getEncryptor(context.getTransparentDataEncryptionOptions().cipher, context.getTransparentDataEncryptionOptions().key_alias);
    File encryptedFile = File.createTempFile("encrypted-segment-", ".log");
    encryptedFile.deleteOnExit();
    FileChannel channel = new RandomAccessFile(encryptedFile, "rw").getChannel();
    channel.write(ByteBufferUtil.bytes(plainTextLength));
    EncryptionUtils.encryptAndWrite(compressedBuffer, channel, true, cipher);
    channel.close();
    try (RandomAccessReader reader = RandomAccessReader.open(encryptedFile)) {
        context = EncryptionContextGenerator.createContext(cipher.getIV(), true);
        EncryptedSegmenter segmenter = new EncryptedSegmenter(reader, context);
        SyncSegment syncSegment = segmenter.nextSegment(0, (int) reader.length());
        // EncryptedSegmenter includes the Sync header length in the syncSegment.endPosition (value)
        Assert.assertEquals(plainTextLength, syncSegment.endPosition - CommitLogSegment.SYNC_MARKER_SIZE);
        ByteBuffer fileBuffer = readFun.apply(syncSegment.input, plainTextLength);
        plainTextBuffer.position(0);
        Assert.assertEquals(plainTextBuffer, fileBuffer);
    }
}
Also used : EncryptionContext(org.apache.cassandra.security.EncryptionContext) RandomAccessFile(java.io.RandomAccessFile) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) FileChannel(java.nio.channels.FileChannel) EncryptedSegmenter(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.EncryptedSegmenter) CipherFactory(org.apache.cassandra.security.CipherFactory) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SyncSegment(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment)

Example 85 with FileChannel

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

the class CompressorTest method testMappedFile.

public void testMappedFile() throws IOException {
    byte[] data = new byte[1 << 20];
    new Random().nextBytes(data);
    ByteBuffer src = makeBB(data.length);
    src.put(data);
    src.flip();
    // create a temp file
    File temp = File.createTempFile("tempfile", ".tmp");
    temp.deleteOnExit();
    // Prepend some random bytes to the output and compress
    final int outOffset = 3;
    byte[] garbage = new byte[outOffset + compressor.initialCompressedBufferLength(data.length)];
    new Random().nextBytes(garbage);
    ByteBuffer dest = makeBB(outOffset + compressor.initialCompressedBufferLength(data.length));
    dest.put(garbage);
    dest.clear();
    dest.position(outOffset);
    compressor.compress(src, dest);
    int compressedLength = dest.position() - outOffset;
    FileChannel channel = FileChannel.open(temp.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
    dest.clear();
    channel.write(dest);
    MappedByteBuffer mappedData = Files.map(temp);
    ByteBuffer result = makeBB(data.length + 100);
    mappedData.position(outOffset).limit(outOffset + compressedLength);
    compressor.uncompress(mappedData, result);
    channel.close();
    result.flip();
    Assert.assertEquals(data.length, result.limit());
    for (int i = 0; i < result.limit(); i++) {
        Assert.assertEquals("Decompression mismatch at byte " + i, data[i], result.get());
    }
}
Also used : Random(java.util.Random) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

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