Skip to content

Commit

Permalink
refactor http execution to compute md5sum when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed Feb 22, 2020
1 parent d242780 commit f774bc4
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 634 deletions.
1 change: 1 addition & 0 deletions api/src/main/java/io/minio/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum ErrorCode {
NO_SUCH_OBJECT("NoSuchKey", "Object does not exist"),
RESOURCE_NOT_FOUND("ResourceNotFound", "Request resource not found"),
RESOURCE_CONFLICT("ResourceConflict", "Request resource conflicts"),
RETRY_HEAD_BUCKET("RetryHeadBucket", "Retry HEAD bucket request"),

// S3 error codes
ACCESS_DENIED("AccessDenied", "Access denied"),
Expand Down
53 changes: 27 additions & 26 deletions api/src/main/java/io/minio/HttpRequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package io.minio;

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.RandomAccessFile;
import java.nio.channels.Channels;

Expand All @@ -30,14 +30,28 @@
* RequestBody that wraps a single data object.
*/
class HttpRequestBody extends RequestBody {
private final String contentType;
private final Object data;
private final int len;
private RandomAccessFile file = null;
private BufferedInputStream stream = null;
private byte[] bytes = null;
private int length = -1;
private String contentType = null;

HttpRequestBody(final String contentType, final Object data, final int len) {
HttpRequestBody(final RandomAccessFile file, final int length, final String contentType) {
this.file = file;
this.length = length;
this.contentType = contentType;
}

HttpRequestBody(final BufferedInputStream stream, final int length, final String contentType) {
this.stream = stream;
this.length = length;
this.contentType = contentType;
}

HttpRequestBody(final byte[] bytes, final int length, final String contentType) {
this.bytes = bytes;
this.length = length;
this.contentType = contentType;
this.data = data;
this.len = len;
}

@Override
Expand All @@ -56,30 +70,17 @@ public MediaType contentType() {

@Override
public long contentLength() {
if (data instanceof InputStream || data instanceof RandomAccessFile || data instanceof byte[]) {
return len;
}

if (len == 0) {
return -1;
} else {
return len;
}
return length;
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
if (data instanceof InputStream) {
InputStream stream = (InputStream) data;
sink.writeAll(Okio.source(stream));
} else if (data instanceof RandomAccessFile) {
RandomAccessFile file = (RandomAccessFile) data;
sink.write(Okio.source(Channels.newInputStream(file.getChannel())), len);
} else if (data instanceof byte[]) {
byte[] bytes = (byte[]) data;
sink.write(bytes, 0, len);
if (file != null) {
sink.write(Okio.source(Channels.newInputStream(file.getChannel())), length);
} else if (stream != null) {
sink.write(Okio.source(stream), length);
} else {
sink.writeUtf8(data.toString());
sink.write(bytes, 0, length);
}
}
}
Loading

0 comments on commit f774bc4

Please sign in to comment.