|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.async; |
| 17 | + |
| 18 | +import java.io.InputStream; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 22 | +import software.amazon.awssdk.utils.Validate; |
| 23 | +import software.amazon.awssdk.utils.builder.CopyableBuilder; |
| 24 | +import software.amazon.awssdk.utils.builder.ToCopyableBuilder; |
| 25 | + |
| 26 | +/** |
| 27 | + * Configuration options for {@link AsyncRequestBody#fromInputStream(AsyncRequestBodyFromInputStreamConfiguration)} |
| 28 | + * to configure how the SDK should create an {@link AsyncRequestBody} from an {@link InputStream}. |
| 29 | + */ |
| 30 | +@SdkPublicApi |
| 31 | +public final class AsyncRequestBodyFromInputStreamConfiguration |
| 32 | + implements ToCopyableBuilder<AsyncRequestBodyFromInputStreamConfiguration.Builder, |
| 33 | + AsyncRequestBodyFromInputStreamConfiguration> { |
| 34 | + private final InputStream inputStream; |
| 35 | + private final Long contentLength; |
| 36 | + private final ExecutorService executor; |
| 37 | + private final Integer maxReadLimit; |
| 38 | + |
| 39 | + private AsyncRequestBodyFromInputStreamConfiguration(DefaultBuilder builder) { |
| 40 | + this.inputStream = Validate.paramNotNull(builder.inputStream, "inputStream"); |
| 41 | + this.contentLength = Validate.isNotNegativeOrNull(builder.contentLength, "contentLength"); |
| 42 | + this.maxReadLimit = Validate.isPositiveOrNull(builder.maxReadLimit, "maxReadLimit"); |
| 43 | + this.executor = Validate.paramNotNull(builder.executor, "executor"); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return the provided {@link InputStream}. |
| 48 | + */ |
| 49 | + public InputStream inputStream() { |
| 50 | + return inputStream; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return the provided content length. |
| 55 | + */ |
| 56 | + public Long contentLength() { |
| 57 | + return contentLength; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return the provided {@link ExecutorService}. |
| 62 | + */ |
| 63 | + public ExecutorService executor() { |
| 64 | + return executor; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return the provided max read limit used to mark and reset the {@link InputStream}). |
| 69 | + */ |
| 70 | + public Integer maxReadLimit() { |
| 71 | + return maxReadLimit; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean equals(Object o) { |
| 76 | + if (this == o) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + if (o == null || getClass() != o.getClass()) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + AsyncRequestBodyFromInputStreamConfiguration that = (AsyncRequestBodyFromInputStreamConfiguration) o; |
| 84 | + |
| 85 | + if (!Objects.equals(inputStream, that.inputStream)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + if (!Objects.equals(contentLength, that.contentLength)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + if (!Objects.equals(executor, that.executor)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + return Objects.equals(maxReadLimit, that.maxReadLimit); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public int hashCode() { |
| 99 | + int result = inputStream != null ? inputStream.hashCode() : 0; |
| 100 | + result = 31 * result + (contentLength != null ? contentLength.hashCode() : 0); |
| 101 | + result = 31 * result + (executor != null ? executor.hashCode() : 0); |
| 102 | + result = 31 * result + (maxReadLimit != null ? maxReadLimit.hashCode() : 0); |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Create a {@link Builder}, used to create a {@link AsyncRequestBodyFromInputStreamConfiguration}. |
| 108 | + */ |
| 109 | + public static Builder builder() { |
| 110 | + return new DefaultBuilder(); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + @Override |
| 115 | + public AsyncRequestBodyFromInputStreamConfiguration.Builder toBuilder() { |
| 116 | + return new DefaultBuilder(this); |
| 117 | + } |
| 118 | + |
| 119 | + public interface Builder extends CopyableBuilder<AsyncRequestBodyFromInputStreamConfiguration.Builder, |
| 120 | + AsyncRequestBodyFromInputStreamConfiguration> { |
| 121 | + |
| 122 | + /** |
| 123 | + * Configures the InputStream. |
| 124 | + * |
| 125 | + * @param inputStream the InputStream |
| 126 | + * @return This object for method chaining. |
| 127 | + */ |
| 128 | + Builder inputStream(InputStream inputStream); |
| 129 | + |
| 130 | + /** |
| 131 | + * Configures the length of the provided {@link InputStream} |
| 132 | + * @param contentLength the content length |
| 133 | + * @return This object for method chaining. |
| 134 | + */ |
| 135 | + Builder contentLength(Long contentLength); |
| 136 | + |
| 137 | + /** |
| 138 | + * Configures the {@link ExecutorService} to perform the blocking data reads. |
| 139 | + * |
| 140 | + * @param executor the executor |
| 141 | + * @return This object for method chaining. |
| 142 | + */ |
| 143 | + Builder executor(ExecutorService executor); |
| 144 | + |
| 145 | + /** |
| 146 | + * Configures max read limit used to mark and reset the {@link InputStream}. This will have no |
| 147 | + * effect if the stream doesn't support mark and reset. |
| 148 | + * |
| 149 | + * <p> |
| 150 | + * By default, it is 128 KiB. |
| 151 | + * |
| 152 | + * @param maxReadLimit the max read limit |
| 153 | + * @return This object for method chaining. |
| 154 | + * @see InputStream#mark(int) |
| 155 | + */ |
| 156 | + Builder maxReadLimit(Integer maxReadLimit); |
| 157 | + } |
| 158 | + |
| 159 | + private static final class DefaultBuilder implements Builder { |
| 160 | + private InputStream inputStream; |
| 161 | + private Long contentLength; |
| 162 | + private ExecutorService executor; |
| 163 | + private Integer maxReadLimit; |
| 164 | + |
| 165 | + private DefaultBuilder(AsyncRequestBodyFromInputStreamConfiguration asyncRequestBodyFromInputStreamConfiguration) { |
| 166 | + this.inputStream = asyncRequestBodyFromInputStreamConfiguration.inputStream; |
| 167 | + this.contentLength = asyncRequestBodyFromInputStreamConfiguration.contentLength; |
| 168 | + this.executor = asyncRequestBodyFromInputStreamConfiguration.executor; |
| 169 | + this.maxReadLimit = asyncRequestBodyFromInputStreamConfiguration.maxReadLimit; |
| 170 | + } |
| 171 | + |
| 172 | + private DefaultBuilder() { |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + public Builder inputStream(InputStream inputStream) { |
| 177 | + this.inputStream = inputStream; |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public Builder contentLength(Long contentLength) { |
| 182 | + this.contentLength = contentLength; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + public Builder executor(ExecutorService executor) { |
| 187 | + this.executor = executor; |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + public Builder maxReadLimit(Integer maxReadLimit) { |
| 192 | + this.maxReadLimit = maxReadLimit; |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public AsyncRequestBodyFromInputStreamConfiguration build() { |
| 198 | + return new AsyncRequestBodyFromInputStreamConfiguration(this); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments