Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.base.Function;
import com.google.common.io.BaseEncoding;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;
import java.security.Key;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -186,6 +192,26 @@ static Storage.BlobGetOption[] toGetOptions(BlobInfo blobInfo, BlobSourceOption.
}
}

/**
* Downloads this blob to the given file path.
*
* @param path destination
* @throws IOException upon failure
*/
public void downloadTo(Path path) throws IOException {

This comment was marked as spam.

This comment was marked as spam.

FileOutputStream outputStream = new FileOutputStream(path.toFile());

This comment was marked as spam.

This comment was marked as spam.

try (ReadChannel reader = reader()) {
WritableByteChannel channel = Channels.newChannel(outputStream);
ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);

This comment was marked as spam.

This comment was marked as spam.

while (reader.read(bytes) > 0) {
bytes.flip();
channel.write(bytes);
bytes.clear();
}
}
outputStream.close();
}

/**
* Builder for {@code Blob}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.google.cloud.storage;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.getCurrentArguments;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertArrayEquals;
Expand All @@ -43,11 +46,15 @@
import com.google.common.io.BaseEncoding;

import org.easymock.Capture;
import org.easymock.IAnswer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.security.Key;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -517,4 +524,34 @@ public void testBuilder() {
assertNull(blob.getUpdateTime());
assertTrue(blob.isDirectory());
}

@Test
public void testDownloadTo() throws Exception {
final byte[] expected = {1, 2};

initializeExpectedBlob(2);
ReadChannel channel = createNiceMock(ReadChannel.class);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.reader(BLOB_INFO.getBlobId())).andReturn(channel);
replay(storage);
// First read should return 2 bytes.
expect(channel.read(anyObject(ByteBuffer.class)))
.andAnswer(new IAnswer<Integer>() {
@Override
public Integer answer() throws Throwable {
// Modify the argument to match the expected behavior of `read`.
((ByteBuffer) getCurrentArguments()[0]).put(expected);
return 2;
}
});
// Second read should return 0 bytes.
expect(channel.read(anyObject(ByteBuffer.class))).andReturn(0);
replay(channel);
initializeBlob();

File file = File.createTempFile("blob", ".tmp");
blob.downloadTo(file.toPath());
byte actual[] = Files.readAllBytes(file.toPath());
assertArrayEquals(expected, actual);
}
}