-
Notifications
You must be signed in to change notification settings - Fork 940
Implementing string and byte[] async response handlers. Porting over … #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2010-2017 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.async; | ||
|
||
import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.ByteBuffer; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.annotation.SdkInternalApi; | ||
import software.amazon.awssdk.utils.BinaryUtils; | ||
|
||
/** | ||
* Implementation of {@link AsyncResponseHandler} that dumps content into a byte array. | ||
* | ||
* @param <ResponseT> Pojo response type. | ||
*/ | ||
@SdkInternalApi | ||
class ByteArrayAsyncResponseHandler<ResponseT> implements AsyncResponseHandler<ResponseT, byte[]> { | ||
|
||
|
||
private ByteArrayOutputStream baos; | ||
|
||
@Override | ||
public void responseReceived(ResponseT response) { | ||
} | ||
|
||
@Override | ||
public void onStream(Publisher<ByteBuffer> publisher) { | ||
baos = new ByteArrayOutputStream(); | ||
publisher.subscribe(new BaosSubscriber()); | ||
} | ||
|
||
@Override | ||
public void exceptionOccurred(Throwable throwable) { | ||
baos = null; | ||
} | ||
|
||
@Override | ||
public byte[] complete() { | ||
try { | ||
return baos.toByteArray(); | ||
} finally { | ||
baos = null; | ||
} | ||
} | ||
|
||
/** | ||
* Requests chunks sequentially and dumps them into a {@link ByteArrayOutputStream}. | ||
*/ | ||
private class BaosSubscriber implements Subscriber<ByteBuffer> { | ||
|
||
private Subscription subscription; | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
this.subscription = s; | ||
subscription.request(1); | ||
} | ||
|
||
@Override | ||
public void onNext(ByteBuffer byteBuffer) { | ||
invokeSafely(() -> baos.write(BinaryUtils.copyBytesFrom(byteBuffer))); | ||
|
||
subscription.request(1); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
// Handled by response handler | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
// Handled by response handler | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2010-2017 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.async; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
import org.reactivestreams.Publisher; | ||
import software.amazon.awssdk.annotation.SdkInternalApi; | ||
|
||
/** | ||
* Implementation of {@link AsyncResponseHandler} that dumps content into a string using the specified {@link Charset}. | ||
* | ||
* @param <ResponseT> Pojo response type. | ||
*/ | ||
@SdkInternalApi | ||
class StringAsyncResponseHandler<ResponseT> implements AsyncResponseHandler<ResponseT, String> { | ||
|
||
|
||
private final AsyncResponseHandler<ResponseT, byte[]> byteArrayResponseHandler; | ||
private final Charset charset; | ||
|
||
/** | ||
* @param byteArrayResponseHandler {@link AsyncResponseHandler} implementation that dumps data into a byte array. | ||
* @param charset Charset to use for String. | ||
*/ | ||
StringAsyncResponseHandler(AsyncResponseHandler<ResponseT, byte[]> byteArrayResponseHandler, | ||
Charset charset) { | ||
this.byteArrayResponseHandler = byteArrayResponseHandler; | ||
this.charset = charset; | ||
} | ||
|
||
@Override | ||
public void responseReceived(ResponseT response) { | ||
byteArrayResponseHandler.responseReceived(response); | ||
} | ||
|
||
@Override | ||
public void onStream(Publisher<ByteBuffer> publisher) { | ||
byteArrayResponseHandler.onStream(publisher); | ||
} | ||
|
||
@Override | ||
public void exceptionOccurred(Throwable throwable) { | ||
byteArrayResponseHandler.exceptionOccurred(throwable); | ||
} | ||
|
||
@Override | ||
public String complete() { | ||
return new String(byteArrayResponseHandler.complete(), charset); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@param charset + other javadoc changes