package org.apache.avro.util;
import java.nio.charset.StandardCharsets;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.io.BinaryData;
import org.slf4j.LoggerFactory;
public class Utf8 implements Comparable<Utf8>, CharSequence {
private static final String MAX_LENGTH_PROPERTY = "org.apache.avro.limits.string.maxLength";
private static final int MAX_LENGTH;
private static final byte[] EMPTY = new byte[0];
static {
String o = System.getProperty(MAX_LENGTH_PROPERTY);
int i = Integer.MAX_VALUE;
if (o != null) {
try {
i = Integer.parseUnsignedInt(o);
} catch (NumberFormatException nfe) {
LoggerFactory.getLogger(Utf8.class).warn("Could not parse property " + MAX_LENGTH_PROPERTY + ": " + o, nfe);
}
}
MAX_LENGTH = i;
}
private byte[] bytes = EMPTY;
private int length;
private String string;
public Utf8() {
}
public Utf8(String string) {
this.bytes = getBytesFor(string);
this.length = bytes.length;
this.string = string;
}
public Utf8(Utf8 other) {
this.length = other.length;
this.bytes = new byte[other.length];
System.arraycopy(other.bytes, 0, this.bytes, 0, this.length);
this.string = other.string;
}
public Utf8(byte[] bytes) {
this.bytes = bytes;
this.length = bytes.length;
}
public byte[] getBytes() {
return bytes;
}
@Deprecated
public int getLength() {
return length;
}
public int getByteLength() {
return length;
}
@Deprecated
public Utf8 setLength(int newLength) {
return setByteLength(newLength);
}
public Utf8 setByteLength(int newLength) {
if (newLength > MAX_LENGTH) {
throw new AvroRuntimeException("String length " + newLength + " exceeds maximum allowed");
}
if (this.bytes.length < newLength) {
byte[] newBytes = new byte[newLength];
System.arraycopy(bytes, 0, newBytes, 0, this.length);
this.bytes = newBytes;
}
this.length = newLength;
this.string = null;
return this;
}
public Utf8 set(String string) {
this.bytes = getBytesFor(string);
this.length = bytes.length;
this.string = string;
return this;
}
@Override
public String toString() {
if (this.length == 0)
return "";
if (this.string == null) {
this.string = new String(bytes, 0, length, StandardCharsets.UTF_8);
}
return this.string;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Utf8))
return false;
Utf8 that = (Utf8) o;
if (!(this.length == that.length))
return false;
byte[] thatBytes = that.bytes;
for (int i = 0; i < this.length; i++)
if (bytes[i] != thatBytes[i])
return false;
return true;
}
@Override
public int hashCode() {
int hash = 0;
for (int i = 0; i < this.length; i++)
hash = hash * 31 + bytes[i];
return hash;
}
@Override
public int compareTo(Utf8 that) {
return BinaryData.compareBytes(this.bytes, 0, this.length, that.bytes, 0, that.length);
}
@Override
public char charAt(int index) {
return toString().charAt(index);
}
@Override
public int length() {
return toString().length();
}
@Override
public CharSequence subSequence(int start, int end) {
return toString().subSequence(start, end);
}
public static byte[] getBytesFor(String str) {
return str.getBytes(StandardCharsets.UTF_8);
}
}