Source:
<code>public static byte[] buildUsermetadata(RestRequest restRequest) throws RestServiceException { Map<String, List<String>> args = restRequest.getArgs(); Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, List<String>> entry : args.entrySet()) { String key = entry.getKey(); if (key.startsWith(Headers.UserMetaData_Header_Prefix)) { sizeToAllocate += 4; <START> sizeToAllocate += key.substring(Headers.UserMetaData_Header_Prefix_Length).getBytes().length; <END> String value = getHeader(args, key, true); userMetadataMap.put(key.substring(Headers.UserMetaData_Header_Prefix_Length), value); sizeToAllocate += 4; sizeToAllocate += value.getBytes().length; } } ByteBuffer userMetadata = null; if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += Crc_Size; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(UserMetadata_Version_V1); userMetadata.putInt(sizeToAllocate - 6 - Crc_Size); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - Crc_Size); userMetadata.putLong(crc.getValue()); } return userMetadata.array(); }</code><technical_language>this inefficient me. creating a String, bytes(why?) length. O(1) wasted memory. easier (faster cheaper) do key.length() - UserMetaData_Header_Prefix.length()</technical_language>

Target:
public static byte[] buildUsermetadata(RestRequest restRequest) throws RestServiceException { Map<String, List<String>> args = restRequest.getArgs(); Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, List<String>> entry : args.entrySet()) { String key = entry.getKey(); if (key.startsWith(Headers.UserMetaData_Header_Prefix)) { sizeToAllocate += 4; String keyToStore = key.substring(Headers.UserMetaData_Header_Prefix.length()); sizeToAllocate += keyToStore.length(); String value = getHeader(args, key, true); userMetadataMap.put(keyToStore, value); sizeToAllocate += 4; sizeToAllocate += value.getBytes().length; } } ByteBuffer userMetadata = null; if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += Crc_Size; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(UserMetadata_Version_V1); userMetadata.putInt(sizeToAllocate - 6 - Crc_Size); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - Crc_Size); userMetadata.putLong(crc.getValue()); } return userMetadata.array(); }