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.l2jfree.gameserver.geodata.pathfinding.geonodes.GeoPathFinding.java

private void LoadPathNodeFile(byte rx, byte ry) {
    String fname = "./data/pathnode/" + rx + "_" + ry + ".pn";
    short regionoffset = getRegionOffset(rx, ry);
    _log.info("PathFinding Engine: - Loading: " + fname + " -> region offset: " + regionoffset + "X: " + rx
            + " Y: " + ry);
    File Pn = new File(fname);
    int node = 0, size, index = 0;
    FileChannel roChannel = null;
    try {//w  w w  .  j  av a  2  s  .com
        // Create a read-only memory-mapped file
        roChannel = new RandomAccessFile(Pn, "r").getChannel();
        size = (int) roChannel.size();
        MappedByteBuffer nodes;
        if (Config.FORCE_GEODATA) //Force O/S to Loads this buffer's content into physical memory.
            //it is not guarantee, because the underlying operating system may have paged out some of the buffer's data
            nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
        else
            nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);

        // Indexing pathnode files, so we will know where each block starts
        IntBuffer indexs = IntBuffer.allocate(65536);

        while (node < 65536) {
            byte layer = nodes.get(index);
            indexs.put(node++, index);
            index += layer * 10 + 1;
        }
        _pathNodesIndex.set(regionoffset, indexs);
        _pathNodes.set(regionoffset, nodes);
    } catch (Exception e) {
        _log.warn("Failed to Load PathNode File: " + fname + "\n", e);
    } finally {
        try {
            if (roChannel != null)
                roChannel.close();
        } catch (Exception e) {
        }
    }

}

From source file:org.gephi.ui.components.ReportSelection.java

