@@ -20,6 +20,7 @@ public class CipherSubscriber implements Subscriber<ByteBuffer> {
2020 private final Long contentLength ;
2121 private boolean isLastPart ;
2222 private int tagLength ;
23+ private boolean onCompleteCalled = false ;
2324
2425 private byte [] outputBuffer ;
2526
@@ -47,16 +48,33 @@ public class CipherSubscriber implements Subscriber<ByteBuffer> {
4748
4849 @ Override
4950 public void onSubscribe (Subscription s ) {
50- wrappedSubscriber .onSubscribe (s );
51+ System .out .println ("[CipherSubscriber] onSubscribe called with subscription: " + s );
52+ wrappedSubscriber .onSubscribe (new Subscription () {
53+ @ Override
54+ public void request (long n ) {
55+ System .out .println ("[CipherSubscriber] Request received for " + n + " items" );
56+ s .request (n );
57+ }
58+
59+ @ Override
60+ public void cancel () {
61+ System .out .println ("[CipherSubscriber] Subscription cancelled" );
62+ s .cancel ();
63+ }
64+ });
5165 }
5266
5367 @ Override
5468 public void onNext (ByteBuffer byteBuffer ) {
69+ System .out .println ("[CipherSubscriber] onNext called with buffer size: " + byteBuffer .remaining () + ", contentRead: " + contentRead .get () + ", contentLength: " + contentLength );
5570 int amountToReadFromByteBuffer = getAmountToReadFromByteBuffer (byteBuffer );
71+ System .out .println ("[CipherSubscriber] Amount to read from buffer: " + amountToReadFromByteBuffer );
5672
5773 if (amountToReadFromByteBuffer > 0 ) {
5874 byte [] buf = BinaryUtils .copyBytesFrom (byteBuffer , amountToReadFromByteBuffer );
75+ System .out .println ("[CipherSubscriber] Copied " + buf .length + " bytes from input buffer" );
5976 outputBuffer = cipher .update (buf , 0 , amountToReadFromByteBuffer );
77+ System .out .println ("[CipherSubscriber] Cipher update produced " + (outputBuffer != null ? outputBuffer .length : 0 ) + " bytes" );
6078
6179 if (outputBuffer == null || outputBuffer .length == 0 ) {
6280 // The underlying data is too short to fill in the block cipher.
@@ -65,12 +83,14 @@ public void onNext(ByteBuffer byteBuffer) {
6583 // null OR length == 0.
6684 if (contentRead .get () + tagLength >= contentLength ) {
6785 // All content has been read, so complete to get the final bytes
86+ System .out .println ("[CipherSubscriber] All content read (" + contentRead .get () + " bytes), proceeding to finalBytes" );
6887 finalBytes ();
6988 return ;
7089 }
7190 // Otherwise, wait for more bytes. To avoid blocking,
7291 // send an empty buffer to the wrapped subscriber.
73- wrappedSubscriber .onNext (ByteBuffer .allocate (0 ));
92+ System .out .println ("[CipherSubscriber] Sending empty buffer to wrapped subscriber" );
93+ // wrappedSubscriber.onNext(ByteBuffer.allocate(0));
7494 } else {
7595 // Check if stream has read all expected content.
7696 // Once all content has been read, call onComplete.
@@ -89,19 +109,23 @@ public void onNext(ByteBuffer byteBuffer) {
89109 // including the result of cipher.doFinal(), if applicable.
90110 // Calling `wrappedSubscriber.onNext` more than once for `request(1)`
91111 // violates the Reactive Streams specification and can cause exceptions downstream.
112+ System .out .println ("[CipherSubscriber] Checking content read threshold: contentRead=" + contentRead .get () + ", tagLength=" + tagLength + ", contentLength=" + contentLength );
92113 if (contentRead .get () + tagLength >= contentLength ) {
93114 // All content has been read; complete the stream.
94115 // (Signalling onComplete from here is Reactive Streams-spec compliant;
95116 // this class is allowed to call onComplete, even if upstream has not yet signaled onComplete.)
117+ System .out .println ("[CipherSubscriber] Content read threshold reached, proceeding to finalBytes" );
96118 finalBytes ();
97119 } else {
98120 // Needs to read more data, so send the data downstream,
99121 // expecting that downstream will continue to request more data.
122+ System .out .println ("[CipherSubscriber] Sending " + outputBuffer .length + " bytes to wrapped subscriber" );
100123 wrappedSubscriber .onNext (ByteBuffer .wrap (outputBuffer ));
101124 }
102125 }
103126 } else {
104127 // Do nothing
128+ System .out .println ("[CipherSubscriber] No data to process, forwarding buffer directly" );
105129 wrappedSubscriber .onNext (byteBuffer );
106130 }
107131 }
@@ -110,48 +134,73 @@ private int getAmountToReadFromByteBuffer(ByteBuffer byteBuffer) {
110134 // If content length is null, we should include everything in the cipher because the stream is essentially
111135 // unbounded.
112136 if (contentLength == null ) {
137+ System .out .println ("[CipherSubscriber] Content length is null, reading entire buffer: " + byteBuffer .remaining ());
113138 return byteBuffer .remaining ();
114139 }
115140
116141 long amountReadSoFar = contentRead .getAndAdd (byteBuffer .remaining ());
117142 long amountRemaining = Math .max (0 , contentLength - amountReadSoFar );
143+ System .out .println ("[CipherSubscriber] Buffer read calculation - read: " + amountReadSoFar + ", remaining: " + amountRemaining + ", buffer size: " + byteBuffer .remaining ());
118144
119145 if (amountRemaining > byteBuffer .remaining ()) {
146+ System .out .println ("[CipherSubscriber] Reading entire buffer: " + byteBuffer .remaining ());
120147 return byteBuffer .remaining ();
121148 } else {
149+ System .out .println ("[CipherSubscriber] Reading partial buffer: " + amountRemaining );
122150 return Math .toIntExact (amountRemaining );
123151 }
124152 }
125153
126154 @ Override
127155 public void onError (Throwable t ) {
156+ System .out .println ("[CipherSubscriber] Error occurred: " + t .getMessage ());
128157 wrappedSubscriber .onError (t );
129158 }
130159
131160 @ Override
132161 public void onComplete () {
162+ System .out .println ("[CipherSubscriber] onComplete called" );
133163 wrappedSubscriber .onComplete ();
134164 }
135165
136166 public void finalBytes () {
167+ System .out .println ("[CipherSubscriber] finalBytes called, isLastPart: " + isLastPart + ", onCompleteCalled: " + onCompleteCalled );
168+ // onComplete can be signalled to CipherSubscriber multiple times,
169+ // but additional calls should be deduped to avoid calling onNext multiple times
170+ // and raising exceptions.
171+ if (onCompleteCalled ) {
172+ System .out .println ("[CipherSubscriber] finalBytes already called, returning" );
173+ return ;
174+ }
175+ onCompleteCalled = true ;
176+
137177 // If this isn't the last part, skip doFinal and just send outputBuffer downstream.
138178 // doFinal requires that all parts have been processed to compute the tag,
139179 // so the tag will only be computed when the last part is processed.
140180 if (!isLastPart ) {
181+ System .out .println ("[CipherSubscriber] Not last part, sending output buffer of size: " + (outputBuffer != null ? outputBuffer .length : 0 ));
182+ // First, propagate the bytes that were in outputBuffer downstream.
141183 wrappedSubscriber .onNext (ByteBuffer .wrap (outputBuffer ));
184+ // Then, propagate the onComplete signal downstream.
185+ // wrappedSubscriber.onComplete();
142186 return ;
143187 }
144188
145189 // If this is the last part, compute doFinal and include its result in the value sent downstream.
146190 // The result of doFinal MUST be included with the bytes that were in outputBuffer in the final onNext call.
147- byte [] finalBytes ;
191+ byte [] finalBytes = null ;
148192 try {
193+ System .out .println ("[CipherSubscriber] Calling cipher.doFinal()" );
149194 finalBytes = cipher .doFinal ();
195+ System .out .println ("[CipherSubscriber] doFinal produced " + (finalBytes != null ? finalBytes .length : 0 ) + " bytes" );
150196 } catch (final GeneralSecurityException exception ) {
151197 // Even if doFinal fails, downstream still expects to receive the bytes that were in outputBuffer
198+ System .out .println ("[CipherSubscriber] Security exception during doFinal: " + exception .getMessage ());
152199 wrappedSubscriber .onNext (ByteBuffer .wrap (outputBuffer ));
153200 // Forward error, else the wrapped subscriber waits indefinitely
154201 wrappedSubscriber .onError (exception );
202+ // Even though doFinal failed, propagate the onComplete signal downstream
203+ // wrappedSubscriber.onComplete();
155204 throw new S3EncryptionClientSecurityException (exception .getMessage (), exception );
156205 }
157206
@@ -160,18 +209,24 @@ public void finalBytes() {
160209 // This single onNext call must contain both the bytes from outputBuffer and the tag.
161210 byte [] combinedBytes ;
162211 if (outputBuffer != null && outputBuffer .length > 0 && finalBytes != null && finalBytes .length > 0 ) {
212+ System .out .println ("[CipherSubscriber] Combining outputBuffer (" + outputBuffer .length + " bytes) with finalBytes (" + finalBytes .length + " bytes)" );
163213 combinedBytes = new byte [outputBuffer .length + finalBytes .length ];
164214 System .arraycopy (outputBuffer , 0 , combinedBytes , 0 , outputBuffer .length );
165215 System .arraycopy (finalBytes , 0 , combinedBytes , outputBuffer .length , finalBytes .length );
166216 } else if (outputBuffer != null && outputBuffer .length > 0 ) {
217+ System .out .println ("[CipherSubscriber] Using only outputBuffer (" + outputBuffer .length + " bytes)" );
167218 combinedBytes = outputBuffer ;
168219 } else if (finalBytes != null && finalBytes .length > 0 ) {
220+ System .out .println ("[CipherSubscriber] Using only finalBytes (" + finalBytes .length + " bytes)" );
169221 combinedBytes = finalBytes ;
170222 } else {
223+ System .out .println ("[CipherSubscriber] No bytes to send" );
171224 combinedBytes = new byte [0 ];
172225 }
173226
227+ System .out .println ("[CipherSubscriber] Sending combined bytes to wrapped subscriber of length " + combinedBytes .length );
174228 wrappedSubscriber .onNext (ByteBuffer .wrap (combinedBytes ));
229+ // wrappedSubscriber.onComplete();
175230 }
176231
177232}
0 commit comments