forked from sofastack/sofa-jraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9611aa7
commit 11e9406
Showing
3 changed files
with
162 additions
and
64 deletions.
There are no files selected for viewing
91 changes: 28 additions & 63 deletions
91
jraft-core/src/main/java/com/alipay/sofa/jraft/util/Bits.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,50 @@ | ||
/* | ||
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. | ||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alipay.sofa.jraft.util; | ||
|
||
/** | ||
* Moved from java.io.Bits | ||
* Bits util. | ||
* | ||
* @author boyan ([email protected]) | ||
* @author jiachun.fjc | ||
*/ | ||
public class Bits { | ||
public static int getInt(byte[] b, int off) { | ||
return (b[off + 3] & 0xFF) + ((b[off + 2] & 0xFF) << 8) + ((b[off + 1] & 0xFF) << 16) + (b[off] << 24); | ||
} | ||
|
||
public static float getFloat(byte[] b, int off) { | ||
return Float.intBitsToFloat(getInt(b, off)); | ||
} | ||
|
||
public static long getLong(byte[] b, int off) { | ||
return (b[off + 7] & 0xFFL) + ((b[off + 6] & 0xFFL) << 8) + ((b[off + 5] & 0xFFL) << 16) | ||
+ ((b[off + 4] & 0xFFL) << 24) + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40) | ||
+ ((b[off + 1] & 0xFFL) << 48) + ((long) b[off] << 56); | ||
} | ||
|
||
public static double getDouble(byte[] b, int off) { | ||
return Double.longBitsToDouble(getLong(b, off)); | ||
} | ||
|
||
public static void putInt(byte[] b, int off, int val) { | ||
b[off + 3] = (byte) val; | ||
b[off + 2] = (byte) (val >>> 8); | ||
b[off + 1] = (byte) (val >>> 16); | ||
b[off] = (byte) (val >>> 24); | ||
public static int getInt(final byte[] b, final int off) { | ||
return HeapByteBufUtil.getInt(b, off); | ||
} | ||
|
||
public static void putShort(byte[] b, int off, short val) { | ||
b[off + 1] = (byte) val; | ||
b[off] = (byte) (val >>> 8); | ||
public static long getLong(final byte[] b, final int off) { | ||
return HeapByteBufUtil.getLong(b, off); | ||
} | ||
|
||
public static short getShort(byte[] b, int off) { | ||
return (short) ((b[off + 1] & 0xFF) + (b[off] << 8)); | ||
public static void putInt(final byte[] b, final int off, final int val) { | ||
HeapByteBufUtil.setInt(b, off, val); | ||
} | ||
|
||
public static void putFloat(byte[] b, int off, float val) { | ||
putInt(b, off, Float.floatToIntBits(val)); | ||
public static void putShort(final byte[] b, final int off, final short val) { | ||
HeapByteBufUtil.setShort(b, off, val); | ||
} | ||
|
||
public static void putLong(byte[] b, int off, long val) { | ||
b[off + 7] = (byte) val; | ||
b[off + 6] = (byte) (val >>> 8); | ||
b[off + 5] = (byte) (val >>> 16); | ||
b[off + 4] = (byte) (val >>> 24); | ||
b[off + 3] = (byte) (val >>> 32); | ||
b[off + 2] = (byte) (val >>> 40); | ||
b[off + 1] = (byte) (val >>> 48); | ||
b[off] = (byte) (val >>> 56); | ||
public static short getShort(final byte[] b, final int off) { | ||
return HeapByteBufUtil.getShort(b, off); | ||
} | ||
|
||
public static void putDouble(byte[] b, int off, double val) { | ||
putLong(b, off, Double.doubleToLongBits(val)); | ||
public static void putLong(final byte[] b, final int off, final long val) { | ||
HeapByteBufUtil.setLong(b, off, val); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
jraft-core/src/main/java/com/alipay/sofa/jraft/util/HeapByteBufUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2015 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.alipay.sofa.jraft.util; | ||
|
||
/** | ||
* Utility class for heap buffers. | ||
* | ||
* Forked from <a href="https://github.com/netty/netty">Netty</a>. | ||
*/ | ||
final class HeapByteBufUtil { | ||
|
||
static byte getByte(byte[] memory, int index) { | ||
return memory[index]; | ||
} | ||
|
||
static short getShort(byte[] memory, int index) { | ||
return (short) (memory[index] << 8 | memory[index + 1] & 0xFF); | ||
} | ||
|
||
static short getShortLE(byte[] memory, int index) { | ||
return (short) (memory[index] & 0xff | memory[index + 1] << 8); | ||
} | ||
|
||
static int getUnsignedMedium(byte[] memory, int index) { | ||
return (memory[index] & 0xff) << 16 | (memory[index + 1] & 0xff) << 8 | memory[index + 2] & 0xff; | ||
} | ||
|
||
static int getUnsignedMediumLE(byte[] memory, int index) { | ||
return memory[index] & 0xff | (memory[index + 1] & 0xff) << 8 | (memory[index + 2] & 0xff) << 16; | ||
} | ||
|
||
static int getInt(byte[] memory, int index) { | ||
return (memory[index] & 0xff) << 24 | (memory[index + 1] & 0xff) << 16 | (memory[index + 2] & 0xff) << 8 | ||
| memory[index + 3] & 0xff; | ||
} | ||
|
||
static int getIntLE(byte[] memory, int index) { | ||
return memory[index] & 0xff | (memory[index + 1] & 0xff) << 8 | (memory[index + 2] & 0xff) << 16 | ||
| (memory[index + 3] & 0xff) << 24; | ||
} | ||
|
||
static long getLong(byte[] memory, int index) { | ||
return ((long) memory[index] & 0xff) << 56 | ((long) memory[index + 1] & 0xff) << 48 | ||
| ((long) memory[index + 2] & 0xff) << 40 | ((long) memory[index + 3] & 0xff) << 32 | ||
| ((long) memory[index + 4] & 0xff) << 24 | ((long) memory[index + 5] & 0xff) << 16 | ||
| ((long) memory[index + 6] & 0xff) << 8 | (long) memory[index + 7] & 0xff; | ||
} | ||
|
||
static long getLongLE(byte[] memory, int index) { | ||
return (long) memory[index] & 0xff | ((long) memory[index + 1] & 0xff) << 8 | ||
| ((long) memory[index + 2] & 0xff) << 16 | ((long) memory[index + 3] & 0xff) << 24 | ||
| ((long) memory[index + 4] & 0xff) << 32 | ((long) memory[index + 5] & 0xff) << 40 | ||
| ((long) memory[index + 6] & 0xff) << 48 | ((long) memory[index + 7] & 0xff) << 56; | ||
} | ||
|
||
static void setByte(byte[] memory, int index, int value) { | ||
memory[index] = (byte) value; | ||
} | ||
|
||
static void setShort(byte[] memory, int index, int value) { | ||
memory[index] = (byte) (value >>> 8); | ||
memory[index + 1] = (byte) value; | ||
} | ||
|
||
static void setShortLE(byte[] memory, int index, int value) { | ||
memory[index] = (byte) value; | ||
memory[index + 1] = (byte) (value >>> 8); | ||
} | ||
|
||
static void setMedium(byte[] memory, int index, int value) { | ||
memory[index] = (byte) (value >>> 16); | ||
memory[index + 1] = (byte) (value >>> 8); | ||
memory[index + 2] = (byte) value; | ||
} | ||
|
||
static void setMediumLE(byte[] memory, int index, int value) { | ||
memory[index] = (byte) value; | ||
memory[index + 1] = (byte) (value >>> 8); | ||
memory[index + 2] = (byte) (value >>> 16); | ||
} | ||
|
||
static void setInt(byte[] memory, int index, int value) { | ||
memory[index] = (byte) (value >>> 24); | ||
memory[index + 1] = (byte) (value >>> 16); | ||
memory[index + 2] = (byte) (value >>> 8); | ||
memory[index + 3] = (byte) value; | ||
} | ||
|
||
static void setIntLE(byte[] memory, int index, int value) { | ||
memory[index] = (byte) value; | ||
memory[index + 1] = (byte) (value >>> 8); | ||
memory[index + 2] = (byte) (value >>> 16); | ||
memory[index + 3] = (byte) (value >>> 24); | ||
} | ||
|
||
static void setLong(byte[] memory, int index, long value) { | ||
memory[index] = (byte) (value >>> 56); | ||
memory[index + 1] = (byte) (value >>> 48); | ||
memory[index + 2] = (byte) (value >>> 40); | ||
memory[index + 3] = (byte) (value >>> 32); | ||
memory[index + 4] = (byte) (value >>> 24); | ||
memory[index + 5] = (byte) (value >>> 16); | ||
memory[index + 6] = (byte) (value >>> 8); | ||
memory[index + 7] = (byte) value; | ||
} | ||
|
||
static void setLongLE(byte[] memory, int index, long value) { | ||
memory[index] = (byte) value; | ||
memory[index + 1] = (byte) (value >>> 8); | ||
memory[index + 2] = (byte) (value >>> 16); | ||
memory[index + 3] = (byte) (value >>> 24); | ||
memory[index + 4] = (byte) (value >>> 32); | ||
memory[index + 5] = (byte) (value >>> 40); | ||
memory[index + 6] = (byte) (value >>> 48); | ||
memory[index + 7] = (byte) (value >>> 56); | ||
} | ||
|
||
private HeapByteBufUtil() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters