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:com.highcharts.export.util.SVGCreator.java

private 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.forName("utf-8").decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:com.linkedin.pinot.core.startree.MmapLinkedListStarTreeTable.java

/** Rolls to next buffer if necessary */
private void checkBuffer() {
    if (backingBuffer == null || backingBuffer.position() == backingBuffer.limit()) {
        try {/*from w  ww.  j  a  v a2  s . c o  m*/
            if (!backingBufferDir.exists()) {
                FileUtils.forceMkdir(backingBufferDir);
            }
            File backingBufferFile = new File(backingBufferDir, String.valueOf(fileCount));
            FileChannel backingFileChannel = new RandomAccessFile(backingBufferFile, "rw").getChannel();
            backingBuffer = backingFileChannel.map(FileChannel.MapMode.READ_WRITE, 0,
                    documentIncrement * rowSize);
            fileCount++;
            documentCount = 0;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

From source file:org.lenskit.data.packed.BinaryRatingDAOTest.java

@Test
public void testBufferDAO() throws IOException {
    File file = folder.newFile("ratings.bin");
    BinaryRatingPacker packer = BinaryRatingPacker.open(file);
    try {/*from   w  ww. j  a v a  2  s.  c  o  m*/
        packer.writeRatings(ratings);
    } finally {
        packer.close();
    }

    ByteBuffer buffer;
    try (FileInputStream istr = new FileInputStream(file)) {
        FileChannel chan = istr.getChannel();
        buffer = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
    }

    BinaryRatingDAO dao = BinaryRatingDAO.fromBuffer(buffer);
    verifySimpleDAO(dao);
}

From source file:org.kevinferrare.solarSystemDataRetriever.jplhorizons.webfetcher.JplHorizonRawDataRetriever.java

/**
 * Loads data from the folder that was set using setFolderName.<br />
 * The loaded keys are the file names and the associated value is the file content.
 * /*from w ww . j ava  2  s .c o  m*/
 * @return a Map with the data loaded from the folder
 * @throws IOException
 */
public Map<String, String> loadFromFolder() throws IOException {
    Map<String, String> rawDataMap = new HashMap<String, String>();
    File folder = new File(folderName);
    if (!folder.exists()) {
        log.error("Invalid folder " + folderName);
        return null;
    }
    File[] listOfFiles = folder.listFiles(new OnlyExtensionFilter(RAW_DATA_FILE_EXTENSION));
    for (File file : listOfFiles) {
        String name = file.getName().replace("." + RAW_DATA_FILE_EXTENSION, "");
        FileInputStream stream = new FileInputStream(file);
        FileChannel fileChannel = stream.getChannel();
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0,
                fileChannel.size());
        String content = Charset.forName("ASCII").decode(mappedByteBuffer).toString();
        stream.close();
        rawDataMap.put(name, content);
    }
    return rawDataMap;
}

From source file:net.praqma.jenkins.memorymap.parser.AbstractMemoryMapParser.java

protected CharSequence createCharSequenceFromFile(String charset, File f) throws IOException {
    String chosenCharset = charset;

    CharBuffer cbuf = null;//from  w ww  . j  a v  a 2  s  .c om
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f.getAbsolutePath());
        FileChannel fc = fis.getChannel();
        ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());

        if (!Charset.isSupported(chosenCharset)) {
            logger.warning(String.format("The charset %s is not supported", charset));
            cbuf = Charset.defaultCharset().newDecoder().decode(bbuf);
        } else {
            cbuf = Charset.forName(charset).newDecoder().decode(bbuf);
        }

    } catch (IOException ex) {
        throw ex;
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    return cbuf;
}

From source file:org.meerkat.util.FileUtil.java

/**
 * readFileContents//from   w  w  w  .  j  a  v  a 2s  .  co m
 * 
 * @param filename
 * @return file contents
 */
