Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / buffer / SwappedBytes.java
1 /*
2  * Copyright 2015-present Open Networking Foundation
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 package io.atomix.storage.buffer;
17
18 import java.nio.ByteOrder;
19
20 /**
21  * Bytes in swapped order.
22  *
23  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
24  */
25 public class SwappedBytes extends WrappedBytes {
26
27   public SwappedBytes(Bytes bytes) {
28     super(bytes);
29   }
30
31   @Override
32   public ByteOrder order() {
33     return bytes.order() == ByteOrder.BIG_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
34   }
35
36   @Override
37   public char readChar(int offset) {
38     return Character.reverseBytes(bytes.readChar(offset));
39   }
40
41   @Override
42   public short readShort(int offset) {
43     return Short.reverseBytes(bytes.readShort(offset));
44   }
45
46   @Override
47   public int readUnsignedShort(int offset) {
48     return Short.reverseBytes(bytes.readShort(offset)) & 0xFFFF;
49   }
50
51   @Override
52   public int readMedium(int offset) {
53     return Integer.reverseBytes(bytes.readMedium(offset));
54   }
55
56   @Override
57   public int readUnsignedMedium(int offset) {
58     return Integer.reverseBytes(bytes.readUnsignedMedium(offset));
59   }
60
61   @Override
62   public int readInt(int offset) {
63     return Integer.reverseBytes(bytes.readInt(offset));
64   }
65
66   @Override
67   public long readUnsignedInt(int offset) {
68     return Integer.reverseBytes(bytes.readInt(offset)) & 0xFFFFFFFFL;
69   }
70
71   @Override
72   public long readLong(int offset) {
73     return Long.reverseBytes(bytes.readLong(offset));
74   }
75
76   @Override
77   public float readFloat(int offset) {
78     return Float.intBitsToFloat(readInt(offset));
79   }
80
81   @Override
82   public double readDouble(int offset) {
83     return Double.longBitsToDouble(readLong(offset));
84   }
85
86   @Override
87   public Bytes writeChar(int offset, char c) {
88     bytes.writeChar(offset, Character.reverseBytes(c));
89     return this;
90   }
91
92   @Override
93   public Bytes writeShort(int offset, short s) {
94     bytes.writeShort(offset, Short.reverseBytes(s));
95     return this;
96   }
97
98   @Override
99   public Bytes writeUnsignedShort(int offset, int s) {
100     bytes.writeUnsignedShort(offset, Short.reverseBytes((short) s));
101     return this;
102   }
103
104   @Override
105   public Bytes writeMedium(int offset, int m) {
106     bytes.writeMedium(offset, Integer.reverseBytes(m));
107     return this;
108   }
109
110   @Override
111   public Bytes writeUnsignedMedium(int offset, int m) {
112     bytes.writeUnsignedMedium(offset, Integer.reverseBytes(m));
113     return this;
114   }
115
116   @Override
117   public Bytes writeInt(int offset, int i) {
118     bytes.writeInt(offset, Integer.reverseBytes(i));
119     return this;
120   }
121
122   @Override
123   public Bytes writeUnsignedInt(int offset, long i) {
124     bytes.writeUnsignedInt(offset, Integer.reverseBytes((int) i));
125     return this;
126   }
127
128   @Override
129   public Bytes writeLong(int offset, long l) {
130     bytes.writeLong(offset, Long.reverseBytes(l));
131     return this;
132   }
133
134   @Override
135   public Bytes writeFloat(int offset, float f) {
136     return writeInt(offset, Float.floatToRawIntBits(f));
137   }
138
139   @Override
140   public Bytes writeDouble(int offset, double d) {
141     return writeLong(offset, Double.doubleToRawLongBits(d));
142   }
143
144 }