2 * Copyright 2015-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.storage.buffer;
18 import java.nio.ByteOrder;
21 * Bytes in swapped order.
23 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
25 public class SwappedBytes extends WrappedBytes {
27 public SwappedBytes(Bytes bytes) {
32 public ByteOrder order() {
33 return bytes.order() == ByteOrder.BIG_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
37 public char readChar(int offset) {
38 return Character.reverseBytes(bytes.readChar(offset));
42 public short readShort(int offset) {
43 return Short.reverseBytes(bytes.readShort(offset));
47 public int readUnsignedShort(int offset) {
48 return Short.reverseBytes(bytes.readShort(offset)) & 0xFFFF;
52 public int readMedium(int offset) {
53 return Integer.reverseBytes(bytes.readMedium(offset));
57 public int readUnsignedMedium(int offset) {
58 return Integer.reverseBytes(bytes.readUnsignedMedium(offset));
62 public int readInt(int offset) {
63 return Integer.reverseBytes(bytes.readInt(offset));
67 public long readUnsignedInt(int offset) {
68 return Integer.reverseBytes(bytes.readInt(offset)) & 0xFFFFFFFFL;
72 public long readLong(int offset) {
73 return Long.reverseBytes(bytes.readLong(offset));
77 public float readFloat(int offset) {
78 return Float.intBitsToFloat(readInt(offset));
82 public double readDouble(int offset) {
83 return Double.longBitsToDouble(readLong(offset));
87 public Bytes writeChar(int offset, char c) {
88 bytes.writeChar(offset, Character.reverseBytes(c));
93 public Bytes writeShort(int offset, short s) {
94 bytes.writeShort(offset, Short.reverseBytes(s));
99 public Bytes writeUnsignedShort(int offset, int s) {
100 bytes.writeUnsignedShort(offset, Short.reverseBytes((short) s));
105 public Bytes writeMedium(int offset, int m) {
106 bytes.writeMedium(offset, Integer.reverseBytes(m));
111 public Bytes writeUnsignedMedium(int offset, int m) {
112 bytes.writeUnsignedMedium(offset, Integer.reverseBytes(m));
117 public Bytes writeInt(int offset, int i) {
118 bytes.writeInt(offset, Integer.reverseBytes(i));
123 public Bytes writeUnsignedInt(int offset, long i) {
124 bytes.writeUnsignedInt(offset, Integer.reverseBytes((int) i));
129 public Bytes writeLong(int offset, long l) {
130 bytes.writeLong(offset, Long.reverseBytes(l));
135 public Bytes writeFloat(int offset, float f) {
136 return writeInt(offset, Float.floatToRawIntBits(f));
140 public Bytes writeDouble(int offset, double d) {
141 return writeLong(offset, Double.doubleToRawLongBits(d));