Skip to content

Commit 57d7890

Browse files
Artem LabazinArtem Labazin
authored andcommitted
minor fixes
1 parent 4461dac commit 57d7890

File tree

7 files changed

+413
-55
lines changed

7 files changed

+413
-55
lines changed

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ limitations under the License.
2424

2525
<groupId>io.appulse</groupId>
2626
<artifactId>utils-java</artifactId>
27-
<version>1.12.0</version>
27+
<version>1.13.0</version>
2828
<packaging>jar</packaging>
2929

3030
<properties>
@@ -66,7 +66,7 @@ limitations under the License.
6666
<url>https://github.com/appulse-projects/utils-java</url>
6767
<connection>scm:git:https://github.com/appulse-projects/utils-java.git</connection>
6868
<developerConnection>scm:git:https://github.com/appulse-projects/utils-java.git</developerConnection>
69-
<tag>1.12.0</tag>
69+
<tag>1.13.0</tag>
7070
</scm>
7171

7272
<distributionManagement>
@@ -111,6 +111,7 @@ limitations under the License.
111111
<artifactId>netty-buffer</artifactId>
112112
<version>4.1.33.Final</version>
113113
<scope>compile</scope>
114+
<optional>true</optional>
114115
</dependency>
115116

116117
<dependency>

src/main/java/io/appulse/utils/Bytes.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.nio.ByteBuffer;
2121
import java.nio.charset.Charset;
2222