public void copy(File source, File dest) throws IOException {
    FileChannel in = null, out = null;
    try {//from  ww w .  j  a  v  a2 s . c o m
        if (dest.isDirectory()) {
            dest = new File(dest, source.getName());
        }
        in = new FileInputStream(source).getChannel();
        out = new FileOutputStream(dest).getChannel();

        long size = in.size();
        MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

        out.write(buf);

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:org.cytobank.fcs_files.events.MemoryEvents.java

public MemoryEvents(File doubleFile) throws IOException, MemoryEventsException {
    this.doubleFile = doubleFile;

    numberOfEvents = (int) doubleFile.length() / BYTES_PER_DOUBLE;

    setupMemoryEventsArray(numberOfEvents);

    try {// w  ww .  j a va 2s .c om
        // Read in the doubles file
        FileInputStream fileInputStream = new FileInputStream(doubleFile);
        FileChannel fileChannel = fileInputStream.getChannel();
        MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_ONLY, 0, doubleFile.length());
        DoubleBuffer doubleBuffer = mappedByteBuffer.asDoubleBuffer();

        doubleBuffer.get(events, 0, numberOfEvents);

        fileChannel.close();
        fileInputStream.close();
    } catch (IOException ioe) {
        destroy();
        throw ioe;
    }

    openedAt = System.currentTimeMillis();
}

From source file:edu.harvard.iq.dvn.ingest.dsb.SubsettableFileChecker.java

public String detectSubsettableFormat(File fh) {
    boolean DEBUG = false;
    String readableFormatType = null;
    try {/*  w w w .ja va  2s.co m*/
        int buffer_size = this.getBufferSize(fh);
        // set-up a FileChannel instance for a given file object
        FileChannel srcChannel = new FileInputStream(fh).getChannel();

        // create a read-only MappedByteBuffer
        MappedByteBuffer buff = srcChannel.map(FileChannel.MapMode.READ_ONLY, 0, buffer_size);

        //this.printHexDump(buff, "hex dump of the byte-buffer");

        //for (String fmt : defaultFormatSet){
        buff.rewind();
        dbgLog.fine("before the for loop");
        for (String fmt : this.getTestFormatSet()) {
            // get a test method
            Method mthd = testMethods.get(fmt);
            try {
                // invoke this method
                Object retobj = mthd.invoke(this, buff);
                String result = (String) retobj;

                if (result != null) {
                    dbgLog.fine("result for (" + fmt + ")=" + result);
                    if (DEBUG) {
                        out.println("result for (" + fmt + ")=" + result);
                    }
                    if (readableFileTypes.contains(result)) {
                        readableFormatType = result;
                    }
                    dbgLog.fine("readableFormatType=" + readableFormatType);
                    return readableFormatType;
                } else {
                    dbgLog.fine("null was returned for " + fmt + " test");
                    if (DEBUG) {
                        out.println("null was returned for " + fmt + " test");
                    }
                }
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                err.format(cause.getMessage());
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return readableFormatType;

    } catch (FileNotFoundException fe) {
        dbgLog.fine("exception detected: file was not foud");
        fe.printStackTrace();
    } catch (IOException ie) {
        dbgLog.fine("other io exception detected");
        ie.printStackTrace();
    }
    return readableFormatType;
}

From source file:com.mmj.app.common.util.IPTools.java

/**
 * ????s?IP/* w ww  .  j av a  2  s. c  o m*/
 * 
 * @param s ?
 * @return ?IPEntryList
 */
public List<IPEntry> getIPEntries(String s) {
    List<IPEntry> ret = new ArrayList<IPEntry>();
    try {
        // IP?
        if (mbb == null) {
            FileChannel fc = ipFile.getChannel();
            mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ipFile.length());
            mbb.order(ByteOrder.LITTLE_ENDIAN);
        }

        int endOffset = (int) ipEnd;
        for (int offset = (int) ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
            int temp = readInt3(offset);
            if (temp != -1) {
                IPLocation loc = getIPLocation(temp);
                // ???s??List
                if (loc.country.indexOf(s) != -1 || loc.area.indexOf(s) != -1) {
                    IPEntry entry = new IPEntry();
                    entry.country = loc.country;
                    entry.area = loc.area;
                    // IP
                    readIP(offset - 4, b4);
                    entry.beginIp = getIpStringFromBytes(b4);
                    // ?IP
                    readIP(temp, b4);
                    entry.endIp = getIpStringFromBytes(b4);
                    // 
                    ret.add(entry);
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return ret;
}

From source file:com.qubole.rubix.core.RemoteReadRequestChain.java

public Integer call() throws IOException {
    Thread.currentThread().setName(threadName);
    checkState(isLocked, "Trying to execute Chain without locking");

    if (readRequests.size() == 0) {
        return 0;
    }//ww w  .j  a  v a  2s . com

    RandomAccessFile localFile = null;
    FileChannel fc = null;

    try {
        localFile = new RandomAccessFile(localFilename, "rw");
        fc = localFile.getChannel();
        for (ReadRequest readRequest : readRequests) {
            log.debug(String.format("Executing ReadRequest: [%d, %d, %d, %d, %d]",
                    readRequest.getBackendReadStart(), readRequest.getBackendReadEnd(),
                    readRequest.getActualReadStart(), readRequest.getActualReadEnd(),
                    readRequest.getDestBufferOffset()));
            inputStream.seek(readRequest.backendReadStart);
            MappedByteBuffer mbuf = fc.map(FileChannel.MapMode.READ_WRITE, readRequest.backendReadStart,
                    readRequest.getBackendReadLength());
            log.debug(String.format("Mapped file from %d till length %d", readRequest.backendReadStart,
                    readRequest.getBackendReadLength()));
            /*
             * MappedByteBuffer does not provide backing byte array, so cannot write directly to it via FSDataOutputStream.read
             * Instead, download to normal destination buffer (+offset buffer to get block boundaries) and then copy to MappedByteBuffer
             */

            int prefixBufferLength = (int) (readRequest.getActualReadStart()
                    - readRequest.getBackendReadStart());
            int suffixBufferLength = (int) (readRequest.getBackendReadEnd() - readRequest.getActualReadEnd());
            log.debug(
                    String.format("PrefixLength: %d SuffixLength: %d", prefixBufferLength, suffixBufferLength));

            // TODO: use single byte buffer for all three streams
            /* TODO: also GC cost can be lowered by shared buffer pool, a small one.
            IOUtils.copyLarge method. A single 4kB byte buffer can be used to copy whole file
                    
              */
            if (prefixBufferLength > 0) {
                byte[] prefixBuffer = new byte[prefixBufferLength];
                log.debug(String.format("Trying to Read %d bytes into prefix buffer", prefixBufferLength));
                totalPrefixRead += readAndCopy(prefixBuffer, 0, mbuf, prefixBufferLength);
                log.debug(String.format("Read %d bytes into prefix buffer", prefixBufferLength));
            }
            log.debug(String.format("Trying to Read %d bytes into destination buffer",
                    readRequest.getActualReadLength()));
            int readBytes = readAndCopy(readRequest.getDestBuffer(), readRequest.destBufferOffset, mbuf,
                    readRequest.getActualReadLength());
            totalRequestedRead += readBytes;
            log.debug(String.format("Read %d bytes into destination buffer", readBytes));
            if (suffixBufferLength > 0) {
                // If already in reading actually required data we get a eof, then there should not have been a suffix request
                checkState(readBytes == readRequest.getActualReadLength(),
                        "Acutal read less than required, still requested for suffix");
                byte[] suffixBuffer = new byte[suffixBufferLength];
                log.debug(String.format("Trying to Read %d bytes into suffix buffer", suffixBufferLength));
                totalSuffixRead += readAndCopy(suffixBuffer, 0, mbuf, suffixBufferLength);
                log.debug(String.format("Read %d bytes into suffix buffer", suffixBufferLength));
            }
        }
    } finally {
        if (fc != null) {
            fc.close();
        }
        if (localFile != null) {
            localFile.close();
        }
    }
    log.info(String.format("Read %d bytes from remote file, added %d to destination buffer",
            totalPrefixRead + totalRequestedRead + totalSuffixRead, totalRequestedRead));
    return totalRequestedRead;
}

From source file:com.wipro.sa349342.wmar.AbstractArchitectCamActivity.java

public void createFCListFromJsonFile() {
    try {//from www  . j  a  va2s .c  o  m

        //downloadAndStoreJson("http://jsonblob.com/api/jsonBlob/57b57cc1e4b0dc55a4edf932");

        File jsonFile = new File(Environment.getExternalStorageDirectory().getPath(),
                MainActivityLanding.DefaultJSONFileFromMemeory);
        /*if (!jsonFile.isFile()) {
        System.out.println("MyGlobalVariables--readJSONFromMemory::-downloaded json file not found.Switching to default file");
        jsonFile = new File(Environment.getExternalStorageDirectory().getPath(), MyGlobalVariables.defaultJSONFileFromMemeory);
                
        }*/

        FileInputStream stream = new FileInputStream(jsonFile);
        String jsonStr = null;

        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

            jsonStr = Charset.defaultCharset().decode(bb).toString();
        } finally {
            stream.close();
        }

        JSONObject jsonObj = new JSONObject(jsonStr);

        // Getting data JSON Array nodes
        // JSONArray data = jsonObj.getJSONArray("utility_lines");

        addFeaturesInList(jsonObj.getJSONArray("utility_lines"), UtlityLinesFeatureList, "utility_lines");
        addFeaturesInList(jsonObj.getJSONArray("equipments"), EquipmentsFeatureList, "equipments");

        com.google.android.gms.maps.model.CameraPosition cameraPosition = new com.google.android.gms.maps.model.CameraPosition.Builder()
                .target(new LatLng(12.8363773, 77.6585139))
                //.target(london_eye)//Sets the center of the map to
                .zoom(19) // Sets the zoom
                .bearing(0) // Sets the orientation of the camera to east
                .tilt(0) // Sets the tilt of the camera to 1 degrees
                .build();

        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:pyromaniac.IO.MMFastaImporter.java

/**
 * _init qual.//  ww w .  j  a v  a  2  s  .com
 *
 * @throws Exception the exception
 */
private void _initQual() throws Exception {
    FileInputStream tempStream = new FileInputStream(new File(this.qualFile));
    FileChannel fcQual = tempStream.getChannel();
    this.qualSizeLong = fcQual.size();

    //qual starts LL contains pairs, marking file #no (in  qualBuffers) and position #no (in the buffer).
    this.qualStartsLL = new ArrayList<Pair<Integer, Long>>();

    for (long startPosition = 0L; startPosition < this.qualSizeLong; startPosition += HALF_GIGA) {
        MappedByteBuffer qualBuffer = fcQual.map(FileChannel.MapMode.READ_ONLY, startPosition,
                Math.min(this.qualSizeLong - startPosition, HALF_GIGA)); //map half a gig to this channel.
        this.qualBuffers.add(qualBuffer);
        int qbf_pos = qualBuffers.size() - 1;
        int maxBuffer = 2048;
        int bufferSize = (qualBuffer.capacity() > maxBuffer) ? maxBuffer : qualBuffer.capacity();

        qualBuffer.limit(bufferSize);
        qualBuffer.position(0);

        while (qualBuffer.position() != qualBuffer.capacity()) {
            int prevPos = qualBuffer.position();
            CharBuffer result = decoder.decode(qualBuffer);
            qualBuffer.position(prevPos);

            for (int i = 0; i < result.capacity(); i++) {
                char curr = result.charAt(i);
                int posInFile = prevPos + i;

                if (curr == BEGINNING_FASTA_HEADER) {
                    qualStartsLL.add(new Pair<Integer, Long>(qbf_pos, new Long(posInFile)));
                }
            }

            int newPos = qualBuffer.limit();

            if (qualBuffer.limit() + bufferSize > qualBuffer.capacity())
                qualBuffer.limit(qualBuffer.capacity());
            else
                qualBuffer.limit(qualBuffer.limit() + bufferSize);
            qualBuffer.position(newPos);
        }
        qualBuffer.rewind();
    }
}

From source file:edu.harvard.iq.dataverse.ingest.IngestableDataChecker.java

public String detectTabularDataFormat(File fh) {
    boolean DEBUG = false;
    String readableFormatType = null;
    try {/*from   www .  ja v a 2s  . c o m*/
        int buffer_size = this.getBufferSize(fh);
        dbgLog.fine("buffer_size: " + buffer_size);

        // set-up a FileChannel instance for a given file object
        FileChannel srcChannel = new FileInputStream(fh).getChannel();

        // create a read-only MappedByteBuffer
        MappedByteBuffer buff = srcChannel.map(FileChannel.MapMode.READ_ONLY, 0, buffer_size);

        //this.printHexDump(buff, "hex dump of the byte-buffer");

        //for (String fmt : defaultFormatSet){
        buff.rewind();
        dbgLog.fine("before the for loop");
        for (String fmt : this.getTestFormatSet()) {

            // get a test method
            Method mthd = testMethods.get(fmt);
            //dbgLog.info("mthd: " + mthd.getName());

            try {
                // invoke this method
                Object retobj = mthd.invoke(this, buff);
                String result = (String) retobj;

                if (result != null) {
                    dbgLog.fine("result for (" + fmt + ")=" + result);
                    if (DEBUG) {
                        out.println("result for (" + fmt + ")=" + result);
                    }
                    if (readableFileTypes.contains(result)) {
                        readableFormatType = result;
                    }
                    dbgLog.fine("readableFormatType=" + readableFormatType);
                    return readableFormatType;
                } else {
                    dbgLog.fine("null was returned for " + fmt + " test");
                    if (DEBUG) {
                        out.println("null was returned for " + fmt + " test");
                    }
                }
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                // added null check because of "homemade.zip" from https://redmine.hmdc.harvard.edu/issues/3273
                if (cause.getMessage() != null) {
                    err.format(cause.getMessage());
                    e.printStackTrace();
                } else {
                    dbgLog.info("cause.getMessage() was null for " + e);
                    e.printStackTrace();
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (BufferUnderflowException e) {
                dbgLog.info("BufferUnderflowException " + e);
                e.printStackTrace();
            }
        }

        return readableFormatType;

    } catch (FileNotFoundException fe) {
        dbgLog.fine("exception detected: file was not foud");
        fe.printStackTrace();
    } catch (IOException ie) {
        dbgLog.fine("other io exception detected");
        ie.printStackTrace();
    }
    return readableFormatType;
}

From source file:org.oscarehr.document.web.ManageDocumentAction.java

public File createCacheVersion(Document d, int pageNum) throws Exception {

    File documentCacheDir = new File(EDocUtil.getCacheDirectory());
    File file = new File(EDocUtil.getDocumentPath(d.getDocfilename()));

    RandomAccessFile raf = new RandomAccessFile(file, "r");
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdffile = new PDFFile(buf);
    if (raf != null)
        raf.close();/*from www  . java2  s.  com*/
    if (channel != null)
        channel.close();
    // draw the first page to an image
    PDFPage ppage = pdffile.getPage(pageNum);

    log.debug("WIDTH " + (int) ppage.getBBox().getWidth() + " height " + (int) ppage.getBBox().getHeight());

    // get the width and height for the doc at the default zoom
    Rectangle rect = new Rectangle(0, 0, (int) ppage.getBBox().getWidth(), (int) ppage.getBBox().getHeight());

    log.debug("generate the image");
    Image img = ppage.getImage(rect.width, rect.height, // width & height
            rect, // clip rect
            null, // null for the ImageObserver
            true, // fill background with white
            true // block until drawing is done
    );

    log.debug("about to Print to stream");
    File outfile = new File(documentCacheDir, d.getDocfilename() + "_" + pageNum + ".png");

    OutputStream outs = null;
    try {
        outs = new FileOutputStream(outfile);

        RenderedImage rendImage = (RenderedImage) img;
        ImageIO.write(rendImage, "png", outs);
        outs.flush();
    } finally {
        if (outs != null)
            outs.close();
    }
    return outfile;

}