Skip to content

Commit 37fe1eb

Browse files
jean-philippe-martinmziccard
authored andcommitted
Handle files over 2GB (googleapis#1065)
* Handle files over 2GB * Use Guava's checkArgument
1 parent 312565c commit 37fe1eb

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public SeekableByteChannel position(long newPosition) throws IOException {
118118
if (newPosition == position) {
119119
return this;
120120
}
121-
channel.seek((int) newPosition);
121+
channel.seek(newPosition);
122122
position = newPosition;
123123
return this;
124124
}

gcloud-java-core/src/main/java/com/google/cloud/ReadChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface ReadChannel extends ReadableByteChannel, Closeable, Restorable<
3838
@Override
3939
void close();
4040

41-
void seek(int position) throws IOException;
41+
void seek(long position) throws IOException;
4242

4343
/**
4444
* Sets the minimum size that will be read by a single RPC.

gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/ParallelCountBytes.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public WorkUnit(SeekableByteChannel chan, int blockSize, int blockIndex) {
7474

7575
@Override
7676
public WorkUnit call() throws IOException {
77-
int pos = blockSize * blockIndex;
77+
long pos = ((long)blockSize) * blockIndex;
7878
if (pos > chan.size()) {
7979
return this;
8080
}
@@ -90,6 +90,7 @@ public WorkUnit resetForIndex(int blockIndex) {
9090
return this;
9191
}
9292

93+
9394
public void close() throws IOException {
9495
chan.close();
9596
}

gcloud-java-storage/src/main/java/com/google/cloud/storage/BlobReadChannel.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BlobReadChannel implements ReadChannel {
4545
private final BlobId blob;
4646
private final Map<StorageRpc.Option, ?> requestOptions;
4747
private String lastEtag;
48-
private int position;
48+
private long position;
4949
private boolean isOpen;
5050
private boolean endOfStream;
5151
private int chunkSize = DEFAULT_CHUNK_SIZE;
@@ -99,7 +99,7 @@ private void validateOpen() throws ClosedChannelException {
9999
}
100100

101101
@Override
102-
public void seek(int position) throws IOException {
102+
public void seek(long position) throws IOException {
103103
validateOpen();
104104
this.position = position;
105105
buffer = null;
@@ -164,7 +164,7 @@ static class StateImpl implements RestorableState<ReadChannel>, Serializable {
164164
private final BlobId blob;
165165
private final Map<StorageRpc.Option, ?> requestOptions;
166166
private final String lastEtag;
167-
private final int position;
167+
private final long position;
168168
private final boolean isOpen;
169169
private final boolean endOfStream;
170170
private final int chunkSize;
@@ -185,7 +185,7 @@ static class Builder {
185185
private final BlobId blob;
186186
private final Map<StorageRpc.Option, ?> requestOptions;
187187
private String lastEtag;
188-
private int position;
188+
private long position;
189189
private boolean isOpen;
190190
private boolean endOfStream;
191191
private int chunkSize;
@@ -201,7 +201,7 @@ Builder lastEtag(String lastEtag) {
201201
return this;
202202
}
203203

204-
Builder position(int position) {
204+
Builder position(long position) {
205205
this.position = position;
206206
return this;
207207
}

gcloud-java-storage/src/main/java/com/google/cloud/storage/spi/DefaultStorageRpc.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static com.google.cloud.storage.spi.StorageRpc.Option.PREFIX;
3232
import static com.google.cloud.storage.spi.StorageRpc.Option.VERSIONS;
3333
import static com.google.common.base.MoreObjects.firstNonNull;
34+
import static com.google.common.base.Preconditions.checkArgument;
3435
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
3536
import static javax.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE;
3637

@@ -467,10 +468,11 @@ public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, lo
467468
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
468469
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
469470
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options));
471+
checkArgument(position >= 0, "Position should be non-negative, is %d", position);
470472
StringBuilder range = new StringBuilder();
471473
range.append("bytes=").append(position).append("-").append(position + bytes - 1);
472474
req.getRequestHeaders().setRange(range.toString());
473-
ByteArrayOutputStream output = new ByteArrayOutputStream();
475+
ByteArrayOutputStream output = new ByteArrayOutputStream(bytes);
474476
req.executeMedia().download(output);
475477
String etag = req.getLastResponseHeaders().getETag();
476478
return Tuple.of(etag, output.toByteArray());

0 commit comments

Comments
 (0)