22
22
import java .nio .charset .Charset ;
23
23
import java .nio .charset .StandardCharsets ;
24
24
import java .nio .file .Path ;
25
+ import java .util .Arrays ;
25
26
import java .util .Optional ;
26
27
import java .util .concurrent .ExecutorService ;
27
28
import org .reactivestreams .Publisher ;
28
29
import org .reactivestreams .Subscriber ;
29
30
import software .amazon .awssdk .annotations .SdkPublicApi ;
30
- import software .amazon .awssdk .core .internal .async .ByteArrayAsyncRequestBody ;
31
+ import software .amazon .awssdk .core .internal .async .ByteBuffersAsyncRequestBody ;
31
32
import software .amazon .awssdk .core .internal .async .FileAsyncRequestBody ;
32
33
import software .amazon .awssdk .core .internal .async .InputStreamWithExecutorAsyncRequestBody ;
33
34
import software .amazon .awssdk .core .internal .util .Mimetype ;
52
53
* </p>
53
54
*
54
55
* @see FileAsyncRequestBody
55
- * @see ByteArrayAsyncRequestBody
56
+ * @see ByteBuffersAsyncRequestBody
56
57
*/
57
58
@ SdkPublicApi
58
59
public interface AsyncRequestBody extends SdkPublisher <ByteBuffer > {
@@ -124,11 +125,11 @@ static AsyncRequestBody fromFile(File file) {
124
125
* @param string The string to provide.
125
126
* @param cs The {@link Charset} to use.
126
127
* @return Implementation of {@link AsyncRequestBody} that uses the specified string.
127
- * @see ByteArrayAsyncRequestBody
128
+ * @see ByteBuffersAsyncRequestBody
128
129
*/
129
130
static AsyncRequestBody fromString (String string , Charset cs ) {
130
- return new ByteArrayAsyncRequestBody ( string . getBytes ( cs ),
131
- Mimetype . MIMETYPE_TEXT_PLAIN + "; charset=" + cs . name ( ));
131
+ return ByteBuffersAsyncRequestBody . from ( Mimetype . MIMETYPE_TEXT_PLAIN + "; charset=" + cs . name ( ),
132
+ string . getBytes ( cs ));
132
133
}
133
134
134
135
/**
@@ -150,18 +151,69 @@ static AsyncRequestBody fromString(String string) {
150
151
* @return AsyncRequestBody instance.
151
152
*/
152
153
static AsyncRequestBody fromBytes (byte [] bytes ) {
153
- return new ByteArrayAsyncRequestBody (bytes , Mimetype .MIMETYPE_OCTET_STREAM );
154
+ byte [] clonedBytes = bytes .clone ();
155
+ return ByteBuffersAsyncRequestBody .from (clonedBytes );
154
156
}
155
157
156
158
/**
157
- * Creates a {@link AsyncRequestBody} from a {@link ByteBuffer}. Buffer contents are copied so any modifications
158
- * made to the original {@link ByteBuffer} are not reflected in the {@link AsyncRequestBody}.
159
+ * Creates a {@link AsyncRequestBody} from a byte array. The contents of the byte array are copied so modifications to the
160
+ * original byte array are not reflected in the {@link AsyncRequestBody}.
161
+ *
162
+ * @param bytes The bytes to send to the service.
163
+ * @return AsyncRequestBody instance.
164
+ */
165
+ static AsyncRequestBody fromBytesUnsafe (byte [] bytes ) {
166
+ return ByteBuffersAsyncRequestBody .from (bytes );
167
+ }
168
+
169
+ /**
170
+ * Creates a {@link AsyncRequestBody} from a {@link ByteBuffer}. Buffer contents are copied with the position set to zero, the
171
+ * mark if defined is discarded, this is to ensure modifications made to the original {@link ByteBuffer} are not reflected in
172
+ * the {@link AsyncRequestBody}.
159
173
*
160
174
* @param byteBuffer ByteBuffer to send to the service.
161
175
* @return AsyncRequestBody instance.
162
176
*/
163
177
static AsyncRequestBody fromByteBuffer (ByteBuffer byteBuffer ) {
164
- return fromBytes (BinaryUtils .copyAllBytesFrom (byteBuffer ));
178
+ ByteBuffer immutableCopy = BinaryUtils .immutableCopyOf (byteBuffer );
179
+ immutableCopy .rewind ();
180
+ return ByteBuffersAsyncRequestBody .of (null , immutableCopy );
181
+ }
182
+
183
+ /**
184
+ * Creates a {@link AsyncRequestBody} from a {@link ByteBuffer}.
185
+ *
186
+ * @param byteBuffer ByteBuffer to send to the service.
187
+ * @return AsyncRequestBody instance.
188
+ */
189
+ static AsyncRequestBody fromByteBufferUnsafe (ByteBuffer byteBuffer ) {
190
+ return ByteBuffersAsyncRequestBody .of (null , byteBuffer );
191
+ }
192
+
193
+ /**
194
+ * Creates a {@link AsyncRequestBody} from an array of {@link ByteBuffer}. Each Buffers contents are copied with their
195
+ * positions set to zero and marks if defined are discarded, this is to ensure modifications made to the original array of
196
+ * {@link ByteBuffer} are not reflected in the {@link AsyncRequestBody}.
197
+ *
198
+ * @param byteBuffers ByteBuffer[] to send to the service.
199
+ * @return AsyncRequestBody instance.
200
+ */
201
+ static AsyncRequestBody fromByteBuffers (ByteBuffer ... byteBuffers ) {
202
+ ByteBuffer [] immutableCopy = Arrays .stream (byteBuffers )
203
+ .map (BinaryUtils ::immutableCopyOf )
204
+ .peek (ByteBuffer ::rewind )
205
+ .toArray (ByteBuffer []::new );
206
+ return ByteBuffersAsyncRequestBody .of (null , immutableCopy );
207
+ }
208
+
209
+ /**
210
+ * Creates a {@link AsyncRequestBody} from an array of {@link ByteBuffer}.
211
+ *
212
+ * @param byteBuffers ByteBuffer[] to send to the service.
213
+ * @return AsyncRequestBody instance.
214
+ */
215
+ static AsyncRequestBody fromByteBuffersUnsafe (ByteBuffer ... byteBuffers ) {
216
+ return ByteBuffersAsyncRequestBody .of (null , byteBuffers );
165
217
}
166
218
167
219
/**
0 commit comments