Example usage for java.nio.channels FileChannel map

List of usage examples for java.nio.channels FileChannel map

Introduction

In this page you can find the example usage for java.nio.channels FileChannel map.

Prototype

public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;

Source Link

Document

Maps a region of this channel's file directly into memory.

Usage

From source file:org.mitre.ccv.mapred.GenerateFeatureVectors.java

/**
 * Returns a read only {@link MappedByteBuffer}.
 * /* www  .  java2 s  .  co m*/
 * @param input full <b>local</b>  path 
 * @throws java.io.IOException
 */
public static MappedByteBuffer getMappedByteBuffer(String input) throws IOException {
    FileInputStream fins = new FileInputStream(input);
    FileChannel channel = fins.getChannel();
    return channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
}

From source file:org.ednovo.gooru.application.util.GooruImageUtil.java

public static PDFFile getPDFFile(String pdfPath) {
    ByteBuffer buf;/*from  w ww.j a v a2 s.co m*/
    PDFFile pdfFile = null;
    try {
        File file = new File(pdfPath);
        @SuppressWarnings("resource")
        RandomAccessFile accessFile = new RandomAccessFile(file, "r");
        FileChannel channel = accessFile.getChannel();
        buf = channel.map(MapMode.READ_ONLY, 0, channel.size());
        pdfFile = new PDFFile(buf);
    } catch (Exception e) {
        LOGGER.error("getPDFFile: " + e);
    }
    return pdfFile;

}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.MappableBlock.java

/**
 * Load the block./*from  ww  w  .  j a v a2  s  .com*/
 *
 * mmap and mlock the block, and then verify its checksum.
 *
 * @param length         The current length of the block.
 * @param blockIn        The block input stream.  Should be positioned at the
 *                       start.  The caller must close this.
 * @param metaIn         The meta file input stream.  Should be positioned at
 *                       the start.  The caller must close this.
 * @param blockFileName  The block file name, for logging purposes.
 *
 * @return               The Mappable block.
 */
public static MappableBlock load(long length, FileInputStream blockIn, FileInputStream metaIn,
        String blockFileName) throws IOException {
    MappableBlock mappableBlock = null;
    MappedByteBuffer mmap = null;
    FileChannel blockChannel = null;
    try {
        blockChannel = blockIn.getChannel();
        if (blockChannel == null) {
            throw new IOException("Block InputStream has no FileChannel.");
        }
        mmap = blockChannel.map(MapMode.READ_ONLY, 0, length);
        NativeIO.POSIX.getCacheManipulator().mlock(blockFileName, mmap, length);
        verifyChecksum(length, metaIn, blockChannel, blockFileName);
        mappableBlock = new MappableBlock(mmap, length);
    } finally {
        IOUtils.closeQuietly(blockChannel);
        if (mappableBlock == null) {
            if (mmap != null) {
                NativeIO.POSIX.munmap(mmap); // unmapping also unlocks
            }
        }
    }
    return mappableBlock;
}

From source file:yui.classes.utils.IOUtils.java

/**
 *
 * good for Large Files >2Mb//from  w w w  .  ja v a 2  s .com
 * @param filename
 * @return
 */
private static byte[] largeFileReader(String filename) {
    byte[] bytes = null;
    FileChannel fc = null;
    try {
        fc = new FileInputStream(filename).getChannel();

        MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        int size = byteBuffer.capacity();
        if (size > 0) {
            // Retrieve all bytes in the buffer
            byteBuffer.clear();
            bytes = new byte[size];
            byteBuffer.get(bytes, 0, bytes.length);
        }
        fc.close();
    } catch (FileNotFoundException fnf) {
        System.err.println("" + fnf);
    } catch (IOException io) {
        System.err.println("" + io);
    } finally {
        if (fc != null) {
            try {
                fc.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
    }
    return bytes;
}

From source file:com.rvl.android.getnzb.LocalNZB.java

public static String readFile(File file) throws IOException {
    Log.d(Tags.LOG, "readfile(): Converting file to string. (" + file.getAbsolutePath() + ")");
    FileInputStream stream = new FileInputStream(file);
    try {//from  ww w . ja  v  a2  s.com
        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.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:com.aliyun.oss.integrationtests.TestUtils.java

public static String genFixedLengthFile(long fixedLength) throws IOException {
    ensureDirExist(TestBase.UPLOAD_DIR);
    String filePath = TestBase.UPLOAD_DIR + System.currentTimeMillis();
    RandomAccessFile raf = new RandomAccessFile(filePath, "rw");
    FileChannel fc = raf.getChannel();

    MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, fixedLength);
    try {/*from   w  w w  . j av a  2  s  .  c om*/
        for (int i = 0; i < fixedLength; i++) {
            mbb.put(pickupAlphabet());
        }
        return filePath;
    } finally {
        if (fc != null) {
            fc.close();
        }

        if (raf != null) {
            raf.close();
        }
    }
}

From source file:com.diffplug.gradle.FileMisc.java

/** Concats the first files and writes them to the last file. */
public static void concat(Iterable<File> toMerge, File dst) throws IOException {
    try (FileChannel dstChannel = FileChannel.open(dst.toPath(), StandardOpenOption.CREATE,
            StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
        for (File file : toMerge) {
            try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
                FileChannel channel = raf.getChannel();
                dstChannel.write(channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length()));
            }//from w w w .java 2 s.c  o  m
        }
    }
}

From source file:de.wikilab.android.friendica01.Max.java

public static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {//from  w w  w .  j a v a 2s. c o m
        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.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:org.codehaus.preon.Codecs.java

public static <T> T decode(Codec<T> codec, Builder builder, File file)
        throws FileNotFoundException, IOException, DecodingException {
    FileInputStream in = null;/*from   w w w  .  j  ava  2  s  .c  om*/
    FileChannel channel = null;
    try {
        in = new FileInputStream(file);
        channel = in.getChannel();
        int fileSize = (int) channel.size();
        ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
        return decode(codec, buffer, builder);
    } finally {
        if (channel != null) {
            channel.close();
        }
    }
}

From source file:org.apache.pig.pigunit.PigTest.java

private static String readFile(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);
    try {/*from   w  w w . java2  s .  c o m*/
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}