Search in sources :

Example 51 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project android_frameworks_base by ResurrectionRemix.

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 52 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project opennms by OpenNMS.

the class AbstractSystemReportPlugin method slurp.

protected String slurp(final File lsb) {
    if (lsb != null && lsb.exists()) {
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(lsb);
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            return Charset.defaultCharset().decode(bb).toString().replace("[\\r\\n]*$", "");
        } catch (final Exception e) {
            LOG.debug("Unable to read from file '{}'", lsb.getPath(), e);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }
    return null;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 53 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project tdi-studio-se by Talend.

the class FileCopy method copyFileL.

private static void copyFileL(String srcFileName, FileInputStream srcInputStream, String desFileName, boolean delSrc) throws Exception {
    File source = new File(srcFileName);
    File dest = new File(desFileName);
    FileChannel in = null, out = null;
    try {
        in = srcInputStream.getChannel();
        out = new FileOutputStream(dest).getChannel();
        long size = in.size();
        long position = 0;
        final long MAP_SIZE = 33525760;
        MappedByteBuffer buf = null;
        while (true) {
            if (position + MAP_SIZE >= size) {
                buf = in.map(FileChannel.MapMode.READ_ONLY, position, size - position);
                out.write(buf);
                //For But TDI-26493, here must clean first, or it can't delete
                clean(buf);
                break;
            } else {
                buf = in.map(FileChannel.MapMode.READ_ONLY, position, MAP_SIZE);
                out.write(buf);
                // here must clean first, or it can't delete
                clean(buf);
                position += MAP_SIZE;
            }
        }
        in.close();
        out.close();
        if (delSrc) {
            source.delete();
        }
    } 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)

Example 54 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project EventHub by Codecademy.

the class UserEventIndexModule method getBlockFactory.

@Provides
public UserEventIndex.Block.Factory getBlockFactory(@Named("eventhub.usereventindex.directory") final String directory, @Named("eventhub.usereventindex.blockCacheSize") int blockCacheSize, @Named("eventhub.usereventindex.numRecordsPerBlock") int numRecordsPerBlock, @Named("eventhub.usereventindex.numBlocksPerFile") int numBlocksPerFile) {
    final int fileSize = numBlocksPerFile * (numRecordsPerBlock * UserEventIndex.ID_SIZE + UserEventIndex.Block.MetaData.SIZE);
    LoadingCache<Integer, MappedByteBuffer> buffers = CacheBuilder.newBuilder().maximumSize(blockCacheSize).recordStats().removalListener(new RemovalListener<Integer, MappedByteBuffer>() {

        @Override
        public void onRemoval(RemovalNotification<Integer, MappedByteBuffer> notification) {
            MappedByteBuffer value = notification.getValue();
            if (value != null) {
                value.force();
            }
        }
    }).build(new CacheLoader<Integer, MappedByteBuffer>() {

        @Override
        public MappedByteBuffer load(Integer key) throws Exception {
            return ByteBufferUtil.createNewBuffer(String.format("%s/block_%d.mem", directory, key), fileSize);
        }
    });
    String filename = directory + "block_factory.ser";
    File file = new File(filename);
    if (file.exists()) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            long currentPointer = ois.readLong();
            return new UserEventIndex.Block.Factory(filename, buffers, numRecordsPerBlock, numBlocksPerFile, currentPointer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return new UserEventIndex.Block.Factory(filename, buffers, numRecordsPerBlock, numBlocksPerFile, 0);
}
Also used : IOException(java.io.IOException) RemovalListener(com.google.common.cache.RemovalListener) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MappedByteBuffer(java.nio.MappedByteBuffer) RemovalNotification(com.google.common.cache.RemovalNotification) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream) Provides(com.google.inject.Provides)

Example 55 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project EventHub by Codecademy.

the class DmaList method build.

public static <T> DmaList<T> build(final Schema<T> schema, final String directory, final int numRecordsPerFile, int cacheSize) {
    //noinspection ResultOfMethodCallIgnored
    new File(directory).mkdirs();
    try (RandomAccessFile raf = new RandomAccessFile(new File(String.format("%s/meta_data.mem", directory)), "rw")) {
        MappedByteBuffer metaDataBuffer = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 8);
        long numRecords = metaDataBuffer.getLong();
        final int fileSize = numRecordsPerFile * schema.getObjectSize();
        LoadingCache<Integer, MappedByteBuffer> buffers = CacheBuilder.newBuilder().maximumSize(cacheSize).recordStats().removalListener(new RemovalListener<Integer, MappedByteBuffer>() {

            @Override
            public void onRemoval(RemovalNotification<Integer, MappedByteBuffer> notification) {
                MappedByteBuffer value = notification.getValue();
                if (value != null) {
                    value.force();
                }
            }
        }).build(new CacheLoader<Integer, MappedByteBuffer>() {

            @Override
            public MappedByteBuffer load(Integer key) throws Exception {
                return ByteBufferUtil.createNewBuffer(String.format("%s/dma_list_%d.mem", directory, key), fileSize);
            }
        });
        return new DmaList<>(directory, schema, metaDataBuffer, buffers, numRecords, numRecordsPerFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IOException(java.io.IOException) RemovalListener(com.google.common.cache.RemovalListener) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) RemovalNotification(com.google.common.cache.RemovalNotification) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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