View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.compression;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.util.ByteProcessor;
20  import io.netty.util.internal.ObjectUtil;
21  
22  import java.nio.ByteBuffer;
23  import java.util.zip.Adler32;
24  import java.util.zip.CRC32;
25  import java.util.zip.Checksum;
26  
27  /**
28   * {@link Checksum} implementation which can directly act on a {@link ByteBuf}.
29   * <p>
30   * Implementations may optimize access patterns depending on if the {@link ByteBuf} is backed by a
31   * byte array ({@link ByteBuf#hasArray()} is {@code true}) or not.
32   */
33  abstract class ByteBufChecksum implements Checksum {
34      private final ByteProcessor updateProcessor = new ByteProcessor() {
35          @Override
36          public boolean process(byte value) throws Exception {
37              update(value);
38              return true;
39          }
40      };
41  
42      static ByteBufChecksum wrapChecksum(Checksum checksum) {
43          ObjectUtil.checkNotNull(checksum, "checksum");
44          if (checksum instanceof ByteBufChecksum) {
45              return (ByteBufChecksum) checksum;
46          }
47          return new JdkByteBufChecksum(checksum);
48      }
49  
50      /**
51       * @see #update(byte[], int, int)
52       */
53      public void update(ByteBuf b, int off, int len) {
54          if (b.hasArray()) {
55              update(b.array(), b.arrayOffset() + off, len);
56          } else {
57              b.forEachByte(off, len, updateProcessor);
58          }
59      }
60  
61      private static class JdkByteBufChecksum extends ByteBufChecksum {
62          protected final Checksum checksum;
63  
64          JdkByteBufChecksum(Checksum checksum) {
65              this.checksum = checksum;
66          }
67  
68          @Override
69          public void update(int b) {
70              checksum.update(b);
71          }
72  
73          @Override
74          public void update(ByteBuf b, int off, int len) {
75              if (b.hasArray()) {
76                  update(b.array(), b.arrayOffset() + off, len);
77              } else if (checksum instanceof CRC32) {
78                  ByteBuffer byteBuffer = CompressionUtil.safeNioBuffer(b, off, len);
79                  ((CRC32) checksum).update(byteBuffer);
80              } else if (checksum instanceof Adler32) {
81                  ByteBuffer byteBuffer = CompressionUtil.safeNioBuffer(b, off, len);
82                  ((Adler32) checksum).update(byteBuffer);
83              } else {
84                  super.update(b, off, len);
85              }
86          }
87  
88          @Override
89          public void update(byte[] b, int off, int len) {
90              checksum.update(b, off, len);
91          }
92  
93          @Override
94          public long getValue() {
95              return checksum.getValue();
96          }
97  
98          @Override
99          public void reset() {
100             checksum.reset();
101         }
102     }
103 }