Package com.afewmoreamps.util

Examples of com.afewmoreamps.util.PrefetchingInputStream


    }

    static final int ENTRY_WITH_CHECKSUM_SIZE = HintCaskOutput.ENTRY_SIZE;

    boolean validateChecksum() throws IOException {
        PrefetchingInputStream pis = new PrefetchingInputStream(m_channel, 1024 * 1024 * 16, 3);
        try {
            ByteBuffer allCRCBytes = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
            int readAllCRCBytes = 0;
            while (readAllCRCBytes < 4) {
                final int readThisTime = pis.read(allCRCBytes.array(), readAllCRCBytes, 4 - readAllCRCBytes);
                if (readThisTime == -1) return false;
                readAllCRCBytes += readThisTime;
            }
            final int allCRCExpected = allCRCBytes.getInt();
            final CRC32 allCRC = new CRC32();
            ByteBuffer entryBuffer = ByteBuffer.allocate(ENTRY_WITH_CHECKSUM_SIZE).order(ByteOrder.nativeOrder());

            while (true) {
                entryBuffer.clear();
                int read = 0;

                while (read < ENTRY_WITH_CHECKSUM_SIZE) {
                    final int readThisTime = pis.read(entryBuffer.array(), read, entryBuffer.capacity() - read);
                    if (readThisTime == -1) break;
                    read += readThisTime;
                }

                if (read == 0 || read < ENTRY_WITH_CHECKSUM_SIZE) {
                    final int actualAllCRC = (int)allCRC.getValue();
                    if (actualAllCRC != allCRCExpected) {
                        m_channel.close();
                        return false;
                    }
                    return true;
                }
                final int expectedCRC = entryBuffer.getInt();
                final CRC32 crc = new CRC32();
                crc.update(entryBuffer.array(), 4, entryBuffer.capacity() - 4);
                final int actualCRC = (int)crc.getValue();

                if (expectedCRC != actualCRC) {
                    m_channel.close();
                    return false;
                }
                allCRC.update(entryBuffer.array(), 0, 4);
            }
        } finally {
            pis.close();
        }
    }
View Full Code Here


        }
    }

    Iterator<CaskEntry> hintIterator() throws IOException {
        m_channel.position(0);
        m_pis = new PrefetchingInputStream(m_channel, 1024 * 1024 * 16, 3);
        int read = 0;
        while (read < 4) {
            int readThisTime = m_pis.read(new byte[4 - read]);
            if (readThisTime == -1) {
                throw new IOException();
View Full Code Here

TOP

Related Classes of com.afewmoreamps.util.PrefetchingInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.