提交 7247db2b 编写于 作者: H huanghaiquan

Merge branch 'master' into develop

* master:
  optimized ByteSequence;
package utils;
import java.io.OutputStream;
/**
* A readable sequence of byte value;
*
......@@ -25,6 +23,22 @@ public interface ByteSequence {
*/
byte byteAt(int index);
/**
* 比较当前字节与指定的字节数组是否相等;
* @param data
*/
default boolean equal(byte[] data) {
if (data.length != size()) {
return false;
}
for (int i = 0; i < data.length; i++) {
if (data[i] != byteAt(i)) {
return false;
}
}
return true;
}
/**
* Returns a {@link ByteSequence} that is a subsequence of this sequence.
*
......
......@@ -422,6 +422,25 @@ public class Bytes implements ByteSequence, BytesSerializable, Serializable {
public byte byteAt(int index) {
return read(index);
}
@Override
public boolean equal(byte[] data) {
if (data.length != size()) {
return false;
}
int i = 0;
for (; i < prefixSize; i++) {
if (prefix.read(i) != data[i]) {
return false;
}
}
for (int j = 0; j < bytes.length; i++, j++) {
if (bytes[j] != data[i]) {
return false;
}
}
return true;
}
@Override
public ByteSequence subSequence(int start, int end) {
......
......@@ -327,6 +327,11 @@ public class ByteArray implements Externalizable, ByteSequence {
public byte byteAt(int index) {
return bytes[index];
}
@Override
public boolean equal(byte[] data) {
return Arrays.equals(bytes, data);
}
@Override
public ByteSequence subSequence(int start, int end) {
......
......@@ -229,6 +229,19 @@ public class BytesSlice implements ByteSequence, BytesSerializable {
public byte byteAt(int index) {
return getByte(index);
}
@Override
public boolean equal(byte[] data) {
if (data.length != size()) {
return false;
}
for (int i = 0; i < data.length; i++) {
if (data[i] != bytes[dataOffset + i]) {
return false;
}
}
return true;
}
@Override
public ByteSequence subSequence(int start, int end) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册