@@ -63,12 +63,60 @@ final class GapicUnbufferedWritableByteChannel<
63
63
}
64
64
65
65
@ Override
66
- public long write (ByteBuffer [] srcs , int srcsOffset , int srcLength ) throws IOException {
66
+ public long write (ByteBuffer [] srcs , int srcsOffset , int srcsLength ) throws IOException {
67
+ return internalWrite (srcs , srcsOffset , srcsLength , false );
68
+ }
69
+
70
+ @ Override
71
+ public long writeAndClose (ByteBuffer [] srcs , int srcsOffset , int srcsLength ) throws IOException {
72
+ long write = internalWrite (srcs , srcsOffset , srcsLength , true );
73
+ close ();
74
+ return write ;
75
+ }
76
+
77
+ @ Override
78
+ public boolean isOpen () {
79
+ return open ;
80
+ }
81
+
82
+ @ Override
83
+ public void close () throws IOException {
84
+ if (!finished ) {
85
+ long offset = writeCtx .getTotalSentBytes ().get ();
86
+ Crc32cLengthKnown crc32cValue = writeCtx .getCumulativeCrc32c ().get ();
87
+
88
+ WriteObjectRequest .Builder b =
89
+ writeCtx .newRequestBuilder ().setFinishWrite (true ).setWriteOffset (offset );
90
+ if (crc32cValue != null ) {
91
+ b .setObjectChecksums (
92
+ ObjectChecksums .newBuilder ().setCrc32C (crc32cValue .getValue ()).build ());
93
+ }
94
+ WriteObjectRequest message = b .build ();
95
+ try {
96
+ flusher .close (message );
97
+ finished = true ;
98
+ } catch (RuntimeException e ) {
99
+ resultFuture .setException (e );
100
+ throw e ;
101
+ }
102
+ } else {
103
+ flusher .close (null );
104
+ }
105
+ open = false ;
106
+ }
107
+
108
+ @ VisibleForTesting
109
+ WriteCtx <RequestFactoryT > getWriteCtx () {
110
+ return writeCtx ;
111
+ }
112
+
113
+ private long internalWrite (ByteBuffer [] srcs , int srcsOffset , int srcsLength , boolean finalize )
114
+ throws ClosedChannelException {
67
115
if (!open ) {
68
116
throw new ClosedChannelException ();
69
117
}
70
118
71
- ChunkSegment [] data = chunkSegmenter .segmentBuffers (srcs , srcsOffset , srcLength );
119
+ ChunkSegment [] data = chunkSegmenter .segmentBuffers (srcs , srcsOffset , srcsLength );
72
120
73
121
List <WriteObjectRequest > messages = new ArrayList <>();
74
122
@@ -91,7 +139,7 @@ public long write(ByteBuffer[] srcs, int srcsOffset, int srcLength) throws IOExc
91
139
.newRequestBuilder ()
92
140
.setWriteOffset (offset )
93
141
.setChecksummedData (checksummedData .build ());
94
- if (!datum .isOnlyFullBlocks ()) {
142
+ if (!datum .isOnlyFullBlocks () || finalize ) {
95
143
builder .setFinishWrite (true );
96
144
if (cumulative != null ) {
97
145
builder .setObjectChecksums (
@@ -114,40 +162,4 @@ public long write(ByteBuffer[] srcs, int srcsOffset, int srcLength) throws IOExc
114
162
115
163
return bytesConsumed ;
116
164
}
117
-
118
- @ Override
119
- public boolean isOpen () {
120
- return open ;
121
- }
122
-
123
- @ Override
124
- public void close () throws IOException {
125
- if (!finished ) {
126
- long offset = writeCtx .getTotalSentBytes ().get ();
127
- Crc32cLengthKnown crc32cValue = writeCtx .getCumulativeCrc32c ().get ();
128
-
129
- WriteObjectRequest .Builder b =
130
- writeCtx .newRequestBuilder ().setFinishWrite (true ).setWriteOffset (offset );
131
- if (crc32cValue != null ) {
132
- b .setObjectChecksums (
133
- ObjectChecksums .newBuilder ().setCrc32C (crc32cValue .getValue ()).build ());
134
- }
135
- WriteObjectRequest message = b .build ();
136
- try {
137
- flusher .close (message );
138
- finished = true ;
139
- } catch (RuntimeException e ) {
140
- resultFuture .setException (e );
141
- throw e ;
142
- }
143
- } else {
144
- flusher .close (null );
145
- }
146
- open = false ;
147
- }
148
-
149
- @ VisibleForTesting
150
- WriteCtx <RequestFactoryT > getWriteCtx () {
151
- return writeCtx ;
152
- }
153
165
}
0 commit comments