View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package org.tikv.common.codec;
19  
20  import com.google.protobuf.ByteString;
21  import javax.annotation.Nonnull;
22  import org.tikv.common.exception.CodecException;
23  
24  public class CodecDataInputLittleEndian extends CodecDataInput {
25  
26    public CodecDataInputLittleEndian(ByteString data) {
27      super(data);
28    }
29  
30    public CodecDataInputLittleEndian(byte[] buf) {
31      super(buf);
32    }
33  
34    @Override
35    public short readShort() {
36      int ch1 = readUnsignedByte();
37      int ch2 = readUnsignedByte();
38      return (short) ((ch1) + (ch2 << 8));
39    }
40  
41    @Override
42    public int readUnsignedShort() {
43      int ch1 = readUnsignedByte();
44      int ch2 = readUnsignedByte();
45      return (ch1) + (ch2 << 8);
46    }
47  
48    @Override
49    public char readChar() {
50      int ch1 = readUnsignedByte();
51      int ch2 = readUnsignedByte();
52      return (char) ((ch1) + (ch2 << 8));
53    }
54  
55    @Override
56    public int readInt() {
57      int ch1 = readUnsignedByte();
58      int ch2 = readUnsignedByte();
59      int ch3 = readUnsignedByte();
60      int ch4 = readUnsignedByte();
61      return ((ch1) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24));
62    }
63  
64    @Override
65    public long readLong() {
66      byte[] readBuffer = new byte[8];
67      readFully(readBuffer, 0, 8);
68      return ((readBuffer[0] & 0xff)
69          + ((readBuffer[1] & 0xff) << 8)
70          + ((readBuffer[2] & 0xff) << 16)
71          + ((long) (readBuffer[3] & 0xff) << 24)
72          + ((long) (readBuffer[4] & 0xff) << 32)
73          + ((long) (readBuffer[5] & 0xff) << 40)
74          + ((long) (readBuffer[6] & 0xff) << 48)
75          + ((long) (readBuffer[7] & 0xff) << 56));
76    }
77  
78    @Override
79    public float readFloat() {
80      return Float.intBitsToFloat(readInt());
81    }
82  
83    @Override
84    public double readDouble() {
85      return Double.longBitsToDouble(readLong());
86    }
87  
88    @Override
89    public String readLine() {
90      throw new CodecException("unimplemented");
91    }
92  
93    @Override
94    @Nonnull
95    public String readUTF() {
96      throw new CodecException("unimplemented");
97    }
98  
99    public int peekByte() {
100     return super.peekByte();
101   }
102 
103   public int currentPos() {
104     return super.currentPos();
105   }
106 
107   public void mark(int givenPos) {
108     super.mark(givenPos);
109   }
110 
111   public void reset() {
112     super.reset();
113   }
114 
115   public boolean eof() {
116     return super.eof();
117   }
118 
119   public int size() {
120     return super.size();
121   }
122 
123   public int available() {
124     return super.available();
125   }
126 
127   public byte[] toByteArray() {
128     return super.toByteArray();
129   }
130 }