Search in sources :

Example 76 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project linuxtools by eclipse.

the class SystemTapView method saveData.

/**
 * Implement this method to save data in whichever format your program
 * needs. Keep in mind that the filePath variable should contain the
 * filePath of the most recently opened file.
 *
 * @param sourcePath
 */
public void saveData(String targetFile) {
    try {
        File file = new File(targetFile);
        file.delete();
        file.createNewFile();
        File sFile = new File(sourcePath);
        if (!sFile.exists()) {
            return;
        }
        try (FileInputStream fileIn = new FileInputStream(sFile);
            FileOutputStream fileOut = new FileOutputStream(file);
            FileChannel channelIn = fileIn.getChannel();
            FileChannel channelOut = fileOut.getChannel()) {
            if (channelIn == null || channelOut == null) {
                return;
            }
            long size = channelIn.size();
            MappedByteBuffer buf = channelIn.map(FileChannel.MapMode.READ_ONLY, 0, size);
            channelOut.write(buf);
        }
    } catch (IOException e) {
        CallgraphCorePlugin.logException(e);
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 77 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project ignite by apache.

the class FileWriteAheadLogManager method restoreWriteHandle.

/**
 * @param lastReadPtr Last read WAL file pointer.
 * @return Initialized file write handle.
 * @throws IgniteCheckedException If failed to initialize WAL write handle.
 */
private FileWriteHandle restoreWriteHandle(FileWALPointer lastReadPtr) throws IgniteCheckedException {
    long absIdx = lastReadPtr == null ? 0 : lastReadPtr.index();
    @Nullable FileArchiver archiver0 = archiver;
    long segNo = archiver0 == null ? absIdx : absIdx % dsCfg.getWalSegments();
    File curFile = new File(walWorkDir, FileDescriptor.fileName(segNo));
    int off = lastReadPtr == null ? 0 : lastReadPtr.fileOffset();
    int len = lastReadPtr == null ? 0 : lastReadPtr.length();
    try {
        FileIO fileIO = ioFactory.create(curFile);
        IgniteInClosure<FileIO> lsnr = createWalFileListener;
        if (lsnr != null)
            lsnr.apply(fileIO);
        try {
            int serVer = serializerVer;
            // If we have existing segment, try to read version from it.
            if (lastReadPtr != null) {
                try {
                    serVer = readSerializerVersionAndCompactedFlag(fileIO).get1();
                } catch (SegmentEofException | EOFException ignore) {
                    serVer = serializerVer;
                }
            }
            RecordSerializer ser = new RecordSerializerFactoryImpl(cctx).createSerializer(serVer);
            if (log.isInfoEnabled())
                log.info("Resuming logging to WAL segment [file=" + curFile.getAbsolutePath() + ", offset=" + off + ", ver=" + serVer + ']');
            SegmentedRingByteBuffer rbuf;
            if (mmap) {
                try {
                    MappedByteBuffer buf = fileIO.map((int) maxWalSegmentSize);
                    rbuf = new SegmentedRingByteBuffer(buf, metrics);
                } catch (IOException e) {
                    throw new IgniteCheckedException(e);
                }
            } else
                rbuf = new SegmentedRingByteBuffer(dsCfg.getWalBufferSize(), maxWalSegmentSize, DIRECT, metrics);
            if (lastReadPtr != null)
                rbuf.init(lastReadPtr.fileOffset() + lastReadPtr.length());
            FileWriteHandle hnd = new FileWriteHandle(fileIO, absIdx, off + len, true, ser, rbuf);
            if (archiver0 != null)
                archiver0.currentWalIndex(absIdx);
            else
                archivedMonitor.setLastArchivedAbsoluteIndex(absIdx - 1);
            return hnd;
        } catch (IgniteCheckedException | IOException e) {
            fileIO.close();
            throw e;
        }
    } catch (IOException e) {
        throw new IgniteCheckedException("Failed to restore WAL write handle: " + curFile.getAbsolutePath(), e);
    }
}
Also used : IOException(java.io.IOException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) MappedByteBuffer(java.nio.MappedByteBuffer) RecordSerializerFactoryImpl(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializerFactoryImpl) EOFException(java.io.EOFException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) RecordSerializer(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializer)

Example 78 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project ignite by apache.

the class FileWriteAheadLogManager method initNextWriteHandle.

/**
 * Fills the file header for a new segment. Calling this method signals we are done with the segment and it can be
 * archived. If we don't have prepared file yet and achiever is busy this method blocks
 *
 * @param cur Current file write handle released by WAL writer
 * @return Initialized file handle.
 * @throws StorageException If IO exception occurred.
 * @throws IgniteCheckedException If failed.
 */
private FileWriteHandle initNextWriteHandle(FileWriteHandle cur) throws StorageException, IgniteCheckedException {
    try {
        File nextFile = pollNextFile(cur.idx);
        if (log.isDebugEnabled())
            log.debug("Switching to a new WAL segment: " + nextFile.getAbsolutePath());
        SegmentedRingByteBuffer rbuf = null;
        FileIO fileIO = null;
        FileWriteHandle hnd;
        boolean interrupted = this.interrupted.get();
        while (true) {
            try {
                fileIO = ioFactory.create(nextFile);
                IgniteInClosure<FileIO> lsnr = createWalFileListener;
                if (lsnr != null)
                    lsnr.apply(fileIO);
                if (mmap) {
                    MappedByteBuffer buf = fileIO.map((int) maxWalSegmentSize);
                    rbuf = new SegmentedRingByteBuffer(buf, metrics);
                } else
                    rbuf = cur.buf.reset();
                hnd = new FileWriteHandle(fileIO, cur.idx + 1, 0, false, serializer, rbuf);
                if (interrupted)
                    Thread.currentThread().interrupt();
                break;
            } catch (ClosedByInterruptException ignore) {
                interrupted = true;
                Thread.interrupted();
                if (fileIO != null) {
                    try {
                        fileIO.close();
                    } catch (IOException ignored) {
                    // No-op.
                    }
                    fileIO = null;
                }
                if (rbuf != null) {
                    rbuf.free();
                    rbuf = null;
                }
            } finally {
                this.interrupted.set(false);
            }
        }
        return hnd;
    } catch (IOException e) {
        StorageException se = new StorageException("Unable to initialize WAL segment", e);
        NodeInvalidator.INSTANCE.invalidate(cctx.kernalContext(), se);
        throw se;
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException) File(java.io.File) StorageException(org.apache.ignite.internal.pagemem.wal.StorageException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO)

Example 79 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project run-wallet-android by runplay.

the class SecureDeleteFile method delete.

/**
 * Securely delete a file.
 *
 * Currently, there is only 1 pass that overwrites the file first
 * with a random bit stream generated by Trivium.
 *
 * @param file
 * @return true if this File was deleted, false otherwise.
 */
public static boolean delete(File file) {
    // Log.e("FILEDEL", file.getAbsolutePath());
    if (file.exists()) {
        SecureRandom random = new SecureRandom();
        Trivium tri = new Trivium();
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            FileChannel channel = raf.getChannel();
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
            byte[] key = new byte[10];
            byte[] nonce = new byte[10];
            random.nextBytes(key);
            random.nextBytes(nonce);
            tri.setupKey(Trivium.MODE_DECRYPT, key, 0);
            tri.setupNonce(nonce, 0);
            int buffersize = 1024;
            byte[] bytes = new byte[1024];
            // overwrite with random numbers
            while (buffer.hasRemaining()) {
                int max = buffer.limit() - buffer.position();
                if (max > buffersize)
                    max = buffersize;
                // random.nextBytes(bytes);
                tri.process(bytes, 0, bytes, 0, max);
                buffer.put(bytes, 0, max);
            }
            buffer.force();
            buffer.rewind();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "FileNotFoundException", e);
        } catch (IOException e) {
            Log.d(TAG, "IOException", e);
        } catch (ESJException e) {
            Log.d(TAG, "ESJException", e);
        }
        // return true;
        return file.delete();
    }
    return false;
}
Also used : Trivium(run.wallet.common.delete.ciphers.Trivium) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException)

