-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip defensive copies and transforms in AsyncRequestBody
- Loading branch information
1 parent
6530cd7
commit cfb4737
Showing
6 changed files
with
334 additions
and
34 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,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "StephenFlavin", | ||
"description": "Skip defensive copies and transforms in AsyncRequestBody" | ||
} |
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
136 changes: 136 additions & 0 deletions
136
...src/main/java/software/amazon/awssdk/core/internal/async/ByteBuffersAsyncRequestBody.java
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,136 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.core.internal.async; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
import software.amazon.awssdk.core.internal.util.Mimetype; | ||
import software.amazon.awssdk.utils.BinaryUtils; | ||
import software.amazon.awssdk.utils.Logger; | ||
|
||
/** | ||
* An implementation of {@link AsyncRequestBody} for providing data from the supplied {@link ByteBuffer} array. This is created | ||
* using static methods on {@link AsyncRequestBody} | ||
* | ||
* @see AsyncRequestBody#fromBytes(byte[]) | ||
* @see AsyncRequestBody#fromByteBuffer(ByteBuffer) | ||
* @see AsyncRequestBody#fromByteBuffers(ByteBuffer...) | ||
* @see AsyncRequestBody#fromString(String) | ||
*/ | ||
@SdkInternalApi | ||
public final class ByteBuffersAsyncRequestBody implements AsyncRequestBody { | ||
private static final Logger log = Logger.loggerFor(ByteBuffersAsyncRequestBody.class); | ||
|
||
private final String mimetype; | ||
private final Long length; | ||
private final ByteBuffer[] buffers; | ||
|
||
private ByteBuffersAsyncRequestBody(String mimetype, Long length, ByteBuffer... buffers) { | ||
this.mimetype = mimetype; | ||
this.length = length; | ||
this.buffers = buffers; | ||
} | ||
|
||
@Override | ||
public Optional<Long> contentLength() { | ||
return Optional.ofNullable(length); | ||
} | ||
|
||
@Override | ||
public String contentType() { | ||
return mimetype; | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super ByteBuffer> s) { | ||
// As per rule 1.9 we must throw NullPointerException if the subscriber parameter is null | ||
if (s == null) { | ||
throw new NullPointerException("Subscription MUST NOT be null."); | ||
} | ||
|
||
// As per 2.13, this method must return normally (i.e. not throw). | ||
try { | ||
s.onSubscribe( | ||
new Subscription() { | ||
private final AtomicInteger index = new AtomicInteger(0); | ||
private final AtomicBoolean completed = new AtomicBoolean(false); | ||
|
||
@Override | ||
public void request(long n) { | ||
if (completed.get()) { | ||
return; | ||
} | ||
|
||
if (n > 0) { | ||
int i = index.getAndIncrement(); | ||
|
||
if (i >= buffers.length) { | ||
return; | ||
} | ||
|
||
long remaining = n; | ||
|
||
do { | ||
ByteBuffer buffer = buffers[i]; | ||
if (!buffer.hasArray()) { | ||
buffer = ByteBuffer.wrap(BinaryUtils.copyBytesFrom(buffer)); | ||
} | ||
s.onNext(buffer); | ||
remaining--; | ||
} while (remaining > 0 && (i = index.getAndIncrement()) < buffers.length); | ||
|
||
if (i >= buffers.length - 1 && completed.compareAndSet(false, true)) { | ||
s.onComplete(); | ||
} | ||
} else { | ||
s.onError(new IllegalArgumentException("§3.9: non-positive requests are not allowed!")); | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
completed.set(true); | ||
} | ||
} | ||
); | ||
} catch (Throwable ex) { | ||
log.error(() -> s + " violated the Reactive Streams rule 2.13 by throwing an exception from onSubscribe.", ex); | ||
} | ||
} | ||
|
||
public static ByteBuffersAsyncRequestBody of(Long length, ByteBuffer... buffers) { | ||
return new ByteBuffersAsyncRequestBody(Mimetype.MIMETYPE_OCTET_STREAM, length, buffers); | ||
} | ||
|
||
public static ByteBuffersAsyncRequestBody of(String mimetype, Long length, ByteBuffer... buffers) { | ||
return new ByteBuffersAsyncRequestBody(mimetype, length, buffers); | ||
} | ||
|
||
public static ByteBuffersAsyncRequestBody from(byte[] bytes) { | ||
return new ByteBuffersAsyncRequestBody(Mimetype.MIMETYPE_OCTET_STREAM, (long) bytes.length, | ||
ByteBuffer.wrap(bytes)); | ||
} | ||
|
||
public static ByteBuffersAsyncRequestBody from(String mimetype, byte[] bytes) { | ||
return new ByteBuffersAsyncRequestBody(mimetype, (long) bytes.length, ByteBuffer.wrap(bytes)); | ||
} | ||
} |
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
Oops, something went wrong.