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:interfazGrafica.frmMoverRFC.java

public void mostrarPDF() {
    String curp = "";
    curp = txtCapturaCurp.getText();//from  ww w .j  a va2 s  .  c  o m
    ArrayList<DocumentoRFC> Docs = new ArrayList<>();
    DocumentoRFC sigExp;
    DocumentoRFC temporal;
    RFCescaneado tempo = new RFCescaneado();

    //tempo.borrartemporal();
    sigExp = expe.obtenerArchivosExp();
    Nombre_Archivo = sigExp.getNombre();
    nombreArchivo.setText(Nombre_Archivo);

    if (Nombre_Archivo != "") {
        doc = sigExp;
        System.out.println("Obtuvo el nombre del archivo.");
        System.out.println(doc.ruta + doc.nombre);
        String file = "C:\\escaneos\\Local\\Temporal\\" + doc.nombre;
        File arch = new File(file);
        System.out.println("Encontr el siguiente archivo:");
        System.out.println(file);
        System.out.println("");
        if (arch.exists()) {
            System.out.println("El archivo existe");
        }
        try {
            System.out.println("Entr al try");
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            System.out.println("Reconoc el archivo" + file);
            FileChannel channel = raf.getChannel();
            System.out.println("Se abrio el canal");
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
            System.out.println("Channel map");
            PDFFile pdffile = new PDFFile(buf);
            System.out.println("Creando un pdf file");
            PDFPage page = pdffile.getPage(0);
            System.out.println("Obteniendo la pagina con " + 0);

            panelpdf2.showPage(page);
            System.out.println("mostrando el panel pdf2");
            repaint();
            System.gc();

            buf.clear();
            raf.close();

            System.gc();

        } catch (Exception ioe) {
            JOptionPane.showMessageDialog(null, "Error al abrir el archivo");
            ioe.printStackTrace();
        }

    }
    // tempo.borrartemporal();
}

From source file:org.wso2.carbon.mediation.library.service.upload.LibraryUploader.java

private void writeResource(DataHandler dataHandler, String tempDestPath, String destPath, String fileName)
        throws IOException {

    File tempDestFile = new File(tempDestPath, fileName);
    FileChannel out = null;//from  ww  w  .  ja  v a  2s .c  om
    FileChannel in = null;
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(tempDestFile);
        dataHandler.writeTo(fos);
        fos.flush();

        /* File stream is copied to a temp directory in order handle hot deployment issue
           occurred in windows */
        dataHandler.writeTo(fos);
        out = new FileOutputStream(destPath + File.separator + fileName).getChannel();
        in = new FileInputStream(tempDestFile).getChannel();
        out.write(in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()));
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            log.warn("Can't close file streams.", e);
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            log.warn("Can't close file streams.", e);
        }
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            log.warn("Can't close file streams.", e);
        }
    }

    if (!tempDestFile.delete()) {
        if (log.isDebugEnabled()) {
            log.debug("temp file: " + tempDestFile.getAbsolutePath()
                    + " deletion failed, scheduled deletion on server exit.");
        }
        tempDestFile.deleteOnExit();
    }
}

From source file:edu.tsinghua.lumaqq.IPSeeker.java

