-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
663 additions
and
6 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
53 changes: 53 additions & 0 deletions
53
...sdk-2.2/src/main/java/datadog/trace/instrumentation/aws/v2/ResponseBodyStreamWrapper.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,53 @@ | ||
package datadog.trace.instrumentation.aws.v2; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Buffers stream data that starts with '{' character meaning it is a JSON object. This is used to | ||
* read the response body from AWS SDK after it has been read by the SDK. | ||
*/ | ||
public class ResponseBodyStreamWrapper extends InputStream { | ||
|
||
private final InputStream originalStream; | ||
private ByteArrayOutputStream buffer; | ||
private boolean hasBeenRead = false; | ||
|
||
public ResponseBodyStreamWrapper(InputStream is) { | ||
this.originalStream = is; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
int value = originalStream.read(); | ||
|
||
if (!hasBeenRead) { | ||
if (value == '{') { | ||
// Start buffering only if it starts with '{' to avoid buffering non-json data | ||
buffer = new ByteArrayOutputStream(); | ||
} | ||
hasBeenRead = true; | ||
} | ||
|
||
if (buffer != null) { | ||
buffer.write(value); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
public Optional<ByteArrayInputStream> toByteArrayInputStream() { | ||
if (buffer == null) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new ByteArrayInputStream(buffer.toByteArray())); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
originalStream.close(); | ||
} | ||
} |
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
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
Oops, something went wrong.