|
| 1 | +/* |
| 2 | + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, |
| 3 | + * (C) 2021 MinIO, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package io.minio; |
| 19 | + |
| 20 | +import com.google.common.io.BaseEncoding; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.RandomAccessFile; |
| 24 | +import java.security.MessageDigest; |
| 25 | +import java.security.NoSuchAlgorithmException; |
| 26 | +import java.util.Base64; |
| 27 | +import java.util.Locale; |
| 28 | +import java.util.Objects; |
| 29 | +import javax.annotation.Nonnull; |
| 30 | + |
| 31 | +/** PartReader reads part data from file or input stream sequentially and returns PartSource. */ |
| 32 | +class PartReader { |
| 33 | + private static final long CHUNK_SIZE = Integer.MAX_VALUE; |
| 34 | + |
| 35 | + private byte[] buf16k = new byte[16384]; // 16KiB buffer for optimization. |
| 36 | + |
| 37 | + private RandomAccessFile file; |
| 38 | + private InputStream stream; |
| 39 | + |
| 40 | + private long objectSize; |
| 41 | + private long partSize; |
| 42 | + private int partCount; |
| 43 | + |
| 44 | + private int partNumber; |
| 45 | + private long totalDataRead; |
| 46 | + |
| 47 | + private ByteBufferStream[] buffers; |
| 48 | + private byte[] oneByte = null; |
| 49 | + boolean eof; |
| 50 | + |
| 51 | + private PartReader(long objectSize, long partSize, int partCount) { |
| 52 | + this.objectSize = objectSize; |
| 53 | + this.partSize = partSize; |
| 54 | + this.partCount = partCount; |
| 55 | + |
| 56 | + long bufferCount = partSize / CHUNK_SIZE; |
| 57 | + if ((partSize - (bufferCount * CHUNK_SIZE)) > 0) bufferCount++; |
| 58 | + if (bufferCount == 0) bufferCount++; |
| 59 | + |
| 60 | + this.buffers = new ByteBufferStream[(int) bufferCount]; |
| 61 | + } |
| 62 | + |
| 63 | + public PartReader(@Nonnull RandomAccessFile file, long objectSize, long partSize, int partCount) { |
| 64 | + this(objectSize, partSize, partCount); |
| 65 | + this.file = Objects.requireNonNull(file, "file must not be null"); |
| 66 | + if (this.objectSize < 0) throw new IllegalArgumentException("object size must be provided"); |
| 67 | + } |
| 68 | + |
| 69 | + public PartReader(@Nonnull InputStream stream, long objectSize, long partSize, int partCount) { |
| 70 | + this(objectSize, partSize, partCount); |
| 71 | + this.stream = Objects.requireNonNull(stream, "stream must not be null"); |
| 72 | + for (int i = 0; i < this.buffers.length; i++) this.buffers[i] = new ByteBufferStream(); |
| 73 | + } |
| 74 | + |
| 75 | + private long readStreamChunk( |
| 76 | + ByteBufferStream buffer, long size, MessageDigest md5, MessageDigest sha256) |
| 77 | + throws IOException { |
| 78 | + long totalBytesRead = 0; |
| 79 | + |
| 80 | + if (this.oneByte != null) { |
| 81 | + buffer.write(this.oneByte); |
| 82 | + md5.update(this.oneByte); |
| 83 | + if (sha256 != null) sha256.update(this.oneByte); |
| 84 | + totalBytesRead++; |
| 85 | + this.oneByte = null; |
| 86 | + } |
| 87 | + |
| 88 | + while (totalBytesRead < size) { |
| 89 | + long bytesToRead = size - totalBytesRead; |
| 90 | + if (bytesToRead > this.buf16k.length) bytesToRead = this.buf16k.length; |
| 91 | + int bytesRead = this.stream.read(this.buf16k, 0, (int) bytesToRead); |
| 92 | + this.eof = (bytesRead < 0); |
| 93 | + if (this.eof) { |
| 94 | + if (this.objectSize < 0) break; |
| 95 | + throw new IOException("unexpected EOF"); |
| 96 | + } |
| 97 | + buffer.write(this.buf16k, 0, bytesRead); |
| 98 | + md5.update(this.buf16k, 0, bytesRead); |
| 99 | + if (sha256 != null) sha256.update(this.buf16k, 0, bytesRead); |
| 100 | + totalBytesRead += bytesRead; |
| 101 | + } |
| 102 | + |
| 103 | + return totalBytesRead; |
| 104 | + } |
| 105 | + |
| 106 | + private long readStream(long size, MessageDigest md5, MessageDigest sha256) throws IOException { |
| 107 | + long count = size / CHUNK_SIZE; |
| 108 | + long lastChunkSize = size - (count * CHUNK_SIZE); |
| 109 | + if (lastChunkSize > 0) { |
| 110 | + count++; |
| 111 | + } else { |
| 112 | + lastChunkSize = CHUNK_SIZE; |
| 113 | + } |
| 114 | + |
| 115 | + long totalBytesRead = 0; |
| 116 | + for (int i = 0; i < buffers.length; i++) buffers[i].reset(); |
| 117 | + for (long i = 1; i <= count && !this.eof; i++) { |
| 118 | + long chunkSize = (i != count) ? CHUNK_SIZE : lastChunkSize; |
| 119 | + long bytesRead = this.readStreamChunk(buffers[(int) (i - 1)], chunkSize, md5, sha256); |
| 120 | + totalBytesRead += bytesRead; |
| 121 | + } |
| 122 | + |
| 123 | + if (!this.eof && this.objectSize < 0) { |
| 124 | + this.oneByte = new byte[1]; |
| 125 | + this.eof = this.stream.read(this.oneByte) < 0; |
| 126 | + } |
| 127 | + |
| 128 | + return totalBytesRead; |
| 129 | + } |
| 130 | + |
| 131 | + private long readFile(long size, MessageDigest md5, MessageDigest sha256) throws IOException { |
| 132 | + long position = this.file.getFilePointer(); |
| 133 | + long totalBytesRead = 0; |
| 134 | + |
| 135 | + while (totalBytesRead < size) { |
| 136 | + long bytesToRead = size - totalBytesRead; |
| 137 | + if (bytesToRead > this.buf16k.length) bytesToRead = this.buf16k.length; |
| 138 | + int bytesRead = this.file.read(this.buf16k, 0, (int) bytesToRead); |
| 139 | + if (bytesRead < 0) throw new IOException("unexpected EOF"); |
| 140 | + md5.update(this.buf16k, 0, bytesRead); |
| 141 | + if (sha256 != null) sha256.update(this.buf16k, 0, bytesRead); |
| 142 | + totalBytesRead += bytesRead; |
| 143 | + } |
| 144 | + |
| 145 | + this.file.seek(position); |
| 146 | + return totalBytesRead; |
| 147 | + } |
| 148 | + |
| 149 | + private long read(long size, MessageDigest md5, MessageDigest sha256) throws IOException { |
| 150 | + return (this.file != null) ? readFile(size, md5, sha256) : readStream(size, md5, sha256); |
| 151 | + } |
| 152 | + |
| 153 | + public PartSource getPart(boolean computeSha256) throws NoSuchAlgorithmException, IOException { |
| 154 | + if (this.partNumber == this.partCount) return null; |
| 155 | + |
| 156 | + this.partNumber++; |
| 157 | + |
| 158 | + MessageDigest md5 = MessageDigest.getInstance("MD5"); |
| 159 | + MessageDigest sha256 = computeSha256 ? MessageDigest.getInstance("SHA-256") : null; |
| 160 | + |
| 161 | + long partSize = this.partSize; |
| 162 | + if (this.partNumber == this.partCount) partSize = this.objectSize - this.totalDataRead; |
| 163 | + long bytesRead = this.read(partSize, md5, sha256); |
| 164 | + this.totalDataRead += bytesRead; |
| 165 | + if (this.objectSize < 0 && this.eof) this.partCount = this.partNumber; |
| 166 | + |
| 167 | + String md5Hash = Base64.getEncoder().encodeToString(md5.digest()); |
| 168 | + String sha256Hash = null; |
| 169 | + if (computeSha256) { |
| 170 | + sha256Hash = BaseEncoding.base16().encode(sha256.digest()).toLowerCase(Locale.US); |
| 171 | + } |
| 172 | + |
| 173 | + if (this.file != null) { |
| 174 | + return new PartSource(this.partNumber, this.file, bytesRead, md5Hash, sha256Hash); |
| 175 | + } |
| 176 | + |
| 177 | + return new PartSource(this.partNumber, this.buffers, bytesRead, md5Hash, sha256Hash); |
| 178 | + } |
| 179 | + |
| 180 | + public int partCount() { |
| 181 | + return this.partCount; |
| 182 | + } |
| 183 | +} |
0 commit comments