Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor http execution to compute md5sum when needed #852

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -30,6 +30,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 @@ -16,8 +16,8 @@

package io.minio;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.Channels;
import okhttp3.MediaType;
Expand All @@ -27,14 +27,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 @@ -53,30 +67,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