Package java.util.zip

Examples of java.util.zip.Adler32


        Location nextBatch = null;
        while (true) {
            if (isChecksum()) {
                ByteBuffer currentBatchBuffer = ByteBuffer.wrap(accessor.readLocation(currentBatch, false));
                long expectedChecksum = currentBatchBuffer.getLong();
                Checksum actualChecksum = new Adler32();
                Location nextLocation = goToNextLocation(currentBatch, Location.ANY_RECORD_TYPE, true);
                while (nextLocation != null && nextLocation.getType() != Location.BATCH_CONTROL_RECORD_TYPE) {
                    byte data[] = accessor.readLocation(nextLocation, false);
                    actualChecksum.update(data, 0, data.length);
                    nextLocation = goToNextLocation(nextLocation, Location.ANY_RECORD_TYPE, true);
                }
                if (expectedChecksum != actualChecksum.getValue()) {
                    throw new IOException("Bad checksum for location: " + currentBatch);
                }
                nextBatch = nextLocation;
            } else {
                nextBatch = goToNextLocation(currentBatch, Location.BATCH_CONTROL_RECORD_TYPE, true);
View Full Code Here


            writes.offer(writeRecord);
        }

        void perform(RandomAccessFile file, boolean checksum, boolean physicalSync, ReplicationTarget replicationTarget) throws IOException {
            ByteBuffer buffer = ByteBuffer.allocate(size);
            Checksum adler32 = new Adler32();
            WriteCommand control = writes.peek();

            // Write an empty batch control record.
            buffer.putInt(control.location.getPointer());
            buffer.putInt(BATCH_CONTROL_RECORD_SIZE);
            buffer.put(Location.BATCH_CONTROL_RECORD_TYPE);
            buffer.putInt(0);
            buffer.putLong(0);

            Iterator<WriteCommand> commands = writes.iterator();
            // Skip the control write:
            commands.next();
            // Process others:
            while (commands.hasNext()) {
                WriteCommand current = commands.next();
                buffer.putInt(current.location.getPointer());
                buffer.putInt(current.location.getSize());
                buffer.put(current.location.getType());
                buffer.put(current.getData());
                if (checksum) {
                    adler32.update(current.getData(), 0, current.getData().length);
                }
            }

            // Now we can fill in the batch control record properly.
            buffer.position(Journal.HEADER_SIZE);
            buffer.putInt(size - Journal.BATCH_CONTROL_RECORD_SIZE);
            if (checksum) {
                buffer.putLong(adler32.getValue());
            }

            // Now do the 1 big write.
            file.seek(offset);
            file.write(buffer.array(), 0, size);
View Full Code Here

    }
   
    public void run(String snapshotFileName) throws IOException {
        InputStream is = new CheckedInputStream(
                new BufferedInputStream(new FileInputStream(snapshotFileName)),
                new Adler32());
        InputArchive ia = BinaryInputArchive.getArchive(is);
       
        FileSnap fileSnap = new FileSnap(null);

        DataTree dataTree = new DataTree();
View Full Code Here

    /**
     * creates a checksum alogrithm to be used
     * @return the checksum used for this txnlog
     */
    protected Checksum makeChecksumAlgorithm(){
        return new Adler32();
    }
View Full Code Here

        /**
         * create a checksum algorithm
         * @return the checksum algorithm
         */
        protected Checksum makeChecksumAlgorithm(){
            return new Adler32();
        }
View Full Code Here

{
    public static long getChecksum(File[] files)
    {
        CheckedInputStream cis = null;
        FileInputStream is = null;
        Checksum checksum = new Adler32();
        byte[] tempBuf = new byte[128];
       
        for ( int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++ )
        {
            try
            {
                is = new FileInputStream(files[i]);
                cis = new CheckedInputStream(is, checksum);
                while (cis.read(tempBuf) >= 0) {}               
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
            finally
            {
                if (cis != null)
                {
                    try
                    {
                        cis.close();
                    }
                    catch (IOException ioe) {}
                    cis = null;
                }
                if (is != null)
                {
                    try
                    {
                        is.close();
                    }
                    catch (IOException ioe) {}
                    is = null;
                }
            }
        }
        return checksum.getValue();
    }
View Full Code Here

    {
        CheckedInputStream cis = null;       
        long checksum = 0;
        try
        {
            cis = new CheckedInputStream(is, new Adler32());
            byte[] tempBuf = new byte[128];
            while (cis.read(tempBuf) >= 0)
            {
            }
            checksum = cis.getChecksum().getValue();
View Full Code Here

{
    public static long getChecksum(File[] files)
    {
        CheckedInputStream cis = null;
        FileInputStream is = null;
        Checksum checksum = new Adler32();
        byte[] tempBuf = new byte[128];
       
        for ( int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++ )
        {
            try
            {
                is = new FileInputStream(files[i]);
                cis = new CheckedInputStream(is, checksum);
                while (cis.read(tempBuf) >= 0) {}               
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
            finally
            {
                if (cis != null)
                {
                    try
                    {
                        cis.close();
                    }
                    catch (IOException ioe) {}
                    cis = null;
                }
                if (is != null)
                {
                    try
                    {
                        is.close();
                    }
                    catch (IOException ioe) {}
                    is = null;
                }
            }
        }
        return checksum.getValue();
    }
View Full Code Here

        int blkId = (block==null) ? NoId : block.getId().intValue() ;
        header.putInt(blkId) ;
        header.flip() ;
        channel.write(header) ;
       
        Adler32 adler = new Adler32() ;
        adler.update(header.array()) ;

        if ( len > 0 )
        {
            // Make buffer include it's full length.
            // [TxDEV:TODO] This is the full buffer, junk and all.
            // This makes the system able to check block sizes (BlockAccess checking).
           
            int bufferLimit = buffer.limit() ;
            int bufferPosition = buffer.position() ;
            buffer.position(0) ;
            buffer.limit(bufferCapacity) ;
            // Clear top.
            for ( int i = len ; i < bufferCapacity ; i++ )
                buffer.put(i, (byte)0) ;
           
            // Write all bytes
            channel.write(buffer) ;
            adler.update(buffer.array()) ;
           
            buffer.position(bufferPosition) ;
            buffer.limit(bufferLimit) ;
        }

        // checksum
        crcTrailer.clear() ;
        Bytes.setInt((int)adler.getValue(), crcTrailer.array()) ;
        channel.write(crcTrailer) ;

        position += Overhead + len + SizeofCRC ; // header + payload + checksum
        return posn ;
    }
View Full Code Here

        int typeId  = header.getInt() ;
        int len     = header.getInt() ;
        int ref     = header.getInt() ;
        int blockId = header.getInt() ;
       
        Adler32 adler = new Adler32() ;
        adler.update(header.array()) ;

        JournalEntryType type = JournalEntryType.type(typeId) ;
        FileRef fileRef = FileRef.get(ref) ;
        ByteBuffer bb = ByteBuffer.allocate(len) ;
        lenRead = channel.read(bb) ;
        adler.update(bb.array()) ;
        bb.rewind() ;
        // checksum
        crcTrailer.clear() ;
        lenRead = channel.read(crcTrailer) ;
        if ( lenRead != SizeofCRC )
            throw new TDBTransactionException("Failed to read block checksum (got "+lenRead+" bytes, not "+SizeofCRC+").") ;
        int checksum = Bytes.getInt(crcTrailer.array()) ;
        if ( checksum != (int)adler.getValue() )
          throw new TDBTransactionException("Checksum error reading from the Journal.") ;

        Block block = new Block(blockId, bb) ;
        return new JournalEntry(type, fileRef, block) ;
    }
View Full Code Here

TOP

Related Classes of java.util.zip.Adler32

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.