public final String readFileContents(String filePath) {
    FileInputStream stream = null;
    String contents = "";
    try {
        stream = new FileInputStream(new File(filePath));
    } catch (Exception e) {
        log.error("File not found: " + filePath, e);
    }
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        contents = Charset.defaultCharset().decode(bb).toString();
    } catch (IOException e) {
        log.error("Error streaming file contents: " + filePath, e);

    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            log.error("Error closing file stream: " + filePath, e);

        }
    }
    return contents;
}

From source file:org.grouplens.lenskit.data.dao.packed.BinaryIndexTableTest.java

@Test
public void testSingleEntry() throws IOException {
    File file = folder.newFile();
    FileChannel chan = new RandomAccessFile(file, "rw").getChannel();
    BinaryIndexTableWriter w = BinaryIndexTableWriter.create(BinaryFormat.create(), chan, 1);
    w.writeEntry(42, new int[] { 0 });

    MappedByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
    BinaryIndexTable tbl = BinaryIndexTable.fromBuffer(1, buf);
    assertThat(tbl.getKeys(), contains(42L));
    assertThat(tbl.getEntry(42), contains(0));
    assertThat(tbl.getEntry(43), nullValue());
}

From source file:test.other.T_DaoTest.java

public void test2() throws SQLException, IOException {
    System.out.println(System.nanoTime() / 1000000);
    ResultSet rs = conn.createStatement().executeQuery("select content from fc_Post where id = 15");
    rs.next();/*ww w  . ja  v a2  s  .co m*/
    String text = rs.getString(1);
    System.out.println(text.length());
    System.out.println(System.nanoTime() / 1000000);
    @SuppressWarnings("resource")
    FileChannel rwChannel = new RandomAccessFile("textfile.txt", "rw").getChannel();
    ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, text.length() + 10);
    int lg = text.length() / 1000000;
    int pos = 0;
    byte[] buf = new byte[text.length() / 1000000];
    System.out.println(System.nanoTime() / 1000000);
    for (int i = 0; i < 1000000; i++) {
        System.arraycopy(text.getBytes(), pos, buf, 0, lg);
        wrBuf.put(buf);
        pos += lg;
    }
    System.out.println(System.nanoTime() / 1000000);
    rwChannel.close();
}

From source file:org.dataconservancy.dcs.access.server.TransformerServiceImpl.java

@Override
public String fgdcToHtml(String inputUrl, String format) {

    if (format.contains("fgdc")) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File(homeDir + "queryFgdcResult.xsl"));
        Transformer transformer;//  w  ww.  java2s .c  o m
        try {
            transformer = factory.newTransformer(xslt);
            String inputPath = homeDir + UUID.randomUUID().toString() + "fgdcinput.xml";
            saveUrl(inputPath, inputUrl);
            Source text = new StreamSource(new File(inputPath));
            String outputPath = homeDir + UUID.randomUUID().toString() + "fgdcoutput.html";
            File outputFile = new File(outputPath);
            transformer.transform(text, new StreamResult(outputFile));
            FileInputStream stream = new FileInputStream(new File(outputPath));
            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.defaultCharset().decode(bb).toString();
            } finally {
                stream.close();
            }

        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        try {
            String inputPath =
                    //getServletContext().getContextPath()+"/xml/"+
                    homeDir + UUID.randomUUID().toString() + "fgdcinput.xml";
            saveUrl(inputPath, inputUrl);
            Source text = new StreamSource(new File(
                    //"/home/kavchand/Desktop/fgdc.xml"
                    inputPath));
            FileInputStream stream = new FileInputStream(new File(inputPath));

            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();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return null;
}

From source file:org.fim.internal.hash.FileHasher.java

private int hashBuffer(FileChannel channel, long filePosition, long size) throws IOException {
    MappedByteBuffer buffer = null;
    try {/*  w w w . ja va  2s.  co m*/
        buffer = channel.map(FileChannel.MapMode.READ_ONLY, filePosition, size);
        int bufferSize = buffer.remaining();

        frontHasher.update(filePosition, buffer);

        return bufferSize;
    } finally {
        unmap(buffer);
    }
}