Search in sources :

Example 56 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project ACS by ACS-Community.

the class FileHelper method copy.

/**
	 * Copies file <code>source</code> to location <code>dest</code>.
	 * Necessary directories are created automatically.
	 * The modification time is preserved if <code>preserveTime</code> is <code>true</code>.
	 */
public static void copy(File source, File dest, boolean preserveTime) throws IOException {
    FileChannel in = null, out = null;
    try {
        dest.getParentFile().mkdirs();
        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);
        if (preserveTime) {
            dest.setLastModified(source.lastModified());
        }
    } 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) FileInputStream(java.io.FileInputStream)

Example 57 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project alluxio by Alluxio.

the class LocalFileBlockWriter method write.

/**
   * Writes data to the block from an input {@link ByteBuffer}.
   *
   * @param offset starting offset of the block file to write
   * @param inputBuf {@link ByteBuffer} that input data is stored in
   * @return the size of data that was written
   * @throws IOException if an I/O error occurs
   */
private long write(long offset, ByteBuffer inputBuf) throws IOException {
    int inputBufLength = inputBuf.limit() - inputBuf.position();
    MappedByteBuffer outputBuf = mLocalFileChannel.map(FileChannel.MapMode.READ_WRITE, offset, inputBufLength);
    outputBuf.put(inputBuf);
    int bytesWritten = outputBuf.limit();
    BufferUtils.cleanDirectBuffer(outputBuf);
    return bytesWritten;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer)

Example 58 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project mastering-java by Kingminghuang.

the class MemoryMapTest method checksumMappedFile.

public static long checksumMappedFile(Path path) throws IOException {
    try (FileChannel channel = FileChannel.open(path)) {
        CRC32 crc = new CRC32();
        int length = (int) channel.size();
        MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
        for (int i = 0; i < length; i++) {
            int c = byteBuffer.get(i);
            crc.update(c);
        }
        return crc.getValue();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) CRC32(java.util.zip.CRC32) FileChannel(java.nio.channels.FileChannel)

Example 59 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project http-kit by http-kit.

the class NBlockingSSL method doHandshake.

private static void doHandshake() throws IOException {
    SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
    isHandshakeDone = hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING || hs == SSLEngineResult.HandshakeStatus.FINISHED;
    loop: while (!isHandshakeDone) {
        switch(hs) {
            case NEED_TASK:
                Runnable runnable;
                while ((runnable = engine.getDelegatedTask()) != null) {
                    logger.info("get task " + runnable);
                    runnable.run();
                }
                break;
            case NEED_UNWRAP:
                int read = socketChannel.read(peerNetData);
                logger.info("read {} bytes", read);
                if (read < 0) {
                    logger.info("closed");
                // TODO closed
                } else {
                    peerNetData.flip();
                    SSLEngineResult res = engine.unwrap(peerNetData, peerAppData);
                    logger.info("hs unwrap, " + res);
                    if (res.getStatus() != Status.OK) {
                        System.out.println("--------------------------");
                    }
                    peerNetData.compact();
                    switch(res.getStatus()) {
                        case OK:
                            break;
                        case BUFFER_UNDERFLOW:
                            // need more data from peer
                            logger.info("waiting for more info");
                            break loop;
                        case BUFFER_OVERFLOW:
                            // need larger peerAppData buffer
                            break;
                        case CLOSED:
                            break;
                    }
                    if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_WRAP) {
                        key.interestOps(SelectionKey.OP_WRITE);
                        logger.info("for write");
                        break loop;
                    }
                }
                break;
            case NEED_WRAP:
                // myNetData.compact();
                SSLEngineResult result = engine.wrap(ByteBuffer.allocate(0), myNetData);
                logger.info("wrap: " + result);
                myNetData.flip();
                socketChannel.write(myNetData);
                if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
                }
                if (!myNetData.hasRemaining()) {
                    // write done, so just for read
                    if ((key.interestOps() & SelectionKey.OP_READ) == 0) {
                        key.interestOps(SelectionKey.OP_READ);
                        logger.info("for read");
                    }
                    myNetData.clear();
                // break loop;
                } else {
                    myNetData.compact();
                }
                break;
        }
        hs = engine.getHandshakeStatus();
        isHandshakeDone = hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING || hs == SSLEngineResult.HandshakeStatus.FINISHED;
        if (isHandshakeDone) {
            logger.info("handshake done");
            peerNetData.clear();
            ByteBuffer buffer = ByteBuffer.wrap(("GET / HTTP/1.1\r\nHost: " + HOST + "\r\n\r\n").getBytes());
            SSLEngineResult res = engine.wrap(buffer, myNetData);
            RandomAccessFile r = new RandomAccessFile("/home/feng/workspace/http-kit/blog.access.log", "r");
            MappedByteBuffer b = r.getChannel().map(MapMode.READ_ONLY, 0, r.getChannel().size());
            ByteBuffer bf = ByteBuffer.allocate(256 * 1024);
            // even though b is big, bf is small, the two buffer just move
            // forward
            SSLEngineResult t = engine.wrap(b, bf);
            System.out.println(t);
            if (res.getStatus() == SSLEngineResult.Status.OK) {
                myNetData.flip();
                socketChannel.write(myNetData);
                if (myNetData.hasRemaining()) {
                    key.interestOps(SelectionKey.OP_WRITE);
                }
            }
        }
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 60 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project intellij-community by JetBrains.

the class MappedBufferWrapper method unmap.

@Override
public final void unmap() {
    long started = IOStatistics.DEBUG ? System.currentTimeMillis() : 0;
    MappedByteBuffer buffer = myBuffer;
    myBuffer = null;
    if (!clean(buffer, isDirty())) {
        LOG.error("Unmapping failed for: " + myFile);
    }
    if (IOStatistics.DEBUG) {
        long finished = System.currentTimeMillis();
        if (finished - started > IOStatistics.MIN_IO_TIME_TO_REPORT) {
            IOStatistics.dump("Unmapped " + myFile + "," + myPosition + "," + myLength + " for " + (finished - started));
        }
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer)

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