package com.sleepycat.bdb;
import com.sleepycat.bdb.bind.DataBuffer;
import com.sleepycat.db.Db;
import com.sleepycat.db.Dbt;
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
public final class DataThang extends Dbt implements DataBuffer {
private static DataThang discardDataThang;
static DataThang getDiscardDataThang() {
if (discardDataThang == null) {
discardDataThang = new DataThang();
discardDataThang.setFlags(Db.DB_DBT_USERMEM | Db.DB_DBT_PARTIAL);
discardDataThang.set_data(new byte[0]);
}
return discardDataThang;
}
private Object formation;
public DataThang() {
setFlags(Db.DB_DBT_MALLOC);
}
public DataThang(byte[] bytes) {
this();
setBytes(bytes);
}
public DataThang(DataThang copyFrom) {
this();
copy(copyFrom);
}
public void copy(DataThang copyFrom) {
setBytes(copyFrom.getBytes());
}
public void setBytes(byte[] bytes) {
formation = null;
set_data(bytes);
set_offset(0);
set_size((bytes != null) ? bytes.length : 0);
}
public void setBytes(byte[] bytes, int offset, int length) {
formation = null;
set_data(bytes);
set_offset(offset);
set_size(length);
}
public byte[] getBytes() {
byte[] bytes = get_data();
if (bytes == null) return null;
int size = get_size();
byte[] data = new byte[size];
System.arraycopy(bytes, get_offset(), data, 0, size);
return data;
}
public ByteArrayInputStream getByteStream() {
return new ByteArrayInputStream(get_data(), get_offset(), get_size());
}
public void increment() {
formation = null;
byte[] data = get_data();
int offset = get_offset();
int size = get_size();
byte[] newData = new byte[size + 1];
System.arraycopy(data, offset, newData, 0, size);
for (int i = offset + size - 1; i >= offset; i--) {
int val = 0xFF & data[i];
if (val != 0xFF) {
newData[i] = (byte) (val + 1);
setBytes(newData, 0, size);
return;
}
}
setBytes(newData);
}
public int compareTo(Dbt key2) {
byte[] d1 = this.get_data();
int o1 = this.get_offset();
int s1 = this.get_size();
byte[] d2 = key2.get_data();
int o2 = key2.get_offset();
int s2 = key2.get_size();
for (int i = 0; i < s1 && i < s2; i++) {
int b1 = 0xFF & d1[o1 + i];
int b2 = 0xFF & d2[o2 + i];
if (b1 < b2)
return -1;
else if (b1 > b2)
return 1;
}
if (s1 < s2)
return -1;
else if (s1 > s2)
return 1;
else
return 0;
}
public boolean equals(Dbt other) {
return compareTo(other) == 0;
}
public void dump(PrintStream out) {
dump(this, out);
}
public static void dump(Dbt dbt, PrintStream out) {
out.print(' ');
out.print(toString(dbt));
out.println();
}
public static String toString(Dbt dbt) {
int len = dbt.get_offset() + dbt.get_size();
StringBuffer buf = new StringBuffer(len * 2);
byte[] data = dbt.get_data();
for (int i = dbt.get_offset(); i < len; i++) {
String num = Integer.toHexString(data[i]);
if (num.length() < 2) buf.append('0');
buf.append(Integer.toHexString(data[i]));
}
if (dbt.get_size() == 4) {
buf.append(" (recno ");
buf.append(dbt.get_recno_key_data());
buf.append(')');
}
return buf.toString();
}
public String toString() {
return toString(this);
}
public void setData(byte[] data, int offset, int length) {
formation = null;
set_data(data);
set_offset(offset);
set_size(length);
}
public byte[] getDataBytes() {
return get_data();
}
public int getDataOffset() {
return get_offset();
}
public int getDataLength() {
return get_size();
}
public void setDataFormation(Object formation) {
this.formation = formation;
}
public Object getDataFormation() {
return formation;
}
public void clearDataFormation() {
formation = null;
}
}