Skip to content

Commit

Permalink
Avoid path.toFile calls
Browse files Browse the repository at this point in the history
  • Loading branch information
telendt authored and dagnir committed Jun 12, 2018
1 parent c3891a4 commit bffab64
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package software.amazon.awssdk.core.async;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -57,7 +59,11 @@ private FileAsyncRequestBody(DefaultBuilder builder) {

@Override
public long contentLength() {
return path.toFile().length();
try {
return Files.size(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@

package software.amazon.awssdk.core.sync;

import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;
import static software.amazon.awssdk.utils.Validate.paramNotNull;
import static software.amazon.awssdk.utils.Validate.validState;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import software.amazon.awssdk.core.runtime.io.ReleasableInputStream;
Expand Down Expand Up @@ -81,7 +82,13 @@ public String contentType() {
* @return RequestBody instance.
*/
public static RequestBody fromFile(Path path) {
return fromFile(path.toFile());
try {
return new RequestBody(Files.newInputStream(path),
Files.size(path),
Mimetype.getInstance().getMimetype(path));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
Expand All @@ -91,9 +98,7 @@ public static RequestBody fromFile(Path path) {
* @return RequestBody instance.
*/
public static RequestBody fromFile(File file) {
return new RequestBody(invokeSafely(() -> new FileInputStream(file)),
file.length(),
Mimetype.getInstance().getMimetype(file));
return fromFile(file.toPath());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -104,6 +105,24 @@ public static Mimetype getInstance() {
return mimetype;
}

/**
* Determines the mimetype of a file by looking up the file's extension in an internal listing
* to find the corresponding mime type. If the file has no extension, or the extension is not
* available in the listing contained in this class, the default mimetype
* <code>application/octet-stream</code> is returned.
* <p>
* A file extension is one or more characters that occur after the last period (.) in the file's name.
* If a file has no extension,
* Guesses the mimetype of file data based on the file's extension.
*
* @param path the file whose extension may match a known mimetype.
* @return the file's mimetype based on its extension, or a default value of
* <code>application/octet-stream</code> if a mime type value cannot be found.
*/
public String getMimetype(Path path) {
return getMimetype(path.getFileName().toString());
}

/**
* Determines the mimetype of a file by looking up the file's extension in an internal listing
* to find the corresponding mime type. If the file has no extension, or the extension is not
Expand All @@ -119,7 +138,7 @@ public static Mimetype getInstance() {
* <code>application/octet-stream</code> if a mime type value cannot be found.
*/
public String getMimetype(File file) {
return getMimetype(file.getName());
return getMimetype(file.toPath());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,81 @@
package software.amazon.awssdk.core.async;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CountDownLatch;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.reactivestreams.Subscriber;
import software.amazon.awssdk.http.async.SimpleSubscriber;

@RunWith(Parameterized.class)
public class AsyncRequestBodyTest {
private final static String testString = "Hello!";
private final static Path path;

static {
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
path = fs.getPath("./test");
try {
Files.write(path, testString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

@Parameterized.Parameters
public static AsyncRequestBody[] data() {
return new AsyncRequestBody[]{
AsyncRequestBody.fromString(testString),
AsyncRequestBody.fromFile(path)
};
}

private AsyncRequestBody provider;

public AsyncRequestBodyTest(AsyncRequestBody provider) {
this.provider = provider;
}

@Test
public void hasCorrectLength() {
assertThat(provider.contentLength()).isEqualTo(testString.length());
}

@Test
public void canCreateAStringRequestBody() {
AsyncRequestBody provider = AsyncRequestBody.fromString("Hello!");
public void hasCorrectContent() throws InterruptedException {
StringBuilder sb = new StringBuilder();
boolean done = false;
CountDownLatch done = new CountDownLatch(1);

Subscriber<ByteBuffer> subscriber = spy(new SimpleSubscriber(buffer -> {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
sb.append(new String(bytes, StandardCharsets.UTF_8));
}));
Subscriber<ByteBuffer> subscriber = new SimpleSubscriber(buffer -> {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
sb.append(new String(bytes, StandardCharsets.UTF_8));
}) {
@Override
public void onError(Throwable t) {
super.onError(t);
done.countDown();
}

provider.subscribe(subscriber);
@Override
public void onComplete() {
super.onComplete();
done.countDown();
}
};

assertThat(sb.toString()).isEqualTo("Hello!");
assertThat(provider.contentLength()).isEqualTo(6);
verify(subscriber).onComplete();
verify(subscriber, times(0)).onError(any(Throwable.class));
provider.subscribe(subscriber);
done.await();
assertThat(sb.toString()).isEqualTo(testString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import org.junit.Test;
import software.amazon.awssdk.core.util.Mimetype;
import software.amazon.awssdk.core.util.StringInputStream;
Expand All @@ -40,6 +47,15 @@ public void stringConstructorHasCorrectContentType() {
assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_TEXT_PLAIN);
}

@Test
public void fileConstructorHasCorrectContentType() throws IOException {
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path path = fs.getPath("./test");
Files.write(path, "hello world".getBytes());
RequestBody requestBody = RequestBody.fromFile(path);
assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM);
}

@Test
public void streamConstructorHasCorrectContentType() {
StringInputStream inputStream = new StringInputStream("hello world");
Expand Down

0 comments on commit bffab64

Please sign in to comment.