Search in sources :

Example 66 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project ceylon-compiler by ceylon.

the class CeylonDocToolTests method assertMatchInFile.

protected void assertMatchInFile(File destDir, String path, Pattern pattern, GrepAsserter asserter) throws Exception {
    assertFileExists(destDir, path);
    Charset charset = Charset.forName("UTF-8");
    File file = new File(destDir, path);
    FileInputStream stream = new FileInputStream(file);
    try {
        FileChannel channel = stream.getChannel();
        try {
            MappedByteBuffer map = channel.map(MapMode.READ_ONLY, 0, channel.size());
            CharBuffer chars = charset.decode(map);
            Matcher matcher = pattern.matcher(chars);
            asserter.makeAssertions(matcher);
        } finally {
            channel.close();
        }
    } finally {
        stream.close();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) Matcher(java.util.regex.Matcher) FileChannel(java.nio.channels.FileChannel) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 67 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project smartmodule by carozhu.

the class FileEnDecryptHelper method encrypt.

/**
	 * 加密
	 * 
	 * @param strFile
	 *            源文件绝对路径
	 * @return
	 */
private boolean encrypt(String strFile) {
    int len = REVERSE_LENGTH;
    try {
        File f = new File(strFile);
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        long totalLen = raf.length();
        if (totalLen < REVERSE_LENGTH)
            len = (int) totalLen;
        FileChannel channel = raf.getChannel();
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, REVERSE_LENGTH);
        byte tmp;
        for (int i = 0; i < len; ++i) {
            byte rawByte = buffer.get(i);
            tmp = (byte) (rawByte ^ i);
            buffer.put(i, tmp);
        }
        buffer.force();
        buffer.clear();
        channel.close();
        raf.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException)

Example 68 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method transferToTrustedChannel.

private long transferToTrustedChannel(long position, long count, WritableByteChannel target) throws IOException {
    boolean isSelChImpl = (target instanceof SelChImpl);
    if (!((target instanceof FileChannelImpl) || isSelChImpl))
        return IOStatus.UNSUPPORTED;
    // Trusted target: Use a mapped buffer
    long remaining = count;
    while (remaining > 0L) {
        long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
        try {
            MappedByteBuffer dbb = map(MapMode.READ_ONLY, position, size);
            try {
                // ## Bug: Closing this channel will not terminate the write
                int n = target.write(dbb);
                assert n >= 0;
                remaining -= n;
                if (isSelChImpl) {
                    // one attempt to write to selectable channel
                    break;
                }
                assert n > 0;
                position += n;
            } finally {
                unmap(dbb);
            }
        } catch (ClosedByInterruptException e) {
            // to be thrown after closing this channel.
            assert !target.isOpen();
            try {
                close();
            } catch (Throwable suppressed) {
                e.addSuppressed(suppressed);
            }
            throw e;
        } catch (IOException ioe) {
            // Only throw exception if no bytes have been written
            if (remaining == count)
                throw ioe;
            break;
        }
    }
    return count - remaining;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Example 69 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method transferFromFileChannel.

private long transferFromFileChannel(FileChannelImpl src, long position, long count) throws IOException {
    if (!src.readable)
        throw new NonReadableChannelException();
    synchronized (src.positionLock) {
        long pos = src.position();
        long max = Math.min(count, src.size() - pos);
        long remaining = max;
        long p = pos;
        while (remaining > 0L) {
            long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
            // ## Bug: Closing this channel will not terminate the write
            MappedByteBuffer bb = src.map(MapMode.READ_ONLY, p, size);
            try {
                long n = write(bb, position);
                assert n > 0;
                p += n;
                position += n;
                remaining -= n;
            } catch (IOException ioe) {
                // Only throw exception if no bytes have been written
                if (remaining == max)
                    throw ioe;
                break;
            } finally {
                unmap(bb);
            }
        }
        long nwritten = max - remaining;
        src.position(pos + nwritten);
        return nwritten;
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Example 70 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project sketches-core by DataSketches.

the class MemoryMappedFile method getInstance.

/**
   * Factory method for creating a memory mapping a file.
   *
   * <p>Memory maps a file directly in off heap leveraging native map0 method used in
   * FileChannelImpl.c. The owner will have read write access to that address space.</p>
   *
   * @param file File to be mapped
   * @param position Memory map starting from this position in the file
   * @param len Memory map at most len bytes &gt; 0 starting from {@code position}
   * @return A new MemoryMappedFile
   * @throws Exception file not found or RuntimeException, etc.
   */
@SuppressWarnings("resource")
public static MemoryMappedFile getInstance(final File file, final long position, final long len) throws Exception {
    checkPositionLen(position, len);
    final RandomAccessFile raf = new RandomAccessFile(file, "rw");
    final FileChannel fc = raf.getChannel();
    final long nativeBaseAddress = map(fc, position, len);
    final long capacityBytes = len;
    // len can be more than the file.length
    raf.setLength(len);
    final MappedByteBuffer mbb = createDummyMbbInstance(nativeBaseAddress);
    return new MemoryMappedFile(raf, mbb, nativeBaseAddress, capacityBytes);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)154 FileChannel (java.nio.channels.FileChannel)75 IOException (java.io.IOException)44 File (java.io.File)38 RandomAccessFile (java.io.RandomAccessFile)36 FileInputStream (java.io.FileInputStream)29 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)18 Path (java.nio.file.Path)11 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 Elf (com.facebook.buck.cxx.elf.Elf)8 FileOutputStream (java.io.FileOutputStream)8 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 FileNotFoundException (java.io.FileNotFoundException)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)4 Pair (com.facebook.buck.model.Pair)3 Date (java.util.Date)3 NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)2 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2