Skip to content

Commit

Permalink
2.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
leonchen83 committed Feb 9, 2017
1 parent 2f672ae commit 0125630
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,31 @@ public class ByteArray implements Iterable<byte[]> {
public static final long MIN_VALUE = 0L;
public static final long MAX_VALUE = 2305843007066210304L; //(Integer.MAX_VALUE - 1) * MAGIC

protected final int cap;
protected final long length;
protected byte[] smallBytes;
protected byte[][] largeBytes;

public ByteArray(byte[] smallBytes) {
this(smallBytes, Integer.MAX_VALUE);
}

public ByteArray(long length) {
this(length, Integer.MAX_VALUE);
}

public ByteArray(byte[] smallBytes, int cap) {
this.cap = cap;
this.length = smallBytes.length;
this.smallBytes = smallBytes;
}

public ByteArray(long length) {
public ByteArray(long length, int cap) {
this.cap = cap;
this.length = length;
if (length > MAX_VALUE || length < 0) {
throw new IllegalArgumentException(String.valueOf(length));
} else if (length <= Integer.MAX_VALUE) {
} else if (length <= cap) {
this.smallBytes = new byte[(int) length];
} else {
int x = (int) (length >> BITS);
Expand All @@ -59,7 +70,7 @@ public ByteArray(long length) {
}

public void set(long idx, byte value) {
if (idx < Integer.MAX_VALUE) {
if (smallBytes != null) {
smallBytes[(int) idx] = value;
return;
}
Expand All @@ -69,7 +80,7 @@ public void set(long idx, byte value) {
}

public byte get(long idx) {
if (idx < Integer.MAX_VALUE) return smallBytes[(int) idx];
if (smallBytes != null) return smallBytes[(int) idx];
int x = (int) (idx >> BITS);
int y = (int) (idx & (MAGIC - 1));
return largeBytes[x][y];
Expand All @@ -80,7 +91,8 @@ public long length() {
}

public byte[] first() {
return this.iterator().next();
Iterator<byte[]> it = this.iterator();
return it.hasNext() ? it.next() : null;
}

@Override
Expand All @@ -92,7 +104,7 @@ public static void arraycopy(ByteArray src, long srcPos, ByteArray dest, long de
if (srcPos + length > src.length || destPos + length > dest.length) {
throw new IndexOutOfBoundsException();
}
if (srcPos + length <= Integer.MAX_VALUE && destPos + length <= Integer.MAX_VALUE) {
if (srcPos + length <= src.cap && destPos + length <= dest.cap) {
System.arraycopy(src.smallBytes, (int) srcPos, dest.smallBytes, (int) destPos, (int) length);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.moilioncircle.redis.replicator.util;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by leon on 2/10/17.
*/
public class ByteArrayTest {
@Test
public void test() throws Exception {

String str = "sdajkl;jlqwjqejqweq89080c中jlxczksaouwq9823djadj";
ByteArray bytes = new ByteArray(str.getBytes().length, 10);
byte[] b1 = str.getBytes();
int i = 0;
for (byte b : b1) {
bytes.set(i, b);
assertEquals(b, bytes.get(i));
i++;
}
ByteArray bytes1 = new ByteArray(str.getBytes().length - 10, 10);
ByteArray.arraycopy(bytes, 10, bytes1, 0, bytes.length - 10);
assertEquals(str.substring(10), new String(bytes1.first()));

str = "sdajk";
ByteArray bytes2 = new ByteArray(str.getBytes().length, 10);
b1 = str.getBytes();
i = 0;
for (byte b : b1) {
bytes2.set(i, b);
assertEquals(b, bytes2.get(i));
i++;
}
assertEquals(new String(bytes2.first()), "sdajk");

ByteArray bytes3 = new ByteArray(bytes2.length() - 1, 10);
ByteArray.arraycopy(bytes2, 1, bytes3, 0, bytes2.length() - 1);
assertEquals(str.substring(1), new String(bytes3.first()));
}

}

0 comments on commit 0125630

Please sign in to comment.