-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.java
46 lines (36 loc) · 1.42 KB
/
Util.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package software.amazon.payloadoffloading;
import java.util.concurrent.CompletionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.util.VersionInfo;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class Util {
private static final Logger LOG = LoggerFactory.getLogger(Util.class);
public static long getStringSizeInBytes(String str) {
CountingOutputStream counterOutputStream = new CountingOutputStream();
try {
Writer writer = new OutputStreamWriter(counterOutputStream, StandardCharsets.UTF_8);
writer.write(str);
writer.flush();
writer.close();
} catch (IOException e) {
String errorMessage = "Failed to calculate the size of payload.";
LOG.error(errorMessage, e);
throw SdkClientException.create(errorMessage, e);
}
return counterOutputStream.getTotalSize();
}
public static String getUserAgentHeader(String clientName) {
return clientName + "/" + VersionInfo.SDK_VERSION;
}
public static Throwable unwrapFutureException(Throwable t) {
if ((t instanceof CompletionException) && t.getCause() != null) {
t = t.getCause();
}
return t;
}
}