From 01256308eb2e1b5e0ae2c1f80e0ea7947752c516 Mon Sep 17 00:00:00 2001 From: leon Date: Fri, 10 Feb 2017 01:19:43 +0800 Subject: [PATCH] 2.0.0 release --- .../redis/replicator/util/ByteArray.java | 24 ++++++++--- .../redis/replicator/util/ByteArrayTest.java | 43 +++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/moilioncircle/redis/replicator/util/ByteArrayTest.java diff --git a/src/main/java/com/moilioncircle/redis/replicator/util/ByteArray.java b/src/main/java/com/moilioncircle/redis/replicator/util/ByteArray.java index 65029444..af3c0039 100644 --- a/src/main/java/com/moilioncircle/redis/replicator/util/ByteArray.java +++ b/src/main/java/com/moilioncircle/redis/replicator/util/ByteArray.java @@ -29,20 +29,31 @@ public class ByteArray implements Iterable { 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); @@ -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; } @@ -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]; @@ -80,7 +91,8 @@ public long length() { } public byte[] first() { - return this.iterator().next(); + Iterator it = this.iterator(); + return it.hasNext() ? it.next() : null; } @Override @@ -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; } diff --git a/src/test/java/com/moilioncircle/redis/replicator/util/ByteArrayTest.java b/src/test/java/com/moilioncircle/redis/replicator/util/ByteArrayTest.java new file mode 100644 index 00000000..1420b450 --- /dev/null +++ b/src/test/java/com/moilioncircle/redis/replicator/util/ByteArrayTest.java @@ -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())); + } + +} \ No newline at end of file