-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract classes to own files, more StorageClient initialization
- Loading branch information
1 parent
7b41aa1
commit 4ad2a62
Showing
5 changed files
with
99 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
google-cloud-storage/src/main/java/com/google/cloud/storage/ReadCursor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.google.cloud.storage; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
/** | ||
* Shrink wraps a beginning, offset and limit for tracking state of an individual invocation of | ||
* {@link #read} | ||
*/ | ||
final class ReadCursor { | ||
private final long beginning; | ||
private long offset; | ||
private final long limit; | ||
|
||
ReadCursor(long beginning, long limit) { | ||
this.limit = limit; | ||
this.beginning = beginning; | ||
this.offset = beginning; | ||
} | ||
|
||
public boolean hasRemaining() { | ||
return limit - offset > 0; | ||
} | ||
|
||
public void advance(long incr) { | ||
checkArgument(incr >= 0); | ||
offset += incr; | ||
} | ||
|
||
public long read() { | ||
return offset - beginning; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("ReadCursor{begin=%d, offset=%d, limit=%d}", beginning, offset, limit); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...-cloud-storage/src/main/java/com/google/cloud/storage/ResponseContentLifecycleHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.google.cloud.storage; | ||
|
||
import com.google.storage.v2.ReadObjectResponse; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
|
||
final class ResponseContentLifecycleHandle implements Closeable { | ||
@Nullable | ||
private final Closeable dispose; | ||
|
||
private final List<ByteBuffer> buffers; | ||
|
||
ResponseContentLifecycleHandle(ReadObjectResponse resp, @Nullable Closeable dispose) { | ||
this.dispose = dispose; | ||
|
||
this.buffers = resp.getChecksummedData().getContent().asReadOnlyByteBufferList(); | ||
} | ||
|
||
void copy(ReadCursor c, ByteBuffer[] dsts, int offset, int length) { | ||
for (ByteBuffer b : buffers) { | ||
long copiedBytes = Buffers.copy(b, dsts, offset, length); | ||
c.advance(copiedBytes); | ||
if (b.hasRemaining()) break; | ||
} | ||
} | ||
|
||
boolean hasRemaining() { | ||
for (ByteBuffer b : buffers) { | ||
if (b.hasRemaining()) return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (dispose != null) { | ||
dispose.close(); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...cloud-storage/src/main/java/com/google/cloud/storage/ResponseContentLifecycleManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.google.cloud.storage; | ||
|
||
import com.google.storage.v2.ReadObjectResponse; | ||
|
||
interface ResponseContentLifecycleManager { | ||
ResponseContentLifecycleHandle get(ReadObjectResponse response); | ||
|
||
static ResponseContentLifecycleManager noop() { | ||
return response -> | ||
new ResponseContentLifecycleHandle( | ||
response, | ||
() -> { | ||
// no-op | ||
}); | ||
} | ||
} |