Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;

class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore {
Expand Down Expand Up @@ -163,16 +164,19 @@ boolean blobExists(String blobName) throws IOException {
*/
InputStream readBlob(String blobName) throws IOException {
final BlobId blobId = BlobId.of(bucketName, blobName);
final Blob blob = SocketAccess.doPrivilegedIOException(() -> client().get(blobId));
if (blob == null) {
throw new NoSuchFileException("Blob [" + blobName + "] does not exit");
}
final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(blob::reader);
final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(() -> client().reader(blobId));
return Channels.newInputStream(new ReadableByteChannel() {
@SuppressForbidden(reason = "Channel is based of a socket not a file")
@Override
public int read(ByteBuffer dst) throws IOException {
return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst));
try {
return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst));
} catch (StorageException e) {
if (e.getCode() == HTTP_NOT_FOUND) {
throw new NoSuchFileException("Blob [" + blobName + "] does not exist");
}
throw e;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,27 @@ public Iterable<Blob> getValues() {
public ReadChannel reader(BlobId blob, BlobSourceOption... options) {
if (bucketName.equals(blob.getBucket())) {
final byte[] bytes = blobs.get(blob.getName());
final ReadableByteChannel readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes));

final ReadableByteChannel readableByteChannel;
if (bytes != null) {
readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes));
} else {
readableByteChannel = new ReadableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
throw new StorageException(404, "Object not found");
}

@Override
public boolean isOpen() {
return false;
}

@Override
public void close() throws IOException {
}
};
}
return new ReadChannel() {
@Override
public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public abstract class ESBlobStoreContainerTestCase extends ESTestCase {
public void testReadNonExistingPath() throws IOException {
try(BlobStore store = newBlobStore()) {
final BlobContainer container = store.blobContainer(new BlobPath());
expectThrows(NoSuchFileException.class, () -> container.readBlob("non-existing"));
expectThrows(NoSuchFileException.class, () -> {
try (InputStream is = container.readBlob("non-existing")) {
is.read();
}
});
}
}

Expand Down