/**
 * ????s?IP//from  w  ww . j av a  2  s .c om
 * @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 ipLoc = getIPLocation(temp);
                // ???s??List
                if (ipLoc.country.indexOf(s) != -1 || ipLoc.area.indexOf(s) != -1) {
                    IPEntry entry = new IPEntry();
                    entry.country = ipLoc.country;
                    entry.area = ipLoc.area;
                    // IP
                    readIP(offset - 4, b4);
                    entry.beginIp = Util.getIpStringFromBytes(b4);
                    // ?IP
                    readIP(temp, b4);
                    entry.endIp = Util.getIpStringFromBytes(b4);
                    // 
                    ret.add(entry);
                }
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage());
    }
    return ret;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java

@Override
public boolean canDecodeInput(File file) throws IOException {
    if (file == null) {
        throw new IllegalArgumentException("file == null!");
    }/*  w  w  w .  j ava 2  s  .c  o m*/
    if (!file.canRead()) {
        throw new IOException("cannot read the input file");
    }

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

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

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

    buff.rewind();

    boolean DEBUG = false;

    dbgLog.fine("applying the spss-por test\n");

    // size test
    if (buff.capacity() < 491) {
        dbgLog.fine("this file is NOT spss-por type");
        return false;
    }

    //windows [0D0A]=>   [1310] = [CR/LF]
    //unix    [0A]  =>   [10]
    //mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    // expected results
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //  : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    buff.rewind();
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }
    if (three == nolines) {
        dbgLog.fine("0D0D0A case");
        windowsNewLine = false;
    } else if ((ucase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0A case");
        windowsNewLine = false;
    } else if ((ucase < nolines) && (wcase == nolines)) {
        dbgLog.fine("0D0A case");
    } else if ((mcase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0D case");
        windowsNewLine = false;
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    dbgLog.fine("pormark =>" + pormarks + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("this file is spss-por type");
        return true;
    } else {
        dbgLog.fine("this file is NOT spss-por type");
    }
    return false;
}

From source file:pyromaniac.IO.MMFastqImporter.java

/**
 * Helper function for init(). Scans this.fastq file for sequence starts and records their position.
 * Multiple MappedByteBuffers are used to handle large files.
 * @throws Exceptions relating to file reading and decoding.
 *//*from   w w  w . ja  v a2s  . co m*/
private void _initFile() throws Exception {
    FileInputStream tempStream = new FileInputStream(new File(this.fastqFile));
    FileChannel fcSeq = tempStream.getChannel();
    this.seqSizeLong = fcSeq.size();
    this.recordStarts = new ArrayList<Pair<Integer, Long>>();

    int state = -1;

    for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) {
        MappedByteBuffer recordBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition,
                Math.min(this.seqSizeLong - startPosition, HALF_GIGA));
        this.recordBuffers.add(recordBuffer);

        int sbf_pos = this.recordBuffers.size() - 1;

        int maxBuffer = 2048;
        int bufferSize = (recordBuffer.capacity() > maxBuffer) ? maxBuffer : recordBuffer.capacity();

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

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

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

                //I see a fastq header, I am either at beginning of file, or last saw the quality line...
                if (curr == BEGINNING_FASTQ_SEQ && (state == -1 || state == 4)) {
                    this.recordStarts.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile)));
                    state = 1;

                } else if (curr == BEGINNING_FASTQ_QUAL && (state == 1)) {
                    state = 2;
                } else if ((curr == '\n' || curr == '\r') & state == 2) {
                    state = 3;
                } else if ((curr == '\n' || curr == '\r') & state == 3) {
                    state = 4;
                }
            }

            int newPos = recordBuffer.limit();

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

From source file:pyromaniac.IO.MMFastaImporter.java

/**
 * _init seq./*from   w  w w.j ava  2s  . co m*/
 *
 * @throws Exception the exception
 */
private void _initSeq() throws Exception {
    FileInputStream tempStream = new FileInputStream(new File(this.seqFile));
    FileChannel fcSeq = tempStream.getChannel();
    this.seqSizeLong = fcSeq.size();
    this.seqStartsLL = new ArrayList<Pair<Integer, Long>>();

    for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) {
        MappedByteBuffer seqBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition,
                Math.min(this.seqSizeLong - startPosition, HALF_GIGA));

        this.seqBuffers.add(seqBuffer);
        int sbf_pos = seqBuffers.size() - 1;
        int maxBuffer = 2048;
        int bufferSize = (seqBuffer.capacity() > maxBuffer) ? maxBuffer : seqBuffer.capacity();

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

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

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

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

            int newPos = seqBuffer.limit();

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

}

From source file:com.koda.integ.hbase.storage.LRUStorageRecycler.java

/**
 * Format of a block in a file://ww w.  j  ava  2  s. com
 * 0..3  - total record size (-4)
 * 4..7  - size of a key in bytes (16 if use hash128)
 * 8 .. x - key data
 * x+1 ..x+1- IN_MEMORY flag ( 1- in memory, 0 - not)
 * x+2 ... block, serialized and compressed
 *
 * @param file the file
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws NativeMemoryException the native memory exception
 */
