src/java.base/share/classes/java/util/zip/Checksum.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 


  28 /**
  29  * An interface representing a data checksum.
  30  *
  31  * @author      David Connelly
  32  */
  33 public
  34 interface Checksum {
  35     /**
  36      * Updates the current checksum with the specified byte.
  37      *
  38      * @param b the byte to update the checksum with
  39      */
  40     public void update(int b);
  41 
  42     /**
  43      * Updates the current checksum with the specified array of bytes.


















  44      * @param b the byte array to update the checksum with
  45      * @param off the start offset of the data
  46      * @param len the number of bytes to use for the update
  47      */
  48     public void update(byte[] b, int off, int len);
  49 
  50     /**
















































  51      * Returns the current checksum value.

  52      * @return the current checksum value
  53      */
  54     public long getValue();
  55 
  56     /**
  57      * Resets the checksum to its initial value.
  58      */
  59     public void reset();
  60 }
   1 /*
   2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */

  25 package java.util.zip;
  26 
  27 import java.nio.ByteBuffer;
  28 
  29 /**
  30  * An interface representing a data checksum.
  31  *
  32  * @author David Connelly
  33  */
  34 public interface Checksum {
  35 
  36     /**
  37      * Updates the current checksum with the specified byte.
  38      *
  39      * @param b the byte to update the checksum with
  40      */
  41     public void update(int b);
  42 
  43     /**
  44      * Updates the current checksum with the specified array of bytes.
  45      *
  46      * @implSpec This default implementation is equal to calling
  47      * {@code update(b, 0, b.length)}.
  48      *
  49      * @param b the array of bytes to update the checksum with
  50      * 
  51      * @throws NullPointerException
  52      *         if {@code b} is {@code null}
  53      * 
  54      * @since 1.9
  55      */
  56     default public void update(byte[] b) {
  57         update(b, 0, b.length);
  58     }
  59 
  60     /**
  61      * Updates the current checksum with the specified array of bytes.
  62      *
  63      * @param b the byte array to update the checksum with
  64      * @param off the start offset of the data
  65      * @param len the number of bytes to use for the update
  66      */
  67     public void update(byte[] b, int off, int len);
  68 
  69     /**
  70      * Updates the current checksum with the bytes from the specified buffer.
  71      *
  72      * The checksum is updated with the remaining bytes in the buffer, starting
  73      * at the buffer's position. Upon return, the buffer's position will be
  74      * updated to its limit; its limit will not have been changed.
  75      *
  76      * @apiNote For best performance with DirectByteBuffer and other ByteBuffer
  77      * implementations without a backing array implementers of this interface
  78      * should override this method.
  79      *
  80      * @implSpec The default implementation has the following behavior.<br>
  81      * For ByteBuffers backed by an accessible byte array.      <pre>{@code
  82      * update(buffer.array(),
  83      *        buffer.position() + buffer.arrayOffset(),
  84      *        buffer.limit() - buffer.position());
  85      * }</pre> For ByteBuffers not backed by an accessible byte array.
  86      * <pre>{@code
  87      * byte[] b = new byte[buffer.limit() - buffer.position()];
  88      * buffer.get(b);
  89      * update(b, 0, b.length);
  90      * }</pre>
  91      *
  92      * @param buffer the ByteBuffer to update the checksum with
  93      * 
  94      * @throws NullPointerException
  95      *         if {@code buffer} is {@code null}
  96      * 
  97      * @since 1.9
  98      */
  99     default public void update(ByteBuffer buffer) {
 100         int pos = buffer.position();
 101         int limit = buffer.limit();
 102         assert (pos <= limit);
 103         int rem = limit - pos;
 104         if (rem <= 0) {
 105             return;
 106         }
 107         if (buffer.hasArray()) {
 108             update(buffer.array(), pos + buffer.arrayOffset(), rem);
 109         } else {
 110             byte[] b = new byte[rem];
 111             buffer.get(b);
 112             update(b, 0, b.length);
 113         }
 114         buffer.position(limit);
 115     }
 116 
 117     /**
 118      * Returns the current checksum value.
 119      *
 120      * @return the current checksum value
 121      */
 122     public long getValue();
 123 
 124     /**
 125      * Resets the checksum to its initial value.
 126      */
 127     public void reset();
 128 }