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.
82 * <pre>{@code
83 * update(buffer.array(),
84 * buffer.position() + buffer.arrayOffset(),
85 * buffer.remaining());
86 * }</pre>
87 * For ByteBuffers not backed by an accessible byte array.
88 * <pre>{@code
89 * byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
90 * while (buffer.hasRemaining()) {
91 * int length = Math.min(buffer.remaining(), b.length);
92 * buffer.get(b, 0, length);
93 * update(b, 0, length);
94 * }
95 * }</pre>
96 *
97 * @param buffer the ByteBuffer to update the checksum with
98 *
99 * @throws NullPointerException
100 * if {@code buffer} is {@code null}
101 *
102 * @since 1.9
103 */
104 default public void update(ByteBuffer buffer) {
105 int pos = buffer.position();
106 int limit = buffer.limit();
107 assert (pos <= limit);
108 int rem = limit - pos;
109 if (rem <= 0) {
110 return;
111 }
112 if (buffer.hasArray()) {
113 update(buffer.array(), pos + buffer.arrayOffset(), rem);
114 } else {
115 byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
116 while (buffer.hasRemaining()) {
117 int length = Math.min(buffer.remaining(), b.length);
118 buffer.get(b, 0, length);
119 update(b, 0, length);
120 }
121 }
122 buffer.position(limit);
|
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 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.
82 * <pre>{@code
83 * update(buffer.array(),
84 * buffer.position() + buffer.arrayOffset(),
85 * buffer.remaining());
86 * }</pre>
87 * For ByteBuffers not backed by an accessible byte array.
88 * <pre>{@code
89 * byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
90 * while (buffer.hasRemaining()) {
91 * int length = Math.min(buffer.remaining(), b.length);
92 * buffer.get(b, 0, length);
93 * update(b, 0, length);
94 * }
95 * }</pre>
96 *
97 * @param buffer the ByteBuffer to update the checksum with
98 *
99 * @throws NullPointerException
100 * if {@code buffer} is {@code null}
101 *
102 * @since 9
103 */
104 default public void update(ByteBuffer buffer) {
105 int pos = buffer.position();
106 int limit = buffer.limit();
107 assert (pos <= limit);
108 int rem = limit - pos;
109 if (rem <= 0) {
110 return;
111 }
112 if (buffer.hasArray()) {
113 update(buffer.array(), pos + buffer.arrayOffset(), rem);
114 } else {
115 byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
116 while (buffer.hasRemaining()) {
117 int length = Math.min(buffer.remaining(), b.length);
118 buffer.get(b, 0, length);
119 update(b, 0, length);
120 }
121 }
122 buffer.position(limit);
|