Example 80 with MappedByteBuffer

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

the class Type1Font method getBuffer.

private synchronized ByteBuffer getBuffer() throws FontFormatException {
    MappedByteBuffer mapBuf = (MappedByteBuffer) bufferRef.get();
    if (mapBuf == null) {
        //System.out.println("open T1 " + platName);
        try {
            RandomAccessFile raf = (RandomAccessFile) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

                public Object run() {
                    try {
                        return new RandomAccessFile(platName, "r");
                    } catch (FileNotFoundException ffne) {
                    }
                    return null;
                }
            });
            FileChannel fc = raf.getChannel();
            fileSize = (int) fc.size();
            mapBuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
            mapBuf.position(0);
            bufferRef = new WeakReference(mapBuf);
            fc.close();
        } catch (NullPointerException e) {
            throw new FontFormatException(e.toString());
        } catch (ClosedChannelException e) {
            /* NIO I/O is interruptible, recurse to retry operation.
                 * Clear interrupts before recursing in case NIO didn't.
                 */
            Thread.interrupted();
            return getBuffer();
        } catch (IOException e) {
            throw new FontFormatException(e.toString());
        }
    }
    return mapBuf;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MappedByteBuffer(java.nio.MappedByteBuffer) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) WeakReference(java.lang.ref.WeakReference) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException)

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