package com.datastax.oss.protocol.internal;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
public class PrimitiveSizes {
public static final int BYTE = 1;
public static final int SHORT = 2;
public static final int INT = 4;
public static final int LONG = 8;
public static final int UUID = 16;
private PrimitiveSizes() {}
public static int sizeOfString(String str) {
return SHORT + encodedUTF8Length(str);
}
public static int sizeOfLongString(String s) {
return INT + encodedUTF8Length(s);
}
public static int sizeOfStringList(List<String> l) {
int size = SHORT;
for (String str : l) {
size += sizeOfString(str);
}
return size;
}
public static int sizeOfBytes(byte[] bytes) {
return INT + (bytes == null ? 0 : bytes.length);
}
public static int sizeOfBytes(ByteBuffer bytes) {
return INT + (bytes == null ? 0 : bytes.remaining());
}
public static int sizeOfShortBytes(byte[] bytes) {
return SHORT + bytes.length;
}
public static int sizeOfShortBytes(ByteBuffer bytes) {
return SHORT + (bytes == null ? 0 : bytes.remaining());
}
public static int sizeOfStringMap(Map<String, String> m) {
int size = SHORT;
for (Map.Entry<String, String> entry : m.entrySet()) {
size += sizeOfString(entry.getKey());
size += sizeOfString(entry.getValue());
}
return size;
}
public static int sizeOfStringMultimap(Map<String, List<String>> m) {
int size = SHORT;
for (Map.Entry<String, List<String>> entry : m.entrySet()) {
size += sizeOfString(entry.getKey());
size += sizeOfStringList(entry.getValue());
}
return size;
}
public static int sizeOfBytesMap(Map<String, ByteBuffer> m) {
int size = SHORT;
for (Map.Entry<String, ByteBuffer> entry : m.entrySet()) {
size += sizeOfString(entry.getKey());
size += sizeOfBytes(entry.getValue());
}
return size;
}
static int encodedUTF8Length(String st) {
int length = 0;
for (int i = 0; i < st.length(); i++) {
char c = st.charAt(i);
if (Character.isHighSurrogate(c)) {
if (i < st.length() - 1) {
char c1 = st.charAt(i + 1);
if (Character.isLowSurrogate(c1)) {
length += 4;
i++;
continue;
}
}
length += 1;
} else if (Character.isLowSurrogate(c)) {
length += 1;
} else {
if (c <= 0x7f) {
length += 1;
} else if (c <= 0x7ff) {
length += 2;
} else {
length += 3;
}
}
}
return length;
}
public static int sizeOfInet(InetSocketAddress address) {
return sizeOfInetAddr(address.getAddress()) + INT;
}
public static int sizeOfInetAddr(InetAddress address) {
byte[] raw = address.getAddress();
return BYTE
+ raw.length;
}
}