Search in sources :

Example 81 with MappedByteBuffer

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

the class NewProjectToolTests method assertMatchInFile.

private void assertMatchInFile(File file, Pattern pattern) throws Exception {
    Charset charset = Charset.forName("UTF-8");
    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);
            Assert.assertTrue(pattern + " not found in " + file, matcher.find());
        } 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) FileInputStream(java.io.FileInputStream)

Example 82 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project FastDev4Android by jiangqqlmj.

the class StrUtils method calcMd5.

/**
     * @param file
     *            文件
     * @return 文件转换MD5
     */
public static String calcMd5(File file) {
    FileInputStream in = null;
    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        in = new FileInputStream(file);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer;
        byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        algorithm.update(byteBuffer);
        return toHexString(algorithm.digest(), "");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 83 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project jvm-tools by aragozin.

the class HprofLongMappedByteBuffer method get.

synchronized void get(long position, byte[] chars) {
    MappedByteBuffer buffer = dumpBuffer[getBufferIndex(position)];
    buffer.position(getBufferOffset(position));
    buffer.get(chars);
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer)

Example 84 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project Japid by branaway.

the class CompilerTests method readFile.

private static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.forName("UTF-8").decode(bb).toString();
    } finally {
        stream.close();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 85 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project druid by druid-io.

the class StringDimensionMergerV9 method writeIndexes.

@Override
public void writeIndexes(List<IntBuffer> segmentRowNumConversions, Closer closer) throws IOException {
    long dimStartTime = System.currentTimeMillis();
    final BitmapSerdeFactory bitmapSerdeFactory = indexSpec.getBitmapSerdeFactory();
    String bmpFilename = String.format("%s.inverted", dimensionName);
    bitmapWriter = new GenericIndexedWriter<>(ioPeon, bmpFilename, bitmapSerdeFactory.getObjectStrategy());
    bitmapWriter.open();
    // write dim values to one single file because we need to read it
    File dimValueFile = IndexIO.makeDimFile(outDir, dimensionName);
    try (FileOutputStream fos = new FileOutputStream(dimValueFile)) {
        ByteStreams.copy(dictionaryWriter.combineStreams(), fos);
    }
    final MappedByteBuffer dimValsMapped = Files.map(dimValueFile);
    try (Closeable toCloseEncodedValueWriter = encodedValueWriter;
        Closeable toCloseBitmapWriter = bitmapWriter;
        Closeable dimValsMappedUnmapper = new Closeable() {

            @Override
            public void close() {
                ByteBufferUtils.unmap(dimValsMapped);
            }
        }) {
        Indexed<String> dimVals = GenericIndexed.read(dimValsMapped, GenericIndexed.STRING_STRATEGY);
        BitmapFactory bmpFactory = bitmapSerdeFactory.getBitmapFactory();
        RTree tree = null;
        boolean hasSpatial = capabilities.hasSpatialIndexes();
        if (hasSpatial) {
            spatialWriter = new ByteBufferWriter<>(ioPeon, String.format("%s.spatial", dimensionName), new IndexedRTree.ImmutableRTreeObjectStrategy(bmpFactory));
            spatialWriter.open();
            tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bmpFactory), bmpFactory);
        }
        IndexSeeker[] dictIdSeeker = toIndexSeekers(adapters, dimConversions, dimensionName);
        //Iterate all dim values's dictionary id in ascending order which in line with dim values's compare result.
        for (int dictId = 0; dictId < dimVals.size(); dictId++) {
            progress.progress();
            mergeBitmaps(segmentRowNumConversions, dimVals, bmpFactory, tree, hasSpatial, dictIdSeeker, dictId, adapters, dimensionName, nullRowsBitmap, bitmapWriter);
        }
        if (hasSpatial) {
            spatialWriter.write(ImmutableRTree.newImmutableFromMutable(tree));
            spatialWriter.close();
        }
        log.info("Completed dim[%s] inverted with cardinality[%,d] in %,d millis.", dimensionName, dimVals.size(), System.currentTimeMillis() - dimStartTime);
    }
}
Also used : Closeable(java.io.Closeable) LinearGutmanSplitStrategy(io.druid.collections.spatial.split.LinearGutmanSplitStrategy) MappedByteBuffer(java.nio.MappedByteBuffer) FileOutputStream(java.io.FileOutputStream) BitmapFactory(io.druid.collections.bitmap.BitmapFactory) ImmutableRTree(io.druid.collections.spatial.ImmutableRTree) IndexedRTree(io.druid.segment.data.IndexedRTree) RTree(io.druid.collections.spatial.RTree) File(java.io.File) BitmapSerdeFactory(io.druid.segment.data.BitmapSerdeFactory)

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