1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.core; 17 18 import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; 19 20 import java.io.InputStream; 21 import java.io.Serializable; 22 import java.nio.ByteBuffer; 23 import java.nio.charset.Charset; 24 import java.nio.charset.StandardCharsets; 25 import java.util.Arrays; 26 import software.amazon.awssdk.annotations.SdkPublicApi; 27 import software.amazon.awssdk.utils.BinaryUtils; 28 import software.amazon.awssdk.utils.IoUtils; 29 import software.amazon.awssdk.utils.ToString; 30 import software.amazon.awssdk.utils.Validate; 31 32 /** 33 * An in-memory representation of data being given to a service or being returned by a service. 34 * 35 * This can be created via static methods, like {@link SdkBytes#fromByteArray(byte[])}. This can be converted to binary types 36 * via instance methods, like {@link SdkBytes#asByteArray()}. 37 */ 38 @SdkPublicApi 39 public final class SdkBytes extends BytesWrapper implements Serializable { 40 41 private static final long serialVersionUID = 1L; 42 43 // Needed for serialization SdkBytes()44 private SdkBytes() { 45 super(); 46 } 47 48 /** 49 * @see #fromByteArray(byte[]) 50 * @see #fromByteBuffer(ByteBuffer) 51 * @see #fromInputStream(InputStream) 52 * @see #fromUtf8String(String) 53 * @see #fromString(String, Charset) 54 */ SdkBytes(byte[] bytes)55 private SdkBytes(byte[] bytes) { 56 super(bytes); 57 } 58 59 /** 60 * Create {@link SdkBytes} from a Byte buffer. This will read the remaining contents of the byte buffer. 61 */ fromByteBuffer(ByteBuffer byteBuffer)62 public static SdkBytes fromByteBuffer(ByteBuffer byteBuffer) { 63 Validate.paramNotNull(byteBuffer, "byteBuffer"); 64 return new SdkBytes(BinaryUtils.copyBytesFrom(byteBuffer)); 65 } 66 67 /** 68 * Create {@link SdkBytes} from a Byte array. This will copy the contents of the byte array. 69 */ fromByteArray(byte[] bytes)70 public static SdkBytes fromByteArray(byte[] bytes) { 71 Validate.paramNotNull(bytes, "bytes"); 72 return new SdkBytes(Arrays.copyOf(bytes, bytes.length)); 73 } 74 75 /** 76 * Create {@link SdkBytes} from a Byte array <b>without</b> copying the contents of the byte array. This introduces 77 * concurrency risks, allowing: (1) the caller to modify the byte array stored in this {@code SdkBytes} implementation AND 78 * (2) any users of {@link #asByteArrayUnsafe()} to modify the byte array passed into this {@code SdkBytes} implementation. 79 * 80 * <p>As the method name implies, this is unsafe. Use {@link #fromByteArray(byte[])} unless you're sure you know the risks. 81 */ fromByteArrayUnsafe(byte[] bytes)82 public static SdkBytes fromByteArrayUnsafe(byte[] bytes) { 83 Validate.paramNotNull(bytes, "bytes"); 84 return new SdkBytes(bytes); 85 } 86 87 /** 88 * Create {@link SdkBytes} from a string, using the provided charset. 89 */ fromString(String string, Charset charset)90 public static SdkBytes fromString(String string, Charset charset) { 91 Validate.paramNotNull(string, "string"); 92 Validate.paramNotNull(charset, "charset"); 93 return new SdkBytes(string.getBytes(charset)); 94 } 95 96 /** 97 * Create {@link SdkBytes} from a string, using the UTF-8 charset. 98 */ fromUtf8String(String string)99 public static SdkBytes fromUtf8String(String string) { 100 return fromString(string, StandardCharsets.UTF_8); 101 } 102 103 /** 104 * Create {@link SdkBytes} from an input stream. This will read all of the remaining contents of the stream, but will not 105 * close it. 106 */ fromInputStream(InputStream inputStream)107 public static SdkBytes fromInputStream(InputStream inputStream) { 108 Validate.paramNotNull(inputStream, "inputStream"); 109 return new SdkBytes(invokeSafely(() -> IoUtils.toByteArray(inputStream))); 110 } 111 112 @Override toString()113 public String toString() { 114 return ToString.builder("SdkBytes") 115 .add("bytes", asByteArrayUnsafe()) 116 .build(); 117 } 118 } 119