Skip to content

Commit e54e2f6

Browse files
Removed unused allocated bytes from encoded character arrays
- Added ByteBufferEncoder for converting ByteBuffer to byte array
1 parent 98e83d7 commit e54e2f6

File tree

6 files changed

+136
-3
lines changed

6 files changed

+136
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2021 Socket Broker Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.exceptionfactory.socketbroker.protocol;
17+
18+
import java.nio.ByteBuffer;
19+
import java.util.Objects;
20+
21+
/**
22+
* ByteBuffer Encoder reads populated byte array contents of the provided buffer
23+
*/
24+
public class ByteBufferEncoder implements PacketEncoder<ByteBuffer> {
25+
/**
26+
* Get encoded buffer after rewinding and read byte array based on the buffer limit length
27+
*
28+
* @param buffer Byte Buffer to be read
29+
* @return Bytes read
30+
*/
31+
@Override
32+
public byte[] getEncoded(final ByteBuffer buffer) {
33+
Objects.requireNonNull(buffer, "Buffer required");
34+
buffer.rewind();
35+
final int length = buffer.limit();
36+
final byte[] encoded = new byte[length];
37+
buffer.get(encoded);
38+
return encoded;
39+
}
40+
}

src/main/java/com/exceptionfactory/socketbroker/protocol/UnicodeStandardCharacterArrayEncoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* Unicode Standard Character Array Encoder converts provided sources to byte arrays using UTF-8 encoding
2525
*/
2626
public class UnicodeStandardCharacterArrayEncoder implements PacketEncoder<char[]> {
27+
private static final PacketEncoder<ByteBuffer> BYTE_BUFFER_ENCODER = new ByteBufferEncoder();
28+
2729
/**
2830
* Get bytes encoded using UTF-8
2931
*
@@ -34,6 +36,6 @@ public class UnicodeStandardCharacterArrayEncoder implements PacketEncoder<char[
3436
public byte[] getEncoded(final char[] characters) {
3537
final CharBuffer buffer = CharBuffer.wrap(Objects.requireNonNull(characters));
3638
final ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(buffer);
37-
return byteBuffer.array();
39+
return BYTE_BUFFER_ENCODER.getEncoded(byteBuffer);
3840
}
3941
}

src/main/java/com/exceptionfactory/socketbroker/protocol/http/authorization/BasicProxyAuthorizationProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.exceptionfactory.socketbroker.configuration.AuthenticationCredentials;
1919
import com.exceptionfactory.socketbroker.configuration.UsernamePasswordAuthenticationCredentials;
20+
import com.exceptionfactory.socketbroker.protocol.ByteBufferEncoder;
2021
import com.exceptionfactory.socketbroker.protocol.PacketEncoder;
2122
import com.exceptionfactory.socketbroker.protocol.UnicodeStandardCharacterArrayEncoder;
2223
import com.exceptionfactory.socketbroker.protocol.UnicodeStandardStringEncoder;
@@ -36,6 +37,8 @@ public class BasicProxyAuthorizationProvider implements ProxyAuthorizationProvid
3637

3738
private static final PacketEncoder<char[]> CHARACTER_ARRAY_ENCODER = new UnicodeStandardCharacterArrayEncoder();
3839

40+
private static final PacketEncoder<ByteBuffer> BYTE_BUFFER_ENCODER = new ByteBufferEncoder();
41+
3942
private static final byte COLON_SEPARATOR = ':';
4043

4144
private static final char SPACE_SEPARATOR = ' ';
@@ -77,6 +80,7 @@ private String getCredentialsEncoded(final UsernamePasswordAuthenticationCredent
7780
buffer.put(COLON_SEPARATOR);
7881
buffer.put(password);
7982

80-
return ENCODER.encodeToString(buffer.array());
83+
final byte[] encodedBuffer = BYTE_BUFFER_ENCODER.getEncoded(buffer);
84+
return ENCODER.encodeToString(encodedBuffer);
8185
}
8286
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2021 Socket Broker Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.exceptionfactory.socketbroker.protocol;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.nio.ByteBuffer;
21+
22+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23+
24+
public class ByteBufferEncoderTest {
25+
private static final byte[] BYTES = new byte[]{1, 2, 3, 4, 5};
26+
27+
private final ByteBufferEncoder encoder = new ByteBufferEncoder();
28+
29+
@Test
30+
public void testGetEncoded() {
31+
final ByteBuffer buffer = ByteBuffer.wrap(BYTES);
32+
final byte[] encoded = encoder.getEncoded(buffer);
33+
assertArrayEquals(BYTES, encoded);
34+
}
35+
36+
@Test
37+
public void testGetEncodedAllocatePutBytes() {
38+
final ByteBuffer buffer = ByteBuffer.allocate(BYTES.length);
39+
buffer.put(BYTES);
40+
final byte[] encoded = encoder.getEncoded(buffer);
41+
assertArrayEquals(BYTES, encoded);
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2021 Socket Broker Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.exceptionfactory.socketbroker.protocol;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21+
22+
public class UnicodeStandardCharacterArrayEncoderTest {
23+
private static final char[] WORD = new char[]{87, 79, 82, 68};
24+
25+
private static final byte[] WORD_ENCODED = new byte[]{87, 79, 82, 68};
26+
27+
private static final char[] UNICODE_WORD = new char[]{913, 937};
28+
29+
private static final byte[] UNICODE_WORD_ENCODED = new byte[]{-50, -111, -50, -87};
30+
31+
private final UnicodeStandardCharacterArrayEncoder encoder = new UnicodeStandardCharacterArrayEncoder();
32+
33+
@Test
34+
public void testGetEncodedWord() {
35+
final byte[] encoded = encoder.getEncoded(WORD);
36+
assertArrayEquals(WORD_ENCODED, encoded);
37+
}
38+
39+
@Test
40+
public void testGetEncodedUnicodeWord() {
41+
final byte[] encoded = encoder.getEncoded(UNICODE_WORD);
42+
assertArrayEquals(UNICODE_WORD_ENCODED, encoded);
43+
}
44+
}

src/test/java/com/exceptionfactory/socketbroker/protocol/http/authorization/BasicProxyAuthorizationProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class BasicProxyAuthorizationProviderTest {
4545

4646
private static final char[] UNICODE_PASSWORD = Character.toChars(937);
4747

48-
private static final String UNICODE_CREDENTIALS = "Basic zpHOqTrOqQA=";
48+
private static final String UNICODE_CREDENTIALS = "Basic zpHOqTrOqQ==";
4949

5050
@Mock
5151
private AuthenticationCredentials authenticationCredentials;

0 commit comments

Comments
 (0)