Skip to content

Commit f029007

Browse files
author
Lucas McDonald
committed
m
1 parent 9ad2d16 commit f029007

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/main/java/software/amazon/encryption/s3/internal/CipherSubscriber.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,92 +53,129 @@ public void cancel() {
5353

5454
@Override
5555
public void onNext(ByteBuffer byteBuffer) {
56+
System.out.println("[CipherSubscriber] onNext called with buffer size: " + byteBuffer.remaining());
57+
System.out.println("[CipherSubscriber] isLastPart: " + isLastPart);
5658
int amountToReadFromByteBuffer = getAmountToReadFromByteBuffer(byteBuffer);
59+
System.out.println("[CipherSubscriber] amountToReadFromByteBuffer: " + amountToReadFromByteBuffer);
5760

5861
if (amountToReadFromByteBuffer > 0) {
62+
System.out.println("[CipherSubscriber] Processing chunk of size: " + amountToReadFromByteBuffer);
5963
byte[] buf = BinaryUtils.copyBytesFrom(byteBuffer, amountToReadFromByteBuffer);
64+
System.out.println("[CipherSubscriber] Copied " + buf.length + " bytes from input buffer");
65+
6066
outputBuffer = cipher.update(buf, 0, amountToReadFromByteBuffer);
67+
System.out.println("[CipherSubscriber] Cipher update produced output buffer of length: " + (outputBuffer != null ? outputBuffer.length : 0));
6168

6269
if (outputBuffer == null || outputBuffer.length == 0) {
70+
System.out.println("[CipherSubscriber] No output from cipher update");
6371
// No bytes provided from upstream; to avoid blocking, send an empty buffer to the wrapped subscriber.
6472
wrappedSubscriber.onNext(ByteBuffer.allocate(0));
6573
} else {
66-
boolean atEnd = isLastPart && contentRead.get() >= contentLength;
74+
boolean atEnd = isLastPart && contentRead.get() >= contentLength - 16;
75+
System.out.println("[CipherSubscriber] atEnd check - isLastPart: " + isLastPart +
76+
", contentRead: " + contentRead.get() +
77+
", contentLength: " + contentLength +
78+
", atEnd: " + atEnd);
6779

6880
if (atEnd) {
81+
System.out.println("[CipherSubscriber] Processing final bytes");
6982
// If all content has been read, send the final bytes in this onNext call.
7083
// The final bytes must be sent with the final onNext call, not during the onComplete call.
7184
byte[] finalBytes;
7285
try {
7386
finalBytes = cipher.doFinal();
7487
finalized = true;
88+
System.out.println("[CipherSubscriber] Cipher doFinal produced " + finalBytes.length + " bytes");
7589
} catch (final GeneralSecurityException exception) {
90+
System.out.println("[CipherSubscriber] Error during doFinal: " + exception.getMessage());
7691
wrappedSubscriber.onError(exception);
7792
throw new S3EncryptionClientSecurityException(exception.getMessage(), exception);
7893
}
7994

8095
// Combine outputBuffer and finalBytes if both exist
8196
byte[] combinedBuffer;
8297
if (outputBuffer != null && outputBuffer.length > 0) {
98+
System.out.println("[CipherSubscriber] Combining outputBuffer (" + outputBuffer.length + " bytes) with finalBytes (" + finalBytes.length + " bytes)");
8399
combinedBuffer = new byte[outputBuffer.length + finalBytes.length];
84100
System.arraycopy(outputBuffer, 0, combinedBuffer, 0, outputBuffer.length);
85101
System.arraycopy(finalBytes, 0, combinedBuffer, outputBuffer.length, finalBytes.length);
102+
System.out.println("[CipherSubscriber] Combined buffer total length: " + combinedBuffer.length);
86103
} else {
104+
System.out.println("[CipherSubscriber] Using only finalBytes (" + finalBytes.length + " bytes)");
87105
combinedBuffer = finalBytes;
88106
}
107+
System.out.println("[CipherSubscriber] Sending combined buffer to wrapped subscriber");
89108
wrappedSubscriber.onNext(ByteBuffer.wrap(combinedBuffer));
90109
} else {
110+
System.out.println("[CipherSubscriber] Sending " + outputBuffer.length + " bytes to wrapped subscriber");
91111
// Not at end; send content so far
92112
wrappedSubscriber.onNext(ByteBuffer.wrap(outputBuffer));
93113
}
94114
}
95115
} else {
116+
System.out.println("[CipherSubscriber] No bytes to read from input buffer, forwarding original buffer");
96117
// Do nothing
97118
wrappedSubscriber.onNext(byteBuffer);
98119
}
99120
}
100121

101122
private int getAmountToReadFromByteBuffer(ByteBuffer byteBuffer) {
123+
System.out.println("[CipherSubscriber] getAmountToReadFromByteBuffer called with buffer remaining: " + byteBuffer.remaining());
124+
System.out.println("[CipherSubscriber] Current contentRead: " + contentRead.get() + ", contentLength: " + contentLength);
125+
102126
// If content length is null, we should include everything in the cipher because the stream is essentially
103127
// unbounded.
104128
if (contentLength == null) {
129+
System.out.println("[CipherSubscriber] No content length specified, reading entire buffer: " + byteBuffer.remaining());
105130
return byteBuffer.remaining();
106131
}
107132

108133
long amountReadSoFar = contentRead.getAndAdd(byteBuffer.remaining());
109134
long amountRemaining = Math.max(0, contentLength - amountReadSoFar);
135+
System.out.println("[CipherSubscriber] amountReadSoFar: " + amountReadSoFar + ", amountRemaining: " + amountRemaining);
110136

111137
if (amountRemaining > byteBuffer.remaining()) {
138+
System.out.println("[CipherSubscriber] More remaining than buffer size, reading entire buffer: " + byteBuffer.remaining());
112139
return byteBuffer.remaining();
113140
} else {
141+
System.out.println("[CipherSubscriber] Reading partial buffer: " + amountRemaining);
114142
return Math.toIntExact(amountRemaining);
115143
}
116144
}
117145

118146
@Override
119147
public void onError(Throwable t) {
148+
System.out.println("[CipherSubscriber] onError called: " + t.getMessage());
120149
wrappedSubscriber.onError(t);
121150
}
122151

123152
@Override
124153
public void onComplete() {
154+
System.out.println("[CipherSubscriber] onComplete called, isLastPart: " + isLastPart);
125155
if (!isLastPart) {
156+
System.out.println("[CipherSubscriber] Not last part, skipping doFinal");
126157
// If this isn't the last part, skip doFinal, we aren't done
127158
wrappedSubscriber.onComplete();
128159
return;
129-
} if (finalized) {
160+
}
161+
if (finalized) {
162+
System.out.println("[CipherSubscriber] Finalized");
130163
wrappedSubscriber.onComplete();
131164
return;
132165
}
133166
try {
167+
System.out.println("[CipherSubscriber] Calling cipher.doFinal()");
134168
outputBuffer = cipher.doFinal();
169+
System.out.println("[CipherSubscriber] doFinal produced " + (outputBuffer != null ? outputBuffer.length : 0) + " bytes");
135170
// Send the final bytes to the wrapped subscriber
136171
wrappedSubscriber.onNext(ByteBuffer.wrap(outputBuffer));
137172
} catch (final GeneralSecurityException exception) {
173+
System.out.println("[CipherSubscriber] Error during doFinal: " + exception.getMessage());
138174
// Forward error, else the wrapped subscriber waits indefinitely
139175
wrappedSubscriber.onError(exception);
140176
throw new S3EncryptionClientSecurityException(exception.getMessage(), exception);
141177
}
178+
System.out.println("[CipherSubscriber] Completing wrapped subscriber");
142179
wrappedSubscriber.onComplete();
143180
}
144181

0 commit comments

Comments
 (0)