TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/MemoizedUTF16String.java
2022-02-09 21:06:02 +01:00

39 lines
947 B
Java

package org.telegram.telegrambots.meta;
import java.nio.charset.StandardCharsets;
public class MemoizedUTF16String {
private final String original;
private byte[] cache;
public MemoizedUTF16String(String string) {
this.original = string;
}
public MemoizedUTF16String(byte[] utf16Data) {
this.original = null;
this.cache = utf16Data;
}
public byte[] getBytes() {
if (original != null && cache == null) {
byte[] newCache = original.getBytes(StandardCharsets.UTF_16LE);
this.cache = newCache;
return newCache;
} else {
return cache;
}
}
public String substring(int offsetUtf16, int lengthUtf16) {
byte[] bytes = getBytes();
if (bytes != null) {
return new String(bytes, offsetUtf16, lengthUtf16, StandardCharsets.UTF_16LE);
} else {
return "";
}
}
}