23-
import io.netty.buffer.ByteBuf;
24-
2523
/**
2624
* This interface provides an abstract view for a primitive byte
2725
* arrays ({@code byte[]}).
@@ -63,20 +61,6 @@ static Bytes wrap (ByteBuffer buffer) {
6361
return new BytesByteBuffer(buffer);
6462
}
6563

66-
/**
67-
* Wraps a {@link ByteBuf} buffer into a new {@link Bytes} object.
68-
* <p>
69-
* Modifications to this buffer's content will cause the wrapped
70-
* buffer's content to be modified, and vice versa.
71-
*
72-
* @param buffer the value to wrap
73-
*
74-
* @return the new {@link Bytes} instance
75-
*/
76-
static Bytes wrap (ByteBuf buffer) {
77-
return new BytesByteBuf(buffer);
78-
}
79-
8064
/**
8165
* Copies a byte array into a new {@link Bytes} object.
8266
* <p>
@@ -106,35 +90,34 @@ static Bytes copy (ByteBuffer buffer) {
10690
}
10791

10892
/**
109-
* Copies a {@link ByteBuf} buffer into a new {@link Bytes} object.
110-
* <p>
111-
* Modifications to this buffer's content will not cause the wrapped
112-
* buffer's content to be modified, and vice versa.
113-
*
114-
* @param buffer the value to copy
93+
* Create a new {@link Bytes} instance with a fixed size content
11594
*
11695
* @return the new {@link Bytes} instance
11796
*/
118-
static Bytes copy (ByteBuf buffer) {
119-
return BytesByteBuf.copy(buffer);
97+
static Bytes allocate (int size) {
98+
return new BytesFixedArray(size);
12099
}
121100

122101
/**
123-
* Create a new {@link Bytes} instance with a fixed size content
102+
* Creates a new {@link Bytes} instance with a resizable content.
124103
*
125104
* @return the new {@link Bytes} instance
126105
*/
127-
static Bytes allocate (int size) {
128-
return new BytesFixedArray(size);
106+
static Bytes resizableArray () {
107+
return new BytesExtendableArray();
129108
}
130109

131110
/**
132111
* Creates a new {@link Bytes} instance with a resizable content.
133112
*
113+
* @param initialSize the initial buffer's size
114+
*
134115
* @return the new {@link Bytes} instance
116+
*
117+
* @since 1.13.0
135118
*/
136-
static Bytes resizableArray () {
137-
return new BytesExtendableArray();
119+
static Bytes resizableArray (int initialSize) {
120+
return new BytesExtendableArray(initialSize);
138121
}
139122

140123
/**
@@ -1773,4 +1756,15 @@ static Bytes resizableArray () {
17731756
* @return the array that backs this buffer
17741757
*/
17751758
byte[] array ();
1759+
1760+
/**
1761+
* Returns the copy of range the buffer's back byte array from
1762+
* <b>0</b> till <b>writeIndex</t>.
1763+
* <p>
1764+
* Modifications to this buffer's content may NOT cause the
1765+
* returned array's content to be modified, and vice versa.
1766+
*
1767+
* @return the copy of the array that backs this buffer
1768+
*/
1769+
byte[] arrayCopy ();
17761770
}

src/main/java/io/appulse/utils/BytesAbstract.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.math.BigInteger;
2222
import java.nio.charset.Charset;
23+
import java.util.Arrays;
2324

2425
import lombok.NonNull;
2526
import lombok.val;
@@ -473,6 +474,11 @@ public Bytes reset () {
473474
return this;
474475
}
475476

477+
@Override
478+
public byte[] arrayCopy () {
479+
return Arrays.copyOfRange(array(), 0, writerIndex());
480+
}
481+
476482
protected void checkWriteBounds (int index, int length) {
477483
if (index < readerIndex() || index + length > capacity()) {
478484
val msg = String.format("Writer index error. index(%d) < readerIndex(%d) || index(%d)+length(%d) > capacity(%d)",

src/main/java/io/appulse/utils/BytesExtendableArray.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,20 @@
2222

2323
class BytesExtendableArray extends BytesFixedArray {
2424

25+
/**
26+
* The maximum size of array to allocate.
27+
* Some VMs reserve some header words in an array.
28+
* Attempts to allocate larger arrays may result in
29+
* OutOfMemoryError: Requested array size exceeds VM limit
30+
*/
31+
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
32+
2533
BytesExtendableArray () {
26-
super(16);
34+
this(16);
35+
}
36+
37+
BytesExtendableArray (int initialSize) {
38+
super(initialSize);
2739
}
2840

2941
@Override
@@ -36,31 +48,31 @@ public Bytes writerIndex (int newIndex) {
3648
if (newIndex < readerIndex()) {
3749
throw new IndexOutOfBoundsException();
3850
}
39-
while (newIndex > capacity()) {
40-
extendBuffer();
41-
}
51+
checkWriteBounds(newIndex, 1);
4252
writerIndex = newIndex;
4353
return this;
4454
}
4555

46-
@Override
47-
public byte[] array () {
48-
return Arrays.copyOfRange(buffer, 0, writerIndex);
49-
}
50-
5156
@Override
5257
protected void checkWriteBounds (int index, int length) {
53-
if (index < readerIndex()) {
54-
throw new IndexOutOfBoundsException();
55-
}
56-
while (index + length > capacity()) {
57-
extendBuffer();
58+
// if (index < readerIndex()) {
59+
// throw new IndexOutOfBoundsException();
60+
// }
61+
val currentCapacity = capacity();
62+
val neededCapacity = index + length;
63+
if (currentCapacity >= neededCapacity) {
64+
return;
5865
}
59-
}
6066

61-
private void extendBuffer () {
62-
val newBuffer = new byte[capacity() * 2];
63-
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
64-
buffer = newBuffer;
67+
int newCapacity = currentCapacity * 2;
68+
if (newCapacity - neededCapacity < 0) {
69+
newCapacity = neededCapacity;
70+
}
71+
if (newCapacity - MAX_ARRAY_SIZE > 0) {
72+
newCapacity = neededCapacity > MAX_ARRAY_SIZE
73+
? Integer.MAX_VALUE
74+
: MAX_ARRAY_SIZE;
75+
}
76+
buffer = Arrays.copyOf(buffer, newCapacity);
6577
}
6678
}

src/main/java/io/appulse/utils/BytesUtils.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424
import java.math.BigInteger;
2525
import java.nio.ByteBuffer;
26+
import java.nio.channels.ReadableByteChannel;
2627
import java.util.stream.Stream;
2728

2829
import io.appulse.utils.exception.CantReadFromArrayException;
@@ -1077,6 +1078,46 @@ public static byte[] read (@NonNull InputStream stream, int length) {
10771078
return result;
10781079
}
10791080

1081+
/**
1082+
* Reads all bytes from the channel till the buffer is full.
1083+
*
1084+
* @param channel the bytes source
1085+
*
1086+
* @param buffer the destination, whre readed bytes store
1087+
*
1088+
* @since 1.13.0
1089+
*/
1090+
@SneakyThrows
1091+
public static void read (@NonNull ReadableByteChannel channel, @NonNull ByteBuffer buffer) {
1092+
int totalReaded = 0;
1093+
do {
1094+
val readed = channel.read(buffer);
1095+
if (readed > 0) {
1096+
totalReaded += readed;
1097+
} else if (readed == -1) {
1098+
return;
1099+
}
1100+
} while (totalReaded < buffer.capacity());
1101+
}
1102+
1103+
/**
1104+
* Reads the bytes from the channel till the buffer's limit.
1105+
*
1106+
* @param channel the bytes source
1107+
*
1108+
* @param buffer the destination, whre readed bytes store
1109+
*
1110+
* @param limit the limit amount of bytes for read
1111+
*
1112+
* @since 1.13.0
1113+
*/
1114+
@SneakyThrows
1115+
public static void read (@NonNull ReadableByteChannel channel, @NonNull ByteBuffer buffer, int limit) {
1116+
val subBuffer = ByteBuffer.allocate(limit);
1117+
read(channel, subBuffer);
1118+
buffer.put(subBuffer);
1119+
}
1120+
10801121
/**
10811122
* Reads all bytes from stream till the end of the stream.
10821123
*

0 commit comments

Comments
 (0)