|
| 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.internal.async; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.core.HttpChecksumConstant.DEFAULT_ASYNC_CHUNK_SIZE; |
| 19 | + |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 25 | +import software.amazon.awssdk.utils.Validate; |
| 26 | +import software.amazon.awssdk.utils.builder.SdkBuilder; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class that will buffer incoming BufferBytes of totalBytes length to chunks of bufferSize* |
| 30 | + */ |
| 31 | +@SdkInternalApi |
| 32 | +public final class ChunkBuffer { |
| 33 | + private final AtomicLong remainingBytes; |
| 34 | + private final ByteBuffer currentBuffer; |
| 35 | + private final int bufferSize; |
| 36 | + |
| 37 | + private ChunkBuffer(Long totalBytes, Integer bufferSize) { |
| 38 | + Validate.notNull(totalBytes, "The totalBytes must not be null"); |
| 39 | + |
| 40 | + int chunkSize = bufferSize != null ? bufferSize : DEFAULT_ASYNC_CHUNK_SIZE; |
| 41 | + this.bufferSize = chunkSize; |
| 42 | + this.currentBuffer = ByteBuffer.allocate(chunkSize); |
| 43 | + this.remainingBytes = new AtomicLong(totalBytes); |
| 44 | + } |
| 45 | + |
| 46 | + public static Builder builder() { |
| 47 | + return new DefaultBuilder(); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + // currentBuffer and bufferedList can get over written if concurrent Threads calls this method at the same time. |
| 52 | + public synchronized Iterable<ByteBuffer> bufferAndCreateChunks(ByteBuffer buffer) { |
| 53 | + int startPosition = 0; |
| 54 | + List<ByteBuffer> bufferedList = new ArrayList<>(); |
| 55 | + int currentBytesRead = buffer.remaining(); |
| 56 | + do { |
| 57 | + int bufferedBytes = currentBuffer.position(); |
| 58 | + int availableToRead = bufferSize - bufferedBytes; |
| 59 | + int bytesToMove = Math.min(availableToRead, currentBytesRead - startPosition); |
| 60 | + |
| 61 | + if (bufferedBytes == 0) { |
| 62 | + currentBuffer.put(buffer.array(), startPosition, bytesToMove); |
| 63 | + } else { |
| 64 | + currentBuffer.put(buffer.array(), 0, bytesToMove); |
| 65 | + } |
| 66 | + |
| 67 | + startPosition = startPosition + bytesToMove; |
| 68 | + |
| 69 | + // Send the data once the buffer is full |
| 70 | + if (currentBuffer.position() == bufferSize) { |
| 71 | + currentBuffer.position(0); |
| 72 | + ByteBuffer bufferToSend = ByteBuffer.allocate(bufferSize); |
| 73 | + bufferToSend.put(currentBuffer.array(), 0, bufferSize); |
| 74 | + bufferToSend.clear(); |
| 75 | + currentBuffer.clear(); |
| 76 | + bufferedList.add(bufferToSend); |
| 77 | + remainingBytes.addAndGet(-bufferSize); |
| 78 | + } |
| 79 | + } while (startPosition < currentBytesRead); |
| 80 | + |
| 81 | + int remainingBytesInBuffer = currentBuffer.position(); |
| 82 | + |
| 83 | + // Send the remaining buffer when |
| 84 | + // 1. remainingBytes in buffer are same as the last few bytes to be read. |
| 85 | + // 2. If it is a zero byte and the last byte to be read. |
| 86 | + if (remainingBytes.get() == remainingBytesInBuffer && |
| 87 | + (buffer.remaining() == 0 || remainingBytesInBuffer > 0)) { |
| 88 | + currentBuffer.clear(); |
| 89 | + ByteBuffer trimmedBuffer = ByteBuffer.allocate(remainingBytesInBuffer); |
| 90 | + trimmedBuffer.put(currentBuffer.array(), 0, remainingBytesInBuffer); |
| 91 | + trimmedBuffer.clear(); |
| 92 | + bufferedList.add(trimmedBuffer); |
| 93 | + remainingBytes.addAndGet(-remainingBytesInBuffer); |
| 94 | + } |
| 95 | + return bufferedList; |
| 96 | + } |
| 97 | + |
| 98 | + public interface Builder extends SdkBuilder<Builder, ChunkBuffer> { |
| 99 | + |
| 100 | + Builder bufferSize(int bufferSize); |
| 101 | + |
| 102 | + Builder totalBytes(long totalBytes); |
| 103 | + |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + private static final class DefaultBuilder implements Builder { |
| 108 | + |
| 109 | + private Integer bufferSize; |
| 110 | + private Long totalBytes; |
| 111 | + |
| 112 | + @Override |
| 113 | + public ChunkBuffer build() { |
| 114 | + return new ChunkBuffer(totalBytes, bufferSize); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Builder bufferSize(int bufferSize) { |
| 119 | + this.bufferSize = bufferSize; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Builder totalBytes(long totalBytes) { |
| 125 | + this.totalBytes = totalBytes; |
| 126 | + return this; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments