Skip to content

Commit dde8989

Browse files
author
Lucas McDonald
committed
m
1 parent c23a407 commit dde8989

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void cancel() {
5858

5959
@Override
6060
public void onNext(ByteBuffer byteBuffer) {
61+
System.out.println("[CipherSubscriber] ByteBuffer content: " + byteBuffer.toString());
62+
// while (byteBuffer.hasRemaining()) {
63+
// byte b = byteBuffer.get();
64+
// System.out.printf("%02x ", b); // Print as hex
65+
// }
6166
System.out.println("[CipherSubscriber] onNext called with buffer size: " + byteBuffer.remaining());
6267
System.out.println("[CipherSubscriber] isLastPart: " + isLastPart);
6368
int amountToReadFromByteBuffer = getAmountToReadFromByteBuffer(byteBuffer);
@@ -67,7 +72,7 @@ public void onNext(ByteBuffer byteBuffer) {
6772
System.out.println("[CipherSubscriber] Processing chunk of size: " + amountToReadFromByteBuffer);
6873
byte[] buf = BinaryUtils.copyBytesFrom(byteBuffer, amountToReadFromByteBuffer);
6974
System.out.println("[CipherSubscriber] Copied " + buf.length + " bytes from input buffer");
70-
75+
7176
outputBuffer = cipher.update(buf, 0, amountToReadFromByteBuffer);
7277
System.out.println("[CipherSubscriber] Cipher update produced output buffer of length: " + (outputBuffer != null ? outputBuffer.length : 0));
7378

@@ -87,8 +92,8 @@ public void onNext(ByteBuffer byteBuffer) {
8792
if (contentRead.get() < amount) {
8893
wrappedSubscriber.onNext(ByteBuffer.wrap(outputBuffer));
8994
} else {
90-
System.out.println("[CipherSubscriber] All content read (contentRead: " + contentRead.get() + ", contentLength: " + contentLength + "), calling onComplete");
91-
this.onComplete();
95+
System.out.println("[CipherSubscriber] All content read (contentRead: " + contentRead.get() + ", contentLength: " + contentLength + "), waiting for onComplete");
96+
// this.onComplete();
9297
}
9398
}
9499
} else {
@@ -138,37 +143,38 @@ public void onComplete() {
138143
wrappedSubscriber.onComplete();
139144
return;
140145
}
146+
byte[] finalBytes;
141147
try {
142148
System.out.println("[CipherSubscriber] Calling cipher.doFinal()");
143-
byte[] finalBytes = cipher.doFinal();
144-
System.out.println("[CipherSubscriber] doFinal produced " + (finalBytes != null ? finalBytes.length : 0) + " bytes");
145-
146-
byte[] combinedBytes;
147-
if (outputBuffer != null && outputBuffer.length > 0 && finalBytes != null && finalBytes.length > 0) {
148-
System.out.println("[CipherSubscriber] Combining outputBuffer (" + outputBuffer.length + " bytes) with finalBytes (" + finalBytes.length + " bytes)");
149-
combinedBytes = new byte[outputBuffer.length + finalBytes.length];
150-
System.arraycopy(outputBuffer, 0, combinedBytes, 0, outputBuffer.length);
151-
System.arraycopy(finalBytes, 0, combinedBytes, outputBuffer.length, finalBytes.length);
152-
} else if (outputBuffer != null && outputBuffer.length > 0) {
153-
System.out.println("[CipherSubscriber] Using only outputBuffer (" + outputBuffer.length + " bytes)");
154-
combinedBytes = outputBuffer;
155-
} else if (finalBytes != null && finalBytes.length > 0) {
156-
System.out.println("[CipherSubscriber] Using only finalBytes (" + finalBytes.length + " bytes)");
157-
combinedBytes = finalBytes;
158-
} else {
159-
System.out.println("[CipherSubscriber] No bytes to send");
160-
combinedBytes = new byte[0];
161-
}
162-
163-
if (combinedBytes.length > 0) {
164-
System.out.println("[CipherSubscriber] Sending combined bytes to wrapped subscriber of length " + combinedBytes.length);
165-
wrappedSubscriber.onNext(ByteBuffer.wrap(combinedBytes));
166-
}
149+
finalBytes = cipher.doFinal();
167150
} catch (final GeneralSecurityException exception) {
168151
System.out.println("[CipherSubscriber] Error during doFinal: " + exception.getMessage());
169152
wrappedSubscriber.onError(exception);
170153
throw new S3EncryptionClientSecurityException(exception.getMessage(), exception);
171154
}
155+
System.out.println("[CipherSubscriber] doFinal produced " + (finalBytes != null ? finalBytes.length : 0) + " bytes");
156+
157+
byte[] combinedBytes;
158+
if (outputBuffer != null && outputBuffer.length > 0 && finalBytes != null && finalBytes.length > 0) {
159+
System.out.println("[CipherSubscriber] Combining outputBuffer (" + outputBuffer.length + " bytes) with finalBytes (" + finalBytes.length + " bytes)");
160+
combinedBytes = new byte[outputBuffer.length + finalBytes.length];
161+
System.arraycopy(outputBuffer, 0, combinedBytes, 0, outputBuffer.length);
162+
System.arraycopy(finalBytes, 0, combinedBytes, outputBuffer.length, finalBytes.length);
163+
} else if (outputBuffer != null && outputBuffer.length > 0) {
164+
System.out.println("[CipherSubscriber] Using only outputBuffer (" + outputBuffer.length + " bytes)");
165+
combinedBytes = outputBuffer;
166+
} else if (finalBytes != null && finalBytes.length > 0) {
167+
System.out.println("[CipherSubscriber] Using only finalBytes (" + finalBytes.length + " bytes)");
168+
combinedBytes = finalBytes;
169+
} else {
170+
System.out.println("[CipherSubscriber] No bytes to send");
171+
combinedBytes = new byte[0];
172+
}
173+
174+
if (combinedBytes.length > 0) {
175+
System.out.println("[CipherSubscriber] Sending combined bytes to wrapped subscriber of length " + combinedBytes.length);
176+
wrappedSubscriber.onNext(ByteBuffer.wrap(combinedBytes));
177+
}
172178
System.out.println("[CipherSubscriber] Completing wrapped subscriber");
173179
wrappedSubscriber.onComplete();
174180
}

0 commit comments

Comments
 (0)