Example usage for java.nio.channels FileChannel read

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

Introduction

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

Prototype

public abstract int read(ByteBuffer dst, long position) throws IOException;

Source Link

Document

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

Usage

From source file:voldemort.store.cachestore.impl.ChannelStore.java

/**
 *
 * @param offset2len/*ww w.ja  va2  s. c o m*/
 * @param channel
 * @return
 * @throws IOException
 */
public byte[] readChannel(long offset2len, FileChannel channel) throws IOException {
    long offset = getOffset(offset2len);
    int len = getLen(offset2len);
    ByteBuffer data = ByteBuffer.allocate(len);
    channel.read(data, offset);
    return data.array();
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

private void appendSitemapIndex(String sitemap) throws IOException {
    File sitemapIndexFile = new File(mapResourcesRoot.getFile(), SITEMAP_INDEX_NAME);
    if (!sitemapIndexFile.exists()) {
        //if a new sitemap file
        List<String> lines = new ArrayList<String>();
        lines.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        lines.add("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
        lines.add("</sitemapindex>");
        FileUtils.writeLines(sitemapIndexFile, lines);
    }// w  ww .  j a  v a2 s  .  c o  m

    RandomAccessFile rfile = new RandomAccessFile(sitemapIndexFile, "rw");
    FileChannel channel = rfile.getChannel();

    //this new content will append to end of file before XML end tag
    StringBuilder lines = new StringBuilder();
    lines.append("   <sitemap>\n");
    lines.append("     <loc>" + WebUtil.getHostAppURL() + SITEMAP_URL_CONTEXT + sitemap + "</loc>\n");
    lines.append("     <lastmod>" + TIME_FORMAT.format(new Date()) + " </lastmod>\n");
    lines.append("   </sitemap>\n");
    //the last tag will be overwrite, so append it again to new content. 
    lines.append(SITEMAP_INDEX_TAIL_FLAG);
    byte[] content = lines.toString().getBytes();

    ByteBuffer byteBuf = ByteBuffer.allocate(512);
    // seek first
    int len = 0, headIdx = 0;
    long tailIdx = channel.size() - 512;
    tailIdx = tailIdx < 0 ? 0 : tailIdx;

    long headPos = -1;
    StringBuilder header = new StringBuilder();
    while ((len = channel.read(byteBuf, tailIdx)) > 0) {
        byteBuf.rewind();
        byte[] dst = new byte[len];
        byteBuf.get(dst, 0, len);
        header.append(new String(dst, "UTF8"));
        headIdx = header.indexOf(SITEMAP_INDEX_TAIL_FLAG);
        if (headIdx != -1) {
            headPos = channel.size() - header.substring(headIdx).getBytes().length;
            break;
        }
    }
    FileLock lock = channel.tryLock(headPos, content.length, false);
    try {
        channel.write(ByteBuffer.wrap(content), headPos);
    } finally {
        lock.release();
    }

    channel.force(false);
    rfile.close();

}

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

@Override
public StorageHandle getData(StorageHandle storeHandle, ByteBuffer buf) {
    FileStorageHandle fsh = (FileStorageHandle) storeHandle;

    // Check if current file and offset > currentFileOffset
    int id = maxId.get();
    if (fsh.getId() > id || (fsh.getId() == id && fsh.getOffset() >= currentFileOffset.get())) {
        // not found
        buf.putInt(0, 0);//w  ww  .ja  v a2  s  .  c  o m
        return fsh;
    }

    RandomAccessFile file = getFile(fsh.getId());//openFile(fsh.getId(), "r");

    boolean needSecondChance = needSecondChance(fsh.getId());

    try {
        if (file == null) {
            // return null
            buf.putInt(0, 0);
        } else {
            buf.clear();
            int toRead = fsh.getSize();
            buf.putInt(fsh.getSize());
            buf.limit(4 + toRead);
            try {
                FileChannel fc = file.getChannel();
                int total = 0;
                int c = 0;
                // offset start with overall object length .add +4
                int off = fsh.getOffset() + 4;
                while (total < toRead) {
                    c = fc.read(buf, off);
                    off += c;
                    if (c < 0) {
                        // return not found
                        buf.putInt(0, 0);
                        break;
                    }
                    total += c;
                }
            } catch (IOException e) {
                // return not found
                if (fsh.getId() > minId.get()) {
                    e.printStackTrace();
                }
                buf.putInt(0, 0);
            }
        }
        if (buf.getInt(0) != 0 && needSecondChance) {
            // store again
            fsh = (FileStorageHandle) storeData(buf);
        }
        return fsh;

    } finally {
        if (file != null) {
            // return file back
            // PUT we need for old version
            putFile(fsh.getId(), file);
        }
    }

}

From source file:com.yobidrive.diskmap.needles.NeedleManager.java

/** Read method for compacting: reads sequentially the log from the preselected needle until end of current files.
 * Does not feed the cache to keep fresh data in it
 * @return the next needle or null when end of file
 * @throws NeedleManagerException/*from   w  w  w  . j  a  va 2  s.  co  m*/
 */
public PointedNeedle readNextNeedleToCompactFromDisk() throws NeedleManagerException {
    try {
        long position = -1L;
        int readBytes = -1;
        FileChannel fc = null;

        fc = getChannel(compactNeedle.getNeedleFileNumber());
        if (fc == null)
            return null;
        // Position and read needle for check
        position = compactNeedle.getNeedleOffset();
        compactBuffer.rewind();
        compactBuffer.limit(MAXKEYSIZE + MAXVERSIONSIZE + Needle.NEEDLEOVERHEAD);

        // First read header to know the data size
        int totalHeaderReadBytes = 0;
        readBytes = 0;
        while (readBytes >= 0 && totalHeaderReadBytes < compactBuffer.limit()) {
            readBytes = fc.read(compactBuffer, position + totalHeaderReadBytes);
            totalHeaderReadBytes += readBytes;
        }
        if (totalHeaderReadBytes <= 0)
            return null;

        // Decode needle
        Needle needle = new Needle();
        if (!needle.getNeedleHeaderFromBuffer(compactBuffer)) {
            // Incorrect header: truncate file at this position and removes all subsequent files
            throw new NeedleManagerException(
                    "Wrong needle header during cleaning at " + compactNeedle.toString());
        }
        // Needle Header is OK, read the rest until end of needle. Change limit to include data
        compactBuffer.position(totalHeaderReadBytes);
        compactBuffer.limit(needle.getTotalSizeFromData());

        readBytes = 0;
        int totalContentReadBytes = 0;
        while (readBytes >= 0 && totalContentReadBytes < compactBuffer.limit() - totalHeaderReadBytes) {
            readBytes = fc.read(compactBuffer, position + totalHeaderReadBytes + totalContentReadBytes);
            totalContentReadBytes += readBytes;
        }

        compactBuffer.rewind();
        compactBuffer.position(needle.getHeaderSize());
        // Parse data and verifies checksum
        if (!needle.getNeedleDataFromBuffer(compactBuffer)) {
            // Incorrect data: truncate file at this position and removes all subsequent files
            throw new NeedleManagerException(
                    "Wrong needle data during cleaning at " + compactNeedle.toString());
        }
        // Now needle is parsed and OK
        PointedNeedle pn = new PointedNeedle();
        pn.setNeedlePointer(compactNeedle.clone());
        pn.setNeedle(needle);
        compactNeedle.positionToNextNeedle(position + needle.getRoundedTotalSize());
        return pn;
    } catch (Throwable th) {
        logger.error("Error reading needle for cleaning at " + compactNeedle.toString(), th);
        throw new NeedleManagerException();
    }
}

From source file:com.yobidrive.diskmap.needles.NeedleManager.java

/** Loads the needle pointed by the needlePointer and checks for validity (checksum, ...) and returns the next linked needle
 * @param needlePointer//  ww  w.j av  a2 s.  c  o  m
 * @param needle
 * @return a chained needle if the read is successful, otherwise null
 * @throws NeedleManagerException
 */
public Needle getNeedleFromDisk(NeedlePointer needlePointer) throws NeedleManagerException {
    ByteBuffer needleBuffer = null;
    try {

        FileChannel fc = getChannel(needlePointer.getNeedleFileNumber());
        if (fc == null)
            return new Needle();
        // Position and read needle for check
        long position = needlePointer.getNeedleOffset();
        // Acquires a ByteBuffer
        if (threadBufferQ == null)
            return new Needle();
        Chrono chr = new Chrono();
        needleBuffer = threadBufferQ.take();
        chr.lap("Wait for thread buffer ", 20);
        // Finally we have a buffer
        needleBuffer.rewind();
        needleBuffer.limit(MAXKEYSIZE + MAXVERSIONSIZE + Needle.NEEDLEOVERHEAD);

        // First read header to know the data size
        int readBytes = 0, totalHeaderReadBytes = 0;
        while (readBytes >= 0 && totalHeaderReadBytes < needleBuffer.limit()) {
            readBytes = fc.read(needleBuffer, position + totalHeaderReadBytes);
            totalHeaderReadBytes += readBytes;
        }
        if (totalHeaderReadBytes <= 0)
            return new Needle();

        Needle needle = new Needle();
        if (!needle.getNeedleHeaderFromBuffer(needleBuffer)) {
            return new Needle(); // Incorrect header
        }
        // Needle Header is OK, read the rest until end of needle. Change limit to include data
        // needleBuffer.rewind() ;
        needleBuffer.position(totalHeaderReadBytes);
        // needleBuffer.limit(needle.getPostDataSize()) ;
        needleBuffer.limit(needle.getTotalSizeFromData());

        readBytes = 0;
        int totalContentReadBytes = 0;
        while (readBytes >= 0 && totalContentReadBytes < needleBuffer.limit() - totalHeaderReadBytes) {
            readBytes = fc.read(needleBuffer, position + totalHeaderReadBytes + totalContentReadBytes);
            totalContentReadBytes += readBytes;
        }
        // readBytes = fc.read(needleBuffer, position+needle.getHeaderSize()) ;
        // Parse data and verifies checksum
        // 
        needleBuffer.rewind();
        needleBuffer.position(needle.getHeaderSize());
        if (!needle.getNeedleDataFromBuffer(needleBuffer))
            return new Needle();
        // Now needle is parsed and OK
        chr.total("Read from disk ", 20);
        return needle;
    } catch (Throwable th) {
        logger.error("Error reading needle at " + needlePointer.getFormattedNeedleFileNumber() + "/"
                + needlePointer.getFormattedNeedleOffset(), th);
        throw new NeedleManagerException();
    } finally {
        if (needleBuffer != null) {
            try {
                threadBufferQ.put(needleBuffer);
            } catch (InterruptedException ie) {
                throw new BucketTableManagerException("Error giving back needle read thread", ie);
            }
        }
    }
}

From source file:com.yobidrive.diskmap.needles.NeedleManager.java

/** Read method for repair routines: reads sequentially the log from the given checkpoint until end of all files.
 * Last file is truncated after the last valid needle (MAGIC numbers and MD5 OK)
 * @param needlePointer/*from  w ww  . ja  v  a2  s.  c  o m*/
 * @param checkPoint No repair will occur for needles <= checkpoint, index should be repaired in this case and checkpoint reseted
 * @return
 * @throws NeedleManagerException
 */
public PointedNeedle readNextNeedleFromDiskInLogSequence(NeedlePointer checkPoint)
        throws NeedleManagerException {
    Boolean repairMode = (checkPoint != null);
    ByteBuffer needleBuffer = null;
    int retry = 2;
    // System.out.print("0") ;
    try {
        long position = -1L;
        int readBytes = -1;
        int totalHeaderReadBytes = 0;
        FileChannel fc = null;
        while (retry > 0) {
            retry--;
            // System.out.print("a") ;
            fc = getChannel(repairNeedle.getNeedleFileNumber());
            if (fc == null)
                return null;
            // System.out.print("b") ;
            // logger.info("Repairing: reading file "+repairNeedle.toString() ) ;
            // Position and read needle for check
            position = repairNeedle.getNeedleOffset();
            // System.out.print("c") ;
            // Acquires a ByteBuffer
            if (threadBufferQ == null)
                return null;
            // System.out.println("1") ;
            if (needleBuffer == null)
                needleBuffer = threadBufferQ.take();
            // System.out.println("2") ;
            // Finally we have a buffer
            needleBuffer.rewind();
            needleBuffer.limit(MAXKEYSIZE + MAXVERSIONSIZE + Needle.NEEDLEOVERHEAD);
            // First read header to know the data size
            totalHeaderReadBytes = 0;
            readBytes = 0;
            while (readBytes >= 0 && totalHeaderReadBytes < needleBuffer.limit()) {
                readBytes = fc.read(needleBuffer, position + totalHeaderReadBytes);
                totalHeaderReadBytes += readBytes;
            }
            if (totalHeaderReadBytes <= 0) {
                if (!repairMode)
                    return null;
                // End of file, select next file
                if (position == 0 || repairNeedle.positionToNextFile() == null) {
                    // Clean end
                    if (repairNeedle.compareTo(checkPoint) <= 0) {
                        // We should NEVER repair a checkpointed needle. Kill checkpoint and rebuild index!
                        throw new BrokenCheckPointException(
                                "Missing checkpointed record " + repairNeedle.toString());
                    }
                    return null;
                } else {
                    // Continue with next file
                    retry = 1;
                    // System.out.println("-") ;
                    logger.info("Reading in sequence: switching to next file, " + repairNeedle.toString());
                    continue;
                }
            } else {
                // We have our needle (good or bad), do not retry
                retry = 0;
            }
        }
        Needle needle = new Needle();
        if (!needle.getNeedleHeaderFromBuffer(needleBuffer)) {
            // Incorrect header: truncate file at this position and removes all subsequent files
            if (!repairMode)
                return null;
            if (repairNeedle.compareTo(checkPoint) <= 0) {
                // We should NEVER repair a checkpointed needle. Kill checkpoint and rebuild index!
                throw new BrokenCheckPointException("Broken checkpointed record " + repairNeedle.toString());
            }
            truncate(repairNeedle);
            return null;
        }
        // System.out.println("3") ;
        // Needle Header is OK, read the rest until end of needle. Change limit to include data
        needleBuffer.position(totalHeaderReadBytes);
        needleBuffer.limit(needle.getTotalSizeFromData());

        readBytes = 0;
        int totalContentReadBytes = 0;
        while (readBytes >= 0 && totalContentReadBytes < needleBuffer.limit() - totalHeaderReadBytes) {
            readBytes = fc.read(needleBuffer, position + totalHeaderReadBytes + totalContentReadBytes);
            totalContentReadBytes += readBytes;
        }

        needleBuffer.rewind();
        needleBuffer.position(needle.getHeaderSize());
        // Parse data and verifies checksum
        if (!needle.getNeedleDataFromBuffer(needleBuffer)) {
            // Incorrect data: truncate file at this position and removes all subsequent files
            if (!repairMode)
                return null;
            if (repairNeedle.compareTo(checkPoint) <= 0) {
                // We should NEVER repair a checkpointed needle. Kill checkpoint and rebuild index!
                throw new BrokenCheckPointException("Broken checkpointed record " + repairNeedle.toString());
            }
            // System.out.print("truncate...") ;
            truncate(repairNeedle);
            // System.out.print("truncated.") ;
            return null;
        }
        // Now needle is parsed and OK
        PointedNeedle pn = new PointedNeedle();
        pn.setNeedlePointer(repairNeedle.clone());
        pn.setNeedle(needle);
        // System.out.println("4") ;
        // Put needle in cache
        needleReadCache.put(pn.getNeedlePointer(), needle);
        // Put needleHeader in cache
        needleHeaderReadCache.put(pn.getNeedlePointer(), needle.getNeedleHeader(pn.getNeedlePointer()));
        repairNeedle.positionToNextNeedle(position + needle.getRoundedTotalSize());
        return pn;
    } catch (Throwable th) {
        logger.error("Error reading needle at " + repairNeedle.getFormattedNeedleFileNumber() + "/"
                + repairNeedle.getFormattedNeedleOffset(), th);
        throw new NeedleManagerException();
    } finally {
        if (needleBuffer != null) {
            try {
                threadBufferQ.put(needleBuffer);
            } catch (InterruptedException ie) {
                throw new BucketTableManagerException("Error giving back needle read thread", ie);
            }
        }
    }
}