View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.redis;
17  
18  import io.netty.util.CharsetUtil;
19  import io.netty.util.internal.PlatformDependent;
20  
21  /**
22   * Utilities for codec-redis.
23   */
24  final class RedisCodecUtil {
25  
26      private RedisCodecUtil() {
27      }
28  
29      static byte[] longToAsciiBytes(long value) {
30          return Long.toString(value).getBytes(CharsetUtil.US_ASCII);
31      }
32  
33      /**
34       * Returns a {@code short} value using endian order.
35       */
36      static short makeShort(char first, char second) {
37          return PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ?
38                  (short) ((second << 8) | first) : (short) ((first << 8) | second);
39      }
40  
41      /**
42       * Returns a {@code byte[]} of {@code short} value. This is opposite of {@code makeShort()}.
43       */
44      static byte[] shortToBytes(short value) {
45          byte[] bytes = new byte[2];
46          if (PlatformDependent.BIG_ENDIAN_NATIVE_ORDER) {
47              bytes[1] = (byte) ((value >> 8) & 0xff);
48              bytes[0] = (byte) (value & 0xff);
49          } else {
50              bytes[0] = (byte) ((value >> 8) & 0xff);
51              bytes[1] = (byte) (value & 0xff);
52          }
53          return bytes;
54      }
55  }