private void processFile(RandomAccessFile file) throws IOException, NativeMemoryException {

    FileChannel fc = file.getChannel();
    // make sure that file size < 2G
    LOG.info("File length=" + file.length());
    MappedByteBuffer buffer = fc.map(MapMode.READ_ONLY, 0, file.length());

    long fileLength = file.length();
    long saved = 0;
    long startTime = System.currentTimeMillis();

    while (buffer.position() < fileLength) {
        int oldOffset = buffer.position();
        //LOG.info(oldOffset);
        // check IO throttle
        ioThrottle(startTime, oldOffset);

        NumericHistogram histogram = refCache.getObjectHistogram();

        int blockSize = buffer.getInt();
        int keySize = buffer.getInt();

        //LOG.info("block size="+blockSize+" key size="+keySize);

        byte[] key = new byte[keySize];
        // STATISTICS
        totalScannedBytes.addAndGet(blockSize + 4);

        // read key
        // WE HAVE TO USE byte[] keys
        long data = refCache.getEvictionData(key);
        if (data < 0) {
            // not found in in_memory cache
            buffer.position(oldOffset + blockSize + 4);
            continue;
        }

        double quantValue = histogram.quantile(evictionThreshold);
        if (data > quantValue) {
            // save block
            saved = blockSize + 4;
            buffer.position(oldOffset);
            StorageHandle handle = storage.storeData(buffer);
            refCache.put(key, handle);

        } else {
            // STATISTICS
            totalPurgedBytes.addAndGet(blockSize + 4);
        }

        if (oldOffset + blockSize + 4 < fileLength) {
            // Advance pointer
            buffer.position(oldOffset + blockSize + 4);

        } else {
            break;
        }
        // Check panic. W/o adaptive processing support - killing file entirely 
        // is the only option to keep up with the load.
        if (storage.getCurrentStorageSize() >= panicLevelWatermark * storage.getMaxStorageSize()) {
            LOG.warn("[PANIC DELETE]. Storage size exceeded " + panicLevelWatermark + " mark.");
            // STATISTICS
            totalPanicEvents.incrementAndGet();
        }
    }

    // Unmap mapped ByteBuffer
    fc.close();
    FileUtils.unmapMmaped(buffer);
    ;
    LOG.info("Stats: total length=" + fileLength + "; purged data=" + (fileLength - saved)
            + " with eviction threshold=" + evictionThreshold + "; purged ratio=["
            + (((double) (fileLength - saved)) / fileLength) + "]");

}

From source file:com.xzjmt.util.ipseek.IPSeeker.java

/**
 * ????s?IP/*from  www. j  av  a2s  .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 = Utils.getIpStringFromBytes(b4);
                    // ?IP
                    readIP(temp, b4);
                    entry.endIp = Utils.getIpStringFromBytes(b4);
                    // 
                    ret.add(entry);
                }
            }
        }
    } catch (Exception e) {
        log.info(e.getMessage());
    } finally {
        this.closeIpFile();
    }
    return ret;
}

From source file:hornet.framework.clamav.service.ClamAVCheckService.java

/**
 * Rcupration du fichier  stream./* w  w w . j  av  a 2  s . c om*/
 *
 * @param fileForTest
 *            fichier de test
 * @param fileForTestSize
 *            taille du fichier
 * @return MappedByteBuffer
 * @throws IOException
 *             probleme de lecture
 */
protected MappedByteBuffer recupFichierStream(final File fileForTest, final long fileForTestSize)
        throws IOException {

    // Rcupration du fichier  stream
    final RandomAccessFile raf = new RandomAccessFile(fileForTest, "r");
    final FileChannel readChannel = raf.getChannel();
    MappedByteBuffer bufFile = null;
    try {
        bufFile = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileForTestSize);
    } finally {
        if (readChannel != null) {
            try {
                readChannel.close();
            } catch (final IOException e) {
                ClamAVCheckService.LOGGER.error("Erreur lors de la fermeture de la socket", e);
            }
        }
        if (raf != null) {
            try {
                raf.close();
            } catch (final IOException e) {
                ClamAVCheckService.LOGGER.error("Erreur lors de la fermeture de la socket", e);
            }
        }
    }
    return bufFile;
}

From source file:org.codehaus.preon.buffer.DefaultBitBuffer.java

/** Read byte buffer containing binary stream and set the bit pointer position to 0. */
public DefaultBitBuffer(String fileName) {

    File file = new File(fileName);

    // Open the file and then get a org.codehaus.preon.channel.channel from the stream
    FileInputStream fis;//from   w  w  w  .  j ava 2 s . c  o m

    try {
        fis = new FileInputStream(file);

        FileChannel fc = fis.getChannel();

        // Get the file's size and then map it into memory
        int fileSize = (int) fc.size();
        ByteBuffer inputByteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);

        // Close the org.codehaus.preon.channel.channel and the stream
        fc.close();

        this.byteBuffer = inputByteBuffer;
        bitBufBitSize = ((long) (inputByteBuffer.capacity())) << 3;
        bitPos = 0;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}