-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from nstdio/micro-ops
chore: Micro optimizations.
- Loading branch information
Showing
9 changed files
with
161 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.nstdio.http.ext; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
class Buffers { | ||
private Buffers() { | ||
} | ||
|
||
static ByteBuffer duplicate(ByteBuffer buf) { | ||
var dup = buf.asReadOnlyBuffer(); | ||
return dup.hasRemaining() ? dup : dup.flip(); | ||
} | ||
|
||
static List<ByteBuffer> duplicate(List<ByteBuffer> item) { | ||
int s = item.size(); | ||
switch (s) { | ||
case 0: | ||
return Collections.emptyList(); | ||
case 1: | ||
return List.of(duplicate(item.get(0))); | ||
case 2: | ||
return List.of(duplicate(item.get(0)), duplicate(item.get(1))); | ||
default: { | ||
List<ByteBuffer> list = new ArrayList<>(s); | ||
for (ByteBuffer byteBuffer : item) { | ||
list.add(duplicate(byteBuffer)); | ||
} | ||
|
||
return Collections.unmodifiableList(list); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.nstdio.http.ext; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.apache.commons.lang3.RandomUtils; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
class BuffersTest { | ||
|
||
static Stream<List<ByteBuffer>> listBuffersData() { | ||
return Stream.of( | ||
List.of(), | ||
List.of(ByteBuffer.wrap("abcde".repeat(16).getBytes(UTF_8))), | ||
List.of(ByteBuffer.wrap("ab".repeat(16).getBytes(UTF_8)), ByteBuffer.wrap("cd".repeat(16).getBytes(UTF_8))), | ||
Helpers.toBuffers(RandomUtils.nextBytes(96), true) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("listBuffersData") | ||
void shouldDuplicatedBufferList(List<ByteBuffer> buffers) { | ||
//when | ||
List<ByteBuffer> actual = Buffers.duplicate(buffers); | ||
|
||
//then | ||
assertThat(actual) | ||
.isNotSameAs(buffers) | ||
.hasSameSizeAs(buffers) | ||
.allMatch(Buffer::isReadOnly, "Expecting duplicated buffet to be read-only") | ||
.containsExactlyElementsOf(buffers); | ||
} | ||
|
||
@Test | ||
void shouldDuplicatedSingleBuffer() { | ||
//given | ||
ByteBuffer buffer = ByteBuffer.wrap(RandomUtils.nextBytes(16)); | ||
|
||
//when | ||
ByteBuffer actual = Buffers.duplicate(buffer); | ||
buffer.get(); | ||
|
||
//then | ||
assertThat(actual.position()).isZero(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters